Trouble with transformation of coordinates for use with OSM

Topics: SharpMap v2.0, WinForms Controls
Jun 10, 2015 at 10:57 AM
Edited Jun 10, 2015 at 11:02 AM
I found a corresponding discussion [discussion:637805] but the solution of this was to look at the howto.. I did that but I still can't figure out, what to do :/

I am not familiar with transformation from one projection to another nor are any comments showing for the methods provided withsharpmap.. So its kind of hard as I just wanted to display some tiles provided by an openstreetmap server. Therefor I used the method shown in example4:
mapBox1.Map.BackgroundLayer.Add(new SharpMap.Layers.TileAsyncLayer( new BruTile.Web.OsmTileSource(), "OSM"));

But what projection is the default?!
Currently, if I want to have the Center at "48.55783 / 8.98079" I have to set it to "1000000.0 / 6200000.0". That is totally confusing and I hope, you can help me with this one.


Erm.. I have two more TileAsyncLayer with a custom URI that I want to Enable/Disable by Click. But what do I have to call after enabling it? map.refresh() doesn't show the tiles but left click and drag will do the trick - I didn' specify a mousedrag event
Jun 10, 2015 at 1:18 PM
Edited Jun 10, 2015 at 1:24 PM
The projection used with OSM is called WebMercator (EPSG:3857), the coordinates you are passing as center are WGS84 (EPSG:4326).
SharpMap does not reproject tiles so you will have to transform your WGS84 Coordinates to WebMercator.
var ctFac = new ProjNet.CoordinateSystems.Transformations.CoordinateTransformationFactory();
var csSrc = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84;
var csTgt = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WebMercator;
var ct = ctFac.Create(csSrc, csTgt);
var ptWebMercator = ct.MathTransform.Transform(new double[] { 48.55783, 8.98079 });
You may have to flip lat/lon.

Have you tried MapBox.Invalidate()
Jun 10, 2015 at 1:45 PM
Hi, thanks for your reply.
I will try that tonight.

I tried MapBox.Refresh() and, I think, invalidate too... not sure though and will try that too.
Jun 10, 2015 at 4:26 PM
Edited Jun 10, 2015 at 4:28 PM
now I tested your code snipped and it works.
Thank you!

Just for anybody else: you have to flip lat/lon.
var ptWebMercator = ct.MathTransform.Transform(new GeoAPI.Geometries.Coordinate(8.9831, 48.5568));
I couldn't figure out the problem with loading additional layers but not showing them until I drag the map.
I used MapBox.Refresh() as well as MapBox.Invalidate() after I did layer.Enabled = true;
OsmTileServerConfig layer1 = new OsmTileServerConfig(layerURI, 1, new string[] { "" }, 0, 18);
OsmTileSource layer1source = new OsmTileSource(new OsmRequest(layer1));
this.Overlay1 = new TileAsyncLayer(layer1source, "Layer 1");

I tried with waiting 10 Seconds bevore invalidating, but this doesn't help either.. :/