简体   繁体   English

在 C# 客户端使用 SqlGeography 类型

[英]using SqlGeography types in C# client-side

In C# client-side, I am trying to use the SQLServerTypes (SqlServerSpatial140.dll) assembly (edit: doing so directly, SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory) ) to measure the distance between points on the pl.net, between one street address and another street address which are usually within 50 miles of each other (but sometimes a lot farther).在 C# 客户端,我正在尝试使用 SQLServerTypes (SqlServerSpatial140.dll) 程序集(编辑:直接这样做, SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory) )来测量 pl.net 上各点之间的距离,在一个街道地址和另一个街道地址之间,通常相距在 50 英里以内(但有时更远)。 Each location is expressed as a latitude/longitude pair.每个位置都表示为纬度/经度对。

This is a Framework application and uses the NuGet package.这是一个框架应用程序,使用 NuGet package。

Is this code correct?这段代码正确吗? I don't think it can be, since the values I'm getting for distance are much too small, eg 24.35946... when the two points are hundreds of miles away from each other, such as two towns, one of them in North Carolina and the other in Puerto Rico.我认为不可能,因为我得到的distance值太小了,例如24.35946...当两个点相距数百英里时,例如两个城镇,其中一个在北卡罗来纳州和波多黎各的另一个。 Isn't meters the standard unit?米不是标准单位吗?

        foreach (Origin o in Origins)
        {
            o.loc = SqlGeometry.Point(o.lat, o.lon, 4326);
            foreach (Destination d in Destinations)
            {
                SqlDouble distance = o.loc.STDistance(SqlGeometry.Point(d.lat, d.lon, 4326));
                <snip>                   
            }
        }

Is 4326 the correct SRID? 4326是正确的 SRID 吗? I get the same results if zero is the SRID.如果 SRID 为零,我会得到相同的结果。 Also, the order in which the parameters to Point are supplied (lat,lon) or (lon,lat) doesn't make the distance numbers much larger.此外,向 Point 提供参数的顺序 (lat,lon) 或 (lon,lat) 不会使距离数字变大。

PS I was able to get it working with Brian's help. PS 在 Brian 的帮助下,我能够让它工作。 Here's how I'm instantiating the Point:这是我实例化点的方式:

 public Microsoft.SqlServer.Types.SqlGeography CreateGeographyPoint(double longitude, double latitude)
        {
            var text = string.Format("POINT({0} {1})", longitude, latitude);
            var ch = new System.Data.SqlTypes.SqlChars(text);
            return Microsoft.SqlServer.Types.SqlGeography.STPointFromText( ch, 4326);
        }

You should use the Sqlgeography class and the Srid of 4326 will give you the result in meters.您应该使用 Sqlgeography class 和 4326 的 Srid 将为您提供以米为单位的结果。

SqlGeometry is for Cartesian coordinates (x, y), and SqlGeography is for Geospatial coordinates Long, Lat in that order. SqlGeometry 用于笛卡尔坐标 (x, y),而 SqlGeography 用于地理空间坐标 Long、Lat 的顺序。

Replace SqlGeometry with SqlGeography.将 SqlGeometry 替换为 SqlGeography。

This is really more of a GIS question than a programming question.这实际上更像是一个 GIS 问题,而不是一个编程问题。

SRID is Spatial Reference ID; SRID是空间参考ID; the spatial reference that the data in that table is recorded in, or the spatial reference that you want to use when working with or displaying the data.记录该表中数据的空间参考,或者您在处理或显示数据时要使用的空间参考。 See:看:

https://desktop.arcgis.com/en/arcmap/10.3/manage-data/using-sql-with-gdbs/what-is-an-srid.htm https://desktop.arcgis.com/en/arcmap/10.3/manage-data/using-sql-with-gdbs/what-is-an-srid.htm

and

https://learn.microsoft.com/en-us/sql/relational-databases/spatial/spatial-reference-identifiers-srids https://learn.microsoft.com/en-us/sql/relational-databases/spatial/spatial-reference-identifiers-srids

You can query data in your table to see what spatial reference it is recorded in. See:您可以查询表中的数据以查看它记录在哪个空间参考中。请参阅:

https://learn.microsoft.com/en-us/sql/t-sql/spatial-geometry/stsrid-geometry-data-type?view=sql-server-ver15 https://learn.microsoft.com/en-us/sql/t-sql/spatial-geometry/stsrid-geometry-data-type?view=sql-server-ver15

As to whether or not that SRID is correct, that's up to you.至于那个 SRID 是否正确,那取决于你。 What SRID was used when the data was entered?输入数据时使用的是什么 SRID? If you want it out with the same spatial reference, use the same SRID.如果您希望它具有相同的空间参考,请使用相同的 SRID。 If you want the results out in a different spatial reference, use a different SRID.如果您希望在不同的空间参考中输出结果,请使用不同的 SRID。 4326 is WGS84: 4326 是 WGS84:

https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84 https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM