'Python terminates when working with Memory layers in Python GDAL/OGR
I've been working on a software project that processes geometric files and combines them with cadastral data. I managed to find solutions for the various subtasks needed to accomplish the main task. Recently I've been trying to streamline the code and to put together the various code snippets I have written so far. This is when I ran into issues with the GDAL/OGR Memory driver. The code worked fine with other drivers (WFS, DXF and ESRI Shapefile), but not really with the Memory driver
- Read a Shapefile/DXF file, pass the layer retrieved from the file to
prepare_layerand return the resulting layer as a Memory layer. Up until the return statement everything works fine. After the layer "is returned" the code terminates shortly after without throwing an exception. This error can be circumvented by moving the code from theprepare_layerfunction to the main function. But anyway, it's not ideal and I would like to understand the reason why this happens.
from osgeo import ogr
def prepare_layer(input_layer):
mem_driver = ogr.GetDriverByName('MEMORY')
mem_ds = mem_driver.CreateDataSource('memData')
mem_open = mem_driver.Open('memData',1)
mem_layer = ogr.DataSource.CreateLayer(mem_ds, 'Layer1')
input_layer_defn = input_layer.GetLayerDefn()
for i in range(input_layer_defn.GetFieldCount()):
field_defn = input_layer_defn.GetFieldDefn(i)
mem_layer.CreateField(field_defn)
mem_defn = mem_layer.GetLayerDefn()
for feature in input_layer:
geom = feature.GetGeometryRef()
geom_type = geom.GetGeometryName()
if geom_type == 'LINESTRING':
geom.Transform(TRANSFORM)
geom = geom.Buffer(2)
out_feature = ogr.Feature(mem_defn)
out_feature.SetGeometry(geom)
for i in range(feature.GetFieldCount()):
field_attr = feature.GetField(i)
out_feature.SetField(i, field_attr)
mem_layer.CreateFeature(out_feature)
out_feature.Destroy()
return mem_layer
input_driver = ogr.GetDriverByName('ESRI Shapefile')
input_open = input_driver.Open(file_path_in, 0)
input_layer = input_open.GetLayer()
output_layer = prepare_layer(input_layer)
- Retrieve WFS data, extract the layer and intersect the layer with the processed memory layer from 1). Result: The terminal throws several HTTP 400 server errors. If I comment out the memory layer declaration under "# Result layer" the data is retrieved without issues. So regardless of the error message this is not a server issue. Further, if I manually paste the exact same HTTP request that
get_wfs()produces into my browser I can download the data without problems.
I left out the code for these functions for brevity since they work smoothly with other drivers:
get_bbox(): returns a string with the bounding box coordinates
get_wfs(feature, box): returns a ogr.Driver.Open object of a WFS driver
# Result Layer
res_driver = ogr.GetDriverByName('MEMORY')
res_ds = res_driver.CreateDataSource('result')
res_open = res_driver.Open('result',1)
res_layer = res_ds.CreateLayer('result', target, ogr.wkbPolygon)
# WFS Flurstück
bbox = get_bbox(mem_layer)
FlSt = get_wfs(feature, bbox)
FlSt_layer = FlSt.GetLayer()
# Intersection
ogr.Layer.Intersection(mem_layer, FlSt_layer, res_layer)
I would appreciate any help or pointers in the right direction. I hope this wasn't too convoluted.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
