[OSM-dev] Kosmos != Mapnik, Osmarender - projection problem

Igor Brejc igor.brejc at gmail.com
Sat Nov 8 20:10:20 GMT 2008


Hi all,

I've added slippymap-like rendering directly into Kosmos GUI (it will be 
available in the next version, probably within a week). Anyway, I've 
noticed that there is an discrepancy between Kosmos rendering and tiles 
generated by Mapnik (and Osmarender). All the stuff Kosmos renders is 
offset slightly towards the north (or south, depending on the 
hemisphere). The difference is noticeable in high-zooms only and it 
seems to increase with latitude (it's zero near equator). As an example 
see this screenshot: http://flickr.com/photos/28786233@N03/3012856297/ 
which displays both Kosmos rendering and the appropriate Mapnik tile 
(zoom level 17+).

I've double-checked my math and cannot find what could be wrong. I'm 
assuming that Mapnik and Osmarender use spherical Mercator projection 
(and not elliptical). Possible explanations:
1. My projection math is wrong
2. There is a difference between .NET math calculation and that of 
Mapnik's C++
3. I use double precision, while others use plain float?
4. ...?

This is the projection code I use (simplified for brevity):

        private static Point2<float> 
ConvertMapToViewCoords(Point2<double> mapCoords, IMapView mapView)
        {
            double resolution = 360.0 / (Math.Pow(2, 
mapView.MapPosition.ZoomFactor) * 256.0);
            double u180dPiResolution = 180.0 / (Math.PI * resolution);
            double y0 = Math.Log(Math.Tan(Math.PI * 0.25 + 
(mapView.MapPosition.Latitude * Math.PI / 180.0) * 0.5)) * 180.0 / 
Math.PI / resolution;

            double x = (mapCoords.X - mapView.MapPosition.Longitude) / 
resolution;
            double y = Math.Log(Math.Tan(Math.PI * (0.25 + mapCoords.Y / 
360.0))) * u180dPiResolution;

            return new Point2<float>((float)(x + mapView.ViewWidth / 
2.0), (float)((y0 - y) + mapView.ViewHeight / 2.0));
        }

I've made a simple unit test to calculate a sample pair of coordinates
map center (lat/lon): 46 / 15
zoom: 17
map point: 46.5 / 15.5
-----
calculated view point: x = 46603, y=-48304

Any help would be appreciated.

Best regards,
Igor

-- 
http://igorbrejc.net





More information about the dev mailing list