<html><head><style type="text/css"><!-- DIV {margin:0px} --></style></head><body><div style="font-family:arial, helvetica, sans-serif;font-size:12pt"><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><div>Hi Chris,<br><br>Even with a morning cup of coffee, my head hurts trying to figure out this Mercator hack we put together.<br><br>My understanding is that, in Mercator projection, the lines of latitude stretch apart towards the poles. <br>So each tile will have a decreasing @dlat towards the poles, which I think is reflected in this code, in part at least.<br><br>I'd say the best way forward is to focus directly on the OpenLayers integration with the OSM tiles, and try to fix things there<br>(in the tile generation). This tile.js business is too funky to fix, and ultimately we want to move beyond it anyway.<br><br>Do you have an OpenLayers instance configured to request tiles from OSM in Mercator? With that, I can look at any problems<br>in
 streets.rbx. For the extent of the world, you can set the northern and southern borders to anything reasonable, say 85/-85.<br><br>Does that sound like a good next step?<br><br>Mikel<br><br><br>----- Original Message ----<br>From: Christopher Schmidt <crschmidt@crschmidt.net><br>To: dev@openstreetmap.org<br>Sent: Tuesday, May 30, 2006 9:11:18 PM<br>Subject: [OSM-dev] Projection Issues<br><br><div>After hours and hours of staring at dao.rb, I think that with pair<br>code-reading, we may have found the reason that using OpenStreetMap WMS<br>is not working in situations other than using OpenStreetMap's specific<br>version of Tile.js:<br><br>> Lines 19 and 20:<br>><br>> 19        @dlon = width / 2 * degrees_per_pixel<br>> 20        @dlat = height / 2 * degrees_per_pixel  * cos(@clat * PI / 180)<br>><br>> dlon and dlat in this case are half-tile distances in
 degrees. However,<br>> this math appears to be backwards from what pretty much everything else <br>> thinks. The reason for this is clear (though not obvious) if you think <br>> about the way that latitude and longitude works.<br><br>Latitudes are equidistant. One degree of latitude is always 111.1km. As<br>you head towards the poles, this is still the case. However, longitude<br>changes: at the poles, obviously, the distance between longitudes is 0.<br><br>It appears as if the code should probably be:<br><br>     @dlon = width / 2 * degrees_per_pixel * cos(@clat * PI / 180)<br>     @dlat = height / 2 * degrees_per_pixel <br><br>My next step was to try and work out *why* this works at all in<br>OpenStreetMap. Looking back to the original version of tile.js, checked<br>into the repository 10/09/05 14:32:02, r624, and diffed it against the<br>original civicmaps Tile engine source (maps.civicactions.net), and found<br>this:
 <br>-  + "&BBOX=" + lt + "," + tp + "," + rt + "," + bt;<br>+  + "&BBOX=" + lt + "," + 180 / 3.141592 * (2 * Math.atan(Math.exp(tp *<br>3.141592 / 180)) - 3.141592 /2)  + "," + rt + "," + 180 / 3.141592 * (2<br>* Math.atan(Math.exp(bt * 3.141592 / 180 )) - 3.141592 /2)  ;<br><br>In other words: modify our latitude to be in a 'mercator' projection.<br><br>Mapserver modifies the Landsat according to the requested bounding box,<br>so the landsat is stretched to fit the request. This is why the streets<br>and landsat layers match up.<br><br>The result when viewing the data in some viewer that doesn't suffer the<br>same confusion with regard to Mercator, is that the streets are<br>vertically much taller than they otherwise would be.<br><br>You can see two resulting tiles from different algorithms:<br><a target="_blank"
 href="http://tile.openstreetmap.org/cgi-bin/steve/mapserv?map=/usr/lib/cgi-bin/steve/wms.map&service=WMS&WMTVER=1.0.0&REQUEST=map&STYLES=&TRANSPARENT=TRUE&bbox=-0.046875,51.5078125,-0.03125,51.515625&width=256&height=128&layers=landsat,streets&service=WMS&version=1.1.1&request=GetMap">http://tile.openstreetmap.org/cgi-bin/steve/mapserv?map=/usr/lib/cgi-bin/steve/wms.map&service=WMS&WMTVER=1.0.0&REQUEST=map&STYLES=&TRANSPARENT=TRUE&bbox=-0.046875,51.5078125,-0.03125,51.515625&width=256&height=128&layers=landsat,streets&service=WMS&version=1.1.1&request=GetMap</a><br><a target="_blank"
 href="http://tile.openstreetmap.org/cgi-bin/steve/mapserv?map=/usr/lib/cgi-bin/steve/wms.map&service=WMS&WMTVER=1.0.0&REQUEST=map&STYLES=&TRANSPARENT=TRUE&LAYERS=landsat,streets&WIDTH=256&HEIGHT=128&BBOX=-0.043946,51.507621666833884,-0.021973,51.51445895579278">http://tile.openstreetmap.org/cgi-bin/steve/mapserv?map=/usr/lib/cgi-bin/steve/wms.map&service=WMS&WMTVER=1.0.0&REQUEST=map&STYLES=&TRANSPARENT=TRUE&LAYERS=landsat,streets&WIDTH=256&HEIGHT=128&BBOX=-0.043946,51.507621666833884,-0.021973,51.51445895579278</a><br><br>Note that despite having the same height and width, and almost exactly<br>the same latitude delta, but the longitude is significantly different...<br><br>There are still some issues with this: specifically,<br><a target="_blank"
 href="http://trac.openstreetmap.org/browser/www.openstreetmap.org/ruby/api/wms/wms.map">http://trac.openstreetmap.org/browser/www.openstreetmap.org/ruby/api/wms/wms.map</a><br>states that the streets are in Lat/Lon WGS-84, when really, they may be<br>something different.<br><br>So, what we have is a combination of two things 'broken' or not written<br>correctly in the exact same way, which seems to have the end result of<br>the code working inside OpenStreetMap and not anywhere else. <br><br>Note that I could well be wrong. This is the closest approximation to<br>a concise description of the problem, and it may be that I'm utterly<br>wrong, but at least the dlon/dlat above seem like they should be<br>backwards from the way they are. And there's a lot of other code that<br>seems to be changing to match that.<br><br>I'm not sure if this helps at all, but it seems like this might be a<br>first step towards getting closer to getting OSM into OpenLayers.<br><br>--
 <br>Christopher Schmidt<br>Web Developer<br><br>_______________________________________________<br>dev mailing list<br>dev@openstreetmap.org<br><a target="_blank" href="http://lists.openstreetmap.org/cgi-bin/mailman/listinfo/dev">http://lists.openstreetmap.org/cgi-bin/mailman/listinfo/dev</a><br></div></div></div></div></body></html>