1

I want to save my 'feat' - selected by criteria polygons in shp.file to save in diferent shp.file on the disk by using SyncToDisk() in osgeo for vector. Code gives me error: AttributeError: SyncToDisk.

What can be wrong?

import osgeo, os
from osgeo import ogr, gdal

shpFile = r'C:\Users\ReBurns.shp'

driver = ogr.GetDriverByName('ESRI Shapefile')

ptsDS = driver.Open(shpFile)
ptsLayer = ptsDS.GetLayer()

#Passing criteria  to select polygons:    
myQuery = 'Area_ha > 39000.00'
ptsLayer.SetAttributeFilter(myQuery)
print ptsLayer
ptsLayer.GetFeatureCount()

for i in range(0, ptsLayer.GetFeatureCount()):
    pt = ptsLayer.GetNextFeature()
    print pt.GetField('Area_ha')

for i in range( 0, ptsLayer.GetFeatureCount()):
    feat = ptsLayer.GetFeature( i )
    print feat
feat.SyncToDisk(r'C:\Users\ReBurns_Diss_Alb111.shp')
feat = None
  • 1
    It is saying that there is no method called SyncToDisk on the object you are calling. I don't get what you are trying to do, so perhaps you need some more explanation. I think you probably want to sync a data source, not the feature. – BradHards Apr 15 '16 at 5:31
2

SyncToDisk is a method of a Layer object, not a feature. It doesn't take any parameters and is not used to save the layer to a new dataset on disk (which I presume you are trying to do). From a quick scan of the documentation, it appears to be used when you are editing/updating a layer and want to commit those changes:

SyncToDisk

Flush pending changes to disk.

This call is intended to force the layer to flush any pending writes to disk, and leave the disk file in a consistent state.

To copy your layer with the attribute filter applied, you could do something like:

outds = driver.CreateDataSource(r'C:\Users\ReBurns_Diss_Alb111.shp')
outlyr = outds.CopyLayer(ptsLayer,'ReBurns_Diss_Alb111')

Note that this code

for i in range( 0, ptsLayer.GetFeatureCount()):
    etc...

can (and probably will) produce unexpected results, especially with a filter applied, as the GetFeature method does not get features by index, but by FID (feature ID). And to make things worse... I think it will ignore any filters...

To loop over the features in the filtered layer, just use:

for feat in ptsLayer:
    etc...
  • Thank you, but there are No any features in the file 'ReBurns_Diss_Alb111'? When I use outds = driver.CreateDataSource(r'C:\Users\ReBurns_Diss_Alb111.shp') outlyr = outds.CopyLayer(ptsLayer,'ReBurns_Diss_Alb111') What can be a problem? . – Adams Apr 23 '16 at 21:41
  • @Adams post a new question – user2856 Apr 23 '16 at 21:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.