我的数据库中包含GEOGRAPHY
的SQL Server 2008 LINESTRING
数据类型。 LINESTRING
描述了一条可能弯曲的道路。
我还有另一张表格,其中包含GEOGRAPHY
POINT
的起点和终点。我需要知道第三点是否落在路上的这两点之间(LINESTRING
)。
目前,我正在测试:
这个有效,但看起来真的不优雅根本不起作用,如果道路你自己开启!有办法吗?
答案 0 :(得分:3)
如您所知,在以下情况下,您的方法将失败,其中S是起点,E是终点,X是您正在测试的点:
使用该方法,点X将错误地导致在点S和点E之间,因为它通过了算法的测试1和测试2:即。点X在线串上,从X到S和从X到E的距离都小于从S到E的距离。
一种可能的解决方案
你可以将你的线串路径“爆炸”成单独的线段,每个只有两个点,所以:
LINESTRING(-122.360 47.656, -122.343 47.656, -122.310 47.690, -122.310 47.670)
会被分解为:
LINESTRING(-122.360 47.656, -122.343 47.656)
LINESTRING(-122.343 47.656, -122.310 47.690)
LINESTRING(-122.310 47.690, -122.310 47.670)
然后,您将能够遍历上述每个线段,并使用STIntersects
测试该点是否位于其中一个线段上。当一个点通过此测试时,您将能够确定它是否在起点和终点之内。
如果可能,我建议将起点/终点存储为线串路径上的点的索引,而不是原始地理点。首先,这将使解决此问题变得更容易,但除此之外,您将消除数据的重复,这也保证了您不能拥有不属于线串的起点/终点。这样做的缺点是您不能在线段的中间有起点/终点,但它们必须位于路径的一个角上。现在您必须确定您的应用程序中是否可以接受此限制。
如果您选择上述表示,我们可以使用以下递归函数解决此问题,其中@path
是表示道路的线串,@start_point
和@end_end
表示索引@path
上的两个点(第一个索引是1),@test_point
是要测试的地理点。测试点可以位于谎言的任何地方。
CREATE FUNCTION [dbo].[func_PointBetween](@path geography,
@start_point int,
@end_point int,
@test_point geography)
RETURNS tinyint
AS
BEGIN
DECLARE @result tinyint = 0;
DECLARE @num_points int = @path.STNumPoints();
DECLARE @line_segment geography;
IF (@start_point < @end_point) AND (@end_point < @num_points)
BEGIN
/* Generate the line segment from the current start point
to the following point (@start_point + 1). */
SET @line_segment = geography::STLineFromText('LINESTRING(' +
CAST(@path.STPointN(@start_point).Long AS varchar(32))+ ' ' +
CAST(@path.STPointN(@start_point).Lat AS varchar(32)) + ',' +
CAST(@path.STPointN(@start_point + 1).Long AS varchar(32))+ ' ' +
CAST(@path.STPointN(@start_point + 1).Lat AS varchar(32)) + ')',
4326);
/* Add a buffer of 25m to @test_point. This is optional, but
recommended, otherwise it will be very difficult to get a
point exactly on the line. The buffer value may be tweaked
as necessary for your application. */
IF @test_point.STBuffer(25).STIntersects(@line_segment) = 1
BEGIN
/* The test point is on one of the line segments between
@start_point and @end_point. Return 1 and stop the
recursion. */
SET @result = 1;
END
ELSE
BEGIN
/* The test point is not between the @start_point and
@start_point + 1. Increment @start_point by 1 and
continue recursively. */
SET @result = [dbo].[func_PointBetween](@path,
@start_point + 1,
@end_point,
@test_point);
END
END
ELSE
BEGIN
/* There are no further points. The test point is not between the
@start_point and @end_point. Return 0 and stop the recursion. */
SET @result = 0;
END
RETURN @result;
END
为了测试上述功能,我定义了6点线串,如上图所示。然后我们将定义两个测试点:@test_point_a
,它位于第三和第四点之间,@test_point_b
,它位于路径之外。
DECLARE @road geography;
DECLARE @test_point_a geography;
DECLARE @test_point_b geography;
SET @road = geography::STGeomFromText('LINESTRING(-122.360 47.656,
-122.343 47.656,
-122.310 47.690,
-122.310 47.670,
-122.300 47.670,
-122.290 47.660)',
4326);
/* This point lies between point 3 and point 4 */
SET @test_point_a = geography::STGeomFromText('POINT(-122.310 47.680)', 4326);
/* This point lies outside the path */
SET @test_point_b = geography::STGeomFromText('POINT(-122.310 47.700)', 4326);
/* This returns 1, because the test point is between start and end */
SELECT dbo.func_PointBetween(@road, 2, 5, @test_point_a);
/* This returns 0 because the test point is not between start and end */
SELECT dbo.func_PointBetween(@road, 4, 5, @test_point_a);
/* This returns 0 because the test point lies outside the path */
SELECT dbo.func_PointBetween(@road, 1, 6, @test_point_b);
答案 1 :(得分:0)
您应该能够检查新点与起点和终点之间的线段子集之间的距离(STDistance)。该距离应评估为零。如果我有机会深入研究地理数据类型,我会尝试将确切的查询放在一起,但希望这可以让你开始。