[Gdal-dev] Modifying OGR geometry in place

Frank Warmerdam warmerdam at pobox.com
Mon Nov 29 09:59:16 EST 2004


Petteri Packalen wrote:
> Hi
> 
> I'm switching from shapelib to OGR and I don't understand how geometry 
> can be modified in place. My purpose is to modify z-values of the point 
> file.
> 
> The current approach is as follows:
> 
>   1. I create a copy of input file by using CopyDataSource(). This
>      will be the output file where z-value is modified.
> 
>   2. I loop through the features by using GetNextFeature() method.
> 
>   3. This is unclear for me. How can I fetch the geometry object from
>      feature such a way that I can manipulate it and write back to the
>      disk? Or is this even possible (or should I create a completely new
>      feature)?
> 
> It is also unclear for me that what will be the update mode of created
> data source after CopyDataSource()?

Petteri,

Currently the shapefile driver does not support in-place update of shapes
even though it wouldn't be hard to implement for this ultra-simple case of
updating single point geometries in place.

What you will need to do is create a new output datasource, create a layer,
setup the schema and then start copying features over.  But instead of using
CopyDataSource() you will need to copy over one feature at a time so that you
can manipulate the geometry during the translation.

In general you can use ogr2ogr.cpp as a guide.   The new OGR API tutorial may
also be helpful.   A few special notes:

  o You can't just write an OGRFeature from one datasource to another.  You
    need to instantiate a new OGRFeature using the OGRFeatureDefn of the
    output datasource.  The SetFrom() method on the OGRFeature can be useful
    for copying all the contents of an input feature to a destination feature
    with potentially different field definitions.  It is used in ogr2ogr.cpp.

  o Once you have the destination feature you will need to create a copy of
    it's geometry (with GetGeometryRef() and Clone() on the geometry), and
    then modify the Z elevation with setZ(), then use SetGeometryDirectly()
    to reassign the modified geometry to the output feature, replacing the
    old geometry.  In particular, you aren't supposed to just modify the
    geometry returned by GetGeometryRef() directly though that will mostly
    work.

It is my hope to make the Shapefile driver support various kinds of in-place
update methods at some point. Once that is done, you would read a feature,
go through the geometry manipulation mentinoed above, and then call
SetFeature() with the feature to write it back to disk.  But for now
SetFeature() (and DeleteFeature()) are not implemented on the OGRSpatialReference.

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