Math functions in JAVA and Delphi give different results -
i'm trying convert open street maps latitude/longitude pixel position. found example in java. when convert code delphi, x result correct, y result out.
java code:
double lon = -89.08805; double lat = 30.6393; double zoom = 6; // 6.5156731549786215 possible double lon_rad = math.toradians(lon); double lat_rad = math.toradians(lat); double n = math.pow(2.0, zoom); double tilex = ((lon + 180) / 360) * n; double tiley = (1 - (math.log(math.tan(lat_rad) + 1.0/math.cos(lat_rad)) / math.pi)) * n / 2.0; system.out.println("tilex = "+tilex+" tiley = "+tiley);
delphi code:
function latlngtotilepixels(lat, lng: double; zoomlevel: integer): tpointf; var lon_rad, lat_rad, n: double; tilex, tiley: double; begin lon_rad := degtorad(lng); lat_rad := degtorad(lat); n := power(2.0, zoomlevel); result.x := ((lng + 180) / 360) * n; result.y := (1 - (math.log10(math.tan(lat_rad) + 1.0/cos(lat_rad)) / pi)) * n / 2.0; end;
the results, using inputs given in example (lat=30.6393, lng=-89.08805, zoom=6) are:
java:
tilex = 16.162124444444444
tiley = 26.273150713795616
delphi:
tilex=16.1621244444444
tiley = 29.5128609563099 <--- wrong result
i have feeling problem may math.log call. java uses log, delphi uses log10. have tried log2, result worse.
you use logarithms of different bases:
java: base e
, i.e. natural logarithm, see math.log(double)
delphi: base 10
you can use following formula calculate logarithm base not supported:
log_a(b) = log_x(b) / log_x(a)
afaik there ln
function in delphi, calculates natural logarithm, @ least it's listed here.
Comments
Post a Comment