Exemple #1
0
        public void MultiPoint()
        {
            var writer = new WktWriter();

            var brackets = writer.Write(new MultiPoint(new Point(65.9, 0), new Point(9, -34.5), new Point(40, -20), new Point(65.9, 0)));

            Assert.AreEqual("MULTIPOINT ((0 65.9), (-34.5 9), (-20 40), (0 65.9))", brackets);

            var empty = writer.Write(new MultiPoint());

            Assert.AreEqual("MULTIPOINT EMPTY", empty);
        }
Exemple #2
0
        public void LineString()
        {
            var writer = new WktWriter();

            var xy = writer.Write(new LineString(new Coordinate(65.9, 0), new Coordinate(9, -34.5)));

            Assert.AreEqual("LINESTRING (0 65.9, -34.5 9)", xy);

            var empty = writer.Write(global::Geo.Geometries.LineString.Empty);

            Assert.AreEqual("LINESTRING EMPTY", empty);
        }
Exemple #3
0
        public void Polygon()
        {
            var writer = new WktWriter();

            var xy = writer.Write(new Polygon(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0))));

            Assert.AreEqual("POLYGON ((0 65.9, -34.5 9, -20 40, 0 65.9))", xy);

            var empty = writer.Write(global::Geo.Geometries.Polygon.Empty);

            Assert.AreEqual("POLYGON EMPTY", empty);
        }
Exemple #4
0
        public void GeometryCollection()
        {
            var writer = new WktWriter();

            var brackets = writer.Write(new GeometryCollection(new Point(65.9, 0), new Point(9, -34.5), new Point(40, -20), new Point(65.9, 0)));

            Assert.AreEqual("GEOMETRYCOLLECTION (POINT (0 65.9), POINT (-34.5 9), POINT (-20 40), POINT (0 65.9))", brackets);

            var empty = writer.Write(new GeometryCollection());

            Assert.AreEqual("GEOMETRYCOLLECTION EMPTY", empty);
        }
Exemple #5
0
        public void If_geometry_is_invalid_then_should_throw_exception()
        {
            var point = new Point();

            Assert.Throws <ArgumentException>(() => WktWriter.Write(point)).Message.Should()
            .BeEquivalentTo("Invalid geometry");
        }
        public void If_polygon_with_one_ring_is_valid_then_should_return_wkt()
        {
            var          polygon   = new Polygon(new[] { new LineString(new[] { new Point(-120, 30), new Point(0, 0), new Point(120, 30), new Point(-120, 30) }) });
            const string expectWkt = "POLYGON ((-120 30, 0 0, 120 30, -120 30))";

            WktWriter.Write(polygon).ShouldBe(expectWkt);
        }
        public void If_linestring_is_valid_then_should_return_wkt()
        {
            var          lineString = new LineString(new[] { new Point(10, 20), new Point(10, 30), new Point(0, 0) });
            const string expectWkt  = "LINESTRING (10 20, 10 30, 0 0)";

            WktWriter.Write(lineString).ShouldBe(expectWkt);
        }
        public void If_multipoint_is_valid_then_should_return_wkt()
        {
            var          multiPoint = new MultiPoint(new[] { new Point(10, 20), new Point(20, 30), new Point(30, 60) });
            const string expectWkt  = "MULTIPOINT (10 20, 20 30, 30 60)";

            WktWriter.Write(multiPoint).ShouldBe(expectWkt);
        }
        public void If_geometry_is_invalid_then_should_throw_exception()
        {
            var point = new Point();

            Should.Throw <ArgumentException>(() => WktWriter.Write(point))
            .Message.ShouldBe("Invalid geometry");
        }
