--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_gis/rpc/json/__init__.py Thu May 18 17:23:48 2017 +0200
@@ -0,0 +1,81 @@
+#
+# Copyright (c) 2008-2017 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+try:
+ from osgeo.osr import SpatialReference, CoordinateTransformation
+ have_gdal = True
+except ImportError:
+ have_gdal = False
+
+# import interfaces
+
+# import packages
+from pyramid_rpc.jsonrpc import jsonrpc_method
+
+
+@jsonrpc_method(endpoint='gis')
+def transformPoint(request, point, from_projection, to_projection):
+ """Transform point given in source projection to another projection
+
+ :param point: point coordinates given as a mapping with 'longitude' and 'latitude' values
+ :param from_projection: source coordinates system given as SRID
+ :param to_projection: target coordinates system given as SRID
+ :return: mapping with new 'point' key containing transformed coordinates, and 'projection'
+ key containing SRID of result projection system
+ """
+ from_projection = int(from_projection)
+ to_projection = int(to_projection)
+ if (not have_gdal) or (from_projection == to_projection):
+ return {'point': point,
+ 'projection': from_projection}
+ source = SpatialReference()
+ source.ImportFromEPSG(from_projection)
+ destination = SpatialReference()
+ destination.ImportFromEPSG(to_projection)
+ target = CoordinateTransformation(source, destination).TransformPoint(point['longitude'],
+ point['latitude'])
+ return {'point': {'longitude': target[0],
+ 'latitude': target[1]},
+ 'projection': to_projection}
+
+
+@jsonrpc_method(endpoint='gis')
+def transformArea(request, area, from_projection, to_projection):
+ """Transform area given in source projection to another projection
+
+ :param area: area coordinates given as a mapping with 'x1', 'y1', 'x2', and 'y2' values
+ :param from_projection: source coordinates system given as SRID
+ :param to_projection: target coordinates system given as SRID
+ :return: mapping with new 'area' key containing transformed coordinates, and 'projection'
+ key containing SRID of result projection system
+ """
+ from_projection = int(from_projection)
+ to_projection = int(to_projection)
+ if (not have_gdal) or (from_projection == to_projection):
+ return {'area': area,
+ 'projection': from_projection}
+ source = SpatialReference()
+ source.ImportFromEPSG(from_projection)
+ destination = SpatialReference()
+ destination.ImportFromEPSG(to_projection)
+ transformation = CoordinateTransformation(source, destination)
+ point1 = transformation.TransformPoint(area['x1'], area['y1'])
+ point2 = transformation.TransformPoint(area['x2'], area['y2'])
+ return {'area': {'x1': point1[0],
+ 'y1': point1[1],
+ 'x2': point2[0],
+ 'y2': point2[1]},
+ 'projection': to_projection}