gpt4 book ai didi

postgresql - 旋转多边形使其与 X 轴平行

转载 作者:行者123 更新时间:2023-11-29 13:56:25 25 4
gpt4 key购买 nike

默认情况下,PostGIS 根据多边形的边界框计算多边形的包络或范围 ((MINX, MINY), (MINX, MAXY), (MAXX, MAXY), (MAXX, MINY) , (MINX, MINY)).

结果如下:

enter image description here

但是,我正在寻找更像这样的结果:

enter image description here

据我所知,我能想到的最佳算法是:

  1. 确定角度a 以旋转多边形X 使其与x 轴平行
  2. 旋转X a度,计算旋转多边形X的包络Y
  3. 旋转 Y -a

如何计算 PostGIS 中的第 1 步?

最佳答案

这是一个实现。它不处理退化的情况,你如果区域平局,则可能不喜欢选择的矩形。我正在尝试提交给 postgis,但这应该足以使用直到我得到了 postgis 的补丁。

create or replace function ST_MinimumRectangle(g geometry) returns geometry
language 'plpgsql' as $$
declare
hull geometry;
begin
hull = ST_ExteriorRing(ST_ConvexHull(g));
-- one side must lie along the rectangle.
-- so, for each side, rotate, bbox, counter-rotate bbox
with sides as (
select ST_PointN(hull, n) as a, ST_PointN(hull, n+1) as b,
n as side
from generate_series(1,ST_NPoints(hull)-1) n
),
angles as (
select side, a, b, st_azimuth(a, b) as angle from sides
),
boxes as (
select ST_Rotate(ST_Envelope(ST_Rotate(hull, -angle)),angle) as rect, side, angle from angles
)
select rect into hull from boxes order by ST_Area(rect), side limit 1
;
return hull;
end;
$$ immutable strict;

关于postgresql - 旋转多边形使其与 X 轴平行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30948064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com