Пример #1
0
        /// <summary>
        /// Gets a feature representing the edge with the given id.
        /// </summary>
        public static Feature GetFeatureForEdge(this RouterDb routerDb, uint edgeId)
        {
            var edge = routerDb.Network.GetEdge(edgeId);

            var edgeAttributes = new Itinero.Attributes.AttributeCollection(routerDb.EdgeMeta.Get(edge.Data.MetaId));

            edgeAttributes.AddOrReplace(routerDb.EdgeProfiles.Get(edge.Data.Profile));

            var geometry   = new LineString(routerDb.Network.GetShape(edge).ToCoordinatesArray());
            var attributes = edgeAttributes.ToAttributesTable();

            attributes.AddAttribute("id", edge.Id.ToInvariantString());
            attributes.AddAttribute("distance", edge.Data.Distance.ToInvariantString());
            return(new Feature(geometry, attributes));
        }
Пример #2
0
        /// <summary>
        /// Gets all features inside the given bounding box.
        /// </summary>
        public static FeatureCollection GetFeaturesIn(this RouterDb db, float minLatitude, float minLongitude,
                                                      float maxLatitude, float maxLongitude, bool includeEdges = true, bool includeVertices = true)
        {
            var features = new FeatureCollection();

            var vertices = Itinero.Algorithms.Search.Hilbert.HilbertExtensions.Search(db.Network.GeometricGraph, minLatitude, minLongitude,
                                                                                      maxLatitude, maxLongitude);
            var edges = new HashSet <long>();

            var edgeEnumerator = db.Network.GetEdgeEnumerator();

            foreach (var vertex in vertices)
            {
                if (includeVertices)
                {
                    features.Add(db.GetFeatureForVertex(vertex));
                }

                if (includeEdges)
                {
                    edgeEnumerator.MoveTo(vertex);
                    edgeEnumerator.Reset();
                    while (edgeEnumerator.MoveNext())
                    {
                        if (edges.Contains(edgeEnumerator.Id))
                        {
                            continue;
                        }
                        edges.Add(edgeEnumerator.Id);

                        var edgeAttributes = new Itinero.Attributes.AttributeCollection(db.EdgeMeta.Get(edgeEnumerator.Data.MetaId));
                        edgeAttributes.AddOrReplace(db.EdgeProfiles.Get(edgeEnumerator.Data.Profile));

                        var geometry   = new LineString(db.Network.GetShape(edgeEnumerator.Current).ToCoordinatesArray());
                        var attributes = edgeAttributes.ToAttributesTable();
                        attributes.AddAttribute("id", edgeEnumerator.Id.ToInvariantString());
                        attributes.AddAttribute("distance", edgeEnumerator.Data.Distance.ToInvariantString());
                        features.Add(new Feature(geometry,
                                                 attributes));
                    }
                }
            }

            return(features);
        }
Пример #3
0
        /// <summary>
        /// Writes a linestring-geometry for the edge currently in the enumerator.
        /// </summary>
        private static void WriteEdge(this RouterDb db, JsonWriter jsonWriter, RoutingNetwork.EdgeEnumerator edgeEnumerator)
        {
            var edgeAttributes = new Itinero.Attributes.AttributeCollection(db.EdgeMeta.Get(edgeEnumerator.Data.MetaId));

            edgeAttributes.AddOrReplace(db.EdgeProfiles.Get(edgeEnumerator.Data.Profile));

            var shape = db.Network.GetShape(edgeEnumerator.Current);

            jsonWriter.WriteOpen();
            jsonWriter.WriteProperty("type", "Feature", true, false);
            jsonWriter.WritePropertyName("geometry", false);

            jsonWriter.WriteOpen();
            jsonWriter.WriteProperty("type", "LineString", true, false);
            jsonWriter.WritePropertyName("coordinates", false);
            jsonWriter.WriteArrayOpen();

            foreach (var coordinate in shape)
            {
                jsonWriter.WriteArrayOpen();
                jsonWriter.WriteArrayValue(coordinate.Longitude.ToInvariantString());
                jsonWriter.WriteArrayValue(coordinate.Latitude.ToInvariantString());
                jsonWriter.WriteArrayClose();
            }

            jsonWriter.WriteArrayClose();
            jsonWriter.WriteClose();

            jsonWriter.WritePropertyName("properties");
            jsonWriter.WriteOpen();
            if (edgeAttributes != null)
            {
                foreach (var attribute in edgeAttributes)
                {
                    jsonWriter.WriteProperty(attribute.Key, attribute.Value, true, true);
                }
            }
            jsonWriter.WriteProperty("edgeid", edgeEnumerator.Id.ToInvariantString());
            jsonWriter.WriteProperty("vertex1", edgeEnumerator.From.ToInvariantString());
            jsonWriter.WriteProperty("vertex2", edgeEnumerator.To.ToInvariantString());
            jsonWriter.WriteClose();

            jsonWriter.WriteClose();
        }
