[gdal-dev] Update existing shape file

Frank Warmerdam warmerdam at pobox.com
Sat Aug 2 09:29:53 EDT 2008


Mateusz Loskot wrote:
> Frank Warmerdam wrote:
>> wow wrote:
>>> Hi,
>>>   I wanna set new field value to an existing shape file which has been
>>> opened with update access, but it doesn't work, my code like this:
>>>  
>>>   OGRFeature* pFeature = pLayer->GetFeature(2);   int nIdx =
>>> pFeature->GetFieldIndex("Name");
>>>  pFeature->UnsetField(nIdx);
>>>  pFeature->SetField( "Name", "hanJ reiver" );
>>>  
>>>   Anyone give me some advices?
>> Chen Xuexia,
>>
>> After modifying the in memory feature, you need to write it back to the
>> file using OGRLayer::SetFeature().
> 
> Frank,
> 
> SyncToDisk has no effect here?

Mateusz,

When you read a feature with GetFeature(), and then modify that feature
you are modifying an essentially "free standing" feature - not a proxy for
a feature in the layer.  There is no presumed effect on the layer till you
write the feature back using SetFeature().  After that SyncToDisk() might
be helpful to ensure the feature isn't being cached by the driver - that is
to ensure it actually gets written to disk.

The important distinction here is that GetFeature(), and GetNextFeature()
are returning a feature that you can modify, etc without having any effect
on the layer.  They are essentially free standing (though they do depend
on the definition of the layer - so you really should not add fields to
a layer when features exist that use it's definition for instance).

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    | President OSGeo, http://osgeo.org



More information about the gdal-dev mailing list