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);
            }
        }
        public void TestSTIntersects()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                const string Wkt1 = "LINESTRING ( 2 0, 0 2 )";
                db.TestGeometries.Value(g => g.Id, 1).Value(g => g.Geometry, () => GeometryInput.STGeomFromText(Wkt1)).Insert();

                const string Wkt2 = "LINESTRING ( 0 0, 0 2 )";
                db.TestGeometries.Value(g => g.Id, 2).Value(g => g.Geometry, () => GeometryInput.STGeomFromText(Wkt2)).Insert();

                const string PointWkt = "POINT(0 0)";
                var          point    = new NTSGS.Point(new NTSGS.Coordinate(0, 0));

                Assert.IsFalse(db.TestGeometries
                               .Where(g => g.Id == 1)
                               .Select(g => g.Geometry.STIntersects(point))
                               .Single());

                Assert.IsTrue(db.TestGeometries
                              .Where(g => g.Id == 2)
                              .Select(g => g.Geometry.STIntersects(point))
                              .Single());

                Assert.IsNull(db.TestGeometries
                              .Where(g => g.Id == 2)
                              .Select(g => g.Geometry.STIntersects(null))
                              .Single());

                Assert.IsFalse(db.Select(() => SpatialRelationships.STIntersects(Wkt1, PointWkt)));
                Assert.IsTrue(db.Select(() => SpatialRelationships.STIntersects(Wkt2, PointWkt)));
                Assert.IsNull(db.Select(() => SpatialRelationships.STIntersects((string)null, (string)null)));

                // geography
                var lineGeography = new NTSGS.LineString(new[] { new NTSGS.Coordinate(-43.23456, 72.4567), new NTSGS.Coordinate(-43.23456, 72.4568) })
                {
                    SRID = SRID4326
                };
                db.Insert(new TestGeographyEntity(1, lineGeography));

                var pointGeography = new NTSGS.Point(-43.23456, 72.4567)
                {
                    SRID = SRID4326
                };
                db.Insert(new TestGeographyEntity(2, pointGeography));

                var intersects = db.TestGeographies
                                 .Where(g => g.Id == 1)
                                 .Select(g => g.Geography.STIntersects(db.TestGeographies.Where(g0 => g0.Id == 2).Single().Geography))
                                 .Single();

                Assert.IsTrue(intersects);
            }
        }
        public void TestSTAzimuth()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                var p1 = db.Select(() => GeometryConstructors.STPoint(25, 45));
                var p2 = db.Select(() => GeometryConstructors.STPoint(75, 100));

                var a1 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAzimuth(p1, p2)));
                var a2 = db.Select(() => MathematicalFunctions.Degrees(MeasurementFunctions.STAzimuth(p2, p1)));

                Assert.AreEqual(42.2736890060937, a1.Value, 1.0E-8);
                Assert.AreEqual(222.273689006094, a2.Value, 1.0E-8);

                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(p1, p1)));
                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(p1, null)));
                Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth(null, p1)));
                ////Assert.IsNull(db.Select(() => MeasurementFunctions.STAzimuth((NTSG)null, (NTSG)null)));


                var pointA = new NTSGS.Point(0, 0)
                {
                    SRID = SRID4326
                };
                db.Insert(new TestGeographyEntity(1, pointA));

                var pointB = new NTSGS.Point(15, 5)
                {
                    SRID = SRID4326
                };
                db.Insert(new TestGeographyEntity(2, pointB));

                var azimuth = db.TestGeographies
                              .Where(g => g.Id == 1)
                              .Select(g => g.Geography.STAzimuth(db.TestGeographies.Where(g0 => g0.Id == 2).Single().Geography))
                              .Single();

                Assert.AreEqual(1.24683, azimuth.Value, 1.0E-5);
            }
        }
        public void TestSTAsGeoJSON()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                const string Wkt1 = "POINT(2.48 4.75)";
                db.TestGeometries
                .Value(g => g.Id, 1)
                .Value(g => g.Geometry, () => GeometryInput.STGeomFromText(Wkt1))
                .Insert();

                var geojson1 = db.TestGeometries
                               .Where(g => g.Id == 1)
                               .Select(g => g.Geometry.STAsGeoJSON())
                               .Single();
                Assert.AreEqual("{\"type\":\"Point\",\"coordinates\":[2.48,4.75]}", geojson1);

                var geojson1crs = db.TestGeometries
                                  .Where(g => g.Id == 1)
                                  .Select(g => g.Geometry.STAsGeoJSON(1, 4))
                                  .Single();
                Assert.AreEqual("{\"type\":\"Point\",\"coordinates\":[2.5,4.8]}", geojson1crs);


                const string Wkt2 = "LINESTRING(1 2 3, 4 5 6)";
                db.TestGeometries.Value(g => g.Id, 2).Value(g => g.Geometry, () => GeometryInput.STGeomFromText(Wkt2)).Insert();

                var geojson2 = db.TestGeometries
                               .Where(g => g.Id == 2)
                               .Select(g => g.Geometry.STAsGeoJSON())
                               .Single();
                Assert.AreEqual("{\"type\":\"LineString\",\"coordinates\":[[1,2,3],[4,5,6]]}", geojson2);


                const string Ewkt3 = "SRID=3857;POINT(2.48 4.75)";
                db.TestGeometries
                .Value(g => g.Id, 3)
                .Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(Ewkt3))
                .Insert();

                var geojson3 = db.TestGeometries
                               .Where(g => g.Id == 3)
                               .Select(g => g.Geometry.STAsGeoJSON())
                               .Single();

                if (this.CurrentVersion >= new Version("3.0.0"))
                {
                    Assert.AreEqual(
                        "{\"type\":\"Point\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:3857\"}},\"coordinates\":[2.48,4.75]}",
                        geojson3);
                }
                else
                {
                    Assert.AreEqual(
                        "{\"type\":\"Point\",\"coordinates\":[2.48,4.75]}",
                        geojson3);
                }

                var geojson3crs = db.TestGeometries
                                  .Where(g => g.Id == 3)
                                  .Select(g => g.Geometry.STAsGeoJSON(1, 4))
                                  .Single();
                Assert.AreEqual("{\"type\":\"Point\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"urn:ogc:def:crs:EPSG::3857\"}},\"coordinates\":[2.5,4.8]}", geojson3crs);

                Assert.IsNull(db.Select(() => GeometryOutput.STAsGeoJSON(null)));


                db.TestGeographies
                .Value(g => g.Id, 1)
                .Value(g => g.Geography, () => GeometryInput.STGeographyFromText("POINT(30 60)"))
                .Insert();

                var geojson4 = db.TestGeographies
                               .Where(g => g.Id == 1)
                               .Select(g => g.Geography.STAsGeoJSON())
                               .Single();

                Assert.AreEqual(
                    "{\"type\":\"Point\",\"coordinates\":[30,60]}",
                    geojson4);

                var pointGeography = new NTSGS.Point(-43.23456, 72.4567772)
                {
                    SRID = SRID4326
                };
                db.Insert(new TestGeographyEntity(2, pointGeography));

                var geojson5 = db.TestGeographies
                               .Where(g => g.Id == 2)
                               .Select(g => g.Geography.STAsGeoJSON(3))
                               .Single();

                Assert.AreEqual(
                    "{\"type\":\"Point\",\"coordinates\":[-43.235,72.457]}",
                    geojson5);
            }
        }
        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);
            }
        }