Ticket #4378: add_esri_column.py

File add_esri_column.py, 4.3 KB (added by etourigny, 4 years ago)
Line 
1#!/usr/bin/env python
2#******************************************************************************
3#  $Id$
4#
5#  Project:  GDAL
6#  Purpose:  This script will append an ESRI_DATUM_NAME field to the
7#            datum file.  This is just used by GDAL for morphToESRI(), and
8#            isn't really related to GeoTIFF at all.
9#  Author:   Frank Warmerdam, warmerdam@pobox.com
10#
11#******************************************************************************
12#  Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com>
13#
14#  Permission is hereby granted, free of charge, to any person obtaining a
15#  copy of this software and associated documentation files (the "Software"),
16#  to deal in the Software without restriction, including without limitation
17#  the rights to use, copy, modify, merge, publish, distribute, sublicense,
18#  and/or sell copies of the Software, and to permit persons to whom the
19#  Software is furnished to do so, subject to the following conditions:
20#
21#  The above copyright notice and this permission notice shall be included
22#  in all copies or substantial portions of the Software.
23#
24#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25#  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27#  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30#  DEALINGS IN THE SOFTWARE.
31#******************************************************************************
32#
33# $Log$
34# Revision 1.2  2004/03/20 07:52:44  warmerda
35# use local paths
36#
37# Revision 1.1  2003/06/20 21:28:24  warmerda
38# New
39#
40
41import string
42import sys
43import osr
44
45sys.path.append( '.' )
46import csv_tools
47import csv
48import os
49
50gcs_table = csv_tools.CSVTable()
51gcs_table.read_from_csv( 'gcs.csv' )
52
53datum_table = csv_tools.CSVTable()
54datum_table.read_from_csv( 'datum.csv' )
55
56print '%d GCS defined.', len(gcs_table.data.keys())
57
58esri_gcs_names = {}
59esri_datum_names = {}
60
61#convert esri_gcs.csv to *.prj files
62
63if not os.path.exists('tmp'):
64    os.mkdir('tmp')
65csv_reader = csv.DictReader(open('esri_gcs.csv'),delimiter=';')
66
67for iline in csv_reader:
68    epsg_code = iline['COORD_REF_SYS_CODE']
69    f = open('tmp/'+epsg_code+'.prj','w')
70    f.write(iline['WKT'])
71    f.close()
72
73#
74# First try looking up all the EPSG GCS codes in the ESRI "epsg" directory,
75# and using those examples to correspond an EPSG datum code with an ESRI datum
76# name ... note that datums that aren't used in the EPSG GCS set will be missed.
77#
78for gcs_code in gcs_table.data.keys():
79    gcs_code = int(gcs_code)
80    filename = 'tmp/%d.prj' % gcs_code
81
82    try:
83        esri_gcs_wkt = open(filename).read()
84    except:
85        print 'Failed to find ', filename
86        continue
87
88    srs = osr.SpatialReference()
89    srs.ImportFromWkt( esri_gcs_wkt )
90
91    gcs_name = srs.GetAttrValue( 'GEOGCS' )
92    datum_name = srs.GetAttrValue( 'DATUM' )
93
94    esri_gcs_names[gcs_code] = gcs_name
95
96    print 'GCS %d = %s, %s' % (gcs_code, gcs_name, datum_name)
97
98    try:
99        gcs_rec = gcs_table.get_record( gcs_code )
100        datum_code = int(gcs_rec['DATUM_CODE'])
101
102        esri_datum_names[datum_code] = datum_name
103    except:
104        print 'Failed to get gcs record, or datum info'
105
106#
107# Now add mappings from the manual override table esri_datum_override.csv
108#
109overrides = csv_tools.CSVTable()
110overrides.read_from_csv( 'esri_datum_override.csv' )
111
112for datum_code in overrides.data.keys():
113    record = overrides.get_record( datum_code )
114    esri_datum_names[datum_code] = record['ESRI_DATUM_NAME']
115
116# Make sure the output datum table has the ESRI_DATUM_NAME field   
117
118if 'ESRI_DATUM_NAME' not in datum_table.fields:
119    datum_table.add_field( 'ESRI_DATUM_NAME' )
120
121# Loop over all datums, adding the esri name where we have it.
122
123for datum_code in datum_table.data.keys():
124    datum_rec = datum_table.get_record( datum_code )
125    if esri_datum_names.has_key(datum_code):
126        print 'match for ', datum_code
127        datum_rec['ESRI_DATUM_NAME'] = esri_datum_names[datum_code]
128    else:
129        print 'no match for ', datum_code
130        datum_rec['ESRI_DATUM_NAME'] = ''
131
132    datum_table.set_record( datum_code, datum_rec )
133
134datum_table.write_to_csv( 'gdal_datum.csv' )
135
136
137
138   
139
140