Exemple #10
0
        public void MultiPolygon()
        {
            var writer = new WktWriter();

            var one = writer.Write(new MultiPolygon(new Polygon(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0)))));

            Assert.AreEqual("MULTIPOLYGON (((0 65.9, -34.5 9, -20 40, 0 65.9)))", one);


            var two = writer.Write(new MultiPolygon(new Polygon(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0))), new Polygon(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0)))));

            Assert.AreEqual("MULTIPOLYGON (((0 65.9, -34.5 9, -20 40, 0 65.9)), ((0 65.9, -34.5 9, -20 40, 0 65.9)))", two);

            var empty = writer.Write(new MultiPolygon());

            Assert.AreEqual("MULTIPOLYGON EMPTY", empty);
        }
Exemple #11
0
        public void MultiLineString()
        {
            var writer = new WktWriter();

            var one = writer.Write(new MultiLineString(new LineString(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0))));

            Assert.AreEqual("MULTILINESTRING ((0 65.9, -34.5 9, -20 40, 0 65.9))", one);


            var two = writer.Write(new MultiLineString(new LineString(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0)), new LineString(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0))));

            Assert.AreEqual("MULTILINESTRING ((0 65.9, -34.5 9, -20 40, 0 65.9), (0 65.9, -34.5 9, -20 40, 0 65.9))", two);

            var empty = writer.Write(new MultiLineString());

            Assert.AreEqual("MULTILINESTRING EMPTY", empty);
        }
        public void If_point_is_valid_then_should_return_wkt()
        {
            var          point     = new Point(10, 20);
            const string expectWkt = "POINT (10 20)";

            var resultWkt = WktWriter.Write(point);

            resultWkt.ShouldBe(expectWkt);
            point.ToWkt().ShouldBe(expectWkt);
        }
Exemple #13
0
        public void LinearRing()
        {
            var writer = new WktWriter();

            var lineString = writer.Write(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(50, 0), new Coordinate(65.9, 0)));

            Assert.AreEqual("LINESTRING (0 65.9, -34.5 9, 0 50, 0 65.9)", lineString);

            var writer2 = new WktWriter(new WktWriterSettings {
                LinearRing = true
            });

            var linearRing = writer2.Write(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(50, 0), new Coordinate(65.9, 0)));

            Assert.AreEqual("LINEARRING (0 65.9, -34.5 9, 0 50, 0 65.9)", linearRing);

            var empty = writer2.Write(global::Geo.Geometries.LinearRing.Empty);

            Assert.AreEqual("LINEARRING EMPTY", empty);
        }
Exemple #14
0
        public void Triangle()
        {
            var writer = new WktWriter();

            var polygon = writer.Write(new Triangle(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0))));

            Assert.AreEqual("POLYGON ((0 65.9, -34.5 9, -20 40, 0 65.9))", polygon);

            var writer2 = new WktWriter(new WktWriterSettings {
                Triangle = true
            });

            var triangle = writer2.Write(new Triangle(new LinearRing(new Coordinate(65.9, 0), new Coordinate(9, -34.5), new Coordinate(40, -20), new Coordinate(65.9, 0))));

            Assert.AreEqual("TRIANGLE ((0 65.9, -34.5 9, -20 40, 0 65.9))", triangle);

            var empty = writer2.Write(global::Geo.Geometries.Triangle.Empty);

            Assert.AreEqual("TRIANGLE EMPTY", empty);
        }
        public void If_multipolygon_is_valid_then_should_return_wkt()
        {
            var multiPolygon = new MultiPolygon(new[]
            {
                new Polygon(new[] { new LineString(new[] { new Point(30, 20), new Point(45, 40), new Point(10, 40), new Point(30, 20) }) }),
                new Polygon(new[] { new LineString(new[] { new Point(15, 5), new Point(40, 10), new Point(10, 20), new Point(5, 10), new Point(15, 5) }) })
            });

            const string expectWkt = "MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))";

            WktWriter.Write(multiPolygon).ShouldBe(expectWkt);
        }
