Пример #1
0
        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(p => p.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(p => p.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(p => p.Geometry, () => GeometryInput.STGeomFromEWKT(ewkt3)).Insert();

                var geojson3 = db.TestGeometries.Where(g => g.Id == 3).Select(g => g.Geometry.STAsGeoJSON()).Single();
                Assert.AreEqual("{\"type\":\"Point\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:3857\"}},\"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)));
            }
        }
        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);
            }
        }