【问题标题】:Can postgis make a square bounding box?postgis可以做一个方形边界框吗?
【发布时间】:2021-12-30 20:39:39
【问题描述】:

我可以为我的特征制作一个边界框,但我希望它是方形的。这可能吗?

有点像这样……

【问题讨论】:

  • 您可以计算边界框,取 2 个较长的边并更新 2 个较短的边,用较长边的值在每端添加 50%,然后再次连接它们
  • 嗨@TimothyDalton - 伟大的思想等等。那是我最终做到的。为了完整性,下面的代码

标签: sql postgresql postgis


【解决方案1】:

Tim Dalton 也建议 - 我解决了这个问题,如下所示。这是使用 Python 和 SRS 27700,所以它适用于我的目的。边界来自 GeoPandas 和 4 个极值的元组。

    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)

【讨论】:

    【解决方案2】:

    使用 PostGIS,您可以应用或多或少相同的原理:1) 计算距多边形质心最远点的距离,2) 使用此距离值设置缓冲区的半径,最后在缓冲区:

    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;
    

    演示:db<>fiddle

    【讨论】:

      猜你喜欢
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2020-07-11
      • 2012-11-13
      • 2011-08-13
      相关资源
      最近更新 更多