我有一组线和面对象(SqlGeometry型)和一个点对象(SqlGeometry类型)。我们如何才能找到从给定的点对象每一行最近的点?是否有这样操作的任何API?

有帮助吗?

解决方案

我不知道这是否可能直接在SQL Server 2008中:

HTTP://社会.msdn.microsoft.com /论坛/ EN / sqlspatial /线程/ cb094fb8-07ba-4219-8d3d-572874c271b5

在该线程中提出的解决方法是:

declare @g geometry = 'LINESTRING(0 0, 10 10)' 
declare @h geometry = 'POINT(0 10)' 

select @h.STBuffer(@h.STDistance(@g)).STIntersection(@g).ToString()

否则,你将不得不编写一个脚本来读取数据库中的几何形状和使用单独的空间库。

其他提示

下面呈现使用SqlGeometry和C#可能的解决方案的样品,则不需要的SQL Server:

using System;
using Microsoft.SqlServer.Types;
namespace MySqlGeometryTest
{
    class ReportNearestPointTest
    {
        static void ReportNearestPoint(string wktPoint, string wktGeom)
        {
            SqlGeometry point = SqlGeometry.Parse(wktPoint);
            SqlGeometry geom = SqlGeometry.Parse(wktGeom);
            double distance = point.STDistance(geom).Value;
            SqlGeometry pointBuffer = point.STBuffer(distance);
            SqlGeometry pointResult = pointBuffer.STIntersection(geom);
            string wktResult = new string(pointResult.STAsText().Value);
            Console.WriteLine(wktResult);
        }

        static void Main(string[] args)
        {
            ReportNearestPoint("POINT(10 10)", "MULTIPOINT (80 70, 20 20, 200 170, 140 120)");
            ReportNearestPoint("POINT(110 200)", "LINESTRING (90 80, 160 150, 300 150, 340 150, 340 240)");
            ReportNearestPoint("POINT(0 0)", "POLYGON((10 20, 10 10, 20 10, 20 20, 10 20))");
            ReportNearestPoint("POINT(70 170)", "POLYGON ((110 230, 80 160, 20 160, 20 20, 200 20, 200 160, 140 160, 110 230))");
        }
    }
}

程序输出:

POINT (20 20)
POINT (160 150)
POINT (10 10)
POINT (70 160)

如果你有兴趣在真正找到线路上的最近点(否则称为节点),则可以将每个线到用相同lineid的一组点。然后查询最近的,而且计算的距离。

如果代替你正在试图从一个点到最近的线calc下的距离 - stdistance http://msdn.microsoft.com/en-us/library/bb933808。 ASPX 我想,对方的回答地址是把你的WHERE子句中,虽然你可以使用stdistance指定上面,你不关心如

的距离问题

<强>其中pointGeom.stdistance(lineGeom)< “你所关心的距离”

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top