/// <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; }
/// <summary> /// Reads the properties/attributes. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static GeometryAttributeCollection ReadAttributes(JsonReader jsonReader) { var attributes = new SimpleGeometryAttributeCollection(); while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { var key = (string)jsonReader.Value; jsonReader.Read(); var value = jsonReader.Value; attributes.Add(new GeometryAttribute() { Key = key, Value = value }); } } return attributes; }
/// <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> /// 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> /// 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; }