首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从一个SqlGeometry对象上获取与另一个SqlGeometry对象最近的点?

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

Stack Overflow用户
提问于 2010-02-17 18:20:16
回答 3查看 5.7K关注 0票数 1

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-02-17 20:35:33

我不确定这是否可以直接在SQL Server 2008中实现:

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

在该线程中建议的解决方法是:

代码语言:javascript
复制
declare @g geometry = 'LINESTRING(0 0, 10 10)' 
declare @h geometry = 'POINT(0 10)' 

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

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

票数 3
EN

Stack Overflow用户

发布于 2010-02-27 09:18:26

下面是一个示例,展示了使用SqlGeometry和C#而不需要SQL Server的可能解决方案:

代码语言:javascript
复制
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))");
        }
    }
}

程序输出:

代码语言:javascript
复制
POINT (20 20)
POINT (160 150)
POINT (10 10)
POINT (70 160)
票数 6
EN

Stack Overflow用户

发布于 2010-02-19 14:03:33

如果您对实际查找直线上最近的点(也称为节点)感兴趣,则可以将每条直线转换为具有相同直线If的一组点。然后查询最近点并计算距离。

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2279795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档