gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:36:21 25 4
gpt4 key购买 nike

我有一组线和多边形对象(SqlGeometry 类型)和一个点对象(SqlGeometry 类型)。我们如何从给定的点对象找到每条线上最近的点?是否有任何 API 可以执行此操作?

最佳答案

此处示例展示了使用 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)

关于c# - 如何从另一个 SqlGeometry 对象获取 SqlGeometry 对象上的最近点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2279795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com