[gdal-dev] Update existing shape file

Mateusz Loskot mateusz at loskot.net
Sun Aug 3 08:11:08 EDT 2008


Frank Warmerdam wrote:
> 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.


Frank,

All it makes sense. I've forgot that feature object has no
reference to its parent layer.

> 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).

Yes, I got it.

Thanks for the reminding explanation.

Best regards,
-- 
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org


More information about the gdal-dev mailing list