Changeset 2446
- Timestamp:
- May 14, 2014, 3:21:37 AM (10 years ago)
- File:
-
- 1 edited
-
trunk/libgeotiff/csv/add_esri_column.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/libgeotiff/csv/add_esri_column.py
r1646 r2446 1 1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 2 3 #****************************************************************************** 3 4 # $Id$ … … 31 32 #****************************************************************************** 32 33 # 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 # 34 35 # This scritps depends on a set of ArcGIS .prj files that are usually only 36 # available on Frank's machine. But it can 37 # also work with an alternate method if you have OGR configured with FileGDB and 38 # OpenFileGDB drivers (GDAL >= 1.11) 40 39 41 40 import string … … 46 45 import csv_tools 47 46 47 48 epsg_prj_path = '/usr3/data/esri/prj/epsg' 49 50 def get_esri_wkt_from_gcs_code_from_epsg_prj(gcs_code): 51 filename = '%s/%d.prj' % (epsg_prj_path, gcs_code) 52 53 try: 54 esri_gcs_wkt = open(filename).read() 55 return esri_gcs_wkt 56 except: 57 print 'Failed to find ', filename 58 return None 59 60 def get_esri_wkt_from_gcs_code_from_filegdb(gcs_code): 61 62 import shutil 63 try: 64 shutil.rmtree('tmp.gdb') 65 except: 66 pass 67 from osgeo import ogr 68 ds = ogr.GetDriverByName('FileGDB').CreateDataSource('tmp.gdb') 69 srs = osr.SpatialReference() 70 # Fake WKT: the important thing is the EPSG code 71 srs.ImportFromWkt( """GEOGCS["BLA",DATUM["BLA",SPHEROID["BLA",1,0]],AUTHORITY["EPSG","%d"]]""" % gcs_code ) 72 lyr = ds.CreateLayer('foo', geom_type = ogr.wkbPoint, srs = srs) 73 ds = None 74 75 # Read with OpenFileGDB driver ! 76 ds = ogr.Open('tmp.gdb/a00000003.gdbtable') 77 lyr = ds.GetLayer(0) 78 # Skip first WKT that is always EPSG:4326 79 feat = lyr.GetNextFeature() 80 # Here's the interesting WKT 81 feat = lyr.GetNextFeature() 82 if feat is not None: 83 ret = feat.GetField('SRTEXT') 84 else: 85 ret = None 86 ds = None 87 88 try: 89 shutil.rmtree('tmp.gdb') 90 except: 91 pass 92 93 return ret 94 48 95 gcs_table = csv_tools.CSVTable() 49 96 gcs_table.read_from_csv( 'gcs.csv' ) … … 52 99 datum_table.read_from_csv( 'datum.csv' ) 53 100 54 print '%d GCS defined.', len(gcs_table.data.keys())101 print('%d GCS defined.' % len(gcs_table.data.keys())) 55 102 56 103 esri_gcs_names = {} 57 104 esri_datum_names = {} 58 105 59 # 60 # First try looking up all the EPSG GCS codes in the ESRI "epsg" directory, 61 # and using those examples to correspond an EPSG datum code with an ESRI datum 62 # name ... note that datums that aren't used in the EPSG GCS set will be missed. 63 # 64 for gcs_code in gcs_table.data.keys(): 65 gcs_code = int(gcs_code) 66 filename = '/usr3/data/esri/prj/epsg/%d.prj' % gcs_code 106 try: 107 os.stat(epsg_prj_path) 108 prj_epsg_exists = True 109 except: 110 prj_epsg_exists = False 111 pass 67 112 68 try: 69 esri_gcs_wkt = open(filename).read() 70 except:71 print 'Failed to find ', filename72 continue113 filegdb_method = False 114 if not prj_epsg_exists: 115 from osgeo import ogr 116 if ogr.GetDriverByName('FileGDB') is not None and ogr.GetDriverByName('OpenFileGDB') is not None: 117 filegdb_method = True 73 118 74 srs = osr.SpatialReference() 75 srs.ImportFromWkt( esri_gcs_wkt ) 119 if prj_epsg_exists or filegdb_method: 120 # 121 # First try looking up all the EPSG GCS codes in the ESRI "epsg" directory, 122 # and using those examples to correspond an EPSG datum code with an ESRI datum 123 # name ... note that datums that aren't used in the EPSG GCS set will be missed. 124 # 125 for gcs_code in gcs_table.data.keys(): 126 gcs_code = int(gcs_code) 76 127 77 gcs_name = srs.GetAttrValue( 'GEOGCS' ) 78 datum_name = srs.GetAttrValue( 'DATUM' ) 128 if prj_epsg_exists: 129 esri_gcs_wkt = get_esri_wkt_from_gcs_code_from_epsg_prj(gcs_code) 130 else: 131 esri_gcs_wkt = get_esri_wkt_from_gcs_code_from_filegdb(gcs_code) 132 if esri_gcs_wkt is None: 133 continue 79 134 80 esri_gcs_names[gcs_code] = gcs_name 135 srs = osr.SpatialReference() 136 srs.ImportFromWkt( esri_gcs_wkt ) 81 137 82 print 'GCS %d = %s, %s' % (gcs_code, gcs_name, datum_name) 138 gcs_name = srs.GetAttrValue( 'GEOGCS' ) 139 datum_name = srs.GetAttrValue( 'DATUM' ) 83 140 84 try: 85 gcs_rec = gcs_table.get_record( gcs_code ) 86 datum_code = int(gcs_rec['DATUM_CODE']) 141 esri_gcs_names[gcs_code] = gcs_name 87 142 88 esri_datum_names[datum_code] = datum_name 89 except: 90 print 'Failed to get gcs record, or datum info' 143 print 'GCS %d = %s, %s' % (gcs_code, gcs_name, datum_name) 144 145 try: 146 gcs_rec = gcs_table.get_record( gcs_code ) 147 datum_code = int(gcs_rec['DATUM_CODE']) 148 149 esri_datum_names[datum_code] = datum_name 150 except: 151 print 'Failed to get gcs record, or datum info' 152 else: 153 print 'WARNING: ESRI_DATUM_NAME column will be empty !' 91 154 92 155 # … … 119 182 120 183 datum_table.write_to_csv( 'gdal_datum.csv' ) 121 122 123 124 125 126
Note:
See TracChangeset
for help on using the changeset viewer.
