Sharpmap - Print points over a map

Topics: Español, SharpMap Project, SharpMap v0.9 / v1.x, SharpMap v2.0, WinForms Controls
Jan 18 at 5:02 PM
Hi,
I've just started using Sharpmap but I have a problem. I'm not able to print points (not even one) over a map using this library.

I didn't find a good example to do it, and my code is not working. It draws a point quite far from where it should be (Madrid, Spain).

I would appreciate some help if someone knows how to use it.

I hope you have been able to understand me because my English is not very good

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

        VectorLayer vlay = new VectorLayer("Spain");
        vlay.DataSource = new SharpMap.Data.Providers.ShapeFile(@"ESP_adm_shp\ESP_adm2.shp", true);

        VectorLayer vlay2 = new VectorLayer("Points");
        Collection<GeoAPI.Geometries.IGeometry> geomColl =   new Collection<GeoAPI.Geometries.IGeometry>();
        //Get the default geometry factory
        GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();
        GeoAPI.Geometries.IGeometryFactory gf =
            GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory();

        geomColl.Add(gf.CreatePoint(new GeoAPI.Geometries.Coordinate(40.4177623, -3.6690416)));
        vlay2.DataSource = new SharpMap.Data.Providers.GeometryProvider(geomColl);

        mapBox1.Map.Layers.Add(vlay);
        mapBox1.Map.Layers.Add(vlay2);

        //ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
        //vlay.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator);
        //vlay.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);


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


    }
Coordinator
Jan 19 at 8:21 AM
Assuming your Madrid coordinate is WGS84 then this should do:
public partial class Form1 : Form
{
    public Form1()
    {
        // Better once in Program.cs!
        GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();

        InitializeComponent();
        var shpProvider = new SharpMap.Data.Providers.ShapeFile(@"ESP_adm_shp\ESP_adm2.shp", true);
        VectorLayer vlay = new VectorLayer("Spain");
        vlay.DataSource = shpProvider;

        VectorLayer vlay2 = new VectorLayer("Points");
        Collection<GeoAPI.Geometries.IGeometry> geomColl =   new Collection<GeoAPI.Geometries.IGeometry>();
        //Get the default geometry factory
        GeoAPI.Geometries.IGeometryFactory gf =
            GeoAPI.GeometryServiceProvider.Instance.CreateGeometryFactory();

        geomColl.Add(gf.CreatePoint(new GeoAPI.Geometries.Coordinate(40.4177623, -3.6690416)));
        vlay2.DataSource = new SharpMap.Data.Providers.GeometryProvider(geomColl);

        ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory ctFact = 
            new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
        vlay2.CoordinateTransformation = ctFact.CreateFromCoordinateSystems(
             ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84, shpProvider.CoordinateSystem);
        vlay2.ReverseCoordinateTransformation = ctFact.CreateFromCoordinateSystems(
             shpProvider.CoordinateSystem, ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84);

        mapBox1.Map.Layers.Add(vlay);
        mapBox1.Map.Layers.Add(vlay2);

        mapBox1.Map.ZoomToExtents();
        mapBox1.Refresh();
        mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
        
        //Image imgMap = mapBox1.Map.GetMap();


    }