private readonly Action <string, List <uint> > _foundRestriction; // restriction found action. /// <summary> /// Creates a new restriction processor. /// </summary> public RestrictionProcessor(IEnumerable <string> vehicleTypes, Func <long, uint> getVertex, Action <string, List <uint> > foundRestriction) { _vehicleTypes = new HashSet <string>(vehicleTypes); _getVertex = getVertex; _foundRestriction = foundRestriction; _restrictedWayIds = new SparseLongIndex(); _restrictedWays = new Dictionary <long, Way>(); _invertedRestrictions = new List <Relation>(); _positiveRestrictions = new Dictionary <long, List <Relation> >(); }
/// <summary> /// Creates a new router db stream target. /// </summary> public RouterDbStreamTarget(RouterDb db, Vehicle[] vehicles, ITagNormalizer tagNormalizer, bool allCore = false, int minimumStages = 1, bool normalizeTags = true, IEnumerable <ITwoPassProcessor> processors = null, bool processRestrictions = false) { _db = db; _vehicles = vehicles; _vehicleTypes = new HashSet <string>(); foreach (var vehicle in _vehicles) { foreach (var vehicleType in vehicle.VehicleTypes) { _vehicleTypes.Add(vehicleType); } } _allNodesAreCore = allCore; _normalizeTags = normalizeTags; _tagNormalizer = tagNormalizer; _createNodeCoordinatesDictionary = () => { return(new NodeCoordinatesDictionary()); }; _stageCoordinates = _createNodeCoordinatesDictionary(); _allRoutingNodes = new SparseLongIndex(); _anyStageNodes = new SparseLongIndex(); _coreNodes = new SparseLongIndex(); _coreNodeIdMap = new CoreNodeIdMap(); _processedWays = new SparseLongIndex(); _minimumStages = minimumStages; foreach (var vehicle in vehicles) { foreach (var profiles in vehicle.GetProfiles()) { db.AddSupportedProfile(profiles); } } if (processors == null) { processors = new List <ITwoPassProcessor>(); } this.Processors = new List <ITwoPassProcessor>(processors); this.InitializeDefaultProcessors(processRestrictions); }
/// <summary> /// Runs the island detection. /// </summary> protected override void DoRun() { _enumerator = _routerDb.Network.GeometricGraph.Graph.GetEdgeEnumerator(); _vertexFlags = new SparseLongIndex(); var vertexCount = _routerDb.Network.GeometricGraph.Graph.VertexCount; // precalculate all edge types for the given profiles. _canTraverse = new HashSet <ushort>(); for (ushort p = 0; p < _routerDb.EdgeProfiles.Count; p++) { if (this.CanTraverse(p)) { _canTraverse.Add(p); } } var island = (ushort)1; uint lower = 0; while (true) { // find the first vertex without an island assignment. var vertex = uint.MaxValue; for (uint v = lower; v < vertexCount; v++) { if (_islands[v] == 0) { lower = v; vertex = v; break; } } if (vertex == uint.MaxValue) { // no more islands left. break; } // expand island until no longer possible. var current = vertex; _islands[vertex] = island; _vertexFlags.Add(vertex); vertex = this.Expand(vertex, island); if (vertex == uint.MaxValue) { // expanding failed, still just the source vertex, this is an island of one. _islands[current] = SINGLETON_ISLAND; } else { while (vertex != uint.MaxValue) { _islands[vertex] = island; _vertexFlags.Add(vertex); vertex = this.Expand(vertex, island); if (vertex < current) { current = vertex; } if (vertex == uint.MaxValue) { while (current < vertexCount) { if (_islands[current] == island && !_vertexFlags.Contains(current)) { // part of island but has not been used to expand yet. vertex = current; break; } current++; } } } // island was no singleton, move to next island. island++; } } }
/// <summary> /// Runs the island detection. /// </summary> protected override void DoRun(CancellationToken cancellationToken) { _onStack = new SparseLongIndex(); var vertexCount = _routerDb.Network.GeometricGraph.Graph.VertexCount; // initialize all islands to NO_ISLAND. for (uint i = 0; i < _islands.Length; i++) { _islands[i] = NO_ISLAND; if (_restrictionCollection != null) { _restrictionCollection.Update(i); for (var r = 0; r < _restrictionCollection.Count; r++) { var restriction = _restrictionCollection[r]; if (restriction.Vertex2 == Constants.NO_VERTEX && restriction.Vertex3 == Constants.NO_VERTEX) { _islands[i] = RESTRICTED; break; } else { // TODO: support other restrictions. } } } } // build index data structure and stack. _index = Context.ArrayFactory.CreateMemoryBackedArray <uint>(vertexCount * 2); for (var i = 0; i < _index.Length; i++) { _index[i] = NO_DATA; } _stack = new Collections.Stack <uint>(); // https://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm for (uint v = 0; v < vertexCount; v++) { var vIndex = _index[v * 2]; if (vIndex != NO_DATA) { continue; } StrongConnect(v); } // sort islands. var sortedIslands = new List <KeyValuePair <ushort, uint> >(_islandSizes); sortedIslands.Sort((x, y) => - x.Value.CompareTo(y.Value)); var newIds = new Dictionary <ushort, ushort>(); for (ushort i = 0; i < sortedIslands.Count; i++) { newIds[sortedIslands[i].Key] = i; } for (var v = 0; v < _islands.Length; v++) { ushort newId; if (newIds.TryGetValue(_islands[v], out newId)) { _islands[v] = newId; } } _islandSizes.Clear(); foreach (var sortedIsland in sortedIslands) { ushort newId; if (newIds.TryGetValue(sortedIsland.Key, out newId)) { _islandSizes[newId] = sortedIsland.Value; } } }