2

When adding a field to a shapefile using the ogr package in python, the program crashes at the last step which is "SetFeature". This problem has been reported before, but I have not been able to resolve it based on other posts.

Here is a sample code where the dummy value '100' is added as a new field for all features in the shapefile, followed by related questions and things I tried.

Any idea what I could try ? Thank you for your time.

SDB

*EDIT : There was a conflict between this code and a module I wrote. It is hard to reproduce, so I'm settling for aworkaround : import the vexating module after the code below is used. Thank you all for your insight and time !

# Load shapefile
shapefile = ogr.Open(shapefile_path, 1)

layer = shapefile.GetLayer()
layer_defn = layer.GetLayerDefn()


new_field_defn = ogr.FieldDefn("New_field", ogr.OFTReal)
new_field_defn.SetWidth(50)
new_field_defn.SetPrecision(11)
layer.CreateField(new_field_defn)


# Walk through shapefile, setting new field for each feature
for feature in layer :
    geometry = feature.GetGeometryRef()             

    band_value = 100
    feature.SetField("New_field",np.double(band_value))

    # This is the line that crashes the program
    layer.SetFeature(feature)

shapefile.Destroy()

Using this solution did not work : https://gis.stackexchange.com/questions/109194/setfeature-creates-infinite-loop-when-updating-sqlite-feature-using-ogr

I believe my code follows this example : http://www.digital-geography.com/create-and-edit-shapefiles-with-python-only/#.V_0YtPnhCUk

I think I've avoided the related Gotcha http://trac.osgeo.org/gdal/wiki/PythonGotchas#PythonbindingsdonotraiseexceptionsunlessyouexplicitlycallUseExceptions

Community
  • 1
  • 1
  • What is the error printed at the crash? I tried your code on a shapefile and it worked without a problem. Are you sure you have write access to the file? – Logan Byers Oct 11 '16 at 19:10
  • There's no error message, python (run from Sublime Text 2) hangs and then crashes printing only the system path. I'll check the permissions ! Thank you. – S Dufour-Beauséjour Oct 11 '16 at 20:37
  • I can't reproduce it with both python 2 and 3; maybe you could indicate the version of python / ogr you are using. Also are you sure that the value you are trying to set isn't larger that what you defined for this field ? (even if its clearly shouldn't crash your python interpreter) – mgc Oct 11 '16 at 22:06
  • It turns out my code also works for me when run as a standalone, but when I try to define it as a function - say "add_data_to_shapefile" - and call it in a main script (which is how I have it set up), it crashes. The function works fine when run locally with "if __name__ == '__main__': add_data_to_shapefile()". But when i call add_data_to_shapefile() in another script python crashes. Any ideas ? – S Dufour-Beauséjour Oct 11 '16 at 22:11
  • Not really, hard to say without seeing the context / the problematic code as, as you pointed, there is some gotchas in ogr/gdal bindings. Good luck anyway! – mgc Oct 11 '16 at 22:34
  • @mgc : Thank you for your answer. I'm using python 2.7 and gdal/ogr 2.0.3. I don't think the cause is the length of what I'm writing, particularly now that I know the program works when run as a standalone. – S Dufour-Beauséjour Oct 11 '16 at 22:40
  • As the snippet of code you provided is working, you will probably obtain more help with a [minimal, complete and verifiable example](http://stackoverflow.com/help/mcve) as you seems to know how to reproduce the problem. – mgc Oct 11 '16 at 22:52

0 Answers0