4

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?

1
  • the order is minx,maxx,miny,maxy
    – nickves
    Commented Jun 21, 2013 at 12:30

4 Answers 4

9

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)
5

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.

0

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.

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

Follow-up on @ian-turton answer, below an example on how to get the center coordinates from a WMS [as an easily reproducible example to get some real "West, South, East, North" coordinates]


from owslib.wms import WebMapService

wms_url="http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/WMS_v1.3/raster/de_agostini.map"

wms = WebMapService(wms_url, version="1.3.0")

def get_center(wms):
    # list(wms.contents)[0] <- get the first available layer from the WMS
    # wms.contents['layer']..boundingBoxWGS84 <- the the box bounds
    w, s, e, n = wms.contents[list(wms.contents)[0]].boundingBoxWGS84
    # get the box width 
    width = max(w, e) - min(w, e)
    # get the box height 
    height = max(s, n) - min(s, n)
    # compute the center
    center = round(min(s, n)+height/2, 4), round(min(w, e)+width/2, 4)
    return center

get_center(wms)

# (41.0603, 13.2123)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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