Added "transform" utility function to transform geographic coordinates
authorThierry Florac <thierry.florac@onf.fr>
Fri, 26 Jan 2018 15:59:12 +0100
changeset 17 2ad08dd851e2
parent 16 5755c811bf11
child 18 8eef3d17a06f
Added "transform" utility function to transform geographic coordinates
src/pyams_gis/rpc/json/__init__.py
src/pyams_gis/transform.py
--- a/src/pyams_gis/rpc/json/__init__.py	Fri Jan 26 15:57:57 2018 +0100
+++ b/src/pyams_gis/rpc/json/__init__.py	Fri Jan 26 15:59:12 2018 +0100
@@ -23,59 +23,48 @@
 # import interfaces
 
 # import packages
+from pyams_gis.transform import transform
 from pyramid_rpc.jsonrpc import jsonrpc_method
 
 
 @jsonrpc_method(endpoint='gis')
-def transformPoint(request, point, from_projection, to_projection):
+def transformPoint(request, point, from_srid, to_srid):
     """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
+    :param from_srid: source coordinates system given as SRID
+    :param to_srid: 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}
+    return transform(point, from_srid, to_srid)
 
 
 @jsonrpc_method(endpoint='gis')
-def transformArea(request, area, from_projection, to_projection):
+def transformArea(request, area, from_srid, to_srid):
     """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
+    :param from_srid: source coordinates system given as SRID
+    :param to_srid: 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}
+    from_srid = int(from_srid)
+    to_srid = int(to_srid)
+    if (not have_gdal) or (from_srid == to_srid):
+        return {
+            'area': area,
+            'srid': from_srid
+        }
+    point1 = transform(area['x1'], area['y1'])
+    point2 = transform(area['x2'], area['y2'])
+    return {
+        'area': {
+            'x1': point1['longitude'],
+            'y1': point1['latitude'],
+            'x2': point2['longitude'],
+            'y2': point2['latitude']
+        },
+        'srid': to_srid
+    }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_gis/transform.py	Fri Jan 26 15:59:12 2018 +0100
@@ -0,0 +1,65 @@
+#
+# Copyright (c) 2008-2015 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
+
+
+def transform(point, from_srid, to_srid):
+    """Transform point coordinates from source projection to another projection
+
+    :param point: source point coordinates; can be given as a (longitude, latitude) tuple
+        or as a mapping containing both keys
+    :param from_srid: source coordinates system given as SRID
+    :param to_srid: target coordinates system given as SRID
+    :return: mapping with new 'point' coordinates containing transformed coordinates, and 'projection'
+        key containing SRID of result projection system
+    """
+    longitude = None
+    latitude = None
+    if isinstance(point, tuple):
+        longitude, latitude = point
+    elif isinstance(point, dict):
+        longitude = point['longitude']
+        latitude = point['latitude']
+    from_srid = int(from_srid)
+    to_srid = int(to_srid)
+    if (not have_gdal) or (from_srid == to_srid):
+        return {
+            'point': {
+                'longitude': longitude,
+                'latitude': latitude
+            },
+            'srid': from_srid
+        }
+    source = SpatialReference()
+    source.ImportFromEPSG(from_srid)
+    destination = SpatialReference()
+    destination.ImportFromEPSG(to_srid)
+    transformed = CoordinateTransformation(source, destination).TransformPoint(longitude, latitude)
+    return {
+        'point': {
+            'longitude': transformed[0],
+            'latitude': transformed[1]
+        },
+        'srid': to_srid
+    }