示例#1
0
        public void deserialize_a_valid_json_should_return_a_geometrycollection()
        {
            JsonTextReader      reader     = new JsonTextReader(new StringReader(serializedCollection));
            JsonSerializer      serializer = new GeoJsonSerializer(factory);
            IGeometryCollection actual     = serializer.Deserialize <GeometryCollection>(reader);

            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.EqualsExact(collection), Is.True);
        }
示例#2
0
        private void TestShapeReadWrite(string shapepath, string outputpath)
        {
            IGeometryCollection collection     = null;
            IGeometryCollection testcollection = null;

            collection = ReadShape(shapepath);
            WriteShape(collection, outputpath);
            testcollection = ReadShape(outputpath);

            if (!collection.EqualsExact(testcollection))
            {
                throw new ArgumentException("Geometries are not equals");
            }
            Console.WriteLine("TEST OK!");
        }
示例#3
0
        private void TestBugMultipolygonHShuntao()
        {
            IGeometryCollection gc1 = null;
            IGeometryCollection gc2 = null;
            string file             = "BJmultipolygon.shp";

            if (!File.Exists(file))
            {
                throw new FileNotFoundException();
            }

            // Test with Default ShapefileReader
            try
            {
                ShapefileReader sfr = new ShapefileReader(file);
                gc1 = sfr.ReadAll();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // Test with MyShapefileReader (only for debug purpose!)
            try
            {
                MyShapeFileReader reader = new MyShapeFileReader();
                gc2 = reader.Read(file);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // Check for equality
            if (!gc1.EqualsExact(gc2))
            {
                throw new TopologyException("Both geometries must be equals!");
            }
        }
示例#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;
            }
        }
        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
                    var equal = true;
                    for (var 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))
                        {
                            var hsm = new HausdorffSimilarityMeasure().Measure(gw, gr);
                            var asm = new AreaSimilarityMeasure().Measure(gw, gr);
                            var 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)
                        {
                            var writeZ = geomsWrite.GetOrdinates(Ordinate.Z);
                            var readZ = geomsRead.GetOrdinates(Ordinate.Z);
                            Assert.IsTrue(ArraysEqual(writeZ, readZ));
                        }

                        if ((ordinates & Ordinates.M) == Ordinates.M)
                        {
                            var writeM = geomsWrite.GetOrdinates(Ordinate.M);
                            var 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;
            }


        }
示例#6
0
        public override void Start()
        {
            _xmlreader = _writer.Write(_point);
            _document  = new XmlDocument();
            _document.Load(_xmlreader);
            Write(_document.InnerXml);
            _result = _reader.Read(_document);
            Debug.Assert(_point.Equals(_result), "ERROR!");

            //string gml = document.InnerXml;
            //gml = gml.Replace("gml:", "");
            //result = reader.Read(gml);

            _xmlreader = _writer.Write(_line);
            _document  = new XmlDocument();
            _document.Load(_xmlreader);
            Write(_document.InnerXml);
            _result = _reader.Read(_document);
            Debug.Assert(_line.Equals(_result), "ERROR!");

            _xmlreader = _writer.Write(_polygon);
            _document  = new XmlDocument();
            _document.Load(_xmlreader);
            Write(_document.InnerXml);
            _result = _reader.Read(_document);
            Debug.Assert(_polygon.Equals(_result), "ERROR!");

            _xmlreader = _writer.Write(_multiPoint);
            _document  = new XmlDocument();
            _document.Load(_xmlreader);
            Write(_document);
            _result = _reader.Read(_document);
            Debug.Assert(_multiPoint.Equals(_result), "ERROR!");

            MultiLineString multiLineString = new WKTReader().Read("MULTILINESTRING ((10 10, 20 20), (30 30, 40 40, 50 50, 70 80, 990 210), (2000.1 22, 457891.2334 3456.2, 33333 44444))") as MultiLineString;

            _xmlreader = _writer.Write(multiLineString);
            _document  = new XmlDocument();
            _document.Load(_xmlreader);
            Write(_document.InnerXml);
            _result = _reader.Read(_document);
            Debug.Assert(multiLineString.Equals(_result), "ERROR!");

            MultiPolygon multiPolygon = new WKTReader().Read("MULTIPOLYGON (((10 10, 10 20, 20 20, 20 10, 10 10), (12 12, 18 12, 18 18, 12 18, 12 12), (14 14, 16 14, 16 16, 14 16, 14 14)), ((30 30, 30 40, 40 40, 40 30, 30 30), (32 32, 38 32, 38 38, 32 38, 32 32)))") as MultiPolygon;

            _xmlreader = _writer.Write(multiPolygon);
            _document  = new XmlDocument();
            _document.Load(_xmlreader);
            Write(_document.InnerXml);
            _result = _reader.Read(_document);
            Debug.Assert(multiPolygon.EqualsExact(_result), "ERROR!");

            IGeometry[]         geometries         = { _point, _line, _polygon, _multiPoint, multiLineString, multiPolygon };
            IGeometryCollection geometryCollection = Factory.CreateGeometryCollection(geometries);

            _xmlreader = _writer.Write(geometryCollection);
            _document  = new XmlDocument();
            _document.Load(_xmlreader);
            Write(_document.InnerXml);
            _result = _reader.Read(_document);
            Debug.Assert(geometryCollection.EqualsExact(_result), "ERROR!");
        }