我有一个表geofences
,用于存储多边形的geometry
。
我还有一个点A
,它位于几何体内部。我要做的是找到点A
中位于多边形几何体表面上的两个最近点。
PostGIS中的功能:
CREATE OR REPLACE FUNCTION accuracyCheck(Polygon geometry
,decimal lat
,decimal lon)
RETURNS VARCHAR AS
$BODY$
DECLARE height DECIMAL;
DECLARE accuracy VARCHAR(250);
BEGIN
CREATE TEMPORARY TABLE closePointStorage AS
SELECT ST_AsText(ST_ClosestPoint(geometry
,ST_GeomFromText('POINT(lat lon)',0)
)
) AS closestPoint
FROM (
SELECT ST_GeomFromText(geometry) as geometry
FROM gfe_geofences
WHERE is_active=true
) As tempName;
CREATE TEMPORARY TABLE areaStorage ON COMMIT DROP AS
SELECT ST_Area(ST_GeomFromText('Polygon((23.0808622876029 96.1304006624291
,28.0808622876029 99.1304006624291
,100 200
,23.0808622876029 96.1304006624291
))'
,0)
) AS area;
CREATE TEMPORARY TABLE distanceStorage ON COMMIT DROP AS
SELECT ST_Distance(
ST_GeomFromText('POINT(23.0808622876029 96.1304006624291)',-1)
,ST_GeomFromText('POINT(28.0808622876029 99.1304006624291)',-1)
) AS distance;
height = (SELECT area FROM areaStorage)
/(0.5*(SELECT distance FROM distanceStorage));
IF height < (SELECT radius_meters
FROM gfe_geofences Where is_active=true) THEN
accuracy = "FullConfirm";
RETURN accuracy;
ELSE
accuracy = "PartiallyConfirm";
RETURN accuracy;
END IF;
END;
$BODY$ LANGUAGE plpgsql;
我只想在多边形几何体的边界上找到两个点。就像我从查询中找到一个:
CREATE TEMPORARY TABLE closePointStorage AS
SELECT ST_AsText(ST_ClosestPoint(geometry
,ST_GeomFromText('POINT(lat lon)',0)
)
) AS closestPoint
FROM (
SELECT ST_GeomFromText(geometry) as geometry
FROM gfe_geofences
WHERE is_active=true
)
AS tempName;
除此之外,我必须找到一个距离大于上面的点但比其余点更小的距离。
答案 0 :(得分:1)
我假设您想要找到最接近相关点的多边形边缘
从[A,B]行获得点'C'的距离'd' 首先翻译所有点,使A为0,0
B -= A //vector subtraction
C -= A
然后归一化B使其长度为1.0
len = sqrt( B . B) //dotproduct of two vectors is the length squared
B /= len //scalar divide by length
从A中找出与C
成直角的长度dotp = B . C //dot product again
closestPointOnLine = B * dotp //scalar multiply
现在得到距离
diff = (C - ClosestPointOnLine)
d = sqrt(diff . diff)
不确定如何在SQL中执行此操作。您需要对多边形上的每个边执行上述操作,然后找到最小值'd'
顺便说一下,B和C的叉积的符号现在会告诉你这个点是否在多边形的内部
答案 1 :(得分:0)
使用ST_DumpPoints()转储多边形的点,然后从ST_Distance到A限制选择该顺序。
所以它就像
SELECT * from ST_DumpPoints(poly) order by ST_Distance(A,geom) asc limit 2;
(假设这是一个内部选择,其中poly是多边形,A是要比较的点,geom是被比较的poly中一个点的geom列)
答案 2 :(得分:0)
如果包含线条,边界多边形上通常没有第二个最近点。就像没有真正的第二个最接近零的数字一样。 要么你只想考虑角落处的点,就像马库斯建议的那样。 或者你只有一个最接近的点。
答案 3 :(得分:0)
1)有一种左场的想法,但要找到第二个 - 最近点到目的地,为什么不找到距离你已找到的点最近的点?
2)或者,与您的具体问题有更多的绅士关系,
3)进入左侧字段,将一些数据集转储到Mongo并使用$ near函数... http://docs.mongodb.org/manual/reference/operator/near/