Changeset 2446


Ignore:
Timestamp:
May 14, 2014, 3:21:37 AM (10 years ago)
Author:
rouault
Message:

add_esri_column.py: add an alternative method to get ESRI datum names based on FileGDB SDK

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libgeotiff/csv/add_esri_column.py

    r1646 r2446  
    11#!/usr/bin/env python
     2# -*- coding: utf-8 -*-
    23#******************************************************************************
    34#  $Id$
     
    3132#******************************************************************************
    3233#
    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)
    4039
    4140import string
     
    4645import csv_tools
    4746
     47
     48epsg_prj_path = '/usr3/data/esri/prj/epsg'
     49
     50def 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
     60def 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
    4895gcs_table = csv_tools.CSVTable()
    4996gcs_table.read_from_csv( 'gcs.csv' )
     
    5299datum_table.read_from_csv( 'datum.csv' )
    53100
    54 print '%d GCS defined.', len(gcs_table.data.keys())
     101print('%d GCS defined.' % len(gcs_table.data.keys()))
    55102
    56103esri_gcs_names = {}
    57104esri_datum_names = {}
    58105
    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
     106try:
     107    os.stat(epsg_prj_path)
     108    prj_epsg_exists = True
     109except:
     110    prj_epsg_exists = False
     111    pass
    67112
    68     try:
    69         esri_gcs_wkt = open(filename).read()
    70     except:
    71         print 'Failed to find ', filename
    72         continue
     113filegdb_method = False
     114if 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
    73118
    74     srs = osr.SpatialReference()
    75     srs.ImportFromWkt( esri_gcs_wkt )
     119if 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)
    76127
    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
    79134
    80     esri_gcs_names[gcs_code] = gcs_name
     135        srs = osr.SpatialReference()
     136        srs.ImportFromWkt( esri_gcs_wkt )
    81137
    82     print 'GCS %d = %s, %s' % (gcs_code, gcs_name, datum_name)
     138        gcs_name = srs.GetAttrValue( 'GEOGCS' )
     139        datum_name = srs.GetAttrValue( 'DATUM' )
    83140
    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
    87142
    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'
     152else:
     153    print 'WARNING: ESRI_DATUM_NAME column will be empty !'
    91154
    92155#
     
    119182
    120183datum_table.write_to_csv( 'gdal_datum.csv' )
    121 
    122 
    123 
    124    
    125 
    126    
Note: See TracChangeset for help on using the changeset viewer.