Simple example of implementing SharpMap using WGS84

Topics: Data Access
May 7, 2010 at 9:00 AM
Edited May 7, 2010 at 9:44 AM

Hi, I am considering using SharpMap in a project I am currently working on, however I need to be able to draw maps from data derived our own entity-framework model.

Given a list of longitude/latitude co-ordinates (all in WGS84), what is the simplest way of accurately plotting these on a map layer?

Thanks, Alex

Coordinator
May 7, 2010 at 10:08 AM
Edited May 7, 2010 at 6:24 PM

Hi AspireAlex, use a GeometryProvider it may look something like:

 

GeometryProvider prov = new GeometryProvider(
  efcontext.EntityName.Select(o => new Point(o.Lon, o.Lat)).Cast<Geometry>().ToList()

)
you would then use prov the same way as any other provider
May 7, 2010 at 10:37 AM

Hi John,

Many thanks for your quick reply.  I have tried something similar, looping through my Road objects, then constructing a Collection<SharpMap.Geometries.Point> with the RoadCoordinate objects.

This creates a map that looks correct and the roads seem to show in roughly the correct places, however on closer inspection the map seems slightly skewed compared to the same set of roads displayed in Google Maps.

Is there anything else I need to do with my WGS84 lat/lon co-ordinates in order to project the map accurately?

I have uploaded a screenshot here to maybe explain the problem more clearly, http://dl.dropbox.com/u/119944/sharpmap-wgs84.png

Thanks, Alex

Coordinator
May 7, 2010 at 12:17 PM
Edited May 7, 2010 at 12:18 PM

Hi Alex, Google Maps is not WGS 84 - it is commonly known as Web Mercator, but also as  900913,  SR-ORG:6864, EPSG:3857, EPSG:3875 consequently the coordinates need to be transformed ,

see http://sharpmap.codeplex.com/wikipage?title=Apply%20on%20the%20fly%20transformation%20to%20a%20layer&referringTitle=How%20to...

hth jd

May 7, 2010 at 12:30 PM

Excellent, thank you very much for your help.

This code transforms the co-ordinates correctly, now it aligns perfectly with Google Maps.

        public static ICoordinateTransformation WGS84ToGoogleTransformation()
        {
            CoordinateSystemFactory csFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
            CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();

            IGeographicCoordinateSystem wgs84 = csFac.CreateGeographicCoordinateSystem(
                "WGS 84", AngularUnit.Degrees, HorizontalDatum.WGS84, PrimeMeridian.Greenwich,
                new AxisInfo("north", AxisOrientationEnum.North), new AxisInfo("east", AxisOrientationEnum.East));

            List<ProjectionParameter> parameters = new List<ProjectionParameter>();
            parameters.Add(new ProjectionParameter("semi_major", 6378137.0));
            parameters.Add(new ProjectionParameter("semi_minor", 6378137.0));
            parameters.Add(new ProjectionParameter("latitude_of_origin", 0.0));
            parameters.Add(new ProjectionParameter("central_meridian", 0.0));
            parameters.Add(new ProjectionParameter("scale_factor", 1.0));
            parameters.Add(new ProjectionParameter("false_easting", 0.0));
            parameters.Add(new ProjectionParameter("false_northing", 0.0));
            IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);

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

            return ctFac.CreateFromCoordinateSystems(wgs84, epsg900913);
        }