1

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)
  • Think you have to call Destroy() on the layer and data source – atlefren Mar 16 '18 at 16:43
1

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)
...
  • Hi @Chau, thanks for your reply. But I still get an empty polygon shapefile. – Matt Mar 14 '18 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? – Chau Mar 14 '18 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. – Matt Mar 16 '18 at 0:34
  • @Matt can you give me some data to test on? – Chau Mar 19 '18 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.