How to put a point into gps coordinate on a tile sharpmap

Topics: SharpMap Project
Jul 30, 2013 at 2:03 PM
Edited Jul 31, 2013 at 2:07 PM
Hello sharpmap dev`s!

I have a problem with adding a point on my tile map. When I run my project the point is everytime in the center of the map. Here is the method:
private void add_point_to_map(string id, double x, double y)
        {
            
            NetTopologySuite.Geometries.Point point = new NetTopologySuite.Geometries.Point(x, y);
            SharpMap.Data.Providers.GeometryProvider provider = new SharpMap.Data.Providers.GeometryProvider(point);
            SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("point", provider);
            layer.Enabled = true;
            layer.MinVisible = double.Epsilon;
            layer.Style.Enabled = true;
            layer.Style.Fill = Brushes.Red;
            mapBox1.Map.Layers.Add(layer);
            mapBox1.Refresh();
            
It doesn`t matter what value x and y have, the point is alwais in the center of the map.
Please help me.
Aug 9, 2013 at 8:08 AM
Nobody can help? Can`t solve this problem :(
Coordinator
Aug 9, 2013 at 9:09 AM
What X and Y are you using?
If they are in WGS84 and your background tiles is using some kind of projectet coordinate-system they will almost everytime be in the center...

Then you need to add a coordinate transformation to your vectorlayer. Se the tutorial on how to do that.
Aug 9, 2013 at 10:09 AM
Edited Aug 9, 2013 at 11:03 AM
For example:
X=55.72011
Y=37.62860

Here is my code:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

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

            string tileCachePath = "C:\\Users\\lysenko\\Desktop\\openstreetmap";
            string tileFileType = "png";
            BruTile.Cache.FileCache fileCache = new BruTile.Cache.FileCache(tileCachePath, tileFileType);
            BruTile.FileSystem.FileTileProvider fileProvider = new BruTile.FileSystem.FileTileProvider(fileCache);
            BruTile.TileSource tileSource = new BruTile.TileSource(fileProvider, new BruTile.PreDefined.SphericalMercatorInvertedWorldSchema());

            var tileLayer = new SharpMap.Layers.TileLayer(tileSource, "OSM");
            mapBox1.Map.Layers.Add(tileLayer);

            mapBox1.Map.ZoomToExtents();
            mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
            mapBox1.Refresh();

            
            
        }

        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;
        }

        private void add_obj_into_map(string id, double x, double y)
        {
            
            NetTopologySuite.Geometries.Point point = new NetTopologySuite.Geometries.Point(x, y);
            SharpMap.Data.Providers.GeometryProvider provider = new SharpMap.Data.Providers.GeometryProvider(point);
            SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("point", provider);
            layer.Enabled = true;
            layer.MinVisible = double.Epsilon;
            layer.Style.Enabled = true;
            layer.Style.Fill = Brushes.Red;
            mapBox1.Map.Layers.Add(layer);

         }

        private void button1_Click(object sender, EventArgs e)
        {
            add_obj_into_map("GPSLayer", 55.72011, 37.62860);
            
        }

    }
You mean transforming between image coordinates and world coordinates? Or no? Sorry for my foolishness, geospatial is very hard for me too understand...
Aug 12, 2013 at 5:53 AM
petlof, Hello!

I added some code from tutorial to the add_obj_into_map method:
private void add_obj_into_map(string id, double x, double y)
        {
            NetTopologySuite.Geometries.Point point = new NetTopologySuite.Geometries.Point(x, y);
            SharpMap.Data.Providers.GeometryProvider provider = new SharpMap.Data.Providers.GeometryProvider(point);
            SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("point", provider);

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

            layer.Enabled = true;
            layer.MinVisible = double.Epsilon;
            layer.Style.Enabled = true;
            layer.Style.Fill = Brushes.Red;
            mapBox1.Map.Layers.Add(layer);
        }
And now sharpmap place this point (55.72011, 37.62860) somewhere near the Ashgabat :)))
Can you tell what is wrong with my code? Thank You.
Coordinator
Aug 12, 2013 at 9:23 AM
And it should have been in Mosow?
In that case i guess you flipped X/Y coordinates

Latitude = Y
Longitude = X

so your point should be 37.62860, 55.72011 instead.
Aug 12, 2013 at 10:48 AM
Edited Aug 12, 2013 at 10:49 AM
Oh! Exactly in Moscow. Sorry for wasted time. That was my fault.

Thank you very much!!!