Search code examples
djangogeodjango

GeoDjango: How to create a geometry object from a bounding box?


I am intercepting a query parameter of bounding box ex. ?bbox=160.6,-55.95,-170,-25.89 in my GeoDjango application to filter my queryset of entries that intersects with the bbox. I want to know if how can I create an geometry object from the bbox or a list of the bbox object [160.6,-55.95,-170,-25.89].

bbox = GEOSGeometry('BBOX [160.6,-55.95,-170,-25.89]')

Solution

  • Here is the code. It is already implemented in geos.

    from django.contrib.gis.geos import Polygon
    
    my_bbox_polygon = Polygon.from_bbox([160.6,-55.95,-170,-25.89]) # [xmin, ymin, xmax, ymax]
    

    You can check out the documentation for Polygon here