public void TestSTPerimeter()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                const string Ewkt1 = "SRID=2249;POLYGON((743238 2967416,743238 2967450,743265 2967450, 743265.625 2967416,743238 2967416))";
                db.TestGeometries
                .Value(g => g.Id, 1)
                .Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(Ewkt1))
                .Insert();

                const string Ewkt2 = @"SRID=2249;
                    MULTIPOLYGON(((763104.471273676 2949418.44119003,
                        763104.477769673 2949418.42538203,
                        763104.189609677 2949418.22343004,763104.471273676 2949418.44119003)),
                        ((763104.471273676 2949418.44119003,763095.804579742 2949436.33850239,
                        763086.132105649 2949451.46730207,763078.452329651 2949462.11549407,
                        763075.354136904 2949466.17407812,763064.362142565 2949477.64291974,
                        763059.953961626 2949481.28983009,762994.637609571 2949532.04103014,
                        762990.568508415 2949535.06640477,762986.710889563 2949539.61421415,
                        763117.237897679 2949709.50493431,763235.236617789 2949617.95619822,
                        763287.718121842 2949562.20592617,763111.553321674 2949423.91664605,
                        763104.471273676 2949418.44119003)))";
                db.TestGeometries
                .Value(g => g.Id, 2)
                .Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(Ewkt2))
                .Insert();

                const string Ewkt3 = "SRID=2249;POINT(0 0)";
                db.TestGeometries
                .Value(g => g.Id, 3)
                .Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(Ewkt3))
                .Insert();
                db.TestGeometries
                .Value(g => g.Id, 4)
                .Value(g => g.Geometry, () => null)
                .Insert();

                Assert.AreEqual(122.630744000095, db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STPerimeter()).Single().Value, 0.000000000001);
                Assert.AreEqual(122.630744000095, db.TestGeometries.Where(g => g.Id == 1).Select(g => g.Geometry.STPerimeter2D()).Single().Value, 0.000000000001);
                Assert.AreEqual(845.227713366825, db.TestGeometries.Where(g => g.Id == 2).Select(g => g.Geometry.STPerimeter()).Single().Value, 0.000000000001);
                Assert.AreEqual(845.227713366825, db.TestGeometries.Where(g => g.Id == 2).Select(g => g.Geometry.STPerimeter2D()).Single().Value, 0.000000000001);
                Assert.AreEqual(0.0, db.TestGeometries.Where(g => g.Id == 3).Select(g => g.Geometry.STPerimeter()).Single());
                Assert.IsNull(db.TestGeometries.Where(g => g.Id == 4).Select(g => g.Geometry.STPerimeter()).Single());
                Assert.IsNull(db.TestGeometries.Where(g => g.Id == 4).Select(g => g.Geometry.STPerimeter2D()).Single());

                Assert.AreEqual(122.630744000095, db.Select(() => MeasurementFunctions.STPerimeter2D(Ewkt1)).Value, 0.000000000001);


                var PolygonGeographyWkt = "POLYGON((-71.1776848522251 42.3902896512902,-71.1776843766326 42.3903829478009, -71.1775844305465 42.3903826677917,-71.1775825927231 42.3902893647987,-71.1776848522251 42.3902896512902))";
                db.TestGeographies
                .Value(g => g.Id, 1)
                .Value(g => g.Geography, () => GeometryInput.STGeographyFromText(PolygonGeographyWkt))
                .Insert();

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

                Assert.AreEqual(37.379046, perimeterGeography.Value, 1.0E-6);
            }
        }
        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 TestSTLength()
        {
            using (var db = new PostGisTestDataConnection(TestDatabaseConnectionString))
            {
                const string Ewkt1 = "SRID=2249;LINESTRING(743238 2967416,743238 2967450,743265 2967450, 743265.625 2967416,743238 2967416)";
                db.TestGeometries
                .Value(g => g.Id, 1)
                .Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(Ewkt1))
                .Insert();

                const string Ewkt2 = "SRID=2249;POINT(0 0)";
                db.TestGeometries
                .Value(g => g.Id, 2)
                .Value(g => g.Geometry, () => GeometryInput.STGeomFromEWKT(Ewkt2))
                .Insert();
                db.TestGeometries
                .Value(g => g.Id, 3)
                .Value(g => g.Geometry, () => null)
                .Insert();

                var length1 = db.TestGeometries
                              .Where(g => g.Id == 1)
                              .Select(g => g.Geometry.STLength())
                              .Single();
                Assert.AreEqual(122.630744000095, length1.Value, 0.000000000001);

                var length2 = db.TestGeometries
                              .Where(g => g.Id == 1)
                              .Select(g => g.Geometry.STLength2D())
                              .Single();
                Assert.AreEqual(122.630744000095, length2.Value, 0.000000000001);

                Assert.AreEqual(0.0,
                                db.TestGeometries
                                .Where(g => g.Id == 2)
                                .Select(g => g.Geometry.STLength())
                                .Single());
                Assert.IsNull(
                    db.TestGeometries.Where(g => g.Id == 3)
                    .Select(g => g.Geometry.STLength())
                    .Single());

                Assert.AreEqual(
                    122.630744000095,
                    db.Select(() => MeasurementFunctions.STLength(Ewkt1)).Value,
                    0.000000000001);

                Assert.AreEqual(
                    122.630744000095,
                    db.Select(() => MeasurementFunctions.STLength2D(Ewkt1)).Value,
                    0.000000000001);

                Assert.IsNull(db.Select(() => MeasurementFunctions.STLength((NTSG)null)));


                var PolygonGeographyWkt = "SRID=4326;LINESTRING(-72.1260 42.45, -72.1240 42.45666, -72.123 42.1546)";
                db.TestGeographies
                .Value(g => g.Id, 1)
                .Value(g => g.Geography, () => GeometryInput.STGeographyFromText(PolygonGeographyWkt))
                .Insert();

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

                Assert.AreEqual(34310.57036, perimeter1.Value, 1.0E-5);

                var perimeter2 = db.TestGeographies
                                 .Where(g => g.Id == 1)
                                 .Select(g => g.Geography.STLength(false))
                                 .Single();

                Assert.AreEqual(34346.20609, perimeter2.Value, 1.0E-5);
            }
        }