Exemple #16
0
        private void TestWriteGeometry(IGeometry geometry, string expectedWkt)
        {
            MemoryStream stream = new MemoryStream();

            using (WktWriter writer = new WktWriter(stream, new WktWriterSettings())) {
                writer.Write(geometry);
            }

            using (TextReader tr = new StreamReader(new MemoryStream(stream.ToArray()))) {
                string wkt = tr.ReadToEnd();
                Assert.Equal(expectedWkt, wkt, StringComparer.OrdinalIgnoreCase);
            }
        }
Exemple #17
0
        public void Point()
        {
            var writer = new WktWriter();

            var xy = writer.Write(new Point(65.9, 0));

            Assert.AreEqual("POINT (0 65.9)", xy);

            var xyz = writer.Write(new Point(65.9, 0, 5));

            Assert.AreEqual("POINT Z (0 65.9 5)", xyz);

            var xym = writer.Write(new Point(new CoordinateM(65.9, 0, 5)));

            Assert.AreEqual("POINT M (0 65.9 5)", xym);

            var xyzm = writer.Write(new Point(65.9, 0, 4, 5));

            Assert.AreEqual("POINT ZM (0 65.9 4 5)", xyzm);

            var empty = writer.Write(global::Geo.Geometries.Point.Empty);

            Assert.AreEqual("POINT EMPTY", empty);
        }
Exemple #18
0
        public static Boolean ExportToShapeUsingOgr(this IFeatureSet dataset, String outputShapefileName)
        {
            OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8");

            var outputFolder = Path.GetDirectoryName(outputShapefileName);

            if (File.Exists(outputShapefileName))
            {
                var exts = new List <String> {
                    ".dbf", ".shx", ".shp", ".prj"
                };
                foreach (var ext in exts)
                {
                    var fn = outputFolder + "\\" + Path.GetFileNameWithoutExtension(outputShapefileName) + ext;
                    if (File.Exists(fn))
                    {
                        File.Delete(fn);
                    }
                }
            }

            var drv = Ogr.GetDriverByName("ESRI Shapefile");

            if (drv == null)
            {
                return(false);
            }
            var ds = drv.CreateDataSource(Path.GetDirectoryName(outputShapefileName), null);

            if (ds == null)
            {
                return(false);
            }

            var fromSrs = new SpatialReference(null);

            fromSrs.ImportFromEPSG(32640);

            Utilities.LogDebug(Path.GetFileNameWithoutExtension(outputShapefileName));

            var lyr = ds.CreateLayer(Path.GetFileNameWithoutExtension(outputShapefileName), fromSrs, wkbGeometryType.wkbPolygon, null);
            var listOfFieldNames = new List <String>();
            var fieldMapping     = new Dictionary <string, string>();

            foreach (System.Data.DataColumn dc in dataset.GetColumns())
            {
                var dbfName = dc.ColumnName.Dbfify(listOfFieldNames);
                listOfFieldNames.Add(dbfName);
                fieldMapping.Add(dc.ColumnName, dbfName);
                lyr.CreateField(new FieldDefn(dbfName, dc.DataType.OgrType()), 0);
            }

            var wktWriter = new WktWriter();

            foreach (var currentFeature in dataset.Features)
            {
                var f = new OSGeo.OGR.Feature(lyr.GetLayerDefn());
                foreach (var dc2 in fieldMapping)
                {
                    var      srcGeom   = currentFeature.BasicGeometry as DotSpatial.Topology.Geometry;
                    var      wktGeom   = wktWriter.Write(srcGeom);
                    Geometry shapeGeom = Geometry.CreateFromWkt(wktGeom);
                    f.SetGeometry(shapeGeom);
                    var val = currentFeature.DataRow[dc2.Key];
                    f.SetField(dc2.Value, val.ToString());
                }
                var s = lyr.CreateFeature(f);
            }

            if (lyr == null)
            {
                return(false);
            }
            else
            {
                ds.ExecuteSQL("CREATE SPATIAL INDEX ON " + Path.GetFileNameWithoutExtension(outputShapefileName), null, "OGRSQL");
            }

            fromSrs.Dispose();
            lyr.Dispose();
            ds.Dispose();
            drv.Dispose();
            return(true);
        }