예제 #1
0
        /// <summary>
        /// Adds the given attributes.
        /// </summary>
        public static void AddFrom(this AttributesTable table, string name, Itinero.Attributes.AttributeCollection tags,
                                   string defaultValue)
        {
            var value = string.Empty;

            if (!tags.TryGetValue(name, out value))
            {
                value = defaultValue;
            }
            table.AddAttribute(name, value);
        }
예제 #2
0
        private IFeature BuildFeature(int index)
        {
            if (((index - 1) % 10000) == 0)
            {
                Itinero.Logging.Logger.Log("FeatureList", TraceEventType.Information,
                                           "Building feature {0}/{1}.", index - 1, this.Count);
            }

            var edge = _routerDb.Network.GetEdge((uint)index);

            var vertexLocation1 = _routerDb.Network.GeometricGraph.GetVertex(edge.From);
            var coordinates     = new List <Coordinate>();

            coordinates.Add(new Coordinate(vertexLocation1.Longitude, vertexLocation1.Latitude));
            var shape = edge.Shape;

            if (shape != null)
            {
                var shapeEnumerable = shape.GetEnumerator();
                shapeEnumerable.Reset();
                while (shapeEnumerable.MoveNext())
                {
                    coordinates.Add(new Coordinate(shapeEnumerable.Current.Longitude,
                                                   shapeEnumerable.Current.Latitude));
                }
            }
            var vertexLocation2 = _routerDb.Network.GeometricGraph.GetVertex(edge.To);

            coordinates.Add(new Coordinate(vertexLocation2.Longitude, vertexLocation2.Latitude));
            var geometry = new LineString(coordinates.ToArray());

            var length = 0.0f;

            for (var i = 0; i < coordinates.Count - 1; i++)
            {
                length += Itinero.LocalGeo.Coordinate.DistanceEstimateInMeter((float)coordinates[i + 0].Y, (float)coordinates[i + 0].X,
                                                                              (float)coordinates[i + 1].Y, (float)coordinates[i + 1].X);
            }

            var tags = new Itinero.Attributes.AttributeCollection(_routerDb.EdgeProfiles.Get(edge.Data.Profile));

            foreach (var tag in _routerDb.EdgeMeta.Get(edge.Data.MetaId))
            {
                tags.AddOrReplace(tag.Key, tag.Value);
            }

            var attributes = new AttributesTable();

            attributes.AddFrom("highway", tags);
            attributes.AddFrom("route", tags);

            foreach (var p in _profiles)
            {
                var vehicleShortName = p.Parent.Name;
                if (vehicleShortName.Length > 4)
                {
                    vehicleShortName = vehicleShortName.Substring(0, 4);
                }

                var profileShortName = p.Name;
                if (profileShortName == null)
                {
                    profileShortName = string.Empty;
                }
                if (profileShortName.Length > 2)
                {
                    profileShortName = profileShortName.Substring(0, 3);
                }

                var profileName = $"{vehicleShortName}_{profileShortName}";

                var factor = p.FactorAndSpeed(tags);
                attributes.Add(profileName.ToLower(), factor.Value != 0);
                attributes.Add(profileName + "_dir", factor.Direction);
                var speed = 1 / factor.SpeedFactor * 3.6;
                if (factor.SpeedFactor <= 0)
                {
                    speed = 65536;
                }
                attributes.Add(profileName + "_sp", System.Math.Round(speed, 2));
                speed = 1 / factor.Value * 3.6;
                if (factor.Value <= 0)
                {
                    speed = 65536;
                }
                attributes.Add(profileName + "_spc", System.Math.Round(speed, 2));
            }

            attributes.Add("length", System.Math.Round(length, 3));

            string lanesString;
            var    lanes         = 1;
            var    lanesVerified = false;

            if (tags.TryGetValue("lanes", out lanesString))
            {
                lanesVerified = true;
                if (!int.TryParse(lanesString, out lanes))
                {
                    lanes         = 1;
                    lanesVerified = false;
                }
            }
            attributes.Add("lanes", lanes);
            attributes.Add("lanes_ve", lanesVerified);

            var name = tags.ExtractName();

            attributes.Add("name", name);

            attributes.AddFrom("way_id", tags);
            attributes.AddFrom("tunnel", tags);
            attributes.AddFrom("bridge", tags);

            attributes.Add("from", edge.From);
            attributes.Add("to", edge.To);

            return(new Feature(geometry, attributes));
        }
