From 57c99a69a231c530fdf2688c77b960b22e669ffd Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Sun, 27 Jan 2013 17:26:54 -0800 Subject: [PATCH] add tests for longlat/merc transformations --- tests/python_tests/projection_test.py | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/python_tests/projection_test.py b/tests/python_tests/projection_test.py index 1b7a8ece1..de8577b40 100644 --- a/tests/python_tests/projection_test.py +++ b/tests/python_tests/projection_test.py @@ -3,6 +3,8 @@ from nose.tools import * import mapnik +import random +import math # Tests that exercise map projections. @@ -46,5 +48,54 @@ def test_wgs84_inverse_forward(): assert_almost_equal(e.forward(p).center().y, e.center().y) assert_almost_equal(e.forward(p).center().x, e.center().x) +def wgs2merc(lon,lat): + x = lon * 20037508.34 / 180; + y = math.log(math.tan((90 + lat) * math.pi / 360)) / (math.pi / 180); + y = y * 20037508.34 / 180; + return [x,y]; + +def merc2wgs(x,y): + x = (x / 20037508.34) * 180; + y = (y / 20037508.34) * 180; + y = 180 / math.pi * (2 * math.atan(math.exp(y * math.pi/180)) - math.pi/2); + if x > 180: x = 180; + if x < -180: x = -180; + if y > 85.0511: y = 85.0511; + if y < -85.0511: y = -85.0511; + return [x,y] + +#echo -109 37 | cs2cs -f "%.10f" +init=epsg:4326 +to +init=epsg:3857 +#-12133824.4964668211 4439106.7872505859 0.0000000000 + +## todo +# benchmarks +# better well known detection +# better srs matching with strip/trim +# python copy to avoid crash + +def test_proj_transform_between_init_and_literal(): + one = mapnik.Projection('+init=epsg:4326') + two = mapnik.Projection('+init=epsg:3857') + tr1 = mapnik.ProjTransform(one,two) + wgs84 = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs' + merc = '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over' + src = mapnik.Projection(wgs84) + dest = mapnik.Projection(merc) + tr2 = mapnik.ProjTransform(src,dest) + for x in xrange(-180,180,10): + for y in xrange(-60,60,10): + coord = mapnik.Coord(x,y) + merc_coord1 = tr1.forward(coord) + merc_coord2 = tr2.forward(coord) + assert_almost_equal(merc_coord1.x,merc_coord2.x) + assert_almost_equal(merc_coord1.y,merc_coord2.y) + lon_lat_coord1 = tr1.backward(merc_coord1) + lon_lat_coord2 = tr2.backward(merc_coord2) + assert_almost_equal(coord.x,lon_lat_coord1.x) + assert_almost_equal(coord.y,lon_lat_coord1.y) + assert_almost_equal(coord.x,lon_lat_coord2.x) + assert_almost_equal(coord.y,lon_lat_coord2.y) + + if __name__ == "__main__": [eval(run)() for run in dir() if 'test_' in run]