/// <summary>
        /// Adds the given node.
        /// </summary>
        /// <param name="node"></param>
        public override void AddNode(Node node)
        {
            if (!_preIndexMode)
            {
                if (_nodesToCache != null &&
                    _nodesToCache.Contains(node.Id.Value))
                { // cache this node?
                    _dataCache.AddNode(node);
                }

                if (_preIndex != null && _preIndex.Contains(node.Id.Value))
                { // only save the coordinates for relevant nodes.
                    // save the node-coordinates.
                    // add the relevant nodes.
                    _coordinates[node.Id.Value] = new GeoCoordinateSimple()
                    {
                        Latitude  = (float)node.Latitude.Value,
                        Longitude = (float)node.Longitude.Value
                    };

                    // add the node as a possible restriction.
                    if (_interpreter.IsRestriction(OsmGeoType.Node, node.Tags))
                    { // tests quickly if a given node is possibly a restriction.
                        var vehicleTypes = _interpreter.CalculateRestrictions(node);
                        if (vehicleTypes != null &&
                            vehicleTypes.Count > 0)
                        { // add all the restrictions.
                            this._relevantNodes.Add(node.Id.Value);

                            var vertexId    = this.AddRoadNode(node.Id.Value).Value; // will always exists, has just been added to coordinates.
                            var restriction = new uint[] { vertexId };
                            if (vehicleTypes.Contains(null))
                            { // restriction is valid for all vehicles.
                                _graph.AddRestriction(restriction);
                            }
                            else
                            { // restriction is restricted to some vehicles only.
                                foreach (string vehicle in vehicleTypes)
                                {
                                    _graph.AddRestriction(vehicle, restriction);
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Adds a given way.
        /// </summary>
        /// <param name="way"></param>
        public override void AddWay(Way way)
        {
            if (!_preIndexMode && _waysToCache != null &&
                _waysToCache.Contains(way.Id.Value))
            { // cache this way?
                _dataCache.AddWay(way);
            }

            // initialize the way interpreter.
            if (_interpreter.EdgeInterpreter.IsRoutable(way.Tags))
            {         // the way is a road.
                if (_preIndexMode)
                {     // index relevant and used nodes.
                    if (way.Nodes != null)
                    { // this way has nodes.
                        // add new routable tags type.
                        var routableWayTags = new TagsCollection(way.Tags);
                        routableWayTags.RemoveAll(x =>
                        {
                            return(!_interpreter.IsRelevantRouting(x.Key) &&
                                   !Vehicle.IsRelevantForOneOrMore(x.Key));
                        });
                        if (_storeTags)
                        {
                            _tagsIndex.Add(routableWayTags);
                        }

                        int wayNodesCount = way.Nodes.Count;
                        for (int idx = 0; idx < wayNodesCount; idx++)
                        {
                            var node = way.Nodes[idx];
                            if (_preIndex.Contains(node))
                            { // node is relevant.
                                _relevantNodes.Add(node);
                            }
                            else
                            { // node is used.
                                _preIndex.Add(node);
                            }
                        }

                        if (wayNodesCount > 0)
                        { // first node is always relevant.
                            _relevantNodes.Add(way.Nodes[0]);
                            if (wayNodesCount > 1)
                            { // last node is always relevant.
                                _relevantNodes.Add(way.Nodes[wayNodesCount - 1]);
                            }
                        }
                    }
                }
                else
                {     // add actual edges.
                    if (way.Nodes != null && way.Nodes.Count > 1)
                    { // way has at least two nodes.
                        if (this.CalculateIsTraversable(_interpreter.EdgeInterpreter, _tagsIndex,
                                                        way.Tags))
                        { // the edge is traversable, add the edges.
                            uint?       from          = this.AddRoadNode(way.Nodes[0]);
                            long        fromNodeId    = way.Nodes[0];
                            List <long> intermediates = new List <long>();
                            for (int idx = 1; idx < way.Nodes.Count; idx++)
                            { // the to-node.
                                long currentNodeId = way.Nodes[idx];
                                if (!_collectIntermediates ||
                                    _relevantNodes.Contains(currentNodeId) ||
                                    idx == way.Nodes.Count - 1)
                                { // node is an important node.
                                    uint?to       = this.AddRoadNode(currentNodeId);
                                    long toNodeId = currentNodeId;

                                    // add the edge(s).
                                    if (from.HasValue && to.HasValue)
                                    { // add a road edge.
                                        while (from.Value == to.Value)
                                        {
                                            if (intermediates.Count > 0)
                                            {
                                                uint?dummy = this.AddRoadNode(intermediates[0]);
                                                intermediates.RemoveAt(0);
                                                if (dummy.HasValue && from.Value != dummy.Value)
                                                {
                                                    this.AddRoadEdge(way.Tags, from.Value, dummy.Value, null);
                                                    from = dummy;
                                                }
                                            }
                                            else
                                            { // no use to continue.
                                                break;
                                            }
                                        }
                                        // build coordinates.
                                        var intermediateCoordinates = new List <GeoCoordinateSimple>(intermediates.Count);
                                        for (int coordIdx = 0; coordIdx < intermediates.Count; coordIdx++)
                                        {
                                            ICoordinate coordinate;
                                            if (!_coordinates.TryGet(intermediates[coordIdx], out coordinate))
                                            {
                                                break;
                                            }
                                            intermediateCoordinates.Add(new GeoCoordinateSimple()
                                            {
                                                Latitude  = coordinate.Latitude,
                                                Longitude = coordinate.Longitude
                                            });
                                        }

                                        if (intermediateCoordinates.Count == intermediates.Count &&
                                            from.Value != to.Value)
                                        { // all coordinates have been found.
                                            this.AddRoadEdge(way.Tags, from.Value, to.Value, intermediateCoordinates);
                                        }
                                    }

                                    // if this way has a restriction save the collapsed nodes information.
                                    if (_restricedWays.Contains(way.Id.Value) && to.HasValue && from.HasValue)
                                    { // loop over all intermediates and save.
                                        var collapsedInfo = new KeyValuePair <KeyValuePair <long, uint>, KeyValuePair <long, uint> >(
                                            new KeyValuePair <long, uint>(fromNodeId, from.Value), new KeyValuePair <long, uint>(toNodeId, to.Value));
                                        foreach (var intermedidate in intermediates)
                                        {
                                            _collapsedNodes[intermedidate] = collapsedInfo;
                                        }
                                    }

                                    from = to; // the to node becomes the from.
                                    intermediates.Clear();
                                }
                                else
                                { // this node is just an intermediate.
                                    intermediates.Add(currentNodeId);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
 public override void AddNode(Node node)
 {
     if (this._firstPass)
     {
         this._nodeCount = this._nodeCount + 1L;
         double num1 = node.Latitude.Value;
         if (num1 < this._minLatitude)
         {
             this._minLatitude = num1;
         }
         if (num1 > this._maxLatitude)
         {
             this._maxLatitude = num1;
         }
         double num2 = node.Longitude.Value;
         if (num2 < this._minLongitude)
         {
             this._minLongitude = num2;
         }
         if (num2 > this._maxLongitude)
         {
             this._maxLongitude = num2;
         }
         if (this.Processors == null)
         {
             return;
         }
         foreach (ITwoPassProcessor processor in this.Processors)
         {
             processor.FirstPass(node);
         }
     }
     else
     {
         if (this.Processors != null)
         {
             foreach (ITwoPassProcessor processor in this.Processors)
             {
                 processor.SecondPass(node);
             }
         }
         long?id1;
         if (!this._stages[this._stage].Contains(node.Longitude.Value, node.Latitude.Value))
         {
             ILongIndex anyStageNodes = this._anyStageNodes;
             id1 = ((OsmGeo)node).Id;
             long number = id1.Value;
             if (!anyStageNodes.Contains(number))
             {
                 return;
             }
         }
         ILongIndex allRoutingNodes = this._allRoutingNodes;
         id1 = ((OsmGeo)node).Id;
         long number1 = id1.Value;
         if (!allRoutingNodes.Contains(number1))
         {
             return;
         }
         NodeCoordinatesDictionary stageCoordinates = this._stageCoordinates;
         id1 = ((OsmGeo)node).Id;
         long id2 = id1.Value;
         // ISSUE: variable of a boxed type
         GeoCoordinateSimple local = new GeoCoordinateSimple()
         {
             Latitude  = (float)node.Latitude.Value,
             Longitude = (float)node.Longitude.Value
         };
         stageCoordinates.Add(id2, (ICoordinate)local);
     }
 }
        public float Calculate(ILongIndex contractedFlags, uint vertex)
        {
            int         num1     = 0;
            int         num2     = 0;
            List <Edge> edgeList = new List <Edge>((IEnumerable <Edge>) this._graph.Graph.GetEdgeEnumerator(vertex));
            int         index1   = 0;

            while (index1 < edgeList.Count)
            {
                DirectedMetaGraph.EdgeEnumerator edgeEnumerator = this._graph.GetEdgeEnumerator(edgeList[index1].Neighbour);
                edgeEnumerator.Reset();
                while (edgeEnumerator.MoveNext())
                {
                    if ((int)edgeEnumerator.Neighbour == (int)vertex)
                    {
                        ++num1;
                    }
                }
                if (contractedFlags.Contains((long)edgeList[index1].Neighbour))
                {
                    edgeEnumerator.MoveTo(vertex);
                    edgeEnumerator.Reset();
                    while (edgeEnumerator.MoveNext())
                    {
                        if ((int)edgeEnumerator.Neighbour == (int)edgeList[index1].Neighbour)
                        {
                            ++num1;
                        }
                    }
                    edgeList.RemoveAt(index1);
                }
                else
                {
                    ++index1;
                }
            }
            for (int capacity = 1; capacity < edgeList.Count; ++capacity)
            {
                Edge  edge1 = edgeList[capacity];
                float weight1;
                bool? direction1;
                ContractedEdgeDataSerializer.Deserialize(edge1.Data[0], out weight1, out direction1);
                bool         flag1           = !direction1.HasValue || direction1.Value;
                bool         flag2           = !direction1.HasValue || !direction1.Value;
                bool[]       forwardWitness  = new bool[capacity];
                bool[]       backwardWitness = new bool[capacity];
                List <uint>  targets         = new List <uint>(capacity);
                List <float> weights         = new List <float>(capacity);
                for (int index2 = 0; index2 < capacity; ++index2)
                {
                    Edge  edge2 = edgeList[index2];
                    float weight2;
                    bool? direction2;
                    ContractedEdgeDataSerializer.Deserialize(edge2.Data[0], out weight2, out direction2);
                    bool flag3 = !direction2.HasValue || direction2.Value;
                    bool flag4 = !direction2.HasValue || !direction2.Value;
                    forwardWitness[index2]  = !(flag2 & flag3);
                    backwardWitness[index2] = !(flag1 & flag4);
                    targets.Add(edge2.Neighbour);
                    weights.Add(weight1 + weight2);
                }
                this._witnessCalculator.Calculate(this._graph.Graph, edge1.Neighbour, targets, weights, ref forwardWitness, ref backwardWitness, vertex);
                for (int index2 = 0; index2 < capacity; ++index2)
                {
                    Edge edge2   = edgeList[index2];
                    int  removed = 0;
                    int  added   = 0;
                    if (!forwardWitness[index2] && !backwardWitness[index2])
                    {
                        this._graph.TryAddOrUpdateEdge(edge1.Neighbour, edge2.Neighbour, weights[index2], new bool?(), vertex, out added, out removed);
                        int num3 = num2 + added;
                        int num4 = num1 + removed;
                        this._graph.TryAddOrUpdateEdge(edge2.Neighbour, edge1.Neighbour, weights[index2], new bool?(), vertex, out added, out removed);
                        num2 = num3 + added;
                        num1 = num4 + removed;
                    }
                    else if (!forwardWitness[index2])
                    {
                        this._graph.TryAddOrUpdateEdge(edge1.Neighbour, edge2.Neighbour, weights[index2], new bool?(true), vertex, out added, out removed);
                        int num3 = num2 + added;
                        int num4 = num1 + removed;
                        this._graph.TryAddOrUpdateEdge(edge2.Neighbour, edge1.Neighbour, weights[index2], new bool?(false), vertex, out added, out removed);
                        num2 = num3 + added;
                        num1 = num4 + removed;
                    }
                    else if (!backwardWitness[index2])
                    {
                        this._graph.TryAddOrUpdateEdge(edge1.Neighbour, edge2.Neighbour, weights[index2], new bool?(false), vertex, out added, out removed);
                        int num3 = num2 + added;
                        int num4 = num1 + removed;
                        this._graph.TryAddOrUpdateEdge(edge2.Neighbour, edge1.Neighbour, weights[index2], new bool?(true), vertex, out added, out removed);
                        num2 = num3 + added;
                        num1 = num4 + removed;
                    }
                }
            }
            int num5 = 0;

            this._contractionCount.TryGetValue(vertex, out num5);
            int num6 = 0;

            this._depth.TryGetValue((long)vertex, out num6);
            return((float)(this.DifferenceFactor * (num2 - num1) + this.DepthFactor * num6 + this.ContractedFactor * num5));
        }