This project has moved and is read-only. For the latest updates, please go here.

How to display shape file in MapViewControl?

Topics: SharpMap v2.0
Aug 16, 2008 at 8:49 AM

I've started working on v2.0, but I have this problem:

I declared a ShapeFileProvider and tried to initialize it with new keyword

but the constructor's 2nd parameter is IGeometryFactory (GeoAPI).

How can I provide an initialized parameter of IGeometryFactory?

and after initializing the ShapeFileProvider, how to assign the MapViewControl to show the right output?

Aug 18, 2008 at 9:30 AM
Hello, to initialize the providers and maps I use this static class:

using NetTopologySuite.Coordinates;
using GeoAPI.Coordinates;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.Geometries;
using ProjNet.CoordinateSystems;

namespace xxx.Tools
    public static class MapFactories
        private static readonly BufferedCoordinate2DFactory _coordinateFactory;
        private static readonly BufferedCoordinate2DSequenceFactory _coordinateSequenceFactory;
        private static readonly GeometryFactory<BufferedCoordinate2D> _geometryFactory;
        private static readonly CoordinateSystemFactory<BufferedCoordinate2D> _coordinateSystemFactory;

        static MapFactories()
            _coordinateFactory = new BufferedCoordinate2DFactory();

            _coordinateSequenceFactory = new BufferedCoordinate2DSequenceFactory(

            _geometryFactory = new GeometryFactory<BufferedCoordinate2D>(

            _coordinateSystemFactory = new CoordinateSystemFactory<BufferedCoordinate2D>(

        public static BufferedCoordinate2DFactory CoordinateFactory
        { get { return _coordinateFactory; } }
        public static BufferedCoordinate2DSequenceFactory CoordinateSequenceFactory
        { get { return _coordinateSequenceFactory; } }
    public static GeometryFactory<BufferedCoordinate2D> GeometryFactory
        { get { return _geometryFactory; } }
    public static CoordinateSystemFactory<BufferedCoordinate2D> CoordinateSystemFactory
        { get { return _coordinateSystemFactory; } }

The initialization of the shapefile-provider then looks like this:
IProvider prov = new SharpMap.Data.Providers.ShapeFile.ShapeFileProvider( connectionId, MapFactories.GeometryFactory, MapFactories.CoordinateSystemFactory );

Here is the map:
frm.Map = new SharpMap.Map("Map",MapFactories.GeometryFactory)


Aug 18, 2008 at 5:24 PM
Edited Aug 19, 2008 at 8:32 AM
Ok, thanks

but where are the layers?

is the shapefile shown just by this code?

for me it is not.

and I tried to add a layer but I faced some problems

I mean your code has no errors but has no functionality to show shape files.

I need your help, thanks in advance.
Aug 19, 2008 at 8:33 AM
Edited Aug 19, 2008 at 2:27 PM
sorry I missed that last part. In case of Shapefiles you need to add Geometrylayers, e.g. like this:

//Create ShapeFileProvider
      SharpMap.Data.Providers.ShapeFile.ShapeFileProvider prov = new SharpMap.Data.Providers.ShapeFile.ShapeFileProvider( <ShapeFile>, xxx.Tools.MapFactories.GeometryFactory );

//Create GeometryLayer
      SharpMap.Layers.GeometryLayer gl = new SharpMap.Layers.GeometryLayer( String.Format("GL_{0}", prov.ConnectionId), prov);

//Modify GeometryLayerStyle
//not that this is a pretty choice ;-)
      gl.Style.AreFeaturesSelectable = true;
      gl.Style.Enabled = true;
      gl.Style.EnableOutline = true;
      gl.Style.Fill = new SharpMap.Styles.SolidStyleBrush(SharpMap.Styles.StyleColor.Azure);
      gl.Style.Line = new SharpMap.Styles.StylePen(
        new SharpMap.Styles.SolidStyleBrush(SharpMap.Styles.StyleColor.Aqua), 3);
      gl.Style.Outline = new SharpMap.Styles.StylePen(
        new SharpMap.Styles.SolidStyleBrush(SharpMap.Styles.StyleColor.Aquamarine), 1);

      gl.Style.HighlightFill = new SharpMap.Styles.SolidStyleBrush(SharpMap.Styles.StyleColor.AliceBlue);
      gl.Style.HighlightLine = new SharpMap.Styles.StylePen(
        new SharpMap.Styles.SolidStyleBrush( SharpMap.Styles.StyleColor.Blue ), 3 );
      gl.Style.HighlightOutline = new SharpMap.Styles.StylePen(
        new SharpMap.Styles.SolidStyleBrush( SharpMap.Styles.StyleColor.BlueViolet ), 1 );

      gl.Style.SelectFill = new SharpMap.Styles.SolidStyleBrush(SharpMap.Styles.StyleColor.IndianRed);
      gl.Style.SelectLine = new SharpMap.Styles.StylePen(
        new SharpMap.Styles.SolidStyleBrush(SharpMap.Styles.StyleColor.Red), 3);
      gl.Style.SelectOutline = new SharpMap.Styles.StylePen(
        new SharpMap.Styles.SolidStyleBrush(SharpMap.Styles.StyleColor.Red), 1);

//Add layer to map
      frm.Map.Layers.AddLayer( gl );

//For the first Layer add

Aug 19, 2008 at 1:27 PM

Hello, thanks

but you didn't get the point I'm talking about.

I do need your last code but in the beginning of your code you wrote:

<shapefile> and this is what I'm having lack in

because the ShapeFileProvider's constructor's 2nd parameter is a IGeometryProvider

which I don't know how to initialize

ok, that is it, I need to know how can I initialize a valid useful IGeometryProvider to use

it in initializing the ShapeFileProvider.

anyone knows what I'm asking for?!


Aug 19, 2008 at 2:29 PM
why don't you pass
from the post above?
Aug 20, 2008 at 3:01 PM
Edited Aug 20, 2008 at 3:03 PM
Oh! You're right but when it comes to interfaces, it happens to get me confused


what do you mean by (frm)?

I used the xxx.Tools.MapFactory.GeometryFactory but
Does (frm) means MapViewControl?

because it gives me an error: the "frm.Map" cannot be accessed this way, because it lacks the get accessor.

What should I do?

by the way, FObermaier, you are great, thanks.
Aug 21, 2008 at 8:50 AM
In my case 'frm' is an MDIChild form containing an MapViewControl.
Over its Map property I realize access to the MapViewControl from the MDIParent.
Sorry for sloppy naming.
Aug 21, 2008 at 12:35 PM

No problem, before I saw your last reply, I realized the subject and

replaced frm by mapViewControl1 but the error:

mapViewControl1.Map Cannot be used in this context, because it lacks the get accessor

I viewed the source code of the MapViewControl.cs and modified the Map property from (private get) to (get)

and everything is well with building.

but when I run the program, I just see the white background of the MapViewControl (BackColor = white).

I traced (by debugging) the cursor and (the issue) was in the OnPaint event of the MapViewControl:



override void OnPaint(PaintEventArgs e)



Graphics g = e.Graphics;

Graphics screenGraphics = e.Graphics;

g.SmoothingMode = SmoothingMode.AntiAlias;


if (DesignMode || _presenter == null)








the _presenter is a MapPresenter  object and it was null.

I then knew the initializing mapViewControl1.Map this way is better:


//Create ShapeFileProvider


SharpMap.Data.Providers.ShapeFile.ShapeFileProvider prov = new SharpMap.Data.Providers.ShapeFile.ShapeFileProvider("C:\\Epi2000\\SY.shp", MapFactories.GeometryFactory);



//Create GeometryLayer



SharpMap.Layers.GeometryLayer gl = new SharpMap.Layers.GeometryLayer(String.Format("GL_{0}", prov.ConnectionId), prov);

Map myMap = new SharpMap.Map(MapFactories.GeometryFactory);

myMap.Title =



//Add layer to map





mapViewControl1.Map = myMap;






// Refresh the view





this way the _presenter object is not null and the execution continues...(we are in the OnPaint event of the MapViewControl)

if (!_presenter.IsRenderingSelection)





while (_renderObjectQueue.Count > 0)


GdiRenderObject ro = _renderObjectQueue.Dequeue();


if (ro.State == RenderState.Unknown)







the _presenter.IsRenderingSelection is false so the if execution enters the if loop body and executes g.Clear(BackColor)

and then the while loop condition:
_renderObjectQueue.Count is zero in my case so the while loop ends with no one loop

and finally, the painting is not done

consequently I see that white background image

I have no building errors but also the ShapeFile is not rendered.

any help?
Does any one of you has a working shapefile example to give the source code of?

I need that please, else, I have to go back to V0.9 which I don't want to.