4

Receiving a segfault when running this very short script in Ubuntu.

from osgeo import ogr, osr

shpfile = 'Census_County_TIGER00_IN.shp'

def cust_field(field):
    '''cust_field(shpfile, field) creates a field definition, which, by calling cust_field(), can be used to create a field using the CreateField() function.
    cust_field() DOES NOT create a field -- it simply creates a "model" for a field, that can then be called later. It's weird, but that's GDAL/OGR, as far as I can tell.'''
    fieldDefn = ogr.FieldDefn(field, ogr.OFTInteger)
    fieldDefn.SetWidth(14)
    fieldDefn.SetPrecision(6)
    return fieldDefn

ds = ogr.Open(shpfile, 1)
lyr = ds.GetLayerByIndex(0)
field = cust_field("Test")
lyr.CreateField(field)

Everything runs smoothly until that last line, when iPython, normal shell Python and the IDLE command line all dump to a segmentation fault. Is this an error on my end or an issue with the underlying C that I'm not addressing properly?

mattdeboard
  • 700
  • 1
  • 7
  • 17

1 Answers1

5

Is this an error on my end or an issue with the underlying C that I'm not addressing properly?

It is probably both. GDAL/OGR's bindings do tend to segfault occasionally, when objects go out of scope and are garbage collected. While this is a known bug, it is unlikely to be fixed any time soon.

Chances are you can find a way to work around this. I can't reproduce this segfault with another shapefile on Windows XP, and the following version of GDAL/OGR:

 >>> gdal.VersionInfo('') 
 'GDAL 1.6.0, released 2008/12/04'

You could try temporarily to refactor the cust_field function into the body of the script like this:

from osgeo import ogr, osr

shpfile = 'Census_County_TIGER00_IN.shp'

ds = ogr.Open(shpfile, 1)
lyr = ds.GetLayerByIndex(0)
fieldDefn = ogr.FieldDefn("Test", ogr.OFTInteger)
fieldDefn.SetWidth(14)
fieldDefn.SetPrecision(6)

lyr.CreateField(fieldDefn)

Let me know if this solves your problem.

fmark
  • 57,259
  • 27
  • 100
  • 107
  • Unfortunately, it does not. I have tried to manually enter each line into iPython, and iPython segfaults at the lyr.CreateField(fieldDefn) line. – mattdeboard Nov 24 '10 at 03:05
  • @mattdeboard Could you post a sample shapefile somewhere online? – fmark Nov 24 '10 at 03:12
  • Here is the download link to the exact shapefile I'm using. http://inmap.indiana.edu/downloads/Census_County_TIGER00_IN.zip // In addition, here is a link to the debug info when I run the script with GDB. http://paste.pocoo.org/show/295344/ // If it matters (I'm sure it doesn't) I'm running all this on Ubuntu in a virtual box. – mattdeboard Nov 25 '10 at 07:54
  • @mattdeboard Even using your shapefile I can't replicate this crash. You might want to check your gdal/ogr installation. – fmark Nov 25 '10 at 08:17
  • I've uninstalled/reinstalled a couple of times. I guess I'm missing something on the uninstall. – mattdeboard Nov 26 '10 at 08:18
  • Have you tried running the sample at the bottom of the [OGR tutorial](http://www.gdal.org/ogr/ogr_apitut.html)? – fmark Nov 27 '10 at 01:06