将SQL地理数据类型转换为C#

16

这个地理空间 T-SQL 代码的 C# 等效代码是什么?

DECLARE @g geography;
DECLARE @h geography;
SET @g = geography::STGeomFromText('POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SET @h = geography::Point(47.653, -122.358, 4326)

SELECT @g.STIntersects(@h)

我正在尝试使用SqlGeometry数据类型在多边形中查找一个点 - 并且可以使用上述T-SQL实现;但我不明白如何实现等效的C#代码。

我试图使用SqlGeometry数据类型在多边形中找到一个点,使用上面的T-SQL语句可以实现,但我不知道如何编写相应的C#代码。

1个回答

14

试试这个:

public bool OneOffSTIntersect()
{
    var g =
        Microsoft.SqlServer.Types.SqlGeography.STGeomFromText(
            new System.Data.SqlTypes.SqlChars(
                "POLYGON((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))"), 4326);
    // suffix "d" on literals below optional but explicit
    var h = Microsoft.SqlServer.Types.SqlGeography.Point(47.653d, -122.358d, 4326);

    // rough equivalent to SELECT
    System.Console.WriteLine(g.STIntersects(h));

    // Alternatively return from a C# method or property (get).
    return g.STIntersects(h);
}

MSDN的SqlGeography Methods页面链接到了有关每个C#等价于T-SQL中关键调用(例如STIntersects)的信息。


4
// 大致相当于 SELECT :-) - Tim
1
@Tim:有趣的是,昨晚我只是按照自己的想法写出了代码,而不知道OP对C#的了解程度或上下文如何使用翻译后的代码;但今天这也让我觉得很有趣。 :) 感谢你指出来。 - J0e3gan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接