ShapeFile composed of points

Topics: SharpMap Project
Mar 26, 2013 at 3:17 PM
Hello everyone,

I want to create a simple project using SharpMap which allows me to display a map from a ShapeFile. It was quite simple with this shapeFile (which is composed of polygons) :

https://www.dropbox.com/s/z7jkjrlggp34bas/States_ugl.zip

But with this ShapeFile (which is composed of points) :

https://www.dropbox.com/s/wmhiokhr3jdooni/Ecoles_WGS84.zip

I get this error : "The OverflowException has not been managed"

System.Drawing.dll!System.Drawing.Graphics.CheckErrorStatus(int status)
System.Drawing.dll!System.Drawing.Graphics.FillEllipse(System.Drawing.Brush brush, float x, float y, float width, float height)
SharpMap.dll!SharpMap.Rendering.VectorRenderer.DrawPoint(System.Drawing.Graphics g, SharpMap.Geometries.Point point, System.Drawing.Brush b, float size, System.Drawing.PointF offset, SharpMap.Map map)
SharpMap.dll!SharpMap.Layers.VectorLayer.RenderGeometry(System.Drawing.Graphics g, SharpMap.Map map, SharpMap.Geometries.Geometry feature, SharpMap.Styles.VectorStyle style)
SharpMap.dll!SharpMap.Layers.VectorLayer.RenderInternal(System.Drawing.Graphics g, SharpMap.Map map, SharpMap.Geometries.BoundingBox envelope)
SharpMap.dll!SharpMap.Layers.VectorLayer.Render(System.Drawing.Graphics g, SharpMap.Map map)
SharpMap.dll!SharpMap.Map.RenderMap(System.Drawing.Graphics g, SharpMap.Layers.LayerCollectionType layerCollectionType, bool drawMapDecorations)
SharpMap.UI.dll!SharpMap.Forms.MapBox.GetMap(SharpMap.Map map, SharpMap.Layers.LayerCollection layers, SharpMap.Layers.LayerCollectionType layerCollectionType, SharpMap.Geometries.BoundingBox extent)
SharpMap.UI.dll!SharpMap.Forms.MapBox.GetImagesAsync(SharpMap.Geometries.BoundingBox extent, int imageGeneration)
SharpMap.UI.dll!SharpMap.Forms.MapBox.UpdateImage.AnonymousMethod__10()
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

Here is my code (based on http://sharpmap.codeplex.com/wikipage?title=Tutorial%20-%20Example%201%3a%20Adding%201%20layer%20to%20the%20Map&referringTitle=SharpMap%20Tutorial) :
public Form1()
        {
            InitializeComponent();

            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("States");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("states_ugl.shp", true);
            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
        }
Could you help me ? Thank you and please forgive me if my english is bad !
Coordinator
Mar 26, 2013 at 3:58 PM
Hi,
It works fine for me...

Is is exactly the code above that fails? (is says states_ugl.shp) in the code and not Ecoles_WGS84
Mar 26, 2013 at 4:30 PM
Hi,

Sorry if i wasn't clear but it's with the other ShapFile that I get my error, I put the wrong code :

public Form1()
        {
            InitializeComponent();

            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("Ecoles");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("Ecoles_WGS84.shp", true);
            mapBox1.Map.Layers.Add(vlay);
            mapBox1.Map.ZoomToExtents();
            mapBox1.Refresh();
        }
Thank you for your answer
Coordinator
Mar 26, 2013 at 4:37 PM
That code works perfectly for me.

What version of sharpmap are you using? The latest one from the downloads section or have you built from sourcecode?
Mar 26, 2013 at 4:59 PM
I downloaded this version :
SharpMap-0.9-AnyCPU-Trunk-2012.04.17
here :
http://sharpmap.codeplex.com/releases/view/87500
Coordinator
Mar 26, 2013 at 5:08 PM
Can you make a zip of your project that does not work and upload...
Mar 26, 2013 at 5:24 PM
Coordinator
Mar 26, 2013 at 6:04 PM
Ah, now i see.
Your shapefile is a PointZ file, there was an issue with that that was fixed in june 2012.
This fix is included in the SharpMap 1.0 RC what will be released this week.

until then you can get around it by doing
mapBox1.Map.ZoomToBox(new SharpMap.Geometries.BoundingBox(4, 42, 7, 45));

instead of ZoomToExtents()

Best Regards
Peter
Mar 27, 2013 at 9:11 AM
Thank a lot for your answers and for you great work
Coordinator
Mar 28, 2013 at 10:16 AM
If you use the newly released 1.0RC1 version you should be able to do ZoomToExtents() (remove the old .sidx file that is located next to your shapefile)
Mar 28, 2013 at 11:37 AM
Thank you, it's working great !
Mar 28, 2013 at 4:19 PM
Hi,

I have a new problem, this time I'm trying to put a BackgroundLayer (BingMap) on my map, and I'm still using the shape file "Ecoles_WGS84.shp".

I also do that in WPF (using a WindowsFormHost) and with the new released 1.0RC1.

The problem is that I'm very confused between GeoAPI and ProjNet (I'm new in WPF and SharpMap world)... I think that my code is wrong :
private static GeoAPI.CoordinateSystems.IProjectedCoordinateSystem GetEPSG900913(ProjNet.CoordinateSystems.CoordinateSystemFactory csFact)
        {
            List<GeoAPI.CoordinateSystems.ProjectionParameter> parameters = new List<GeoAPI.CoordinateSystems.ProjectionParameter>();

            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_major", 6378137.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("semi_minor", 6378137.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("latitude_of_origin", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("central_meridian", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("scale_factor", 1.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("false_easting", 0.0));
            parameters.Add(new GeoAPI.CoordinateSystems.ProjectionParameter("false_northing", 0.0));

            GeoAPI.CoordinateSystems.IProjection projection = csFact.CreateProjection("Google Mercator", "mercator_1sp", parameters);
            GeoAPI.CoordinateSystems.IGeographicCoordinateSystem wgs84 = csFact.CreateGeographicCoordinateSystem(
                "WGS 84", ProjNet.CoordinateSystems.AngularUnit.Degrees,
                ProjNet.CoordinateSystems.HorizontalDatum.WGS84,
                ProjNet.CoordinateSystems.PrimeMeridian.Greenwich,
                new GeoAPI.CoordinateSystems.AxisInfo("north", GeoAPI.CoordinateSystems.AxisOrientationEnum.North),
                new GeoAPI.CoordinateSystems.AxisInfo("east", GeoAPI.CoordinateSystems.AxisOrientationEnum.East)
            );

            GeoAPI.CoordinateSystems.IProjectedCoordinateSystem epsg900913 = csFact.CreateProjectedCoordinateSystem("Google Mercator", wgs84, projection,
                ProjNet.CoordinateSystems.LinearUnit.Metre, new GeoAPI.CoordinateSystems.AxisInfo("East", GeoAPI.CoordinateSystems.AxisOrientationEnum.East),
                new GeoAPI.CoordinateSystems.AxisInfo("North", GeoAPI.CoordinateSystems.AxisOrientationEnum.North)
            );

            return epsg900913;
        }
protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("Ecoles");
            vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("Ecoles_WGS84.shp", true);

            MyMapBox.Map.Layers.Add(vlay);

            ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
            ProjNet.CoordinateSystems.CoordinateSystemFactory csFact = new ProjNet.CoordinateSystems.CoordinateSystemFactory();
            vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeocentricCoordinateSystem.WGS84, GetEPSG900913(csFact));
            vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(GetEPSG900913(csFact), ProjNet.CoordinateSystems.GeocentricCoordinateSystem.WGS84);

            string token = "AhTX3xRRXWO77kOL9eNhVvxVvVlAUAa_ryyStS1upRNaMUMqK-FhzH6z0F9AaLqd";
            string layerName = "Bing roads layer";
            BruTile.Web.BingTileSource tileSource = new BruTile.Web.BingTileSource(BruTile.Web.BingRequest.UrlBing, token, BruTile.Web.BingMapType.Roads);
            SharpMap.Layers.TileAsyncLayer tileAsyncLayer = new SharpMap.Layers.TileAsyncLayer(tileSource, layerName);

            MyMapBox.Map.BackgroundLayer.Add(tileAsyncLayer);
            MyMapBox.Map.ZoomToExtents();
            MyMapBox.Refresh();
        }
