Beispiel #1
0
        /// <summary>
        /// Deserializes from the given stream.
        /// </summary>
        public static MultimodalDb Deserialize(Stream stream)
        {
            if (stream.ReadByte() != 1)
            {
                throw new Exception("Cannot deserialize db, version # doesn't match.");
            }

            // deserialize router db.
            var routerDb = RouterDb.Deserialize(stream);

            // read transit db.
            var transitDb = TransitDb.Deserialize(stream);

            // create db.
            var db = new MultimodalDb(routerDb, transitDb);

            // read stop links db count.
            var stopLinksDbCount = stream.ReadByte();

            for (var i = 0; i < stopLinksDbCount; i++)
            {
                db.AddStopLinksDb(StopLinksDb.Deserialize(stream));
            }
            return(db);
        }
Beispiel #2
0
        /// <summary>
        /// Adds a new stop links db for the given profile.
        /// </summary>
        public static void AddStopLinksDb(this MultimodalDb db, Profile profile, float searchOffset = DefaultSearchOffset,
                                          float maxDistance = DefaultMaxDistance, int maxRouterPoints = DefaultMaxRouterPoints)
        {
            var stopsDbEnumerator = db.TransitDb.GetStopsEnumerator();
            var linksDb           = new StopLinksDb(stopsDbEnumerator.Count, db.RouterDb, profile);

            while (stopsDbEnumerator.MoveNext())
            {
                var stopId        = stopsDbEnumerator.Id;
                var multiResolver = new ResolveMultipleAlgorithm(db.RouterDb.Network.GeometricGraph,
                                                                 stopsDbEnumerator.Latitude, stopsDbEnumerator.Longitude, searchOffset, maxDistance, (edge) =>
                {
                    // get profile.
                    float distance;
                    ushort edgeProfileId;
                    Itinero.Data.Edges.EdgeDataSerializer.Deserialize(edge.Data[0],
                                                                      out distance, out edgeProfileId);
                    var edgeProfile = db.RouterDb.EdgeProfiles.Get(edgeProfileId);
                    // get factor from profile.
                    if (profile.Factor(edgeProfile).Value <= 0)
                    {     // cannot be traversed by this profile.
                        return(false);
                    }
                    // verify stoppable.
                    if (!profile.CanStopOn(edgeProfile))
                    {     // this profile cannot stop on this edge.
                        return(false);
                    }
                    return(true);
                });
                multiResolver.Run();
                if (multiResolver.HasSucceeded)
                {
                    // get the n-closest.
                    var closest = multiResolver.Results.GetLowestN(maxRouterPoints,
                                                                   (p) => Coordinate.DistanceEstimateInMeter(stopsDbEnumerator.Latitude, stopsDbEnumerator.Longitude,
                                                                                                             p.Latitude, p.Longitude));

                    // add them as new links.
                    for (var i = 0; i < closest.Count; i++)
                    {
                        linksDb.Add((uint)stopId, closest[i]);
                    }
                }
            }

            db.AddStopLinksDb(linksDb);
        }