1

我使用来自https://docs.microsoft.com/de-de/ef/core/modeling/spatial的文章来启动和使用

  • 实体框架核心
  • 微软 SQL
  • NetTopologySuite.Core 1.15.3

我有一个 IGeometry 类型的 City 类。它是自动创建的,因为我在数据库中创建了表:

CREATE TABLE [dbo].[City] (
    [Location][sys].[geography] NULL,
);

public partial class City
{
    public IGeometry Location { get; set; }
}

起初我创建了数据

GeoAPI.Geometries.IGeometryFactory geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
GeoAPI.Geometries.IPoint point = geometryFactory.CreatePoint(new GeoAPI.Geometries.Coordinate((double)c.Lat, (double)c.Lng));
City city = new City();
city.Location = point;

在数据库中,位置现在类似于 0xE6100000010CCC7A319413CD2A4044696FF085314A40

现在我想查询。我想要 searchCity 附近的所有城市:

from city in cityRepository.Table
where city.Location.Distance(searchCity.Location) < 20 // 20 kilometers
select city;

问题就在这里,计算出的距离非常小。例如,城市到另一个城市的距离为 300 公里,计算距离约为 4。当我阅读文档时,距离以度为单位。但我需要按公里过滤。
文档中还有一些关于 ProjNet4GeoAPI 的内容。但我不想在我查询后计算。我只想通过查询获取半径范围内的城市。

4

0 回答 0