예제 #3
0
        /// <summary>
        /// Loads a test network from geojson.
        /// </summary>
        public static void LoadTestNetwork(this RouterDb db, string geoJson, float tolerance = 20)
        {
            var geoJsonReader = new NetTopologySuite.IO.GeoJsonReader();
            var features      = geoJsonReader.Read <FeatureCollection>(geoJson);

            foreach (var feature in features.Features)
            {
                if (feature.Geometry is Point)
                {
                    var  point = feature.Geometry as Point;
                    uint id;
                    if (feature.Attributes.Exists("id") &&
                        uint.TryParse(feature.Attributes["id"].ToInvariantString(), out id))
                    { // has and id, add as vertex.
                        db.Network.AddVertex(id,
                                             (float)point.Coordinate.Y,
                                             (float)point.Coordinate.X);
                    }
                }
            }

            foreach (var feature in features.Features)
            {
                if (feature.Geometry is LineString)
                {
                    if (feature.Attributes.Contains("restriction", "yes"))
                    {
                        continue;
                    }

                    var line    = feature.Geometry as LineString;
                    var profile = new Itinero.Attributes.AttributeCollection();
                    var names   = feature.Attributes.GetNames();
                    foreach (var name in names)
                    {
                        if (!name.StartsWith("meta:") &&
                            !name.StartsWith("stroke"))
                        {
                            profile.AddOrReplace(name, feature.Attributes[name].ToInvariantString());
                        }
                    }
                    var meta = new Itinero.Attributes.AttributeCollection();
                    foreach (var name in names)
                    {
                        if (name.StartsWith("meta:"))
                        {
                            meta.AddOrReplace(name.Remove(0, "meta:".Length),
                                              feature.Attributes[name].ToInvariantString());
                        }
                    }

                    var profileId = db.EdgeProfiles.Add(profile);
                    var metaId    = db.EdgeMeta.Add(meta);

                    var vertex1 = db.SearchVertexFor(
                        (float)line.Coordinates[0].Y,
                        (float)line.Coordinates[0].X, tolerance);
                    var distance = 0.0;
                    var shape    = new List <Coordinate>();
                    for (var i = 1; i < line.Coordinates.Length; i++)
                    {
                        var vertex2 = db.SearchVertexFor(
                            (float)line.Coordinates[i].Y,
                            (float)line.Coordinates[i].X, tolerance);
                        distance += Coordinate.DistanceEstimateInMeter(
                            (float)line.Coordinates[i - 1].Y, (float)line.Coordinates[i - 1].X,
                            (float)line.Coordinates[i].Y, (float)line.Coordinates[i].X);
                        if (vertex2 == Itinero.Constants.NO_VERTEX)
                        { // add this point as shapepoint.
                            shape.Add(line.Coordinates[i].FromCoordinate());
                            continue;
                        }
                        db.Network.AddEdge(vertex1, vertex2, new Itinero.Data.Network.Edges.EdgeData()
                        {
                            Distance = (float)distance,
                            MetaId   = metaId,
                            Profile  = (ushort)profileId
                        }, shape);
                        shape.Clear();
                        vertex1  = vertex2;
                        distance = 0;
                    }
                }
            }

            foreach (var feature in features.Features)
            {
                if (feature.Geometry is LineString &&
                    feature.Attributes.Contains("restriction", "yes"))
                {
                    var line     = feature.Geometry as LineString;
                    var sequence = new List <uint>();
                    sequence.Add(db.SearchVertexFor(
                                     (float)line.Coordinates[0].Y,
                                     (float)line.Coordinates[0].X, tolerance));
                    for (var i = 1; i < line.Coordinates.Length; i++)
                    {
                        sequence.Add(db.SearchVertexFor(
                                         (float)line.Coordinates[i].Y,
                                         (float)line.Coordinates[i].X, tolerance));
                    }

                    var vehicleType = string.Empty;
                    if (!feature.Attributes.TryGetValueAsString("vehicle_type", out vehicleType))
                    {
                        vehicleType = string.Empty;
                    }
                    RestrictionsDb restrictions;
                    if (!db.TryGetRestrictions(vehicleType, out restrictions))
                    {
                        restrictions = new RestrictionsDb();
                        db.AddRestrictions(vehicleType, restrictions);
                    }

                    restrictions.Add(sequence.ToArray());
                }
            }
        }
예제 #4
0
 /// <summary>
 /// Adds the given attributes.
 /// </summary>
 public static void AddFrom(this AttributesTable table, string name, Itinero.Attributes.AttributeCollection tags)
 {
     table.AddFrom(name, tags, string.Empty);
 }
