Exemplo n.º 1
0
        /// <summary>
        /// Converts a line string into an osm object.
        /// </summary>
        /// <param name="lineString"></param>
        /// <returns></returns>
        private static LineString ConvertLineString(OsmSharp.Xml.Kml.v2_1.LineStringType lineString)
        {
            // convert the coordinates.
            IList<GeoCoordinate> coordinates = KmlGeoStreamSource.ConvertCoordinates(lineString.coordinates);

            // create the ring.
            LineString lineStringGeometry = new LineString(coordinates);
            lineStringGeometry.Attributes = new SimpleGeometryAttributeCollection();
            lineStringGeometry.Attributes.Add("id", lineString.id);

            return lineStringGeometry;
        }
        /// <summary>
        /// Interprets an OSM-object and returns the corresponding geometry.
        /// </summary>
        /// <param name="osmObject"></param>
        /// <returns></returns>
        public override GeometryCollection Interpret(CompleteOsmGeo osmObject)
        {
            // DISCLAIMER: this is a very very very simple geometry interpreter and
            // contains hardcoded all relevant tags.

            GeometryCollection collection = new GeometryCollection();
            TagsCollectionBase tags;
            if (osmObject != null)
            {
                switch (osmObject.Type)
                {
                    case CompleteOsmType.Node:
                        TagsCollection newCollection = new TagsCollection(
                            osmObject.Tags);
                        newCollection.RemoveKey("FIXME");
                        newCollection.RemoveKey("node");
                        newCollection.RemoveKey("source");

                        if (newCollection.Count > 0)
                        { // there is still some relevant information left.
                            collection.Add(new Point((osmObject as CompleteNode).Coordinate));
                        }
                        break;
                    case CompleteOsmType.Way:
                        tags = osmObject.Tags;

                        bool isArea = false;
                        if ((tags.ContainsKey("building") && !tags.IsFalse("building")) ||
                            (tags.ContainsKey("landuse") && !tags.IsFalse("landuse")) ||
                            (tags.ContainsKey("amenity") && !tags.IsFalse("amenity")) ||
                            (tags.ContainsKey("harbour") && !tags.IsFalse("harbour")) ||
                            (tags.ContainsKey("historic") && !tags.IsFalse("historic")) ||
                            (tags.ContainsKey("leisure") && !tags.IsFalse("leisure")) ||
                            (tags.ContainsKey("man_made") && !tags.IsFalse("man_made")) ||
                            (tags.ContainsKey("military") && !tags.IsFalse("military")) ||
                            (tags.ContainsKey("natural") && !tags.IsFalse("natural")) ||
                            (tags.ContainsKey("office") && !tags.IsFalse("office")) ||
                            (tags.ContainsKey("place") && !tags.IsFalse("place")) ||
                            (tags.ContainsKey("power") && !tags.IsFalse("power")) ||
                            (tags.ContainsKey("public_transport") && !tags.IsFalse("public_transport")) ||
                            (tags.ContainsKey("shop") && !tags.IsFalse("shop")) ||
                            (tags.ContainsKey("sport") && !tags.IsFalse("sport")) ||
                            (tags.ContainsKey("tourism") && !tags.IsFalse("tourism")) ||
                            (tags.ContainsKey("waterway") && !tags.IsFalse("waterway")) ||
                            (tags.ContainsKey("wetland") && !tags.IsFalse("wetland")) ||
                            (tags.ContainsKey("water") && !tags.IsFalse("water")) ||
                            (tags.ContainsKey("aeroway") && !tags.IsFalse("aeroway")))
                        { // these tags usually indicate an area.
                            isArea = true;
                        }

                        if (tags.IsTrue("area"))
                        { // explicitly indicated that this is an area.
                            isArea = true;
                        }
                        else if (tags.IsFalse("area"))
                        { // explicitly indicated that this is not an area.
                            isArea = false;
                        }

                        if (isArea)
                        { // area tags leads to simple polygon
                            LineairRing lineairRing = new LineairRing((osmObject as CompleteWay).GetCoordinates().ToArray<GeoCoordinate>());
                            lineairRing.Attributes = new SimpleGeometryAttributeCollection(tags);
                            collection.Add(lineairRing);
                        }
                        else
                        { // no area tag leads to just a line.
                            LineString lineString = new LineString((osmObject as CompleteWay).GetCoordinates().ToArray<GeoCoordinate>());
                            lineString.Attributes = new SimpleGeometryAttributeCollection(tags);
                            collection.Add(lineString);
                        }
                        break;
                    case CompleteOsmType.Relation:
                        CompleteRelation relation = (osmObject as CompleteRelation);
                        tags = relation.Tags;

                        string typeValue;
                        if (tags.TryGetValue("type", out typeValue))
                        { // there is a type in this relation.
                            if (typeValue == "multipolygon")
                            { // this relation is a multipolygon.
                                Geometry geometry = this.InterpretMultipolygonRelation(relation);
                                if (geometry != null)
                                { // add the geometry.
                                    collection.Add(geometry);
                                }
                            }
                            else if (typeValue == "boundary")
                            { // this relation is a boundary.

                            }
                        }
                        break;
                }
            }
            return collection;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generates GeoJson for the given geometry.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="geometry"></param>
        /// <returns></returns>
        internal static void Write(JsonWriter writer, LineString geometry)
        {
            if (writer == null) { throw new ArgumentNullException("writer"); }
            if (geometry == null) { throw new ArgumentNullException("geometry"); }

            writer.WriteStartObject();
            writer.WritePropertyName("type");
            writer.WriteValue("LineString");
            writer.WritePropertyName("coordinates");
            writer.WriteStartArray();
            foreach (var coordinate in geometry.Coordinates)
            {
                writer.WriteStartArray();
                writer.WriteValue(coordinate.Longitude);
                writer.WriteValue(coordinate.Latitude);
                writer.WriteEndArray();
            }
            writer.WriteEndArray();
            writer.WriteEndObject();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Converts this route to a feature collection.
        /// </summary>
        /// <returns></returns>
        public FeatureCollection ToFeatureCollection()
        {
            var featureCollection = new FeatureCollection();
            if(this.Segments == null)
            {
                return featureCollection;
            }
            for (int i = 0; i < this.Segments.Length; i++)
            {
                // create a line string for the current segment.
                if (i > 0)
                { // but only do so when there is a previous point available.
                    var segmentLineString = new LineString(
                        new GeoCoordinate(this.Segments[i - 1].Latitude, this.Segments[i - 1].Longitude),
                        new GeoCoordinate(this.Segments[i].Latitude, this.Segments[i].Longitude));

                    var segmentTags = this.Segments[i].Tags;
                    var attributesTable = new SimpleGeometryAttributeCollection();
                    if (segmentTags != null)
                    { // there are tags.
                        foreach (var tag in segmentTags)
                        {
                            attributesTable.Add(tag.Key, tag.Value);
                        }
                    }
                    attributesTable.Add("time", this.Segments[i].Time);
                    attributesTable.Add("distance", this.Segments[i].Distance);
                    if (this.Segments[i].Vehicle != null)
                    {
                        attributesTable.Add("vehicle", this.Segments[i].Vehicle);
                    }
                    featureCollection.Add(new Feature(segmentLineString, attributesTable));
                }

                // create points.
                if (this.Segments[i].Points != null)
                {
                    foreach (var point in this.Segments[i].Points)
                    {
                        // build attributes.
                        var currentPointTags = point.Tags;
                        var attributesTable = new SimpleGeometryAttributeCollection();
                        if (currentPointTags != null)
                        { // there are tags.
                            foreach (var tag in currentPointTags)
                            {
                                attributesTable.Add(tag.Key, tag.Value);
                            }
                        }

                        // build feature.
                        var pointGeometry = new Point(new GeoCoordinate(point.Latitude, point.Longitude));
                        featureCollection.Add(new Feature(pointGeometry, attributesTable));
                    }
                }
            }
            return featureCollection;
        }
Exemplo n.º 5
0
        public void TestLineStringSerialization()
        {
            var geometry = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0)
                });

            var serialized = geometry.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]]}",
                serialized);
        }
