2

I wrote a very simple python function that gives me the envelope/bounding box/...based on two coordinate pairs (ower left and upper right corner):

# ... def get_bounding_box(self, ll_corner, ur_corner): try: from shapely.geometry import MultiPoint mp_env = MultiPoint([ll_corner, ur_corner]).envelope return ogr.CreateGeometryFromWkt( mp_env.to_wkt() ) print mp_env POLYGON ((4.8439699999999997 52.3661099999999990, 4.8469199999999999 52.3661099999999990, 4.8469199999999999 52.3706000000000031, 4.8439699999999997 52.3706000000000031, 4.8439699999999997 52.3661099999999990)) 

As you can see I'm returning an ogr geometry which I use, for instance, in this manner:

bbox = geo.get_bounding_box(lowerleft,upper_right) ... p=ogr.Geometry(ogr.wkbPoint) p.AddPoint(x,y) if p.Within(bbox): .... 

Isn't there an equivalent function to shapely's envelope in ogr? Both .ConvexHull() and GetEnvelope() return only the two corner coordinate pairs.

1 Answer 1

10

The Ogr function GetEnvelope() returns "a tuple (minX, maxX, minY, maxY)" (from here), but what you want (from what I can understand) is a Polygon describing the envelope/bbox?

This is actually rather simple, as the tuple (minX, maxX, minY, maxY) is all you need to create a Polygon.

Just create a Polygon based these, like so:

from osgeo import ogr def my_envelope(geom): (minX, maxX, minY, maxY) = geom.GetEnvelope() # Create ring 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) # Create polygon poly_envelope = ogr.Geometry(ogr.wkbPolygon) poly_envelope.AddGeometry(ring) return poly_envelope 
7
  • Thanks for your answer. So to makes things clear: there is no ogr build-in function that does this? Commented Mar 19, 2014 at 14:12
  • I don't think so Commented Mar 19, 2014 at 14:16
  • Hi @atlefren, thanks for the code, I've tried to create polygon shapefiles out of this based on my polygon shapefiles. but I can't seem to get it work. any help would be greatly appreciated. Commented Mar 14, 2018 at 5:57
  • @Matt "can't seem to get it to work" is a far too vague "question". What does not work? My suggested function? How does it not work? Commented Mar 15, 2018 at 6:57
  • @atlefren as I said I'm trying to create polygon shapfiles out of my_envelope function you provided above. But it only creates an empty shapefile. Here's the link to the question I've raised. gis.stackexchange.com/questions/274687/… Commented Mar 16, 2018 at 0:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.