public virtual RoadNetwork CreateNetwork(FeatureSource featureSource) { featureSource.Open(); QTreeSpatialIndex qtree = new QTreeSpatialIndex(featureSource.GetBoundingBox()); #if DEBUG long featureCount = featureSource.GetCount(); #endif RoadNetwork roadNetwork = new RoadNetwork(); Collection<Feature> features = featureSource.GetAllFeatures(ReturningColumnsType.NoColumns); featureSource.Close(); Collection<Collection<Feature>> featureGroups = GroupFeatures(features); int done = 0; var tasks = (from items in featureGroups select Task.Factory.StartNew(() => { var clonedFeatureSource = featureSource.CloneDeep(); clonedFeatureSource.Open(); foreach (var feature in items) { Collection<LineShape> processingLineShapes = GeometryHelper.GetLineShapes(feature); // Get the lineshape of the processing feature. foreach (LineShape processingLineShape in processingLineShapes) { BuildNetworkNode(clonedFeatureSource, qtree, roadNetwork, processingLineShape.Vertices[0]); BuildNetworkNode(clonedFeatureSource, qtree, roadNetwork, processingLineShape.Vertices[processingLineShape.Vertices.Count - 1]); } done++; Console.WriteLine(string.Format("Done {0} in {1}", feature.Id, done)); } })).ToArray(); //foreach (Feature feature in features) //{ // Task.Factory.StartNew(() => // { // Collection<LineShape> processingLineShapes = GeometryHelper.GetLineShapes(feature); // // Get the lineshape of the processing feature. // foreach (LineShape processingLineShape in processingLineShapes) // { // BuildNetworkNode(featureSource, qtree, roadNetwork, processingLineShape.Vertices[0]); // BuildNetworkNode(featureSource, qtree, roadNetwork, processingLineShape.Vertices[processingLineShape.Vertices.Count - 1]); // } // } // ); Task.WaitAll(tasks); #if DEBUG #endif //} //featureSource.Close(); return roadNetwork; }
public virtual RoadNetwork CreateNetwork(FeatureSource featureSource) { featureSource.Open(); QTreeSpatialIndex qtree = new QTreeSpatialIndex(featureSource.GetBoundingBox()); #if DEBUG long featureCount = featureSource.GetCount(); #endif RoadNetwork roadNetwork = new RoadNetwork(); Collection <Feature> features = featureSource.GetAllFeatures(ReturningColumnsType.NoColumns); featureSource.Close(); Collection <Collection <Feature> > featureGroups = GroupFeatures(features); int done = 0; var tasks = (from items in featureGroups select Task.Factory.StartNew(() => { var clonedFeatureSource = featureSource.CloneDeep(); clonedFeatureSource.Open(); foreach (var feature in items) { Collection <LineShape> processingLineShapes = GeometryHelper.GetLineShapes(feature); // Get the lineshape of the processing feature. foreach (LineShape processingLineShape in processingLineShapes) { BuildNetworkNode(clonedFeatureSource, qtree, roadNetwork, processingLineShape.Vertices[0]); BuildNetworkNode(clonedFeatureSource, qtree, roadNetwork, processingLineShape.Vertices[processingLineShape.Vertices.Count - 1]); } done++; Console.WriteLine(string.Format("Done {0} in {1}", feature.Id, done)); } })).ToArray(); //foreach (Feature feature in features) //{ // Task.Factory.StartNew(() => // { // Collection<LineShape> processingLineShapes = GeometryHelper.GetLineShapes(feature); // // Get the lineshape of the processing feature. // foreach (LineShape processingLineShape in processingLineShapes) // { // BuildNetworkNode(featureSource, qtree, roadNetwork, processingLineShape.Vertices[0]); // BuildNetworkNode(featureSource, qtree, roadNetwork, processingLineShape.Vertices[processingLineShape.Vertices.Count - 1]); // } // } // ); Task.WaitAll(tasks); #if DEBUG #endif //} //featureSource.Close(); return(roadNetwork); }
private Node CreateNode(FeatureSource featureSource, RoadNetwork roadNetwork, Vertex vertex) { // Create node based on vertex. // Todo: check if it has been existed. Collection <Feature> adjacentTailNodeFeatures = GetAdjacentFeaturesOfVertex(featureSource, vertex); Node tailNode = InitializeNodeFromVeterx(vertex, adjacentTailNodeFeatures); roadNetwork.Nodes.Add(tailNode); // Loop and see if the queried shape is intersected with processing shape. foreach (Feature adjacentTailNodeFeature in adjacentTailNodeFeatures) { // Given the adjacent line is line shape, and create adjacent list without un-direction consideration. LineShape adjacentLineShape = GeometryHelper.GetLineShapes(adjacentTailNodeFeature)[0]; adjacentLineShape.Id = adjacentTailNodeFeature.Id; // Check if it's start vertext or end vertex of the adjacent line shape. int vertexIndex = 0; if (GeometryHelper.IsSamePoint(vertex, adjacentLineShape.Vertices[adjacentLineShape.Vertices.Count - 1], tolerance)) { vertexIndex = adjacentLineShape.Vertices.Count - 1; } // Todo: check if it has been existed. Collection <Feature> adjacentHeadNodeFeatures = GetAdjacentFeaturesOfVertex(featureSource, adjacentLineShape.Vertices[vertexIndex]); Node headNode = InitializeNodeFromVeterx(adjacentLineShape.Vertices[vertexIndex], adjacentHeadNodeFeatures); roadNetwork.Nodes.Add(headNode); Arc adjacentArc = new Arc(adjacentTailNodeFeature.Id, headNode, tailNode, CalculateRoadCost(adjacentLineShape)); // Check if the direction is the allowed of current segment? RoadDirection roadDirection = CalculateRoadDirection(adjacentTailNodeFeature, adjacentLineShape, vertex); if (roadDirection == RoadDirection.Forward) { tailNode.OutgoingArcs.Add(adjacentArc); } else if (roadDirection == RoadDirection.Backward) { tailNode.IncomingArcs.Add(adjacentArc); } } return(tailNode); }
static void Main(string[] args) { RoadNetwork rn = new RoadNetwork(); Node n1 = new Node("1", 0, 0); Node n2 = new Node("2", 0, 0); Node n3 = new Node("3", 0, 0); Node n4 = new Node("4", 0, 0); Node n5 = new Node("5", 0, 0); Node n6 = new Node("6", 1, 1); Node n7 = new Node("7", 0, 0); Node n8 = new Node("8", 0, 0); Node n9 = new Node("9", 0, 0); Arc arc12 = new Arc(n1, n2, 2); Arc arc16 = new Arc(n1, n6, 1); Arc arc21 = new Arc(n2, n1, 2); Arc arc23 = new Arc(n2, n3, 2); Arc arc32 = new Arc(n3, n2, 2); Arc arc34 = new Arc(n3, n4, 2); Arc arc43 = new Arc(n4, n3, 2); Arc arc45 = new Arc(n4, n5, 2); Arc arc54 = new Arc(n5, n4, 2); Arc arc57 = new Arc(n5, n7, 10); Arc arc58 = new Arc(n5, n8, 10); Arc arc59 = new Arc(n5, n9, 10); Arc arc61 = new Arc(n6, n1, 1); Arc arc67 = new Arc(n6, n7, 1); Arc arc68 = new Arc(n6, n8, 1); Arc arc69 = new Arc(n6, n9, 1); Arc arc76 = new Arc(n7, n6, 1); Arc arc75 = new Arc(n7, n5, 10); Arc arc86 = new Arc(n8, n6, 1); Arc arc85 = new Arc(n8, n5, 10); Arc arc96 = new Arc(n9, n6, 1); Arc arc95 = new Arc(n9, n5, 10); rn.Nodes.Add(n1); rn.Nodes.Add(n2); rn.Nodes.Add(n3); rn.Nodes.Add(n4); rn.Nodes.Add(n5); rn.Nodes.Add(n6); rn.Nodes.Add(n7); rn.Nodes.Add(n8); rn.Nodes.Add(n9); rn.MapNodes.Add("1", n1); rn.MapNodes.Add("2", n2); rn.MapNodes.Add("3", n3); rn.MapNodes.Add("4", n4); rn.MapNodes.Add("5", n5); rn.MapNodes.Add("6", n6); rn.MapNodes.Add("7", n7); rn.MapNodes.Add("8", n8); rn.MapNodes.Add("9", n9); Collection<Arc> aa = new Collection<Arc>(); aa.Add(arc12); aa.Add(arc16); rn.AdjacentArcs.Add(n1.Id, aa); Collection<Arc> bb = new Collection<Arc>(); bb.Add(arc21); bb.Add(arc23); rn.AdjacentArcs.Add(n2.Id, bb); Collection<Arc> cc = new Collection<Arc>(); cc.Add(arc32); cc.Add(arc34); rn.AdjacentArcs.Add(n3.Id, cc); Collection<Arc> dd = new Collection<Arc>(); dd.Add(arc43); dd.Add(arc45); rn.AdjacentArcs.Add(n4.Id, dd); Collection<Arc> ee = new Collection<Arc>(); ee.Add(arc57); ee.Add(arc54); ee.Add(arc58); ee.Add(arc59); rn.AdjacentArcs.Add(n5.Id, ee); Collection<Arc> ff = new Collection<Arc>(); ff.Add(arc67); ff.Add(arc68); ff.Add(arc61); ff.Add(arc69); rn.AdjacentArcs.Add(n6.Id, ff); Collection<Arc> gg = new Collection<Arc>(); gg.Add(arc75); gg.Add(arc76); rn.AdjacentArcs.Add(n7.Id, gg); Collection<Arc> hh = new Collection<Arc>(); hh.Add(arc85); hh.Add(arc86); rn.AdjacentArcs.Add(n8.Id, hh); Collection<Arc> tt = new Collection<Arc>(); tt.Add(arc95); tt.Add(arc96); rn.AdjacentArcs.Add(n9.Id, tt); DijkstraAlgorithm dij = new DijkstraAlgorithm(rn); double cost = dij.GetShortPath("1", "5"); Console.WriteLine("cost is:" + cost); dij.ShortPathToString("1", "5"); Console.ReadLine(); }
public virtual void CreateIndex(RoadNetwork roadNetwork) { }
private void BuildNetworkNode(FeatureSource featureSource, QTreeSpatialIndex qtree, RoadNetwork roadNetwork, Vertex vertex) { RectangleShape startSmallBounds = GeometryHelper.CreateSmallRectangle(vertex, tolerance); Collection <string> idsInside = qtree.GetFeatureIdsIntersectingBoundingBox(startSmallBounds); if (idsInside.Count <= 0) { Node startNode = CreateNode(featureSource, roadNetwork, vertex); roadNetwork.Nodes.Add(startNode); qtree.Add(new PointShape(vertex)); } }
private Node CreateNode(FeatureSource featureSource, RoadNetwork roadNetwork, Vertex vertex) { // Create node based on vertex. // Todo: check if it has been existed. Collection<Feature> adjacentTailNodeFeatures = GetAdjacentFeaturesOfVertex(featureSource, vertex); Node tailNode = InitializeNodeFromVeterx(vertex, adjacentTailNodeFeatures); roadNetwork.Nodes.Add(tailNode); // Loop and see if the queried shape is intersected with processing shape. foreach (Feature adjacentTailNodeFeature in adjacentTailNodeFeatures) { // Given the adjacent line is line shape, and create adjacent list without un-direction consideration. LineShape adjacentLineShape = GeometryHelper.GetLineShapes(adjacentTailNodeFeature)[0]; adjacentLineShape.Id = adjacentTailNodeFeature.Id; // Check if it's start vertext or end vertex of the adjacent line shape. int vertexIndex = 0; if (GeometryHelper.IsSamePoint(vertex, adjacentLineShape.Vertices[adjacentLineShape.Vertices.Count - 1], tolerance)) { vertexIndex = adjacentLineShape.Vertices.Count - 1; } // Todo: check if it has been existed. Collection<Feature> adjacentHeadNodeFeatures = GetAdjacentFeaturesOfVertex(featureSource, adjacentLineShape.Vertices[vertexIndex]); Node headNode = InitializeNodeFromVeterx(adjacentLineShape.Vertices[vertexIndex], adjacentHeadNodeFeatures); roadNetwork.Nodes.Add(headNode); Arc adjacentArc = new Arc(adjacentTailNodeFeature.Id, headNode, tailNode, CalculateRoadCost(adjacentLineShape)); // Check if the direction is the allowed of current segment? RoadDirection roadDirection = CalculateRoadDirection(adjacentTailNodeFeature, adjacentLineShape, vertex); if (roadDirection == RoadDirection.Forward) { tailNode.OutgoingArcs.Add(adjacentArc); } else if (roadDirection == RoadDirection.Backward) { tailNode.IncomingArcs.Add(adjacentArc); } } return tailNode; }
public DijkstraAlgorithm(RoadNetwork graph) { this.graph = graph; }
private void BuildNetworkNode(FeatureSource featureSource, QTreeSpatialIndex qtree, RoadNetwork roadNetwork, Vertex vertex) { RectangleShape startSmallBounds = GeometryHelper.CreateSmallRectangle(vertex, tolerance); Collection<string> idsInside = qtree.GetFeatureIdsIntersectingBoundingBox(startSmallBounds); if (idsInside.Count <= 0) { Node startNode = CreateNode(featureSource, roadNetwork, vertex); roadNetwork.Nodes.Add(startNode); qtree.Add(new PointShape(vertex)); } }
static void Main(string[] args) { RoadNetwork rn = new RoadNetwork(); Node n1 = new Node("1", 0, 0); Node n2 = new Node("2", 0, 0); Node n3 = new Node("3", 0, 0); Node n4 = new Node("4", 0, 0); Node n5 = new Node("5", 0, 0); Node n6 = new Node("6", 1, 1); Node n7 = new Node("7", 0, 0); Node n8 = new Node("8", 0, 0); Node n9 = new Node("9", 0, 0); Arc arc12 = new Arc(n1, n2, 2); Arc arc16 = new Arc(n1, n6, 1); Arc arc21 = new Arc(n2, n1, 2); Arc arc23 = new Arc(n2, n3, 2); Arc arc32 = new Arc(n3, n2, 2); Arc arc34 = new Arc(n3, n4, 2); Arc arc43 = new Arc(n4, n3, 2); Arc arc45 = new Arc(n4, n5, 2); Arc arc54 = new Arc(n5, n4, 2); Arc arc57 = new Arc(n5, n7, 10); Arc arc58 = new Arc(n5, n8, 10); Arc arc59 = new Arc(n5, n9, 10); Arc arc61 = new Arc(n6, n1, 1); Arc arc67 = new Arc(n6, n7, 1); Arc arc68 = new Arc(n6, n8, 1); Arc arc69 = new Arc(n6, n9, 1); Arc arc76 = new Arc(n7, n6, 1); Arc arc75 = new Arc(n7, n5, 10); Arc arc86 = new Arc(n8, n6, 1); Arc arc85 = new Arc(n8, n5, 10); Arc arc96 = new Arc(n9, n6, 1); Arc arc95 = new Arc(n9, n5, 10); rn.Nodes.Add(n1); rn.Nodes.Add(n2); rn.Nodes.Add(n3); rn.Nodes.Add(n4); rn.Nodes.Add(n5); rn.Nodes.Add(n6); rn.Nodes.Add(n7); rn.Nodes.Add(n8); rn.Nodes.Add(n9); rn.MapNodes.Add("1", n1); rn.MapNodes.Add("2", n2); rn.MapNodes.Add("3", n3); rn.MapNodes.Add("4", n4); rn.MapNodes.Add("5", n5); rn.MapNodes.Add("6", n6); rn.MapNodes.Add("7", n7); rn.MapNodes.Add("8", n8); rn.MapNodes.Add("9", n9); Collection <Arc> aa = new Collection <Arc>(); aa.Add(arc12); aa.Add(arc16); rn.AdjacentArcs.Add(n1.Id, aa); Collection <Arc> bb = new Collection <Arc>(); bb.Add(arc21); bb.Add(arc23); rn.AdjacentArcs.Add(n2.Id, bb); Collection <Arc> cc = new Collection <Arc>(); cc.Add(arc32); cc.Add(arc34); rn.AdjacentArcs.Add(n3.Id, cc); Collection <Arc> dd = new Collection <Arc>(); dd.Add(arc43); dd.Add(arc45); rn.AdjacentArcs.Add(n4.Id, dd); Collection <Arc> ee = new Collection <Arc>(); ee.Add(arc57); ee.Add(arc54); ee.Add(arc58); ee.Add(arc59); rn.AdjacentArcs.Add(n5.Id, ee); Collection <Arc> ff = new Collection <Arc>(); ff.Add(arc67); ff.Add(arc68); ff.Add(arc61); ff.Add(arc69); rn.AdjacentArcs.Add(n6.Id, ff); Collection <Arc> gg = new Collection <Arc>(); gg.Add(arc75); gg.Add(arc76); rn.AdjacentArcs.Add(n7.Id, gg); Collection <Arc> hh = new Collection <Arc>(); hh.Add(arc85); hh.Add(arc86); rn.AdjacentArcs.Add(n8.Id, hh); Collection <Arc> tt = new Collection <Arc>(); tt.Add(arc95); tt.Add(arc96); rn.AdjacentArcs.Add(n9.Id, tt); DijkstraAlgorithm dij = new DijkstraAlgorithm(rn); double cost = dij.GetShortPath("1", "5"); Console.WriteLine("cost is:" + cost); dij.ShortPathToString("1", "5"); Console.ReadLine(); }
public void GetShortPathTest() { RoadNetwork rn = new RoadNetwork(); Node n1 = new Node("1", 0, 0); Node n2 = new Node("2", 0, 0); Node n3 = new Node("3", 0, 0); Node n4 = new Node("4", 0, 0); Node n5 = new Node("5", 0, 0); Node n6 = new Node("6", 0, 0); Arc arc12 = new Arc(n1, n2, 7); Arc arc13 = new Arc(n1, n3, 9); Arc arc16 = new Arc(n1, n6, 14); Arc arc23 = new Arc(n2, n3, 10); Arc arc24 = new Arc(n2, n4, 15); Arc arc34 = new Arc(n3, n4, 11); Arc arc36 = new Arc(n3, n6, 2); Arc arc45 = new Arc(n4, n5, 6); Arc arc65 = new Arc(n6, n5, 9); Arc arc21 = new Arc(n2, n1, 7); Arc arc31 = new Arc(n3, n1, 9); Arc arc61 = new Arc(n6, n1, 14); Arc arc32 = new Arc(n3, n2, 10); Arc arc42 = new Arc(n4, n2, 15); Arc arc43 = new Arc(n4, n3, 11); Arc arc63 = new Arc(n6, n3, 2); Arc arc54 = new Arc(n5, n4, 6); Arc arc56 = new Arc(n6, n5, 9); rn.Nodes.Add(n1); rn.Nodes.Add(n2); rn.Nodes.Add(n3); rn.Nodes.Add(n4); rn.Nodes.Add(n5); rn.Nodes.Add(n6); rn.Arcs.Add(arc12); rn.Arcs.Add(arc13); rn.Arcs.Add(arc16); rn.AdjacentArcs.Add(n1.Id, rn.Arcs); Collection<Arc> aa = new Collection<Arc>(); aa.Add(arc21); aa.Add(arc23); aa.Add(arc24); rn.AdjacentArcs.Add(n2.Id, aa); Collection<Arc> bb = new Collection<Arc>(); bb.Add(arc31); bb.Add(arc32); bb.Add(arc34); bb.Add(arc36); rn.AdjacentArcs.Add(n3.Id, bb); Collection<Arc> cc = new Collection<Arc>(); cc.Add(arc42); cc.Add(arc43); cc.Add(arc45); rn.AdjacentArcs.Add(n4.Id, cc); Collection<Arc> dd = new Collection<Arc>(); dd.Add(arc63); dd.Add(arc65); dd.Add(arc61); rn.AdjacentArcs.Add(n6.Id, dd); Collection<Arc> ee = new Collection<Arc>(); ee.Add(arc56); ee.Add(arc54); rn.AdjacentArcs.Add(n5.Id, ee); rn.MapNodes.Add("1", n1); rn.MapNodes.Add("2", n2); rn.MapNodes.Add("3", n3); rn.MapNodes.Add("4", n4); rn.MapNodes.Add("5", n5); rn.MapNodes.Add("6", n6); DijkstraAlgorithm dij = new DijkstraAlgorithm(rn); double cost = dij.GetShortPath("1", "5"); dij.ShortPathToString("1", "5"); double actual = 20; Assert.AreEqual(cost, actual); }
public LandmarkAlgorithm(RoadNetwork graph) { this.graph = graph; }