Exemplo n.º 6
0
        public void TestMultiLineStringSerialization()
        {
            var geometry1 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0)
                });
            var geometry2 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 2),
                    new GeoCoordinate(2, 2),
                    new GeoCoordinate(2, 0)
                });
            var geometry3 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 3),
                    new GeoCoordinate(3, 3),
                    new GeoCoordinate(3, 0)
                });
            var geometryCollection = new MultiLineString(new LineString[] { geometry1, geometry2, geometry3 });

            var serialized = geometryCollection.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"MultiLineString\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]],[[0.0,0.0],[2.0,0.0],[2.0,2.0],[0.0,2.0]],[[0.0,0.0],[3.0,0.0],[3.0,3.0],[0.0,3.0]]]}",
                serialized);
        }
Exemplo n.º 7
0
        public void TestFeatureSerialization()
        {
            // a feature with a point.
            var geometry = (Geometry)new Point(new GeoCoordinate(0, 1));
            var feature = new Feature(geometry);

            var serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.0,0.0]}}",
                serialized);

            feature = new Feature(geometry, new SimpleGeometryAttributeCollection(new GeometryAttribute[]
            {
                new GeometryAttribute()
                {
                    Key = "key1",
                    Value = "value1"
                }
            }));

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{\"key1\":\"value1\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[1.0,0.0]}}",
                serialized);

            // a feature with a linestring.
            geometry = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0)
                });
            feature = new Feature(geometry);

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]]}}",
                serialized);

            // a featurer with a linearring.
            geometry = new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                });
            feature = new Feature(geometry);

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]}}",
                serialized);

            // a featurer with a polygon.
            geometry = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                }));
            feature = new Feature(geometry);

            serialized = feature.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]}}",
                serialized);
        }
