[gdal-dev] [python-ogr] Transform datasource problems

Frank Broniewski brfr at metrico.lu
Wed Feb 9 02:54:14 EST 2011


Hello,

I have trouble transforming a datasource from one srs to another. I am 
using the python bindings and gdal 1.6.0 on Windows. Problem is, 
whatever I do to the datasource and the contained feature geometries, 
the coordinates just don't get transformed. My code, from what I've 
figured out so far:

def transform_datasource(datasource, source_epsg_id, target_epsg_id):
     source_srs = osr.SpatialReference()
     source_srs.ImportFromEPSG(source_epsg_id)
     target_srs = osr.SpatialReference()
     target_srs.ImportFromEPSG(target_epsg_id)
     transformation = osr.CoordinateTransformation(source_srs, target_srs)

     target_driver = ogr.GetDriverByName('Memory')
     target_datasource = target_driver.CreateDataSource('')

     for layer_index in xrange(datasource.GetLayerCount()):
         source_layer = datasource.GetLayer(layer_index)
         target_datasource.CopyLayer(source_layer, source_layer.GetName())
         target_layer = target_datasource.GetLayer(layer_index)
         for feature_index in xrange(source_layer.GetFeatureCount()):
             source_feature = source_layer.GetFeature(feature_index)
             target_feature = target_layer.GetFeature(feature_index)
             geometry = source_feature.GetGeometryRef()
             target_geometry = geometry.Clone()
             target_geometry.Transform(transformation)
             target_feature.SetGeometry(target_geometry)

     return target_datasource


My testing setup: Load a Shapefile, transform it from epsg 31466 to 4326 
and save as GeoJSON. Saving as GeoJSON works, but not the transformation:
     shp_path = 'xyz.shp'
     shp_driver = driver_by_name('ESRI Shapefile')
     shp_datasource = open_datasource(shp_driver, shp_path, False)

     path1 = '%s.geojson' % ''.join(random.choice(string.ascii_letters) 
for i in xrange(7))

     # see function above
     transformed = transform_datasource(shp_datasource, 31466, 4326)
     geojson_datasource = convert_datasource(transformed, 'GeoJSON', path1)
     transformed.Release()
     geojson_datasource.Release()

Many thanks for tipps, why nothing happens when transforming. I looked 
already at the python version of ogr2ogr, but I don't understand well 
what's going on in there ...

Frank



-- 
Frank BRONIEWSKI

METRICO s.à r.l.
géomètres
technologies d'information géographique
rue des Romains 36
L-5433 NIEDERDONVEN

tél.: +352 26 74 94 - 28
fax.: +352 26 74 94 99
http://www.metrico.lu


More information about the gdal-dev mailing list