我正在使用kartoza码头工人图像来运行带有Postgis的Postgres服务器。我有一个使用ASP.NET Core应用程序和Enity Framework Core消耗的数据库。该数据库包含一个名为Park的表,该表由以下实体表示:
[Table("Park")]
public class Park
{
[Key]
public int Id { get; set; }
[Column(TypeName = "geography (point)")]
public Point Location { get; set; }
}
我正在创建文档指定的DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasPostgresExtension("postgis");
modelBuilder.Entity<Park>()
.Property(p => p.Location)
.HasColumnType("geography (point)");
}
我用以下代码为数据库播种:
if (!Parks.Any())
{
var park = new Park
{
Location = new Point(48.8566, 2.3522)
{
SRID = 4326
}
};
Parks.Add(park);
this.SaveChanges();
}
现在要测试我的代码,我计算出该公园与某个点之间的距离:
结果对应于笛卡尔坐标系中两点之间的几何距离,而不是地理距离。是否有可能获得与以下查询等效的结果:
答案 0 :(得分:1)
如here所述,需要坐标系统的投影才能计算地理距离。 ProjNet4GeoAPI用于使用防护方法进行投影:
static class GeometryExtensions
{
static readonly IGeometryServices _geometryServices = NtsGeometryServices.Instance;
static readonly ICoordinateSystemServices _coordinateSystemServices
= new CoordinateSystemServices(
new CoordinateSystemFactory(),
new CoordinateTransformationFactory(),
new Dictionary<int, string>
{
// Coordinate systems:
// (3857 and 4326 included automatically)
// This coordinate system covers the area of our data.
// Different data requires a different coordinate system.
[2855] =
@"
PROJCS[""NAD83(HARN) / Washington North"",
GEOGCS[""NAD83(HARN)"",
DATUM[""NAD83_High_Accuracy_Regional_Network"",
SPHEROID[""GRS 1980"",6378137,298.257222101,
AUTHORITY[""EPSG"",""7019""]],
AUTHORITY[""EPSG"",""6152""]],
PRIMEM[""Greenwich"",0,
AUTHORITY[""EPSG"",""8901""]],
UNIT[""degree"",0.01745329251994328,
AUTHORITY[""EPSG"",""9122""]],
AUTHORITY[""EPSG"",""4152""]],
PROJECTION[""Lambert_Conformal_Conic_2SP""],
PARAMETER[""standard_parallel_1"",48.73333333333333],
PARAMETER[""standard_parallel_2"",47.5],
PARAMETER[""latitude_of_origin"",47],
PARAMETER[""central_meridian"",-120.8333333333333],
PARAMETER[""false_easting"",500000],
PARAMETER[""false_northing"",0],
UNIT[""metre"",1,
AUTHORITY[""EPSG"",""9001""]],
AUTHORITY[""EPSG"",""2855""]]
"
});
public static IGeometry ProjectTo(this IGeometry geometry, int srid)
{
var geometryFactory = _geometryServices.CreateGeometryFactory(srid);
var transformation = _coordinateSystemServices.CreateTransformation(geometry.SRID, srid);
return GeometryTransform.TransformGeometry(
geometryFactory,
geometry,
transformation.MathTransform);
}
}
现在结果似乎更正确:
PS:除了ProjNet4GeoAPI金块包装外,扩展方法还需要NetTopologySuite。