# Center of a bounding box

I have bounding box coordinates of my shapefile
(fetched from layer.GetExtent() Python with GDAL/OGR):

(-38.6102467739459, -38.017601026186576, 33.01563382506067, 33.624945228091406)


So I assume it's SW and NE points of the rectangle.

I would like to find out the center of this rectangle (in lat/lon).
How should I proceed?

• the order is minx,maxx,miny,maxy – nickves Jun 21 '13 at 12:30

for a small bbox, in a long/lat coordinate system, you can assume the earth is flat at that area and you can use the average of x and y:

 >>> coords = (-38.6102467739459, -38.017601026186576, 33.01563382506067, 33.624945228091406)

>>> centerx,centery = ( numpy.average(coords[:2]),numpy.average(coords[2:]))

>>> centerx,centery
(-38.313923900066243, 33.320289526576033)


calculate the width and height of the box by subtracting one corner from the other then add half the width to the left edge and half the height to the bottom edge.

you can use st_box2 and st_centroid functions

select
st_astext(st_centroid(st_box2d(
st_geomfromtext('LINESTRING(-38.6102467739459 33.624945228091406, -38.017601026186576 33.01563382506067)',4326)
)
)
) CENTER;

center
------------------------------------------
POINT(-38.3139239000662 33.320289526576)


Be careful of the order you plug in the coordinates.

• I'm not using postgis, but python gdal/ogr... – eouti Jun 21 '13 at 12:10
• lol, sorry i was reading some postgis questions :) – nickves Jun 21 '13 at 12:16