EF核心空间数据。以米为单位查询和获取距离

问题描述 投票:0回答:1

我可以查询并得到离客户最近的位置,有度。

serviceQuery = serviceQuery.Where(s => s.Location.Distance(currentLocation) < <degree>)
    .OrderBy(s => s.Location.Distance(currentLocation));

位置是 NetTopologySuite.Geometries.Point 在C#和 geography 在SQL Server中,如何在查询中提供米数而不是度数?

如何在查询中提供米数而不是度数,同时也能得到米数距离而不是度数?在客户端将结果度数转换为米数是可以接受的。

sql-server asp.net-core entity-framework-core spatial
1个回答
0
投票

据我所知,在插入地理坐标时,使用哪个SRID(空间参考系统)是很重要的。这个文档有点帮助。https:/docs.microsoft.comen-usefcoremodelingspatial。

我正在为SRID 4326使用这段代码。

public static Point CreatePoint(double latitude, double longitude)
{
    // 4326 is most common coordinate system used by GPS/Maps
    var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);

    // see https://docs.microsoft.com/en-us/ef/core/modeling/spatial
    // Longitude and Latitude
    var newLocation = geometryFactory.CreatePoint(new Coordinate(longitude, latitude));

    return newLocation;
}

假设你在SRID4326中也创建了currentLocation。

var currentLocation = CreatePoint(...

那么..,

var meters = 500;
serviceQuery = serviceQuery
    .Where(s => s.Location.Distance(currentLocation) < meters )
    .OrderBy(s => s.Location.Distance(currentLocation));

应运而生

© www.soinside.com 2019 - 2024. All rights reserved.