C# 如何从另一个SqlGeometry对象获取SqlGeometry对象上的最近点?
我有一组直线和多边形对象SqlGeometry类型和一组点对象SqlGeometry类型。我们如何从给定的点对象在每条线上找到最近的点?是否有用于执行此操作的API?我不确定是否可以直接在SQL Server 2008中执行此操作: 该线程中建议的解决方法是:C# 如何从另一个SqlGeometry对象获取SqlGeometry对象上的最近点?,c#,spatial,spatial-query,C#,Spatial,Spatial Query,我有一组直线和多边形对象SqlGeometry类型和一组点对象SqlGeometry类型。我们如何从给定的点对象在每条线上找到最近的点?是否有用于执行此操作的API?我不确定是否可以直接在SQL Server 2008中执行此操作: 该线程中建议的解决方法是: declare @g geometry = 'LINESTRING(0 0, 10 10)' declare @h geometry = 'POINT(0 10)' select @h.STBuffer(@h.STDistance(
declare @g geometry = 'LINESTRING(0 0, 10 10)'
declare @h geometry = 'POINT(0 10)'
select @h.STBuffer(@h.STDistance(@g)).STIntersection(@g).ToString()
否则,必须编写脚本从数据库中读取几何图形,并使用单独的空间库 我不确定这是否可以直接在SQL Server 2008中实现: 该线程中建议的解决方法是:
declare @g geometry = 'LINESTRING(0 0, 10 10)'
declare @h geometry = 'POINT(0 10)'
select @h.STBuffer(@h.STDistance(@g)).STIntersection(@g).ToString()
否则,必须编写脚本从数据库中读取几何图形,并使用单独的空间库 如果您对实际查找直线上最近的点(也称为节点)感兴趣,可以将每条直线转换为一组具有相同lineid的点。然后查询最近的距离并计算距离 如果您试图计算从一个点到最近直线的距离-stdistance 我猜其他答案的问题是在where子句中放什么,尽管可以使用stdistance指定一个你不在乎的距离,比如
其中pointGeom.stdistancelineGeom<您关心的距离如果您有兴趣在直线上实际查找最近的点(也称为节点),则可以将每条直线转换为具有相同线ID的一组点。然后查询最近的距离并计算距离 如果您试图计算从一个点到最近直线的距离-stdistance 我猜其他答案的问题是在where子句中放什么,尽管可以使用stdistance指定一个你不在乎的距离,比如
其中pointGeom.stdistancelineGeom<您关心的距离这里是一个示例,介绍了使用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)
这里的示例展示了使用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)
对于地理对象,需要添加0.1米的额外缓冲区,并获取结果的第一个点。另一方面,它产生了空的几何集合@HSTBuffer@h.STDistance@g+0.1。STIntersection@g.STFirstPoint.ToStringfor地理对象,需要添加0.1米的额外缓冲区并获取结果的第一个点。另一方面,它产生了空的几何集合@HSTBuffer@h.STDistance@g+0.1。STIntersection@g.STFirstPoint.ToStringYes,的确,我对geographika的答案投了赞成票,但也决定提供这种变体。是的,的确,我对geographika的答案投了赞成票,但也决定提供这种变体。顺便说一句,这个问题是关于C API的,但公认的答案使用SQL Server。对于任何希望获得类似问答的读者来说,这是一个相当困惑的问题。建议:直截了当地回答问题/标签。顺便说一句,这个问题是关于C API的,但公认的答案使用SQL Server。对于任何想找类似问答的读者来说,这都会让他们感到困惑。建议:把问题/标签弄清楚。