如何从另一个 SqlGeometry 对象获取 SqlGeometry 对象上的最近点?

发布于 2024-08-22 01:17:11 字数 93 浏览 5 评论 0原文

我有一组线和多边形对象(SqlGeometry 类型)和一个点对象(SqlGeometry 类型)。我们如何找到每条线上距给定点对象最近的点?有没有API可以实现这个操作?

I have a set of line and polygon object (SqlGeometry type) and a point object (SqlGeometry type). How can we find the the nearest point on each line from the given point object? Are there any API for doing this operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗梦空间 2024-08-29 01:17:11

这里的示例展示了使用 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)

Here a sample presenting possible solution using SqlGeometry and C#, no SQL Server is required:

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))");
        }
    }
}

The program output:

POINT (20 20)
POINT (160 150)
POINT (10 10)
POINT (70 160)
沉默的熊 2024-08-29 01:17:11

我不确定这是否可以直接在 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()

否则您必须编写一个脚本来从数据库中读取几何图形并使用单独的空间库。

I'm not sure if this is possible directly in SQL Server 2008:

http://social.msdn.microsoft.com/Forums/en/sqlspatial/thread/cb094fb8-07ba-4219-8d3d-572874c271b5

The workaround suggested in that thread is:

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

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

Otherwise you would have to write a script to read the geometry from your database and use separate spatial libraries.

小红帽 2024-08-29 01:17:11

如果您有兴趣实际找到线上最近的点(也称为节点),您可以将每条线变成一组具有相同 lineid 的点。然后查询最近的并计算距离。

相反,如果您尝试计算从一个点到最近的线的距离 - stdistance
http://msdn.microsoft.com/en-us/library/bb933808。 ASPX
我想其他答案解决的问题是在 where 子句中放入什么,尽管您可以使用 stdistance 来指定您不关心的距离,例如

Where pointGeom.stdistance(lineGeom) Where pointGeom.stdistance(lineGeom) < “你关心的距离”

If you are interested in actually finding the nearest point on the line (otherwise called a node) you can turn each line into a set of points with the same lineid. Then query for the closest and calc the distance.

If instead you are trying to calc the distance from a point to the nearest line - stdistance
http://msdn.microsoft.com/en-us/library/bb933808.aspx
I guess the problem that the other answer addresses is what to put in your where clause though you could use stdistance to specify a distance above which you don't care such as

Where pointGeom.stdistance(lineGeom) < "distance you care about"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文