Can you see what I do wrong ?
the exception show up here :
vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeocentricCoordinateSystem.WGS84, GetEPSG900913(csFact));

Thx,
François
Coordinator
Mar 28, 2013 at 5:50 PM
Hi,
You should use:

ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84
Coordinator
Mar 28, 2013 at 6:03 PM
And another note..
There seem to be something wrong with brutile and bing at the moment (i will report it to Brutile)..

But you can instead use this as tilesource:
        string userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14";
        string referer = "http://www.bing.com/maps/";

        ITileSource tileSource = new TileSource(new WebTileProvider(new BruTile.Web.BingRequest(BruTile.Web.BingRequest.UrlBing, token, BruTile.Web.BingMapType.Roads),
             userAgent, referer, false), new SphericalMercatorInvertedWorldSchema());
Mar 29, 2013 at 9:12 AM
Thank you a lot, it's working find with this tileSource.
Apr 2, 2013 at 10:14 AM
Hello Petlof,

I have some questions for you (again).

Thank to you I have this code which is perfectly working :
private static GeoAPI.CoordinateSystems.IProjectedCoordinateSystem GetEPSG900913(ProjNet.CoordinateSystems.CoordinateSystemFactory csFact)
        {
          ...
        }
 protected override void OnInitialized(EventArgs e)
         {
             base.OnInitialized(e);
            
             //Creating a VectorLayer for the shape "Ecoles_WGS84"
             SharpMap.Layers.VectorLayer vlay = new SharpMap.Layers.VectorLayer("Ecoles");
             vlay.DataSource = new SharpMap.Data.Providers.ShapeFile("C:/Shapes/Ecoles_WGS84.shp", true);
             vlay.DataSource.Open();
            
             //Adding this layer to the MapBox
             MyMapBox.Map.Layers.Add(vlay);

             ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
             ProjNet.CoordinateSystems.CoordinateSystemFactory csFact = new ProjNet.CoordinateSystems.CoordinateSystemFactory();

             vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, GetEPSG900913(csFact));
             vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(GetEPSG900913(csFact), ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);
             vlayP.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, GetEPSG900913(csFact));
             vlayP.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(GetEPSG900913(csFact), ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);

             string userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14";
             string referer = "http://www.bing.com/maps/";
             string token = "xxxxx";
             string layerName = "Bing roads layer";

             ITileSource tileSource = new TileSource(new WebTileProvider(new BruTile.Web.BingRequest(BruTile.Web.BingRequest.UrlBing, token, BruTile.Web.BingMapType.Hybrid),
              userAgent, referer, false), new SphericalMercatorInvertedWorldSchema());

             SharpMap.Layers.TileAsyncLayer tileAsyncLayer = new SharpMap.Layers.TileAsyncLayer(tileSource, layerName);

             MyMapBox.Map.BackgroundLayer.Add(tileAsyncLayer);

             MyMapBox.Map.ZoomToExtents();
             MyMapBox.Refresh();
         }
However, the map display is very slow when zooming or panning.
As a beginner in mapping I was wondering if you could give me some tips to improve the speed of appearance of tiles.
Maybe I could use an offline map, but in this case where can I get the tiles ? I'm really starting from scratch in this area so any idea could help me !
I tried to search in the existing discussions but without success...

Thank you in advance
Coordinator
Apr 2, 2013 at 10:24 AM
Hi,

What do you mean by slow, how long does loading of tiles take?

If your internetconnection is slow you can use a filecache where bing-tiles are stored for some days..
SharpMap.Layers.TileAsyncLayer tileAsyncLayer = new SharpMap.Layers.TileAsyncLayer(tileSource, layerName, Color.Transparent, false, "c:\\temp\\mytilecache\\");
Apr 2, 2013 at 10:57 AM
It can take five seconds to load tiles, what I mean is that it's really not fluid so it doesn't offer a great experience for the end user !

Indeed I think it's due to my bad internet connection.

Why do you mean by "for some days", can you give me some details ?

Thank again