Home > Blockchain >  Can postgis make a square bounding box?
Can postgis make a square bounding box?

Time:11-23

I can make a bounding box about my features but I would like it to be square. Is this possible?

A bit like this...

enter image description here

CodePudding user response:

Also suggested by Tim Dalton - I solved this as shown below. This is using Python and SRS 27700 so it works for my purposes. The bounds are from GeoPandas and a tuple of the 4 extremes.

    bounds = tuple(reportData.total_bounds)
    left,bottom,right,top = bounds
    bounds_width = right-left
    bounds_height = top-bottom
    is_tall = True if bounds_height > bounds_width else False
    if is_tall:
        pad = (bounds_height-bounds_width) * 0.05
        diff = (bounds_height-bounds_width) / 2
        plt.xlim(bounds[0]-diff-pad, bounds[2] diff pad)
        plt.ylim(bounds[1]-pad, bounds[3] pad)
    else:
        pad = (bounds_width-bounds_height) * 0.05
        diff = (bounds_width-bounds_height) / 2
        plt.ylim(bounds[1]-diff-pad, bounds[3] diff pad)
        plt.xlim(bounds[0]-pad, bounds[2] pad)

CodePudding user response:

Using PostGIS you can apply more or less the same principle: 1) calculate the distance from the furthest point from the polygon centroid, 2) use this distance value to set the radius of a buffer, and finally create an envelope around the buffer:

SELECT
  ST_Envelope(
    ST_Buffer(
      ST_Centroid(geom),
      (SELECT max(ST_Distance(ST_Centroid(j.geom),i.geom)) 
       FROM ST_DumpPoints(j.geom) i))
  )
FROM your_table j;

Demo: enter image description here

enter image description here

  • Related