public void TestReadingShapeFileAfvalbakken()
        {
            IGeometryFactory factory  = GeometryFactory.Default;
            List <IPolygon>  polys    = new List <IPolygon>();
            const int        distance = 500;

            using (ShapefileDataReader reader = new ShapefileDataReader("afvalbakken", factory))
            {
                int index = 0;
                while (reader.Read())
                {
                    IGeometry geom = reader.Geometry;
                    Assert.IsNotNull(geom);
                    Assert.IsTrue(geom.IsValid);
                    Debug.WriteLine(String.Format("Geom {0}: {1}", index++, geom));

                    IGeometry buff = geom.Buffer(distance);
                    Assert.IsNotNull(buff);

                    polys.Add((IPolygon)geom);
                }
            }

            IMultiPolygon multiPolygon = factory.CreateMultiPolygon(polys.ToArray());

            Assert.IsNotNull(multiPolygon);
            Assert.IsTrue(multiPolygon.IsValid);

            IMultiPolygon multiBuffer = (IMultiPolygon)multiPolygon.Buffer(distance);

            Assert.IsNotNull(multiBuffer);
            Assert.IsTrue(multiBuffer.IsValid);

            ShapefileWriter.WriteGeometryCollection(@"test_buffer", multiBuffer);
        }
        private static void WriteShape(GeometryCollection geometries, string shapepath)
        {
            if (File.Exists(shapepath))
            {
                File.Delete(shapepath);
            }
            var sfw = new ShapefileWriter(geometries.Factory);

            ShapefileWriter.WriteGeometryCollection(Path.GetFileNameWithoutExtension(shapepath), geometries);
        }
예제 #3
0
        public void TestWriteSimpleShapeFile()
        {
            var p1 = Factory.CreatePoint(new Coordinate(100, 100));
            var p2 = Factory.CreatePoint(new Coordinate(200, 200));

            var coll = new GeometryCollection(new IGeometry[] { p1, p2, });

            ShapefileWriter.WriteGeometryCollection(@"test_arcview", coll);

            // Not read by ArcView!!!
        }
예제 #4
0
        private static void DoTest(IGeometryCollection geomsWrite, Ordinates ordinates, bool testGetOrdinate = true)
        {
            string fileName = string.Empty;

            try
            {
                fileName = Path.GetTempFileName();
                fileName = Path.ChangeExtension(fileName, "shp");
                ShapefileWriter.WriteGeometryCollection(fileName, geomsWrite);
                var reader    = new ShapefileReader(fileName, ShapeFileShapeFactory.FactoryRead);
                var geomsRead = reader.ReadAll();

                // This tests x- and y- values
                if (!geomsWrite.EqualsExact(geomsRead))
                {
                    Assert.AreEqual(geomsWrite.NumGeometries, geomsRead.NumGeometries);
                    //
                    // This akward test is necessary since EqualsTopologically throws currently exceptions
                    bool equal = true;
                    for (int i = 0; i < geomsRead.NumGeometries; i++)
                    {
                        var gw = geomsWrite.GetGeometryN(i);
                        var gr = geomsRead.GetGeometryN(i);
                        if (gw.IsEmpty && gr.IsEmpty)
                        {
                            if ((gw is ILineal && gr is ILineal) ||
                                (gw is IPolygonal && gr is IPolygonal))
                            {
                                // suppose these are equal
                            }
                            else
                            {
                                Console.WriteLine(string.Format("Geometries don't match at index {0}", i));
                                Console.WriteLine(string.Format("  written: {0}", gw.AsText()));
                                Console.WriteLine(string.Format("  read   : {0}", gr.AsText()));
                                equal = false;
                                Assert.IsTrue(equal, "Differenced found in geometries written and read!");
                            }
                        }
                        else if (!gw.EqualsExact(gr))
                        {
                            double hsm = new HausdorffSimilarityMeasure().Measure(gw, gr);
                            double asm = new AreaSimilarityMeasure().Measure(gw, gr);
                            double smc = SimilarityMeasureCombiner.Combine(hsm, asm);
                            if (!gw.EqualsNormalized(gr) || (1d - smc) > 1e-7)
                            {
                                Console.WriteLine(string.Format("Geometries don't match at index {0}", i));
                                Console.WriteLine(string.Format("  written: {0}", gw.AsText()));
                                Console.WriteLine(string.Format("  read   : {0}", gr.AsText()));
                                equal = false;
                                Assert.IsTrue(equal, "Differenced found in geometries written and read!");
                            }
                        }
                    }

                    //For polygons this has a tendency to fail, since the polygonhandler might rearrange the whole thing
                    if (testGetOrdinate)
                    {
                        if ((ordinates & Ordinates.Z) == Ordinates.Z)
                        {
                            double[] writeZ = geomsWrite.GetOrdinates(Ordinate.Z);
                            double[] readZ  = geomsRead.GetOrdinates(Ordinate.Z);
                            Assert.IsTrue(ArraysEqual(writeZ, readZ));
                        }

                        if ((ordinates & Ordinates.M) == Ordinates.M)
                        {
                            double[] writeM = geomsWrite.GetOrdinates(Ordinate.M);
                            double[] readM  = geomsRead.GetOrdinates(Ordinate.M);
                            Assert.IsTrue(ArraysEqual(writeM, readM));
                        }
                    }
                }

                // delete sample files
                File.Delete(fileName);
                File.Delete(Path.ChangeExtension(fileName, "shx"));
                File.Delete(Path.ChangeExtension(fileName, "dbf"));
            }
            catch (AssertionException ex)
            {
                Console.WriteLine("Failed test with {0}", ordinates);
                Console.WriteLine(ex.Message);
                Console.WriteLine("  Testfile '{0}' not deleted!", fileName);
                throw;
            }
        }