예제 #5
0
        /// <summary>
        /// Loads a test network from geojson.
        /// </summary>
        public static void LoadTestNetwork(this RouterDb db, string geoJson)
        {
            var geoJsonReader = new NetTopologySuite.IO.GeoJsonReader();
            var features      = geoJsonReader.Read <FeatureCollection>(geoJson);

            foreach (var feature in features.Features)
            {
                if (feature.Geometry is Point)
                {
                    var  point = feature.Geometry as Point;
                    uint id;
                    if (feature.Attributes.Exists("id") &&
                        uint.TryParse(feature.Attributes["id"].ToInvariantString(), out id))
                    { // has and id, add as vertex.
                        db.Network.AddVertex(id,
                                             (float)point.Coordinate.Y,
                                             (float)point.Coordinate.X);
                    }
                }
            }

            foreach (var feature in features.Features)
            {
                if (feature.Geometry is LineString)
                {
                    var line    = feature.Geometry as LineString;
                    var profile = new Itinero.Attributes.AttributeCollection();
                    var names   = feature.Attributes.GetNames();
                    foreach (var name in names)
                    {
                        if (!name.StartsWith("meta:") &&
                            !name.StartsWith("stroke"))
                        {
                            profile.AddOrReplace(name, feature.Attributes[name].ToInvariantString());
                        }
                    }
                    var meta = new Itinero.Attributes.AttributeCollection();
                    foreach (var name in names)
                    {
                        if (name.StartsWith("meta:"))
                        {
                            meta.AddOrReplace(name.Remove(0, "meta:".Length),
                                              feature.Attributes[name].ToInvariantString());
                        }
                    }

                    var profileId = db.EdgeProfiles.Add(profile);
                    var metaId    = db.EdgeMeta.Add(meta);

                    var vertex1 = db.SearchVertexFor(
                        (float)line.Coordinates[0].Y,
                        (float)line.Coordinates[0].X);
                    var distance = 0.0;
                    var shape    = new List <Coordinate>();
                    for (var i = 1; i < line.Coordinates.Length; i++)
                    {
                        var vertex2 = db.SearchVertexFor(
                            (float)line.Coordinates[i].Y,
                            (float)line.Coordinates[i].X);
                        distance += Coordinate.DistanceEstimateInMeter(
                            (float)line.Coordinates[i - 1].Y, (float)line.Coordinates[i - 1].X,
                            (float)line.Coordinates[i].Y, (float)line.Coordinates[i].X);
                        if (vertex2 == Itinero.Constants.NO_VERTEX)
                        { // add this point as shapepoint.
                            shape.Add(line.Coordinates[i].FromCoordinate());
                            continue;
                        }
                        db.Network.AddEdge(vertex1, vertex2, new Itinero.Data.Network.Edges.EdgeData()
                        {
                            Distance = (float)distance,
                            MetaId   = metaId,
                            Profile  = (ushort)profileId
                        }, shape);
                        shape.Clear();
                        vertex1  = vertex2;
                        distance = 0;
                    }
                }
            }

            //var features = GeoJsonConverter.ToFeatureCollection(geoJson);

            //foreach (var feature in features)
            //{
            //    if (feature.Geometry is Point)
            //    {
            //        var point = feature.Geometry as Point;
            //        uint id;
            //        if (feature.Attributes.ContainsKey("id") &&
            //           uint.TryParse(feature.Attributes["id"].ToInvariantString(), out id))
            //        { // has and id, add as vertex.
            //            db.Network.AddVertex(id,
            //                (float)point.Coordinate.Latitude,
            //                (float)point.Coordinate.Longitude);
            //        }
            //    }
            //}

            //foreach (var feature in features)
            //{
            //    if (feature.Geometry is LineString)
            //    {
            //        var line = feature.Geometry as LineString;
            //        var profile = new TagsCollection();
            //        foreach (var attribute in feature.Attributes)
            //        {
            //            if (!attribute.Key.StartsWith("meta:") &&
            //                !attribute.Key.StartsWith("stroke"))
            //            {
            //                profile.Add(attribute.Key, attribute.Value.ToInvariantString());
            //            }
            //        }
            //        var meta = new TagsCollection();
            //        foreach (var attribute in feature.Attributes)
            //        {
            //            if (attribute.Key.StartsWith("meta:"))
            //            {
            //                meta.Add(attribute.Key.Remove(0, "meta:".Length),
            //                    attribute.Value.ToInvariantString());
            //            }
            //        }

            //        var profileId = db.EdgeProfiles.Add(profile);
            //        var metaId = db.EdgeMeta.Add(meta);

            //        var vertex1 = db.SearchVertexFor(
            //            (float)line.Coordinates[0].Latitude,
            //            (float)line.Coordinates[0].Longitude);
            //        var distance = 0.0;
            //        var shape = new List<ICoordinate>();
            //        for (var i = 1; i < line.Coordinates.Count; i++)
            //        {
            //            var vertex2 = db.SearchVertexFor(
            //                (float)line.Coordinates[i].Latitude,
            //                (float)line.Coordinates[i].Longitude);
            //            distance += GeoCoordinate.DistanceEstimateInMeter(line.Coordinates[i - 1],
            //                line.Coordinates[i]);
            //            if (vertex2 == Itinero.Constants.NO_VERTEX)
            //            { // add this point as shapepoint.
            //                shape.Add(line.Coordinates[i]);
            //                continue;
            //            }
            //            db.Network.AddEdge(vertex1, vertex2, new Routing.Network.Data.EdgeData()
            //            {
            //                Distance = (float)distance,
            //                MetaId = metaId,
            //                Profile = (ushort)profileId
            //            }, shape);
            //            shape.Clear();
            //            vertex1 = vertex2;
            //            distance = 0;
            //        }
            //    }
            //}
        }