Пример #4
0
        /// <summary>
        /// Gets all features.
        /// </summary>
        public static FeatureCollection GetFeatures(this RouterDb db)
        {
            var network  = db.Network;
            var features = new FeatureCollection();

            var edges = new HashSet <long>();

            var edgeEnumerator = network.GetEdgeEnumerator();

            for (uint vertex = 0; vertex < network.VertexCount; vertex++)
            {
                var vertexLocation = network.GeometricGraph.GetVertex(vertex);
                var attributes     = new AttributesTable();
                attributes.AddAttribute("id", vertex.ToInvariantString());
                features.Add(new Feature(new Point(vertexLocation.ToCoordinate()),
                                         attributes));
                edgeEnumerator.MoveTo(vertex);
                edgeEnumerator.Reset();
                while (edgeEnumerator.MoveNext())
                {
                    if (edges.Contains(edgeEnumerator.Id))
                    {
                        continue;
                    }
                    edges.Add(edgeEnumerator.Id);

                    var edgeAttributes = new Itinero.Attributes.AttributeCollection(db.EdgeMeta.Get(edgeEnumerator.Data.MetaId));
                    edgeAttributes.AddOrReplace(db.EdgeProfiles.Get(edgeEnumerator.Data.Profile));

                    var geometry = new LineString(network.GetShape(edgeEnumerator.Current).ToCoordinatesArray());
                    attributes = edgeAttributes.ToAttributesTable();
                    attributes.AddAttribute("id", edgeEnumerator.Id.ToInvariantString());
                    attributes.AddAttribute("distance", edgeEnumerator.Data.Distance.ToInvariantString());
                    var tags = db.GetProfileAndMeta(edgeEnumerator.Data.Profile, edgeEnumerator.Data.MetaId);
                    features.Add(new Feature(geometry,
                                             attributes));
                }
            }

            return(features);
        }
Пример #5
0
        /// <summary>
        /// Loads a test network from geojson.
        /// </summary>
        public static void LoadTestNetwork(this RouterDb db, string geoJson, float tolerance = 20)
        {
            var nodeIds = db.VertexData.AddInt64("node_ids");

            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 an id, add as vertex.
                        db.Network.AddVertex(id,
                                             (float)point.Coordinate.Y,
                                             (float)point.Coordinate.X);

                        nodeIds[id] = id;

                        var vertexMeta = new AttributeCollection();
                        foreach (var name in feature.Attributes.GetNames())
                        {
                            if (name == "id" ||
                                name.StartsWith("marker"))
                            {
                                continue;
                            }

                            var value = feature.Attributes[name];
                            if (value == null)
                            {
                                vertexMeta.AddOrReplace(name, string.Empty);
                            }
                            else
                            {
                                vertexMeta.AddOrReplace(name, value.ToInvariantString());
                            }
                        }

                        if (vertexMeta.Count > 0)
                        {
                            db.VertexMeta[id] = vertexMeta;
                        }
                    }
                }
            }

            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());
                }

                if (feature.Geometry is Point &&
                    feature.Attributes.Contains("restriction", "yes"))
                {
                    var point    = feature.Geometry as Point;
                    var sequence = new List <uint>();
                    sequence.Add(db.SearchVertexFor(
                                     (float)point.Coordinate.Y,
                                     (float)point.Coordinate.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());
                }
            }
        }