Exemplo n.º 8
0
        public void TestGeometryCollectionSerialization()
        {
            var geometry1 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0)
                });
            var geometry2 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 2),
                    new GeoCoordinate(2, 2),
                    new GeoCoordinate(2, 0)
                });
            var geometry3 = new LineString(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 3),
                    new GeoCoordinate(3, 3),
                    new GeoCoordinate(3, 0)
                });
            var geometry4 = new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                });
            var geometry5 = new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 1),
                    new GeoCoordinate(1, 1),
                    new GeoCoordinate(1, 0),
                    new GeoCoordinate(0, 0)
                }));
            var geometry6 = new MultiPolygon(geometry5, new Polygon(new LineairRing(
                new GeoCoordinate[]
                {
                    new GeoCoordinate(0, 0),
                    new GeoCoordinate(0, 2),
                    new GeoCoordinate(2, 2),
                    new GeoCoordinate(2, 0),
                    new GeoCoordinate(0, 0)
                })));
            var geometry7 = new Point(new GeoCoordinate(0, 1));
            var geometry8 = new MultiPoint(geometry7, new Point(new GeoCoordinate(0, 2)));
            var geometryCollection = new GeometryCollection(
                geometry1, geometry2, geometry3,
                geometry4, geometry5, geometry6,
                geometry7, geometry8);

            var serialized = geometryCollection.ToGeoJson();
            serialized = serialized.RemoveWhitespace();

            Assert.AreEqual("{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0]]},{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[2.0,0.0],[2.0,2.0],[0.0,2.0]]},{\"type\":\"LineString\",\"coordinates\":[[0.0,0.0],[3.0,0.0],[3.0,3.0],[0.0,3.0]]},{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]},{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]]},{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0.0,0.0],[1.0,0.0],[1.0,1.0],[0.0,1.0],[0.0,0.0]]],[[[0.0,0.0],[2.0,0.0],[2.0,2.0],[0.0,2.0],[0.0,0.0]]]]},{\"type\":\"Point\",\"coordinates\":[1.0,0.0]},{\"type\":\"MultiPoint\",\"coordinates\":[[1.0,0.0],[2.0,0.0]]}]}",
                serialized);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Reads a gpx v1.1 object into corresponding geometries.
        /// </summary>
        /// <param name="gpx"></param>
        private void ReadGpxv1_1(Xml.Gpx.v1_1.gpxType gpx)
        {
            this.GeometryCollection.Clear();

            // do the waypoints.
            if (gpx.wpt != null)
            { // there are waypoints.
                foreach (var wpt in gpx.wpt)
                {
                    Point point = new Point(
                        new GeoCoordinate((double)wpt.lat, (double)wpt.lon));
                    point.Attributes = new SimpleGeometryAttributeCollection();

                    if (wpt.ageofdgpsdataSpecified) { point.Attributes.Add("ageofdgpsdata", wpt.ageofdgpsdata); }
                    if (wpt.eleSpecified) { point.Attributes.Add("ele", wpt.ele); }
                    if (wpt.fixSpecified) { point.Attributes.Add("fix", wpt.fix); }
                    if (wpt.geoidheightSpecified) { point.Attributes.Add("geoidheight", wpt.geoidheight); }
                    if (wpt.hdopSpecified) { point.Attributes.Add("hdop", wpt.hdop); }
                    if (wpt.magvarSpecified) { point.Attributes.Add("magvar", wpt.magvar); }
                    if (wpt.pdopSpecified) { point.Attributes.Add("pdop", wpt.pdop); }
                    if (wpt.timeSpecified) { point.Attributes.Add("time", wpt.time); }
                    if (wpt.vdopSpecified) { point.Attributes.Add("vdop", wpt.vdop); }

                    if (wpt.cmt != null) { point.Attributes.Add("cmt", wpt.cmt); }
                    if (wpt.desc != null) { point.Attributes.Add("desc", wpt.desc); }
                    if (wpt.dgpsid != null) { point.Attributes.Add("dgpsid", wpt.dgpsid); }
                    if (wpt.name != null) { point.Attributes.Add("name", wpt.name); }
                    if (wpt.sat != null) { point.Attributes.Add("sat", wpt.sat); }
                    if (wpt.src != null) { point.Attributes.Add("src", wpt.src); }
                    if (wpt.sym != null) { point.Attributes.Add("sym", wpt.sym); }
                    if (wpt.type != null) { point.Attributes.Add("type", wpt.type); }

                    this.GeometryCollection.Add(point);
                }
            }

            // do the routes.
            if (gpx.rte != null)
            {
                foreach (var rte in gpx.rte)
                { // convert to a line-string
                    List<GeoCoordinate> coordinates = new List<GeoCoordinate>();
                    foreach (var rtept in rte.rtept)
                    {
                        GeoCoordinate coordinate = new GeoCoordinate((double)rtept.lat, (double)rtept.lon);
                        coordinates.Add(coordinate);

                        if (_createTrackPoints)
                        {
                            Point point = new Point(coordinate);
                            point.Attributes = new SimpleGeometryAttributeCollection();

                            if (rtept.ageofdgpsdataSpecified) { point.Attributes.Add("ageofdgpsdata", rtept.ageofdgpsdata); }
                            if (rtept.eleSpecified) { point.Attributes.Add("ele", rtept.ele); }
                            if (rtept.fixSpecified) { point.Attributes.Add("fix", rtept.fix); }
                            if (rtept.geoidheightSpecified) { point.Attributes.Add("geoidheight", rtept.geoidheight); }
                            if (rtept.hdopSpecified) { point.Attributes.Add("hdop", rtept.hdop); }
                            if (rtept.magvarSpecified) { point.Attributes.Add("magvar", rtept.magvar); }
                            if (rtept.pdopSpecified) { point.Attributes.Add("pdop", rtept.pdop); }
                            if (rtept.timeSpecified) { point.Attributes.Add("time", rtept.time); }
                            if (rtept.vdopSpecified) { point.Attributes.Add("vdop", rtept.vdop); }

                            if (rtept.cmt != null) { point.Attributes.Add("cmt", rtept.cmt); }
                            if (rtept.desc != null) { point.Attributes.Add("desc", rtept.desc); }
                            if (rtept.dgpsid != null) { point.Attributes.Add("dgpsid", rtept.dgpsid); }
                            if (rtept.name != null) { point.Attributes.Add("name", rtept.name); }
                            if (rtept.sat != null) { point.Attributes.Add("sat", rtept.sat); }
                            if (rtept.src != null) { point.Attributes.Add("src", rtept.src); }
                            if (rtept.sym != null) { point.Attributes.Add("sym", rtept.sym); }
                            if (rtept.type != null) { point.Attributes.Add("type", rtept.type); }

                            this.GeometryCollection.Add(point);
                        }
                    }

                    // creates a new linestring.
                    LineString lineString = new LineString(coordinates);
                    lineString.Attributes = new SimpleGeometryAttributeCollection();
                    if (rte.cmt != null) { lineString.Attributes.Add("cmt", rte.cmt); }
                    if (rte.desc != null) { lineString.Attributes.Add("desc", rte.desc); }
                    if (rte.name != null) { lineString.Attributes.Add("name", rte.name); }
                    if (rte.number != null) { lineString.Attributes.Add("number", rte.number); }
                    if (rte.src != null) { lineString.Attributes.Add("src", rte.src); }

                    this.GeometryCollection.Add(lineString);
                }
            }

            // do the tracks.
            foreach (var trk in gpx.trk)
            { // convert to a line-string
                List<GeoCoordinate> coordinates = new List<GeoCoordinate>();
                foreach (var trkseg in trk.trkseg)
                {
                    foreach (var wpt in trkseg.trkpt)
                    {
                        GeoCoordinate coordinate = new GeoCoordinate((double)wpt.lat, (double)wpt.lon);

                        // only add new coordinates.
                        if (coordinates.Count == 0 || coordinates[coordinates.Count - 1] != coordinate)
                        {
                            coordinates.Add(coordinate);
                        }

                        if (_createTrackPoints)
                        {
                            Point point = new Point(coordinate);
                            point.Attributes = new SimpleGeometryAttributeCollection();

                            if (wpt.ageofdgpsdataSpecified) { point.Attributes.Add("ageofdgpsdata", wpt.ageofdgpsdata); }
                            if (wpt.eleSpecified) { point.Attributes.Add("ele", wpt.ele); }
                            if (wpt.fixSpecified) { point.Attributes.Add("fix", wpt.fix); }
                            if (wpt.geoidheightSpecified) { point.Attributes.Add("geoidheight", wpt.geoidheight); }
                            if (wpt.hdopSpecified) { point.Attributes.Add("hdop", wpt.hdop); }
                            if (wpt.magvarSpecified) { point.Attributes.Add("magvar", wpt.magvar); }
                            if (wpt.pdopSpecified) { point.Attributes.Add("pdop", wpt.pdop); }
                            if (wpt.timeSpecified) { point.Attributes.Add("time", wpt.time); }
                            if (wpt.vdopSpecified) { point.Attributes.Add("vdop", wpt.vdop); }

                            if (wpt.cmt != null) { point.Attributes.Add("cmt", wpt.cmt); }
                            if (wpt.desc != null) { point.Attributes.Add("desc", wpt.desc); }
                            if (wpt.dgpsid != null) { point.Attributes.Add("dgpsid", wpt.dgpsid); }
                            if (wpt.name != null) { point.Attributes.Add("name", wpt.name); }
                            if (wpt.sat != null) { point.Attributes.Add("sat", wpt.sat); }
                            if (wpt.src != null) { point.Attributes.Add("src", wpt.src); }
                            if (wpt.sym != null) { point.Attributes.Add("sym", wpt.sym); }
                            if (wpt.type != null) { point.Attributes.Add("type", wpt.type); }

                            this.GeometryCollection.Add(point);
                        }
                    }
                }

                // creates a new linestring.
                LineString lineString = new LineString(coordinates);
                lineString.Attributes = new SimpleGeometryAttributeCollection();
                if (trk.cmt != null) { lineString.Attributes.Add("cmt", trk.cmt); }
                if (trk.desc != null) { lineString.Attributes.Add("desc", trk.desc); }
                if (trk.name != null) { lineString.Attributes.Add("name", trk.name); }
                if (trk.number != null) { lineString.Attributes.Add("number", trk.number); }
                if (trk.src != null) { lineString.Attributes.Add("src", trk.src); }

                this.GeometryCollection.Add(lineString);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Adds a linestring.
        /// </summary>
        private void AddGeometry(LineString geometry, int color, float width)
        {
            if (geometry == null) { throw new ArgumentNullException(); }

            this.AddPolyline(geometry.Coordinates.ToArray(), width, color);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Converts the given route to a line string.
        /// </summary>
        /// <returns></returns>
        public override FeatureCollection GetFeatures(Route route, RouteAggregationType aggregationType)
        {
            if (aggregationType == RouteAggregationType.All ||
                aggregationType == RouteAggregationType.Modal)
            { // one mode, one LineString.
                var featureCollection = new FeatureCollection();
                var coordinates = route.GetPoints();
                if (coordinates.Count > 1)
                {
                    var lineString = new LineString(coordinates.ToArray());

                    var attributes = new SimpleGeometryAttributeCollection();
                    attributes.Add("osmsharp:total_time", route.TotalTime.ToInvariantString());
                    attributes.Add("osmsharp:total_distance", route.TotalDistance.ToInvariantString());

                    var feature = new Feature(lineString, attributes);

                    featureCollection.Add(feature);
                }
                return featureCollection;
            }
            else
            { // one LineString per instruction.
                var instructions = this.GetInstructions(route);
                var featureCollection = new FeatureCollection();
                for (int i = 0; i < instructions.Count; i++)
                {
                    var instruction = instructions[i];
                    var coordinates = new List<GeoCoordinate>();
                    for (int segmentIdx = instruction.FirstSegmentIdx; segmentIdx <= instruction.LastSegmentIdx; segmentIdx++)
                    {
                        coordinates.Add(new GeoCoordinate(route.Segments[segmentIdx].Latitude, route.Segments[segmentIdx].Longitude));
                    }

                    // build attributes.
                    var currentArcTags = instruction.MetaData;
                    var attributesTable = new SimpleGeometryAttributeCollection();
                    if (currentArcTags != null)
                    { // there are tags.
                        foreach (var tag in currentArcTags)
                        {
                            if (tag.Value != null)
                            {
                                var t = tag.Value.GetType();
                                if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(String))
                                {
                                    attributesTable.Add(tag.Key, tag.Value);
                                }
                            }
                        }
                    }

                    // build feature.
                    var lineString = new LineString(coordinates);
                    featureCollection.Add(new Feature(lineString, attributesTable));

                    // build poi-features if any.
                    if (instruction.Pois != null)
                    {
                        foreach (var poi in instruction.Pois)
                        {
                            // build attributes.
                            var poiTags = poi.Tags;
                            var poiAttributesTable = new SimpleGeometryAttributeCollection();
                            if (poiTags != null)
                            { // there are tags.
                                foreach (var tag in poiTags)
                                {
                                    poiAttributesTable.Add(tag.Key, tag.Value);
                                }
                            }

                            // build feature.
                            var point = new Point(poi.Location);
                            featureCollection.Add(new Feature(point, poiAttributesTable));
                        }
                    }
                }
                return featureCollection;
            }
        }
        /// <summary>
        /// Gets a feature collection representing the given route based on the given aggregation type.
        /// </summary>
        /// <param name="route"></param>
        /// <param name="aggregationType"></param>
        /// <returns></returns>
        public override FeatureCollection GetFeatures(Route route, RouteAggregationType aggregationType)
        {
            if (route == null) { throw new ArgumentNullException("route"); }
            if (aggregationType == RouteAggregationType.All)
            {  // one LineString.
                var featureCollection = new FeatureCollection();
                var coordinates = route.GetPoints();
                if (coordinates.Count > 1)
                {
                    var lineString = new LineString(coordinates.ToArray());

                    var attributes = new SimpleGeometryAttributeCollection();
                    attributes.Add("osmsharp:total_time", route.TotalTime.ToInvariantString());
                    attributes.Add("osmsharp:total_distance", route.TotalDistance.ToInvariantString());

                    var feature = new Feature(lineString, attributes);

                    featureCollection.Add(feature);
                }
                return featureCollection;
            }
            else if(aggregationType == RouteAggregationType.Modal)
            { // modal.
                // aggregate.
                var aggregator = new ModalAggregator(new OsmRoutingInterpreter());
                var microPlanner = new ModalMicroPlanner(new ModalLanguageGenerator(), new OsmRoutingInterpreter());
                var aggregated = aggregator.Aggregate(route);
                var instructions = InstructionGenerator.Generate(microPlanner, route, aggregated);

                return this.GetFeatures(route, instructions);
            }
            else // modal and instructions.
            { // instructions.
                var instructions = this.GetInstructions(route);

                return this.GetFeatures(route, instructions);
            }
        }
        /// <summary>
        /// Generates a feature collection with one LineString per instruction and all poi's as Points.
        /// </summary>
        /// <returns></returns>
        private FeatureCollection GetFeatures(Route route, List<Instruction> instructions)
        {
            var featureCollection = new FeatureCollection();
            for (int i = 0; i < instructions.Count; i++)
            {
                var instruction = instructions[i];
                var coordinates = new List<GeoCoordinate>();
                for (int segmentIdx = instruction.FirstSegmentIdx; segmentIdx <= instruction.LastSegmentIdx; segmentIdx++)
                {
                    coordinates.Add(new GeoCoordinate(route.Segments[segmentIdx].Latitude, route.Segments[segmentIdx].Longitude));
                }

                // build attributes.
                var currentArcTags = instruction.MetaData;
                var attributesTable = new SimpleGeometryAttributeCollection();
                if (currentArcTags != null)
                { // there are tags.
                    foreach (var tag in currentArcTags)
                    {
                        if (tag.Value != null)
                        {
                            var t = tag.Value.GetType();
                            if (t.IsPrimitive || t == typeof(Decimal) || t == typeof(String))
                            {
                                attributesTable.Add(tag.Key, tag.Value);
                            }
                        }
                    }
                }

                // build feature.
                var lineString = new LineString(coordinates);
                featureCollection.Add(new Feature(lineString, attributesTable));

                // build poi-features if any.
                if (instruction.Pois != null)
                {
                    foreach (var poi in instruction.Pois)
                    {
                        // build attributes.
                        var poiTags = poi.Tags;
                        var poiAttributesTable = new SimpleGeometryAttributeCollection();
                        if (poiTags != null)
                        { // there are tags.
                            foreach (var tag in poiTags)
                            {
                                poiAttributesTable.Add(tag.Key, tag.Value);
                            }
                        }

                        // build feature.
                        var point = new Point(poi.Location);
                        featureCollection.Add(new Feature(point, poiAttributesTable));
                    }
                }
            }
            return featureCollection;
        }