FW: [gdal-dev] Shapefile read/write

Mullins, Steven Steven.Mullins at dmme.virginia.gov
Tue Dec 4 10:45:21 EST 2007


Frank,

Thanks for the reply.  It runs without an error, but is not updating the shapefile on disk.  My Python code is below.  Perhaps I am not opening the file correctly.  I can't find any good example to go by.  Every example I found copies the schema and data to a new file using recursion over the fields.  I also found an old list email (2005) that stated that shapefile write-backs did not work in OGR at that time.  A shapefile write-back would be ideal for me, if It can be done.  Do any errors jump out at you?

Thanks,

Steve

###############################################
#!/usr/local/bin/python
# read SHP file and update attributes per coordinates

import ogr
import osr
import string

###############################################
# Data Source
data_source = ogr.Open('./shapefiles83/rainfalls-active/rainfalls-active.shp')
layer_0 = data_source.GetLayer(0)
###############################################

current_feature = layer_0.GetNextFeature()

while current_feature is not None:
    current_point = current_feature.GetGeometryRef()
    current_feature.SetField ( 0, current_point.GetX())
    current_feature.SetField ( 1, current_point.GetY())
    current_feature = layer_0.GetNextFeature()
layer_0.SyncToDisk()
data_source.Destroy()
#################################################













-----Original Message-----
From: gdal-dev-bounces at lists.osgeo.org
[mailto:gdal-dev-bounces at lists.osgeo.org]On Behalf Of Frank Warmerdam
Sent: Tue, December 04, 2007 10:09 AM
To: Mullins, Steven
Cc: gdal-dev at lists.osgeo.org
Subject: Re: FW: [gdal-dev] Shapefile read/write


Mullins, Steven wrote:
> Thanks again.  I inserted data_source.SyncToDisk() just before the data_source.Destroy() and got the following message at run-time: 
> 
> Traceback (most recent call last):
>   File "test.py", line 38, in ?
>     data_source.SyncToDisk()
> AttributeError: DataSource instance has no attribute 'SyncToDisk'
> 
> Is SyncToDisk() supported in Python?

Steven,

SyncToDisk() is a method on the layer, not the datasource.

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

_______________________________________________
gdal-dev mailing list
gdal-dev at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list