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.

LarsVegas
  • 2,536
  • 3
  • 30
  • 47

1 Answers1

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
atlefren
  • 4,307
  • 20
  • 35
  • Thanks for your answer. So to makes things clear: there is no ogr build-in function that does this? – LarsVegas Mar 19 '14 at 14:12
  • I don't think so – radouxju Mar 19 '14 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. – Mehdi Mar 14 '18 at 05: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? – atlefren Mar 15 '18 at 06: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. https://gis.stackexchange.com/questions/274687/creating-polygon-shapefile-from-bounding-box-in-ogr-from-osgeo – Mehdi Mar 16 '18 at 00:31
  • @Matt there are several steps between using my function and creating a shapefile, so that's why I'm asking. Might be something wrong as well, but the idea should be fine. – atlefren Mar 16 '18 at 16:39
  • Just leaving this here: GetEnvelope is not the Minimum Bouding Rectangle. – Gilles-Antoine Nys Aug 13 '19 at 08:31