[Gdal-dev] Transforming an ogr layer

Frank Warmerdam fwarmerdam at gmail.com
Wed Sep 7 09:30:26 EDT 2005


On 9/7/05, Ari Jolma <ari.jolma at tkk.fi> wrote:
> Works fine for the vertices but the "name" field is lost from the
> feature and instead there is a new one FID of type Real with seemingly
> integer value. How do I make the feature not loose its existing field?

Ari, 

The perl bindings are looking good!

You need to explicitly create all the desired fields on the
new layer.  In python this looks something like:

shp_layer = shp_ds.CreateLayer( 'out', geom_type = ogr.wkbPolygon,
                                srs = nad83 )

src_defn = poly_layer.GetLayerDefn()
poly_field_count = src_defn.GetFieldCount()

for fld_index in range(poly_field_count):
    src_fd = src_defn.GetFieldDefn( fld_index )
    
    fd = ogr.FieldDefn( src_fd.GetName(), src_fd.GetType() )
    fd.SetWidth( src_fd.GetWidth() )
    fd.SetPrecision( src_fd.GetPrecision() )
    shp_layer.CreateField( fd )

Actually, in retrospect, there is no compelling reason to create
a new field definition, you can use the original one with
CreateField(), so this can likely be shortened to:

shp_layer = shp_ds.CreateLayer( 'out', geom_type = ogr.wkbPolygon,
                                srs = nad83 )

src_defn = poly_layer.GetLayerDefn()
poly_field_count = src_defn.GetFieldCount()

for fld_index in range(poly_field_count):
    src_fd = src_defn.GetFieldDefn( fld_index )
    shp_layer.CreateField( src_fd )

The "FID" field is only created on shapefiles if no other
fields are created.  There seems to be a need for at
least one field in the .dbf file for a shapefile dataset to 
be valid.

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list