Search code examples
postgis

How to get coordinates of a bounding box at a certain distance from a point in postGIS?


My goal is to define a bounding box of a circle of a given radius built around a point in postGIS.

Point, extent are ok but when I do this:

select ST_AsGeoJSON( st_extent( st_buffer( ST_SetSRID(ST_MakePoint(11.0120, 49.5897), 4326), 6000 ) ) );

I get

{"type":"Polygon","coordinates":[[[-5988.988,-5950.4103],
                                  [-5988.988,6049.5897],
                                  [6011.012,6049.5897],
                                  [6011.012,-5950.4103],
                                  [-5988.988,-5950.4103]]]}

when I was expecting something along the lines of (

{"type":"Polygon","coordinates":[[[10.929017, 49.535753],
                                 [10.929017, 49.643646],
                                 [11.094983, 49.643646],
                                 [11.094983, 49.535753],
                                 [10.929017, 49.535753]]]}

(handmade GeoJSON - might contain errors)

So - how do I get the postGIS SQL to output geographical coordinates instead of what looks like geometry?


Solution

  • If geometry is passed into ST_Buffer, then the second argument is degrees, not meters. To use meters for the second argument, cast the first argument to geography. Then cast it back to geometry for use with extent:

    select
      ST_AsGeoJSON(
        st_extent(
          st_buffer(
            ST_SetSRID(
              ST_MakePoint(11.0120, 49.5897),
              4326
            )::geography,
            6000
          )::geometry
        )
      );