2

I'm trying to create a polygon shapefile from a bounding box in ogr but it creates an empty shapefile. Here's the code:

from osgeo import ogr infc = r"C:\Temp\infc.shp" layer_name = 'outfc_bbx' driver = ogr.GetDriverByName("ESRI Shapefile") vector = driver.Open(infc) in_lyr = vector.GetLayer() driver = ogr.GetDriverByName('ESRI Shapefile') # will select the driver foir our shp-file creation. shapeData = driver.CreateDataSource(r"C:\Temp") #so there we will store our data out_lyr = shapeData.CreateLayer(layer_name, in_lyr.GetSpatialRef(), ogr.wkbPolygon) #this will create a corresponding layer for our data with given spatial information. out_lyr.CreateFields(in_lyr.schema) out_defn = out_lyr.GetLayerDefn() out_feat = ogr.Feature(out_defn) feature = in_lyr.GetFeature(0) geom = feature.GetGeometryRef() geom.GetEnvelope() (minX, maxX, minY, maxY) = geom.GetEnvelope() ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(minX, minY) ring.AddPoint(maxX, minY) ring.AddPoint(maxX, maxY) ring.AddPoint(minX, maxY) ring.AddPoint(minX, minY) polygon_env = ogr.Geometry(ogr.wkbPolygon) #poly = polygon_env.GetGeometryRef(0) out_feat.SetGeometry(polygon_env) out_lyr.CreateFeature(out_feat) 
1
  • Think you have to call Destroy() on the layer and data source Commented Mar 16, 2018 at 16:43

1 Answer 1

2

You are creating a new feature:

feature = in_lyr.GetFeature(0) geom = feature.GetGeometryRef() geom.GetEnvelope() (minX, maxX, minY, maxY) = geom.GetEnvelope() 

Then you create the ring for the polygon:

ring = ogr.Geometry(ogr.wkbLinearRing) ring.AddPoint(minX, minY) ring.AddPoint(maxX, minY) ring.AddPoint(maxX, maxY) ring.AddPoint(minX, maxY) ring.AddPoint(minX, minY) 

Now you create your polygon for the bounding box:

polygon_env = ogr.Geometry(ogr.wkbPolygon) #poly = polygon_env.GetGeometryRef(0) 

And here you add the bounding box polygon to the layer:

out_feat.SetGeometry(polygon_env) out_lyr.CreateFeature(out_feat) 

But you haven't actually connected the ring to the bounding box polygon. The bounding box polygon is empty, because you still need to add the ring to the polygon:

... polygon_env = ogr.Geometry(ogr.wkbPolygon) # This is the missing line: polygon_env.AddGeometry(ring) # Add the polygon to the new feature. out_feat.SetGeometry(polygon_env) ... 
4
  • Hi @Chau, thanks for your reply. But I still get an empty polygon shapefile. Commented Mar 14, 2018 at 10:57
  • @Matt, then I think we need some test data to try out (maybe just the WKT of the first feature in your data). When I run your code with my own linestring input data, I get a shape file with one bbox. Are you sure that the first geometry isn't an empty geometry or null geometry? Commented Mar 14, 2018 at 15:24
  • doesn't work. I checked the features, all are good. I've tried polyline shapefiles as input as well as polygons shapefiles as input. Commented Mar 16, 2018 at 0:34
  • @Matt can you give me some data to test on? Commented Mar 19, 2018 at 11:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.