[Openstreetmap-dev] Algorithm in Java for getting tile boundaries

Immanuel Scholz immanuel.scholz at gmx.de
Thu Feb 16 18:31:35 GMT 2006


Hi,

not that much time today evening, so I only implemented the "short
version" of the promised algorithm ;-).

This just dump out the calculated boundaries of the tile lat/lon is in
without thinking about floating point rounding problems (means: no table
lookup implemented).

public String bboxUrlPart(double lat, double lon, int zoom) {
  lat = Math.log(Math.tan(Math.PI/4+lat*Math.PI/360));
  double pivotLon = 0, pivotLat = 0;
  double deltaLon = 90, deltaLat = Math.log(Math.tan(Math.PI/4+Math.PI/8));
  for (int i = 0; i < zoom; i++) {
    pivotLon += deltaLon * (lon<pivotLon ? -1 : 1);
    pivotLat += deltaLat * (lat<pivotLat ? -1 : 1);
    deltaLon /= 2;
    deltaLat /= 2;
  }
  deltaLat = Math.atan(Math.sinh(deltaLat*2))*180/Math.PI;
  pivotLat = Math.atan(Math.sinh(pivotLat))*180/Math.PI;
  return "bbox="+(pivotLat-deltaLat)+","+(pivotLon-deltaLon*2)+
    ","+(pivotLat+deltaLat)+","+(pivotLon+deltaLon*2);
}

Output is: bbox=<minlat>,<minlon>,<maxlat>,<maxlon>

This may be useful, if anyone want to test whether rounding the latitude
values is an option. Insert the rounding for latitude just before the
return statement.

Ciao, Imi.

PS: Remember: The longitude does not need rounding, since the algorithm
does not do any "harmful" operation on the floating value when calculating
longitude ;)






More information about the dev mailing list