簡體   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