繁体   English   中英

使用带有EF Core的NetTopologySuite和postgis计算两点之间的地理距离

[英]Calculate the geographic distance between two points using NetTopologySuite with EF Core and postgis

我正在使用kartoza docker映像来运行带有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();
}

现在测试我的代码,我计算出该公园与某个点之间的距离:

在此处输入图片说明

结果对应于笛卡尔坐标系中两点之间的几何距离,而不是地理距离。 是否有可能获得以下查询的等效项:

敲击

如所提到的在这里 ,需要在为了计算地理距离的坐标系统的投影。 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。

暂无
暂无

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

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