コード例 #1
0
        public void BuildGraphFromCompleteGraphShapefile()
        {
            var shapepath = "graph.shp";
            var count     = 1179;

            Assert.IsTrue(File.Exists(shapepath), string.Format("File not found: '{0}'", shapepath));
            var reader = new ShapefileReader(shapepath);
            var edges  = reader.ReadAll();

            Assert.IsNotNull(edges);
            Assert.IsInstanceOf(typeof(GeometryCollection), edges);
            Assert.AreEqual(count, edges.NumGeometries);

            // Insert arbitrary userdata
            for (var i = 0; i < count; i++)
            {
                var g = edges.GetGeometryN(i) as IMultiLineString;
                Assert.IsNotNull(g);
                var ls = g.GetGeometryN(0) as ILineString;
                Assert.IsNotNull(ls);

                Assert.IsNull(ls.UserData);
                ls.UserData = i;
                Assert.IsNotNull(ls.UserData);
            }

            var startls = edges.GetGeometryN(515).GetGeometryN(0) as ILineString;

            Assert.IsNotNull(startls);
            var startPoint = startls.EndPoint;

            Assert.AreEqual(2317300d, startPoint.X);
            Assert.AreEqual(4843961d, startPoint.Y);

            var endls = edges.GetGeometryN(141).GetGeometryN(0) as ILineString;;

            Assert.IsNotNull(endls);
            var endPoint = endls.StartPoint;

            Assert.AreEqual(2322739d, endPoint.X);
            Assert.AreEqual(4844539d, endPoint.Y);

            var finder = new PathFinder(true);

            foreach (IMultiLineString mlstr in edges.Geometries)
            {
                Assert.AreEqual(1, mlstr.NumGeometries);
                var str = mlstr.GetGeometryN(0) as ILineString;
                Assert.IsNotNull(str);
                Assert.IsNotNull(str.UserData);
                Assert.IsTrue(finder.Add(str));
            }
            finder.Initialize();

            var expectedResultCount = 8;
            var path = finder.Find(startPoint, endPoint);

            Assert.IsNotNull(path);
            Assert.IsInstanceOf(typeof(IMultiLineString), path);
            var strings = (IMultiLineString)path;

            Assert.AreEqual(expectedResultCount, strings.NumGeometries);
            foreach (var g in strings.Geometries)
            {
                Assert.IsNotNull(g.UserData);
                Console.WriteLine("{0} : {1}", g.UserData, g);
            }

            var reversedPath = finder.Find(endPoint, startPoint);

            Assert.IsNotNull(reversedPath);
            Assert.IsInstanceOf(typeof(IMultiLineString), reversedPath);

            var reversedStrings = (IMultiLineString)reversedPath;

            Assert.AreEqual(expectedResultCount, reversedStrings.NumGeometries);
            foreach (var g in reversedStrings.Geometries)
            {
                Assert.IsNotNull(g.UserData);
                Console.WriteLine("{0} : {1}", g.UserData, g);
            }

            for (var i = 0; i < expectedResultCount; i++)
            {
                var item         = strings.GetGeometryN(i);
                var itemReversed = strings.GetGeometryN(expectedResultCount - 1 - i);
                Assert.AreNotEqual(item.UserData, itemReversed.UserData);
                Assert.AreNotEqual(item, itemReversed);
            }
        }