[OSM-talk] calculating lon/lat bounds from tilename
higgy at bandnet.org
higgy at bandnet.org
Fri Jun 22 11:20:28 BST 2007
Hi Daniel,
I've actually been doing something similar with PHP. You can get an
example by visiting my static map [1] and clicking the button in the
'create trekbuddy map' box.
Unfortunately I never bothered to make the map browser very intuitive
(worksforme) and I think there's a bug in my lat/lon conversion because my
position always seems to be slightly off when actually using the map. I
can't work on either at the moment, though, as I'm waiting on a new PSU
for my home machine.
Anyway, the code I'm using (which I got from one of OJW's scripts) is below:
class geo
{
function Project($Y, $Zoom){
$LimitY = M_PI;
$RangeY = 2 * $LimitY;
$Unit = 1 / pow(2, $Zoom);
$relY1 = $Y * $Unit;
$relY2 = $relY1 + $Unit;
$relY1 = $LimitY - $RangeY * $relY1;
$relY2 = $LimitY - $RangeY * $relY2;
$Lat1 = geo::ProjectMercToLat($relY1);
$Lat2 = geo::ProjectMercToLat($relY2);
return(array($Lat1, $Lat2));
}
function ProjectMercToLat($MercY){
return(rad2deg(atan(sinh($MercY))));
}
function ProjectL($X, $Zoom){
$Unit = 360 / pow(2, $Zoom);
$Long1 = -180 + $X * $Unit;
return(array($Long1, $Long1 + $Unit));
}
function getLatLonXYZ ($x, $y, $zoom = 12)
{
$lat = geo::Project($y, $zoom);
$lon = geo::ProjectL($x, $zoom);
return array($lon[0], $lat[0], $lon[1], $lat[1]);
}
}
HTH,
Tom.
[1] http://osm.bandnet.org/browse/
> Hi, not sure if this is the right place to post this question, so
> forgive me if it's off topic...
> I've been working on a script that takes a lon/lat position, zoom level
> and a size of grid to capture.
> and it retrieves all the tiles in a grid of your size centered around
> the lon/lat provided.
> so I might say zoom=15, gridsize=4 and it will get me a 4x4 grid of
> tiles at that zoom level around my lon/lat.
> Now I'm doing this to use with TrekBuddy which is very cool. And I want
> a script that will build me the Atlas inputs it requires using the
> openstreetmap images.
> Everything works great.. however I need to generate a file that
> specifies the lon/lat of the top left corner and bottom right corner of
> my grid. I have no idea how to do that (I stole the calculation for
> lon/lat -> mercator tile from the osm page that contained a perl
> example.
>
> Any one have any ideas? I'm really keen to make it simple to use open
> streetmap data in trekbuddy so that I can see what currenty exists on
> the map as I walk around new places, to avoid going mapping stuff
> already there.
>
> Thanks for any assistance, even if only to point me at a better place to
> ask the question!
>
> Sincerely
>
> Daniel Would
>
>
> _______________________________________________
> talk mailing list
> talk at openstreetmap.org
> http://lists.openstreetmap.org/cgi-bin/mailman/listinfo/talk
>
More information about the talk
mailing list