/// <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> /// 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> /// 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); }
public static FeatureCollection ToFeatureCollection(this Route route) { FeatureCollection featureCollection = new FeatureCollection(); if (route.Segments == null) { return(featureCollection); } for (int index = 0; index < route.Segments.Count; ++index) { if (index > 0) { LineString lineString = new LineString(new GeoCoordinate[2] { new GeoCoordinate((double)route.Segments[index - 1].Latitude, (double)route.Segments[index - 1].Longitude), new GeoCoordinate((double)route.Segments[index].Latitude, (double)route.Segments[index].Longitude) }); RouteTags[] tags = route.Segments[index].Tags; SimpleGeometryAttributeCollection attributeCollection = new SimpleGeometryAttributeCollection(); if (tags != null) { foreach (RouteTags routeTags in tags) { attributeCollection.Add(routeTags.Key, (object)routeTags.Value); } } attributeCollection.Add("time", (object)route.Segments[index].Time); attributeCollection.Add("distance", (object)route.Segments[index].Distance); attributeCollection.Add("profile", (object)route.Segments[index].Profile); featureCollection.Add(new Feature((Geometry)lineString, (GeometryAttributeCollection)attributeCollection)); } if (route.Segments[index].Points != null) { foreach (RouteStop point1 in route.Segments[index].Points) { RouteTags[] tags = point1.Tags; SimpleGeometryAttributeCollection attributeCollection = new SimpleGeometryAttributeCollection(); if (tags != null) { foreach (RouteTags routeTags in tags) { attributeCollection.Add(routeTags.Key, (object)routeTags.Value); } } Point point2 = new Point(new GeoCoordinate((double)point1.Latitude, (double)point1.Longitude)); featureCollection.Add(new Feature((Geometry)point2, (GeometryAttributeCollection)attributeCollection)); } } } return(featureCollection); }
private void AddSegment(RouteSegment segment, List <GeoCoordinate> shape) { LineString lineString = new LineString((IEnumerable <GeoCoordinate>)shape); RouteTags[] tags = segment.Tags; SimpleGeometryAttributeCollection attributeCollection = new SimpleGeometryAttributeCollection(); if (tags != null) { foreach (RouteTags routeTags in tags) { attributeCollection.Add(routeTags.Key, (object)routeTags.Value); } } attributeCollection.Add("time", (object)segment.Time); attributeCollection.Add("distance", (object)segment.Distance); attributeCollection.Add("profile", (object)segment.Profile); this._features.Add(new Feature((Geometry)lineString, (GeometryAttributeCollection)attributeCollection)); }
internal static GeometryAttributeCollection ReadAttributes(JsonReader jsonReader) { SimpleGeometryAttributeCollection attributeCollection = new SimpleGeometryAttributeCollection(); while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndObject) { if (jsonReader.TokenType == JsonToken.PropertyName) { string str = (string)jsonReader.Value; jsonReader.Read(); object obj = jsonReader.Value; attributeCollection.Add(new GeometryAttribute() { Key = str, Value = obj }); } } return((GeometryAttributeCollection)attributeCollection); }
private void AddPoints(RouteSegment segment) { if (segment.Points == null) { return; } foreach (RouteStop point in segment.Points) { RouteTags[] tags = point.Tags; SimpleGeometryAttributeCollection attributeCollection = new SimpleGeometryAttributeCollection(); if (tags != null) { foreach (RouteTags routeTags in tags) { attributeCollection.Add(routeTags.Key, (object)routeTags.Value); } } this._features.Add(new Feature((Geometry) new Point(new GeoCoordinate((double)point.Latitude, (double)point.Longitude)), (GeometryAttributeCollection)attributeCollection)); } }
/// <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); } }