ラインとポリゴン オブジェクト (SqlGeometry タイプ) とポイント オブジェクト (SqlGeometry タイプ) のセットがあります。与えられた点オブジェクトから各線上で最も近い点を見つけるにはどうすればよいでしょうか? この操作を行うための API はありますか?
3 に答える
ここでは、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)
これが 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()
そうしないと、スクリプトを作成してデータベースからジオメトリを読み取り、別の空間ライブラリを使用する必要があります。
ライン上の最も近いポイント (ノードとも呼ばれます) を実際に見つけたい場合は、各ラインを同じライン ID を持つポイントのセットに変換できます。次に、最も近いものをクエリして、距離を計算します。
代わりに、ポイントから最も近いラインまでの距離を計算しようとしている場合-stdistance http://msdn.microsoft.com/en-us/library/bb933808.aspx 他の回答が対処する問題は、何を配置するかだと思いますただし、where句で stdistance を使用して、気にしない距離を指定できます
どこで pointGeom.stdistance(lineGeom) < "気になる距離"