def test_forward_mercator(self): arctic = forward_mercator((0, 85)) antarctic = forward_mercator((0, -85)) hawaii = forward_mercator((-180, 0)) phillipines = forward_mercator((180, 0)) ne = forward_mercator((180, 90)) sw = forward_mercator((-180, -90)) self.assertEqual(round(arctic[0]), 0, "Arctic longitude is correct") self.assertEqual(round(arctic[1]), 19971869, "Arctic latitude is correct") self.assertEqual(round(antarctic[0]), 0, "Antarctic longitude is correct") self.assertEqual(round(antarctic[1]), -19971869, "Antarctic latitude is correct") self.assertEqual(round(hawaii[0]), -20037508, "Hawaiian lon is correct") self.assertEqual(round(hawaii[1]), 0, "Hawaiian lat is correct") self.assertEqual(round(phillipines[0]), 20037508, "Phillipines lon is correct") self.assertEqual(round(phillipines[1]), 0, "Phillipines lat is correct") self.assertEqual(round(ne[0]), 20037508, "NE lon is correct") self.assertTrue(ne[1] > 50000000, "NE lat is correct") self.assertEqual(round(sw[0]), -20037508, "SW lon is correct") self.assertTrue(math.isinf(sw[1]), "SW lat is correct") # verify behavior for invalid y values self.assertEqual(float('-inf'), forward_mercator((0, 135))[1]) self.assertEqual(float('-inf'), forward_mercator((0, -135))[1])
def add_to_map(req, id, typename): if req.method != 'POST': return HttpResponse('POST required', status=400) mapobj = get_object_or_404(Map, id=id) if mapobj.owner != req.user and not req.user.has_perm( 'maps.change_map', mapobj): return HttpResponse('Not sufficient permissions', status=401) layer = get_object_or_404(Layer, typename=typename) existing = MapLayer.objects.filter(map=mapobj) vs_url = settings.GEOSERVER_BASE_URL + '%s/%s/wms' % tuple( layer.typename.split(':')) stack_order = max([l.stack_order for l in existing]) + 1 # have to use local name, not full typename when using ows_url maplayer = MapLayer(name=layer.name, ows_url=vs_url, map=mapobj, stack_order=stack_order) maplayer.save() # if bounding box is equivalent to default, compute and save ints = lambda t: map(int, t) if ints(mapobj.center) == ints( forward_mercator(settings.DEFAULT_MAP_CENTER)): bbox = layer.resource.latlon_bbox[0:4] # @todo copy-paste from geonode.maps.views - extract this for reuse minx, maxx, miny, maxy = [float(c) for c in bbox] x = (minx + maxx) / 2 y = (miny + maxy) / 2 center = forward_mercator((x, y)) if center[1] == float('-inf'): center = (center[0], 0) if maxx == minx: width_zoom = 15 else: width_zoom = math.log(360 / (maxx - minx), 2) if maxy == miny: height_zoom = 15 else: height_zoom = math.log(360 / (maxy - miny), 2) mapobj.center_x = center[0] mapobj.center_y = center[1] mapobj.zoom = math.ceil(min(width_zoom, height_zoom)) mapobj.save() return HttpResponse('OK', status=200)
def test_inverse_mercator(self): arctic = inverse_mercator(forward_mercator((0, 85))) antarctic = inverse_mercator(forward_mercator((0, -85))) hawaii = inverse_mercator(forward_mercator((-180, 0))) phillipines = inverse_mercator(forward_mercator((180, 0))) ne = inverse_mercator(forward_mercator((180, 90))) sw = inverse_mercator(forward_mercator((-180, -90))) self.assertAlmostEqual(arctic[0], 0.0, msg="Arctic longitude is correct") self.assertAlmostEqual(arctic[1], 85.0, msg="Arctic latitude is correct") self.assertAlmostEqual(antarctic[0], 0.0, msg="Antarctic longitude is correct") self.assertAlmostEqual(antarctic[1], -85.0, msg="Antarctic latitude is correct") self.assertAlmostEqual(hawaii[0], -180.0, msg="Hawaiian lon is correct") self.assertAlmostEqual(hawaii[1], 0.0, msg="Hawaiian lat is correct") self.assertAlmostEqual(phillipines[0], 180.0, msg="Phillipines lon is correct") self.assertAlmostEqual(phillipines[1], 0.0, msg="Phillipines lat is correct") self.assertAlmostEqual(ne[0], 180.0, msg="NE lon is correct") self.assertAlmostEqual(ne[1], 90.0, msg="NE lat is correct") self.assertAlmostEqual(sw[0], -180.0, msg="SW lon is correct") self.assertAlmostEqual(sw[1], -90.0, msg="SW lat is correct")
def add_to_map(req,id,typename): if req.method != 'POST': return HttpResponse('POST required',status=400) mapobj = get_object_or_404(Map, id=id) if mapobj.owner != req.user and not req.user.has_perm('maps.change_map', mapobj): return HttpResponse('Not sufficient permissions',status=401) layer = get_object_or_404(Layer, typename=typename) existing = MapLayer.objects.filter(map = mapobj) vs_url = settings.GEOSERVER_BASE_URL + '%s/%s/wms' % tuple(layer.typename.split(':')) stack_order = max([l.stack_order for l in existing]) + 1 # have to use local name, not full typename when using ows_url maplayer = MapLayer(name = layer.name, ows_url=vs_url, map=mapobj, stack_order=stack_order) maplayer.save() # if bounding box is equivalent to default, compute and save ints = lambda t: map(int,t) if ints(mapobj.center) == ints(forward_mercator(settings.DEFAULT_MAP_CENTER)): bbox = layer.resource.latlon_bbox[0:4] # @todo copy-paste from geonode.maps.views - extract this for reuse minx, maxx, miny, maxy = [float(c) for c in bbox] x = (minx + maxx) / 2 y = (miny + maxy) / 2 center = forward_mercator((x, y)) if center[1] == float('-inf'): center = (center[0], 0) if maxx == minx: width_zoom = 15 else: width_zoom = math.log(360 / (maxx - minx), 2) if maxy == miny: height_zoom = 15 else: height_zoom = math.log(360 / (maxy - miny), 2) mapobj.center_x = center[0] mapobj.center_y = center[1] mapobj.zoom = math.ceil(min(width_zoom, height_zoom)) mapobj.save() return HttpResponse('OK', status=200)