Is there a library available in Python 3.6 using which I could convert a GeoJSON object (geometry) into a bounding box object?

Actually I am calling a WMS layer from GeoServer and I want to set the bounds of the map to the bounding box of the requested polygon (WMS layer) so the only method I could figure out to get bounds of an individual polygon from geometry is to get the geometry of the polygon using a getfeatureInfo request of WMS service and then converting that geojson to bounding box at the backend.

If there is any other method that I could do to get the bbox of an individual polygon from geoserver using WMS service then please suggest it?

  • Using a WFS request with a filter is probably faster than a GetFeatureInfo request
    – Ian Turton
    Feb 21, 2019 at 8:53
  • No I cannot use WFS request due to some security reasons
    – Gaurav
    Feb 21, 2019 at 8:54
  • seems odd they didn't turn off the geojson getfeatureinfo then
    – Ian Turton
    Feb 21, 2019 at 8:56
  • Actually they have provisioned the getfeatureinfo request and established a validation on propertyName
    – Gaurav
    Feb 21, 2019 at 9:03

3 Answers 3


You can probably use the geojson package to handle the JSON parsing, then finding the min/max corners of the bounding box is something like:

import geojson

def bbox(coord_list):
     box = []
     for i in (0,1):
         res = sorted(coord_list, key=lambda x:x[i])
     ret = f"({box[0][0]} {box[1][0]}, {box[0][1]} {box[1][1]})"
     return ret

# obviously you need to parse your json here
poly=geojson.Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]])
line = bbox(list(geojson.utils.coords(poly)))

which gives (-120.43 -20.28, 23.194 57.322) as a result.

  • If your polygon cross 180, -180 longitude this may not work. Lucky nothing "live" there
    – James
    Sep 9, 2022 at 8:30
import numpy as np
import geojson

def get_bounding_box(geometry):
    coords = np.array(list(geojson.utils.coords(geometry)))
    return coords[:,0].min(), coords[:,0].max(),
           coords[:,1].min(), coords[:,1].max()

After extracting the coordinates with geojson.utils.coords as described in the existing answers, the bounds function of the shapely packages might be used:

from geojson.utils import coords
from shapely.geometry import LineString

def get_bbox(geometry):
    return LineString(coords(geometry)).bounds

geometry might be a parsed GeoJSON file, the bounding box is returned as tuple with 4 floats.

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.