1

I've been at this for quite a while now. What I'm trying to achieve here is to add Google map as background layer to my Sharpmap, I'm able to achieve this but the problem I'm facing now is My map always centre's to a point near a Greenland sea in Google Map , it's like it won't take my centre point coordinates.

I'm using Sharpmap 1.1 with BruTile 0.7.4.4

So far I've done the below.

SharpMap.Map _map = new SharpMap.Map();
SharpMap.Layers.VectorLayer layer = new SharpMap.Layers.VectorLayer("parcel");
SharpMap.Data.Providers.MsSqlSpatial DBlayer = new SharpMap.Data.Providers.MsSqlSpatial(_connectionString, "XXXXXX", "XXXX", "XXX");
layer.Style.Fill = new SolidBrush(Color.Transparent);
layer.Style.Outline = new Pen(Color.Black);
layer.Style.EnableOutline = true;
layer.MaxVisible = 13000;

layer.DataSource = DBlayer;

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

//SharpMap.Layers.TileLayer layerBackground = new TileLayer(new BingTileSource(BingRequest.UrlBing, "", BingMapType.Aerial), "TileLayer");
SharpMap.Layers.TileLayer layerBackground = new TileLayer(new GoogleTileSource(GoogleMapType.GoogleMap), "googlemaps");   

_map.Layers.Add(layerBackground);

_map.Layers.Add(layer);

_map.BackColor = Color.White;

//-98.526890,29.411539  
_map.Center = new GeoAPI.Geometries.Coordinate(0,0);

return _map;

Even if I manually give Geo Coordinates It just points to the same spot in the sea.

Please see the below Google map Geo point, this is the point where my map shows as centre. Every time I generate no matter what I do it shows this point as its centre.

71.946088, -3.956171

Any help is much appreciated. Thanks and Cheers!

Mark
  • 143,421
  • 24
  • 428
  • 436
  • Are you sure the DBLayer provider is in WGS84? What happens if you remove that layer? Which region is the DBLayer cover? Do you have the same effect if you use the OSM background, of bing layer? – pauldendulk Aug 22 '16 at 18:15
  • Yes the Layer is in WGS84, I've converted it using QGIS. I haven't tried with OSM but with bing the same happens. same place gets centred. – Rupesh Mankavil Aug 23 '16 at 04:41
  • What happens if you remove this line?: _map.Layers.Add(layer); – pauldendulk Aug 23 '16 at 13:44
  • If I remove it, I get only google map without my shapefile and again pinned to the same location. I suspect the problem is with the coordinate transformation but can't figure out what's wrong. – Rupesh Mankavil Aug 24 '16 at 04:18
  • This surprises me. The transformation is only on the layer. If you do not add the layer to the map the layer and all its properties (transformation, db provider) can have no effect at all. It seems the cause of the problem is not in the code you pasted. Do you set a CRS somewhere else? What if you start with a brand new project, add a google layer and center on (0,0)? Surely it must center correctly. Then add the layer that you configured in the code above. – pauldendulk Aug 24 '16 at 09:13
  • No Paul, I don't set any CSR anywhere. I tried with a brand new project added just the google map layer and still it points to the same point. I'm going bonkers over this one. – Rupesh Mankavil Aug 24 '16 at 10:45

2 Answers2

1

I found the problem for Google map layer not centring. The reason is that I used QGIS to convert my ESRI shapefile from WGS84(EPSG:4326) format to Spherical Mercator(EPSG:900913) and it changed the coordinates format but

Google Maps use Google Maps Global Mercator (Spherical Mercator).

When I used an online converter to test this out which you can find here. When I gave the resulting coordinates, it worked out just fine. Now all I have to do is find a way to convert Google Maps Global Mercator (Spherical Mercator). Thanks for your help @pauldendulk.

0

I was running into the same problem and in theirExamples they provide a LatLongToGoogle transformation function

public static ICoordinateTransformation LatLonToGoogle()
    {
        CoordinateSystemFactory csFac = new CoordinateSystemFactory();
        CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
        IGeographicCoordinateSystem sourceCs = 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>
        {
            new ProjectionParameter("semi_major", 6378137.0),
            new ProjectionParameter("semi_minor", 6378137.0),
            new ProjectionParameter("latitude_of_origin", 0.0),
            new ProjectionParameter("central_meridian", 0.0),
            new ProjectionParameter("scale_factor", 1.0),
            new ProjectionParameter("false_easting", 0.0),
            new ProjectionParameter("false_northing", 0.0)
        };
        IProjection projection = csFac.CreateProjection("Google Mercator", "mercator_1sp", parameters);
        IProjectedCoordinateSystem targetCs = csFac.CreateProjectedCoordinateSystem(
            "Google Mercator",
            sourceCs,
            projection,
            LinearUnit.Metre,
            new AxisInfo("East", AxisOrientationEnum.East),
            new AxisInfo("North", AxisOrientationEnum.North));
        ICoordinateTransformation transformation = ctFac.CreateFromCoordinateSystems(sourceCs, targetCs);
        return transformation;
theDrifter
  • 1,631
  • 6
  • 24
  • 40