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

.net-core entity-framework-core nettopologysuite postgis postgresql

Question

I am using the kartoza docker image to run a postgres server with postgis. I have a database which I am consuming using an ASP.NET Core application and Enity Framework Core. The database contains a table named Park which is represented by the following entity:

[Table("Park")]
public class Park
{
     [Key]
     public int Id { get; set; }

     [Column(TypeName = "geography (point)")]
     public Point Location { get; set; }
}

I am creating the DbContext as specified by the documentation:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.HasPostgresExtension("postgis");

    modelBuilder.Entity<Park>()
        .Property(p => p.Location)
        .HasColumnType("geography (point)");
}

I seed the database with the following code:

if (!Parks.Any())
{
    var park = new Park
    {
        Location = new Point(48.8566, 2.3522)
        {
            SRID = 4326
        }
    };

    Parks.Add(park);
    this.SaveChanges();
}

Now to test my code, I calculate the distance between this park and some point:

enter image description here

The result correspond to the geometric distance between the two points in a cartesian frame, not the geographic distance. Is it possibe to get the equivalent of the following query:

rap

1
1
7/1/2019 7:57:22 PM

Popular Answer

As mentioned here, a projection of the coordinates system is needed in order to calculate the geographic distance. ProjNet4GeoAPI is used to perform the projection using an extetion method:

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);
    }
}

Now the result seems more correct:

enter image description here

PS: In addition to the ProjNet4GeoAPI nugget pakage, the NetTopologySuite is also needed for the extension method.

3
7/2/2019 7:48:27 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow