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