# Osmarender bug

Jump to: navigation, search

The so-called Osmarender bug refers to the fact that when the osmarender renders an area, it selects the by lon/lat, but only truly projects one point in the middle; the rest is a linear interpolation, not a true projection.

A way of thinking about it is that instead of projecting a sphere, we're projecting a sphere-like object constructed out of 4096×4096 parallelograms. An advantage of this is that straight lines (like roads) project straight whereas in a true projection they would be very slightly curved. This means that at the edge of a tile roads may get a kink as they step from one projection to another. But the real advantage is that it saves the servers a few CPU cycles and can be coded into a web page without needing fancy math. For large outfits these few CPU cycles add up to real money+time.

Originally the EPSG refused to add "Google's Mercator on a sphere" to their authoritative map projection database, stating:

 "We have reviewed the coordinate reference system used by Microsoft, Google, etc. and believe that it is technically flawed. We will not devalue the EPSG dataset by including such inappropriate geodesy and cartography."

And so it lived in a number of softwares as unofficial code 900913. (e.g. esri.extra)

But eventually the EPSG did bow to the immense user pressure and have now added it as EPSG code 3785 (later renamed EPSG:3857). That should not be misinterpreted as it being legitimized for serious use!

## Error

Obviously this means that when you put a point on a map, the actual lat/long coordinates are not what you expect. However, the error is not very large. If you look at the math behind Linear interpolation you get the following formula:

Error ≤ ((x1−x0)2)/8 max | f″(x) |

When f(x) is the function transforming the mercator y coordinate the actual degrees. f(x) = arctan(sinh(x)) (from Wikipedia:Mercator projection). Since the osmarender interpolation works over the range of π/4096 and the second derivative has a maximum of 0.5, it makes the maximum error of 3.63e−8 degrees, or about 1.5 metres on the earths surface. This error will be achieved at a latitude of ±51 degrees (approximately the latitude of London or Brussels).

### Low zoom

If you look at the formula you can see that as you move to lower zoom levels, the error grows by the square. This error doesn't occur if you stitch the tile together. Thus error for low zoom tiles are:

Zoom Error (distance) Scale (256px tiles) Error in pixels
11 6m 76m/px 1/12px
10 24m 153m/px 1/6px
8 384m 610m/px 1/2px
6 6144m 2441m/px 3px
4 98304m 9765m/px 10px

The pixel amounts seems small, but these are best case scenarios. If you do all your projection at zoom 8 and then go render zoom 12 tiles, the error gets magnified, in this case to 16*1/2 = 8 pixels. But if you use z4 as a base to project z8 tiles, it'll be up to 16*10 = 160 pixels off!

## Possible fix

The problem can be fixed by doing the projection outside of Osmarender. One would have to translate an OSM file with latitudes and longitudes to one with already projected x and y values (or Eastings and Northings if you prefer). This would likely have the following advantages:

• No projection error
• Faster processing since XSLT is not good at maths
• Even faster in the tiles@home context since projection would have to be done only once per tileset, not once per zoom level, and tiles could be cut from one big bitmap (a la tilesPipe@home)
• Easy changeover to other projections if desired

A disadvantage of this would probably be the fact that Osmarender would be unable to draw a lat/lon grid or in fact display a scale in metres or miles because it would not know anything about the geographic bounding box it is rendering.

User:Frederik Ramm has a prototype projection script and a modified Osmarender to work with its output. It seems to do what it should, but has to be tweaked and polished a bit before it can be used to revive tilesPipe@home.

• These problems have already been solidly resolved in other free software,
• The PROJ.4 mailing list will give sage advice in these areas; they are a mighty collection of experts. Search their mailing list archives for threads (i.e. rants) about the "Google Sphere" projection.
See also these proj4 parameters for Google's Mercator projection on a spherical Earth.
• Also the GDAL/OGR tools and developers could be of help for generating correctly georeferenced tiles and setting up a WMS MapServer, which would be an ideal (online) solution.

## See also

Google Maps Deconstructed (note: Google Maps is not the same as Google Earth!)