I have a method that creates a new Shapefile layer and returns the layer. As soon as I try to operate on that layer, I get a Segmentation fault. I verified the code works by pulling all the code out of the method and sticking it in the calling method. Thoughts on why this happens? This is a different problem than I posted that ended up being a PIP/Conda conflict.
def CreateMergedLayer(merged_layer_file_name):
"""
Initializes an ESRI Shapefile with the fields of two
layers that will be merged - could be improved by creating
a set of fields from both (assuming they have the same
definition if sharing names), but just made purpose-built
for this function - if i have to do again, i'll refactor
Parameters
----------
merged_layer_file_name : string
The full path and file name of the new merged layer
Returns
-------
The pointer to the layer
"""
print ("About to create shapefile for {}".format(merged_layer_file_name))
driver = ogr.GetDriverByName("ESRI Shapefile")
data_source = driver.CreateDataSource(merged_layer_file_name)
# create the spatial reference, WGS84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
new_layer = data_source.CreateLayer("la_streets_with_block_centroid_connectors", srs, ogr.wkbLineString)
new_field = ogr.FieldDefn("GeoID", ogr.OFTString)
new_field.SetWidth(16)
new_layer.CreateField(new_field)
new_field = ogr.FieldDefn("FULLNAME", ogr.OFTString)
new_field.SetWidth(100)
new_layer.CreateField(new_field)
return new_layer
Calling this method here:
merged_layer = CreateMergedLayer("/tmp/la_streets_with_block_centroid_connectors.shp")
print ("Created Layer, getting definition: layer: {}".format(merged_layer))
merged_layer_defn = merged_layer.GetLayerDefn()
This outputs the following without getting past the GetLayerDefn() request:
About to create shapefile for /tmp/la_streets_with_block_centroid_connectors.shp
Created Layer, getting definition: layer: <osgeo.ogr.Layer; proxy of <Swig Object of type 'OGRLayerShadow *' at 0x114ad4630> >
Segmentation fault: 11