我有一组线和多边形对象(SqlGeometry类型)和一个点对象(SqlGeometry类型)。我们如何从给定的点对象中找到每条线上最近的点?有没有做这个操作的API?
发布于 2010-02-17 20:35:33
我不确定这是否可以直接在SQL Server 2008中实现:
http://social.msdn.microsoft.com/Forums/en/sqlspatial/thread/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()否则,您将不得不编写一个脚本来从数据库中读取几何图形并使用单独的空间库。
发布于 2010-02-27 09:18:26
下面是一个示例,展示了使用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)发布于 2010-02-19 14:03:33
如果您对实际查找直线上最近的点(也称为节点)感兴趣,则可以将每条直线转换为具有相同直线If的一组点。然后查询最近点并计算距离。
相反,如果您正在尝试计算一个点到最近的线的距离- stdistance http://msdn.microsoft.com/en-us/library/bb933808.aspx,我猜其他答案解决的问题是在where子句中放入什么,尽管您可以使用stdistance指定一个您并不关心的距离,例如
Where pointGeom.stdistance(lineGeom) <“你关心的距离”
https://stackoverflow.com/questions/2279795
复制相似问题