Package osgeo :: Package utils :: Module epsg_tr |
[hide private]
[frames] | no frames]

# Source Code for Module osgeo.utils.epsg_tr

      1  #!/usr/bin/env python3
      2  # -*- coding: utf-8 -*-
      3  # ******************************************************************************
      4  #  $Id$
      5  #
      6  #  Project:  CFS OGC MapServer
      7  #  Purpose:  Script to create WKT and PROJ.4 dictionaries for EPSG GCS/PCS
      8  #            codes.
      9  #  Author:   Frank Warmerdam, warmerdam@pobox.com
     10  #
     11  # ******************************************************************************
     12  #  Copyright (c) 2001, Frank Warmerdam
     13  #  Copyright (c) 2009-2010, 2019, Even Rouault <even dot rouault at spatialys.com>
     14  #
     15  #  Permission is hereby granted, free of charge, to any person obtaining a
     16  #  copy of this software and associated documentation files (the "Software"),
     17  #  to deal in the Software without restriction, including without limitation
     18  #  the rights to use, copy, modify, merge, publish, distribute, sublicense,
     19  #  and/or sell copies of the Software, and to permit persons to whom the
     20  #  Software is furnished to do so, subject to the following conditions:
     21  #
     22  #  The above copyright notice and this permission notice shall be included
     23  #  in all copies or substantial portions of the Software.
     24  #
     25  #  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     26  #  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     27  #  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
     28  #  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     29  #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     30  #  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     31  #  DEALINGS IN THE SOFTWARE.
     32  # ******************************************************************************
     33
     34  import sys
     35
     36  from osgeo import osr
     37  from osgeo import gdal
     38
     39  # =============================================================================
     40
     41
     42 -def Usage():
     43
     44      print('Usage: epsg_tr.py [-wkt] [-pretty_wkt] [-proj4] [-xml] [-postgis]')
     45      print('                  [-authority name]')
     46      sys.exit(1)
     47
     48  # =============================================================================
     49
     50
     51 -def trHandleCode(set_srid, srs, auth_name, code, deprecated, output_format):
     52
     53      if output_format == '-pretty_wkt':
     54          print('%s:%s' % (auth_name, str(code)))
     55          print(srs.ExportToPrettyWkt())
     56
     57      if output_format == '-xml':
     58          print(srs.ExportToXML())
     59
     60      if output_format == '-wkt':
     61          print('EPSG:%d' % code)
     62          print(srs.ExportToWkt())
     63
     64      if output_format == '-proj4':
     65          out_string = srs.ExportToProj4()
     66
     67          name = srs.GetName()
     68
     69          print('# %s' % name)
     70          if out_string.find('+proj=') > -1:
     71              print('<%s> %s <>' % (str(code), out_string))
     72          else:
     73              print('# Unable to translate coordinate system '
     74                      '%s:%s into PROJ.4 format.' % (auth_name, str(code)))
     75              print('#')
     76
     77      if output_format == '-postgis':
     78
     79          if code in set_srid:
     80              if auth_name == 'ESRI':
     81                  if int(code) < 32767:
     82                      return
     83          assert code not in set_srid, (auth_name, code)
     84          set_srid.add(code)
     85
     86          name = srs.GetName()
     87          if deprecated and 'deprecated' not in name:
     88              name += " (deprecated)"
     89          wkt = srs.ExportToWkt()
     90          proj4text = srs.ExportToProj4()
     91
     92          print('---')
     93          print('--- %s %s : %s' % (auth_name, str(code), name))
     94          print('---')
     95
     96          if proj4text is None or len(proj4text) == 0:
     97              print('-- (unable to translate to PROJ.4)')
     98          else:
     99              wkt = gdal.EscapeString(wkt, scheme=gdal.CPLES_SQL)
    100              proj4text = gdal.EscapeString(proj4text, scheme=gdal.CPLES_SQL)
    101              print('INSERT INTO "spatial_ref_sys" ("srid","auth_name","auth_srid","srtext","proj4text") VALUES (%d,\'%s\',%d,\'%s\',\'%s\');' %
    102                      (int(code), auth_name, int(code), wkt, proj4text))
    103
    104      # INGRES COPY command input.
    105      if output_format == '-copy':
    106
    107          try:
    108              wkt = srs.ExportToWkt()
    109              proj4text = srs.ExportToProj4()
    110
    111              print('%s\t%d%s\t%s\t%d%s\t%d%s\n'
    112                      % (str(code), 4, auth_name, str(code), len(wkt), wkt,
    113                          len(proj4text), proj4text))
    114          except:
    115              pass
    116
    117  # =============================================================================
    118
    119 -def main(argv):
    120      output_format = '-pretty_wkt'
    121      authority = None
    122
    123      argv = gdal.GeneralCmdLineProcessor(argv)
    124      if argv is None:
    125          sys.exit(0)
    126
    127      # Parse command line arguments.
    128
    129      i = 1
    130      while i < len(argv):
    131          arg = argv[i]
    132
    133          if arg == '-wkt' or arg == '-pretty_wkt' or arg == '-proj4' \
    134             or arg == '-postgis' or arg == '-xml' or arg == '-copy':
    135              output_format = arg
    136
    137          elif arg == '-authority':
    138              i = i + 1
    139              authority = argv[i]
    140
    141          elif arg[0] == '-':
    142              Usage()
    143
    144          else:
    145              Usage()
    146
    147          i = i + 1
    148
    149      # Output BEGIN transaction for PostGIS
    150      if output_format == '-postgis':
    151          print('BEGIN;')
    152
    153      # loop over all codes to generate output
    154
    155      if authority:
    156          authorities = [ authority ]
    157      elif output_format == '-postgis' :
    158          authorities = [ 'EPSG', 'ESRI' ]
    159      else:
    160          authorities = [ 'EPSG', 'ESRI', 'IGNF' ]
    161
    162      set_srid = set()
    163      for authority in authorities:
    164          if authority in ('EPSG', 'ESRI'):
    165              set_codes_geographic = set()
    166              set_codes_geographic_3d = set()
    167              set_codes_projected = set()
    168              set_codes_geocentric = set()
    169              set_codes_compound = set()
    170              set_deprecated = set()
    171
    172              for crs_info in osr.GetCRSInfoListFromDatabase(authority):
    173                  code = int(crs_info.code)
    174                  if crs_info.type == osr.OSR_CRS_TYPE_COMPOUND:
    175                      set_codes_compound.add(code)
    176                  elif crs_info.type == osr.OSR_CRS_TYPE_GEOGRAPHIC_3D:
    177                      set_codes_geographic_3d.add(code)
    178                  elif crs_info.type == osr.OSR_CRS_TYPE_GEOGRAPHIC_2D:
    179                      set_codes_geographic.add(code)
    180                  elif crs_info.type == osr.OSR_CRS_TYPE_PROJECTED:
    181                      set_codes_projected.add(code)
    182                  elif crs_info.type == osr.OSR_CRS_TYPE_GEOCENTRIC:
    183                      set_codes_geocentric.add(code)
    184
    185                  if crs_info.deprecated:
    186                      set_deprecated.add(code)
    187
    188              set_codes_geographic = sorted(set_codes_geographic)
    189              set_codes_geographic_3d = sorted(set_codes_geographic_3d)
    190              set_codes_projected = sorted(set_codes_projected)
    191              set_codes_geocentric = sorted(set_codes_geocentric)
    192              set_codes_compound = sorted(set_codes_compound)
    193              for typestr, set_codes in (('Geographic 2D CRS', set_codes_geographic),
    194                                         ('Projected CRS', set_codes_projected),
    195                                         ('Geocentric CRS', set_codes_geocentric),
    196                                         ('Compound CRS', set_codes_compound),
    197                                         ('Geographic 3D CRS', set_codes_geographic_3d)):
    198                  if set_codes and output_format == '-postgis':
    199                      print('-' * 80)
    200                      print('--- ' + authority + ' ' + typestr)
    201                      print('-' * 80)
    202
    203                  for code in set_codes:
    204                      srs = osr.SpatialReference()
    205                      srs.SetFromUserInput(authority + ':' + str(code))
    206                      deprecated = False
    207                      if code in set_deprecated:
    208                          deprecated = True
    209                      trHandleCode(set_srid, srs, authority, str(code), deprecated, output_format)
    210
    211          else:
    212              for crs_info in osr.GetCRSInfoListFromDatabase(authority):
    213                  srs = osr.SpatialReference()
    214                  srs.SetFromUserInput(authority + ':' + crs_info.code)
    215                  trHandleCode(set_srid, srs, authority, crs_info.code, crs_info.deprecated, output_format)
    216
    217      # Output COMMIT transaction for PostGIS
    218      if output_format == '-postgis':
    219          print('COMMIT;')
    220          print('VACUUM ANALYZE spatial_ref_sys;')
    221
    222
    223  if __name__ == '__main__':
    224      sys.exit(main(sys.argv))
    225