src/pyams_gis/rpc/json/__init__.py
changeset 0 c73bb834ccbe
child 17 2ad08dd851e2
equal deleted inserted replaced
-1:000000000000 0:c73bb834ccbe
       
     1 #
       
     2 # Copyright (c) 2008-2017 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 try:
       
    18     from osgeo.osr import SpatialReference, CoordinateTransformation
       
    19     have_gdal = True
       
    20 except ImportError:
       
    21     have_gdal = False
       
    22 
       
    23 # import interfaces
       
    24 
       
    25 # import packages
       
    26 from pyramid_rpc.jsonrpc import jsonrpc_method
       
    27 
       
    28 
       
    29 @jsonrpc_method(endpoint='gis')
       
    30 def transformPoint(request, point, from_projection, to_projection):
       
    31     """Transform point given in source projection to another projection
       
    32 
       
    33     :param point: point coordinates given as a mapping with 'longitude' and 'latitude' values
       
    34     :param from_projection: source coordinates system given as SRID
       
    35     :param to_projection: target coordinates system given as SRID
       
    36     :return: mapping with new 'point' key containing transformed coordinates, and 'projection'
       
    37         key containing SRID of result projection system
       
    38     """
       
    39     from_projection = int(from_projection)
       
    40     to_projection = int(to_projection)
       
    41     if (not have_gdal) or (from_projection == to_projection):
       
    42         return {'point': point,
       
    43                 'projection': from_projection}
       
    44     source = SpatialReference()
       
    45     source.ImportFromEPSG(from_projection)
       
    46     destination = SpatialReference()
       
    47     destination.ImportFromEPSG(to_projection)
       
    48     target = CoordinateTransformation(source, destination).TransformPoint(point['longitude'],
       
    49                                                                           point['latitude'])
       
    50     return {'point': {'longitude': target[0],
       
    51                       'latitude': target[1]},
       
    52             'projection': to_projection}
       
    53 
       
    54 
       
    55 @jsonrpc_method(endpoint='gis')
       
    56 def transformArea(request, area, from_projection, to_projection):
       
    57     """Transform area given in source projection to another projection
       
    58 
       
    59     :param area: area coordinates given as a mapping with 'x1', 'y1', 'x2', and 'y2' values
       
    60     :param from_projection: source coordinates system given as SRID
       
    61     :param to_projection: target coordinates system given as SRID
       
    62     :return: mapping with new 'area' key containing transformed coordinates, and 'projection'
       
    63         key containing SRID of result projection system
       
    64     """
       
    65     from_projection = int(from_projection)
       
    66     to_projection = int(to_projection)
       
    67     if (not have_gdal) or (from_projection == to_projection):
       
    68         return {'area': area,
       
    69                 'projection': from_projection}
       
    70     source = SpatialReference()
       
    71     source.ImportFromEPSG(from_projection)
       
    72     destination = SpatialReference()
       
    73     destination.ImportFromEPSG(to_projection)
       
    74     transformation = CoordinateTransformation(source, destination)
       
    75     point1 = transformation.TransformPoint(area['x1'], area['y1'])
       
    76     point2 = transformation.TransformPoint(area['x2'], area['y2'])
       
    77     return {'area': {'x1': point1[0],
       
    78                      'y1': point1[1],
       
    79                      'x2': point2[0],
       
    80                      'y2': point2[1]},
       
    81             'projection': to_projection}