1 | #!/usr/bin/env python |
---|
2 | #****************************************************************************** |
---|
3 | # $Id: epsg_tr.py 18194 2009-12-06 20:07:45Z rouault $ |
---|
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, warmerdam@pobox.com |
---|
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 ',code,', either not a valid EPSG') |
---|
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:',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:',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 | # ============================================================================= |
---|
120 | |
---|
121 | if __name__ == '__main__': |
---|
122 | |
---|
123 | start_code = -1 |
---|
124 | end_code = -1 |
---|
125 | list_file = None |
---|
126 | output_format = '-pretty_wkt' |
---|
127 | report_error = 1 |
---|
128 | |
---|
129 | argv = gdal.GeneralCmdLineProcessor( sys.argv ) |
---|
130 | if argv is None: |
---|
131 | sys.exit( 0 ) |
---|
132 | |
---|
133 | # Parse command line arguments. |
---|
134 | |
---|
135 | i = 1 |
---|
136 | while i < len(argv): |
---|
137 | arg = argv[i] |
---|
138 | |
---|
139 | if arg == '-wkt' or arg == '-pretty_wkt' or arg == '-proj4' \ |
---|
140 | or arg == '-postgis' or arg == '-xml': |
---|
141 | output_format = arg |
---|
142 | |
---|
143 | elif arg[:5] == '-skip': |
---|
144 | report_error = 0 |
---|
145 | |
---|
146 | elif arg == '-list' and i < len(argv)-1: |
---|
147 | i = i + 1 |
---|
148 | list_file = argv[i] |
---|
149 | |
---|
150 | elif arg[0] == '-': |
---|
151 | Usage() |
---|
152 | |
---|
153 | elif int(arg) > 0: |
---|
154 | |
---|
155 | if start_code == -1: |
---|
156 | start_code = int(arg) |
---|
157 | end_code = int(arg) |
---|
158 | elif end_code == start_code: |
---|
159 | end_code = int(arg) |
---|
160 | else: |
---|
161 | Usage() |
---|
162 | else: |
---|
163 | Usage() |
---|
164 | |
---|
165 | i = i + 1 |
---|
166 | |
---|
167 | # Output BEGIN transaction for PostGIS |
---|
168 | if output_format == '-postgis': |
---|
169 | print('BEGIN;') |
---|
170 | |
---|
171 | # Do we need to produce a single output definition, or include a |
---|
172 | # dictionary line for each entry? |
---|
173 | |
---|
174 | gen_dict_line = start_code != end_code |
---|
175 | |
---|
176 | # loop over all codes to generate output |
---|
177 | |
---|
178 | prj_srs = osr.SpatialReference() |
---|
179 | |
---|
180 | if start_code != -1: |
---|
181 | for code in range(start_code,end_code+1): |
---|
182 | trHandleCode(code, gen_dict_line, report_error, output_format) |
---|
183 | |
---|
184 | # loop over codes read from list file. |
---|
185 | |
---|
186 | elif list_file is not None: |
---|
187 | |
---|
188 | list_fd = open( list_file ) |
---|
189 | line = list_fd.readline() |
---|
190 | while len(line) > 0: |
---|
191 | try: |
---|
192 | c_offset = line.find(',') |
---|
193 | if c_offset > 0: |
---|
194 | line = line[:c_offset] |
---|
195 | |
---|
196 | code = string.atoi(line) |
---|
197 | except: |
---|
198 | code = -1 |
---|
199 | |
---|
200 | if code != -1: |
---|
201 | trHandleCode(code, gen_dict_line, report_error, output_format) |
---|
202 | |
---|
203 | line = list_fd.readline() |
---|
204 | |
---|
205 | else: |
---|
206 | Usage() |
---|
207 | |
---|
208 | # Output COMMIT transaction for PostGIS |
---|
209 | if output_format == '-postgis': |
---|
210 | print('COMMIT;') |
---|
211 | print('VACUUM ANALYZE spatial_ref_sys;') |
---|
212 | |
---|
213 | |
---|