public void TestSTDistance() { using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString)) { var point = new NTSGS.Point(new NTSGS.Coordinate(-72.1235, 42.3521)) { SRID = SRID4326 }; db.Insert(new TestGeometryEntity(1, point)); var lineString = new NTSGS.LineString(new[] { new NTSGS.Coordinate(-72.1260, 42.45), new NTSGS.Coordinate(-72.123, 42.1546) }) { SRID = SRID4326 }; db.Insert(new TestGeometryEntity(2, lineString)); // Geometry example - units in planar degrees 4326 is WGS 84 long lat, units are degrees. var distances4326 = db.TestGeometries .Select(g => g.Geometry.STDistance(point)) .ToList(); Assert.AreEqual(2, distances4326.Count); Assert.AreEqual(0.0, distances4326[0]); Assert.AreEqual(0.00150567726382822, distances4326[1].Value, 1.0E-9); // Geometry example - units in meters (SRID:3857, proportional to pixels on popular web maps). var distances3857 = db.TestGeometries .Select(g => g.Geometry.STTransform(SRID3857).STDistance(point.STTransform(SRID3857))) .ToList(); Assert.AreEqual(2, distances3857.Count); Assert.AreEqual(0.0, distances3857[0]); Assert.AreEqual(167.441410065196, distances3857[1].Value, 1.0E-9); var nullDistance = db.TestGeometries .Where(g => g.Id == 1) .Select(g => g.Geometry.STDistance(null)) .Single(); Assert.IsNull(nullDistance); Assert.AreEqual( 0.00150567726382282, db.Select(() => MeasurementFunctions.STDistance( "SRID=4326;POINT(-72.1235 42.3521)", "SRID=4326;LINESTRING(-72.1260 42.45, -72.123 42.1546)")).Value, 1.0E-12); Assert.IsNull(db.Select(() => MeasurementFunctions.STDistance((NTSG)null, (NTSG)null))); // geography var pointGeography = new NTSGS.Point(-72.1235, 42.3521) { SRID = SRID4326 }; db.Insert(new TestGeographyEntity(1, pointGeography)); var lineGeography = new NTSGS.LineString(new[] { new NTSGS.Coordinate(-72.1260, 42.45), new NTSGS.Coordinate(-72.123, 42.1546) }) { SRID = SRID4326 }; db.Insert(new TestGeographyEntity(2, lineGeography)); var distance1 = db.TestGeographies .Where(g => g.Id == 1) .Select(g => g.Geography.STDistance(db.TestGeographies.Where(g0 => g0.Id == 2).Single().Geography)) .Single(); Assert.AreEqual(123.802077, distance1.Value, 1.0E-6); var distance2 = db.TestGeographies .Where(g => g.Id == 1) .Select(g => g.Geography.STDistance(db.TestGeographies.Where(g0 => g0.Id == 2).Single().Geography, false)) .Single(); Assert.AreEqual(123.475737, distance2.Value, 1.0E-6); } }
public void TestSTDistance() { using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString)) { var point = new NTSG.Point(new NTSG.Coordinate(-72.1235, 42.3521)) { SRID = SRID4326 }; db.Insert(new TestGeometryEntity(1, point)); var lineString = new NTSG.LineString(new[] { new NTSG.Coordinate(-72.1260, 42.45), new NTSG.Coordinate(-72.123, 42.1546) }) { SRID = SRID4326 }; db.Insert(new TestGeometryEntity(2, lineString)); // Geometry example - units in planar degrees 4326 is WGS 84 long lat, units are degrees. var distances4326 = db.TestGeometries.Select(g => g.Geometry.STDistance(point)).ToList(); Assert.AreEqual(2, distances4326.Count); Assert.AreEqual(0.0, distances4326[0]); Assert.AreEqual(0.00150567726382822, distances4326[1], 1.0E9); // Geometry example - units in meters (SRID:3857, proportional to pixels on popular web maps). var distances3857 = db.TestGeometries.Select(g => g.Geometry.STTransform(SRID3857).STDistance(point.STTransform(SRID3857))).ToList(); Assert.AreEqual(2, distances3857.Count); Assert.AreEqual(0.0, distances3857[0]); Assert.AreEqual(167.441410065196, distances3857[1], 1.0E9); var nullDistance = db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STDistance(null)).Single(); Assert.IsNull(nullDistance); } }