/// <summary> /// Converts the given OsmSharp feature collection into a list of NTS feature collection. /// </summary> /// <param name="featureCollection"></param> /// <returns></returns> public static List<Feature> Convert(OsmSharp.Geo.Features.FeatureCollection featureCollection) { var features = new List<Feature>(featureCollection.Count); foreach (var feature in featureCollection) { features.Add(OsmSharpToNTSFeatureConvertor.Convert(feature)); } return features; }
/// <summary> /// Converts boundary type into an osm object. /// </summary> /// <param name="boundary"></param> /// <returns></returns> private static IEnumerable<LineairRing> ConvertBoundary(OsmSharp.IO.Xml.Kml.v2_1.boundaryType[] boundary) { List<LineairRing> rings = new List<LineairRing>(); foreach (OsmSharp.IO.Xml.Kml.v2_1.boundaryType geo in boundary) { rings.Add(KmlFeatureStreamSource.ConvertLinearRing(geo.LinearRing).Geometry as LineairRing); } return rings; }
/// <summary> /// Creates a new polygon filter. /// </summary> public OsmStreamFilterPoly(OsmSharp.Geo.Geometries.LineairRing poly) : base() { if (poly == null) { throw new ArgumentNullException("poly"); } _poly = poly; _box = new GeoCoordinateBox(poly.Coordinates); this.Meta.Add("poly", OsmSharp.Geo.Streams.GeoJson.GeoJsonConverter.ToGeoJson(_poly)); }
/// <summary> /// Generates an immidiate turn. /// </summary> /// <param name="instruction"></param> /// <param name="firstStreetCountTo"></param> /// <param name="firstStreetTo"></param> /// <param name="firstDirection"></param> /// <param name="secondStreetTo"></param> /// <param name="secondDirection"></param> /// <returns></returns> public Instruction GenerateImmidiateTurn(Instruction instruction, int firstStreetCountTo, List<KeyValuePair<string, string>> firstStreetTo, OsmSharp.Tools.Math.Geo.Meta.RelativeDirection firstDirection, List<KeyValuePair<string, string>> secondStreetTo, RelativeDirection secondDirection) { instruction.Text = string.Format("GenerateImmidiateTurn:{0}_{1}_{2}_{3}", firstStreetCountTo, firstDirection, firstDirection.ToString(), secondDirection.ToString()); return instruction; }
/// <summary> /// Converts a polygon. /// </summary> /// <param name="polygon"></param> /// <returns></returns> private static Polygon ConvertPolygon(OsmSharp.Xml.Kml.v2_0_response.Polygon polygon) { LineairRing inner = KmlGeoStreamSource.ConvertLinearRing(polygon.innerBoundaryIs.LinearRing); LineairRing outer = KmlGeoStreamSource.ConvertLinearRing(polygon.outerBoundaryIs.LinearRing); return new Polygon(outer, new LineairRing[] { inner }); }
/// <summary> /// Converts a point into an oms object. /// </summary> /// <param name="point"></param> /// <returns></returns> private static Point ConvertPoint(OsmSharp.Xml.Kml.v2_1.PointType point) { // convert the coordiantes. IList<GeoCoordinate> coordinates = KmlGeoStreamSource.ConvertCoordinates(point.coordinates); // create the point. Point pointGeometry = new Point(coordinates[0]); pointGeometry.Attributes = new SimpleGeometryAttributeCollection(); if (point.targetId != null) { pointGeometry.Attributes.Add("targetId", point.targetId); } pointGeometry.Attributes.Add("altitude", point.altitudeMode); if (point.extrude) { pointGeometry.Attributes.Add("extrude", point.extrude); } if (point.id != null) { pointGeometry.Attributes.Add("id", point.id); } return pointGeometry; }
/// <summary> /// Converts a multipolygon into osm objects. /// </summary> /// <param name="multiPolygon"></param> /// <returns></returns> private static MultiPolygon ConvertMultiPolygon(OsmSharp.Xml.Kml.v2_0_response.MultiPolygon multiPolygon) { return new MultiPolygon(new Polygon[] { KmlGeoStreamSource.ConvertPolygon(multiPolygon.Polygon) }); }
/// <summary> /// Converts a multipoint to osm objects. /// </summary> /// <param name="multiPoint"></param> /// <returns></returns> private static MultiPoint ConvertMultiPoint(OsmSharp.Xml.Kml.v2_0_response.MultiPoint multiPoint) { return new MultiPoint(new Point[] { KmlGeoStreamSource.ConvertPoint(multiPoint.Point) }); }
/// <summary> /// Converts a multilinestring to osm objects. /// </summary> /// <param name="multiLineString"></param> /// <returns></returns> private static MultiLineString ConvertMultiLineString(OsmSharp.Xml.Kml.v2_0_response.MultiLineString multiLineString) { return new MultiLineString(new LineString[] { KmlGeoStreamSource.ConvertLineString(multiLineString.LineString) }); }
/// <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> /// Converts a lineairring into an osm object. /// </summary> /// <param name="linearRing"></param> /// <returns></returns> private static LineairRing ConvertLinearRing(OsmSharp.Xml.Kml.v2_0_response.LinearRing linearRing) { // convert the coordinates. IList<GeoCoordinate> coordinates = KmlGeoStreamSource.ConvertCoordinates(linearRing.coordinates); // create the ring. LineairRing ring = new LineairRing(coordinates); ring.Attributes = new SimpleGeometryAttributeCollection(); ring.Attributes.Add("id", linearRing.id); return ring; }
/// <summary> /// Converts a PBF way into an OsmSharp-way. /// </summary> /// <param name="block"></param> /// <param name="way"></param> /// <returns></returns> internal OsmSharp.Osm.Way ConvertWay(PrimitiveBlock block, OsmSharp.Osm.PBF.Way way) { var simpleWay = new OsmSharp.Osm.Way(); simpleWay.Id = way.id; simpleWay.Nodes = new List<long>(way.refs.Count); long node_id = 0; for (int node_idx = 0; node_idx < way.refs.Count; node_idx++) { node_id = node_id + way.refs[node_idx]; simpleWay.Nodes.Add(node_id); } simpleWay.Tags = new TagsCollection(way.keys.Count); if (way.keys.Count > 0) { for (int tag_idx = 0; tag_idx < way.keys.Count; tag_idx++) { string key = Encoding.UTF8.GetString(block.stringtable.s[(int)way.keys[tag_idx]]); string value = Encoding.UTF8.GetString(block.stringtable.s[(int)way.vals[tag_idx]]); simpleWay.Tags.Add(new Tag(key, value)); } } if (way.info != null) { // add the metadata if any. simpleWay.ChangeSetId = way.info.changeset; simpleWay.TimeStamp = Utilities.FromUnixTime((long)way.info.timestamp * (long)block.date_granularity); simpleWay.UserId = way.info.uid; simpleWay.UserName = Encoding.UTF8.GetString(block.stringtable.s[way.info.user_sid]); simpleWay.Version = (ulong)way.info.version; } simpleWay.Visible = true; return simpleWay; }
/// <summary> /// Converts a PBF way into an OsmSharp-relation. /// </summary> /// <param name="block"></param> /// <param name="relation"></param> /// <returns></returns> internal OsmSharp.Osm.Relation ConvertRelation(PrimitiveBlock block, OsmSharp.Osm.PBF.Relation relation) { var simpleRelation = new OsmSharp.Osm.Relation(); simpleRelation.Id = relation.id; if (relation.types.Count > 0) { simpleRelation.Members = new List<OsmSharp.Osm.RelationMember>(); long member_id = 0; for (int member_idx = 0; member_idx < relation.types.Count; member_idx++) { member_id = member_id + relation.memids[member_idx]; string role = Encoding.UTF8.GetString( block.stringtable.s[relation.roles_sid[member_idx]]); var member = new OsmSharp.Osm.RelationMember(); member.MemberId = member_id; member.MemberRole = role; switch (relation.types[member_idx]) { case Relation.MemberType.NODE: member.MemberType = OsmSharp.Osm.OsmGeoType.Node; break; case Relation.MemberType.WAY: member.MemberType = OsmSharp.Osm.OsmGeoType.Way; break; case Relation.MemberType.RELATION: member.MemberType = OsmSharp.Osm.OsmGeoType.Relation; break; } simpleRelation.Members.Add(member); } } simpleRelation.Tags = new TagsCollection(relation.keys.Count); if (relation.keys.Count > 0) { for (int tag_idx = 0; tag_idx < relation.keys.Count; tag_idx++) { string key = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.keys[tag_idx]]); string value = Encoding.UTF8.GetString(block.stringtable.s[(int)relation.vals[tag_idx]]); simpleRelation.Tags.Add(new Tag(key, value)); } } if (relation.info != null) { // read metadata if any. simpleRelation.ChangeSetId = relation.info.changeset; simpleRelation.TimeStamp = Utilities.FromUnixTime((long)relation.info.timestamp * (long)block.date_granularity); simpleRelation.UserId = relation.info.uid; simpleRelation.UserName = Encoding.UTF8.GetString(block.stringtable.s[relation.info.user_sid]); simpleRelation.Version = (ulong)relation.info.version; } simpleRelation.Visible = true; return simpleRelation; }
/// <summary> /// Convests the polygon to osm objects. /// </summary> /// <param name="polygon"></param> /// <returns></returns> private static Polygon ConvertPolygon(OsmSharp.Xml.Kml.v2_1.PolygonType polygon) { IEnumerable<LineairRing> inners = KmlGeoStreamSource.ConvertBoundary(polygon.innerBoundaryIs); LineairRing outer = KmlGeoStreamSource.ConvertLinearRing(polygon.outerBoundaryIs.LinearRing); return new Polygon(outer, inners ); }
/// <summary> /// Called when the map was first initialized. /// </summary> /// <param name="mapView">Map view.</param> /// <param name="newZoom">New zoom.</param> /// <param name="newTilt">New tilt.</param> /// <param name="newCenter">New center.</param> private void _mapView_MapInitialized(OsmSharp.UI.IMapView mapView, float newZoom, OsmSharp.Units.Angle.Degree newTilt, GeoCoordinate newCenter) { // make sure the center marker stays in place from now on. _centerMarker.MoveWithMap = false; }
/// <summary> /// Solves the problem using a GA. /// </summary> /// <param name="problem"></param> /// <returns></returns> protected override IRoute DoSolve(OsmSharp.Tools.Math.TSP.Problems.IProblem problem) { //int population_size = 10; //if (problem.Size < 100) //{ // population_size = System.Math.Max(problem.Size * 3, 10); // if (problem.Size < 10) // { // population_size = 1; // } //} //if (problem.Size < 1000) //{ // population_size = problem.Size / 4; //} // create the settings. SolverSettings settings = new SolverSettings( _stagnation_count, _population, 1000000000, _eltism, _cross, _mutation); //List<IMutationOperation<List<int>, GeneticProblem, Fitness>> mutators = new List<IMutationOperation<int,GeneticProblem,Fitness>>(); ////mutators.Add(new DefaultMutationOperation()); ////mutators.Add(new BestPlacementMutationOperation()); //mutators.Add(new BestDetailedPlacementMutationOperation()); //List<double> probabilities = new List<double>(); //probabilities.Add(1); ////probabilities.Add(0.5); ////probabilities.Add(0.3); //CombinedMutation<List<int>, GeneticProblem, Fitness> mutation = new CombinedMutation<int,GeneticProblem,Fitness>( // StaticRandomGenerator.Get(), // mutators, // probabilities); ////SequentialContructiveCrossoverOperator cross_over = new SequentialContructiveCrossoverOperator(); //BestDetailedPlacementCrossOverOperation cross_over = new BestDetailedPlacementCrossOverOperation(); ////BestPlacementCrossOverOperation cross_over = new BestPlacementCrossOverOperation(); ////EdgeRecombinationCrossOverOperation cross_over = new EdgeRecombinationCrossOverOperation(); //BestPlacementGenerationOperation generation = new BestPlacementGenerationOperation(); ////RandomGenerationOperation generation = new RandomGenerationOperation(); ISelector<List<int>, GeneticProblem, Fitness> selector = new RandomSelector<List<int>, GeneticProblem, Fitness>(); //ISelector<List<int>, GeneticProblem, Fitness> selector = new TournamentBasedSelector<List<int>, GeneticProblem, Fitness>(75, 0.01); solver = new Solver<List<int>, GeneticProblem, Fitness>( new GeneticProblem(problem), settings, selector, _mutation_operation, _cross_over_operation, _generation_operation, new FitnessCalculator(), true, false); solver.NewFittest += new Solver<List<int>, GeneticProblem, Fitness>.NewFittestDelegate(solver_NewFittest); solver.NewGeneration += new Solver<List<int>, GeneticProblem, Fitness>.NewGenerationDelegate(solver_NewGeneration); List<int> result = new List<int>(solver.Start(null).Genomes); result.Insert(0, 0); return new SimpleAsymmetricRoute(result, true); }
/// <summary> /// Converts a container and it's contents to osm elements. /// </summary> /// <param name="container"></param> private void ConvertContainer(OsmSharp.Xml.Kml.v2_1.ContainerType container) { // get the features. if (container is OsmSharp.Xml.Kml.v2_1.FolderType) { OsmSharp.Xml.Kml.v2_1.FolderType folder = (container as OsmSharp.Xml.Kml.v2_1.FolderType); // items1 are the features. this.ConvertFeatures(folder.Items1); } else if (container is OsmSharp.Xml.Kml.v2_1.DocumentType) { OsmSharp.Xml.Kml.v2_1.DocumentType document = (container as OsmSharp.Xml.Kml.v2_1.DocumentType); // items1 are the features. this.ConvertFeatures(document.Items1); } }
/// <summary> /// Converts a response into an osm object. /// </summary> /// <param name="response"></param> /// <returns></returns> private void ConvertResponse(OsmSharp.Xml.Kml.v2_0_response.Response response) { foreach (object item in response.Items) { if (item is OsmSharp.Xml.Kml.v2_0_response.Document) { this.ConvertDocument(item as OsmSharp.Xml.Kml.v2_0_response.Document); } else if (item is OsmSharp.Xml.Kml.v2_0_response.Folder) { this.ConvertFolder(item as OsmSharp.Xml.Kml.v2_0_response.Folder); } else if (item is OsmSharp.Xml.Kml.v2_0_response.Placemark) { this.ConvertPlacemark(item as OsmSharp.Xml.Kml.v2_0_response.Placemark); } else if (item is OsmSharp.Xml.Kml.v2_0_response.Response) { this.ConvertResponse(item as OsmSharp.Xml.Kml.v2_0_response.Response); } } }
/// <summary> /// Returns all objects with the given bounding box and valid for the given filter; /// </summary> /// <param name="box"></param> /// <param name="filter"></param> /// <returns></returns> public override IList<OsmGeo> Get(GeoCoordinateBox box, OsmSharp.Osm.Filters.Filter filter) { // initialize connection. SQLiteConnection con = this.CreateConnection(); List<OsmGeo> res = new List<OsmGeo>(); // calculate bounding box parameters to query db. long latitude_min = (long)(box.MinLat * 10000000.0); long longitude_min = (long)(box.MinLon * 10000000.0); long latitude_max = (long)(box.MaxLat * 10000000.0); long longitude_max = (long)(box.MaxLon * 10000000.0); // TODO: improve this to allow loading of bigger bb's. uint x_min = lon2x(box.MinLon); uint x_max = lon2x(box.MaxLon); uint y_min = lat2y(box.MinLat); uint y_max = lat2y(box.MaxLat); IList<long> boxes = new List<long>(); for (uint x = x_min; x <= x_max; x++) { for (uint y = y_min; y <= y_max; y++) { boxes.Add(this.xy2tile(x, y)); } } // STEP 1: query nodes table. //id latitude longitude changeset_id visible timestamp tile version //string sql // = "SELECT node.id, node.latitude, node.longitude, node.changeset_id, node.timestamp, node.version, " + // "node.usr, node.usr_id, node.visible FROM node WHERE (tile IN ({4})) AND (visible = 1) AND (latitude BETWEEN {0} AND {1} AND longitude BETWEEN {2} AND {3})"; // remove this nasty BETWEEN operation because it depends on the database (!) what results are returned (including or excluding bounds!!!) string sql = "SELECT node.id, node.latitude, node.longitude, node.changeset_id, node.timestamp, node.version, " + "node.usr, node.usr_id, node.visible FROM node WHERE (tile IN ({4})) AND (visible = 1) AND (latitude >= {0} AND latitude < {1} AND longitude >= {2} AND longitude < {3})"; sql = string.Format(sql, latitude_min.ToString(System.Globalization.CultureInfo.InvariantCulture), latitude_max.ToString(System.Globalization.CultureInfo.InvariantCulture), longitude_min.ToString(System.Globalization.CultureInfo.InvariantCulture), longitude_max.ToString(System.Globalization.CultureInfo.InvariantCulture), this.ConstructIdList(boxes)); // TODO: parameters. var com = new SQLiteCommand(sql); com.Connection = con; SQLiteDataReader reader = ExecuteReader(com); Node node = null; var nodes = new Dictionary<long, Node>(); var nodeIds = new List<long>(); while (reader.Read()) { node = new Node(); node.Id = reader.GetInt64(0); int latitude_int = reader.GetInt32(1); int longitude_int = reader.GetInt32(2); node.ChangeSetId = reader.IsDBNull(3) ? null : (long?)reader.GetInt64(3); node.TimeStamp = reader.IsDBNull(4) ? null : (DateTime?)this.ConvertDateTime(reader.GetInt64(4)); node.Version = reader.IsDBNull(5) ? null : (ulong?)reader.GetInt64(5); node.Latitude = latitude_int / 10000000.0; node.Longitude = longitude_int / 10000000.0; node.UserName = reader.IsDBNull(6) ? null : reader.GetString(6); node.UserId = reader.IsDBNull(7) ? null : (long?)reader.GetInt64(7); node.Visible = reader.IsDBNull(8) ? null : (bool?)reader.GetBoolean(8); nodeIds.Add(node.Id.Value); nodes.Add(node.Id.Value, node); } reader.Close(); // STEP2: Load all node tags. this.LoadNodeTags(nodes); res.AddRange(nodes.Values); // load all ways that contain the nodes that have been found. res.AddRange(this.GetWaysFor(nodeIds)); // get relations containing any of the nodes or ways in the current results-list. List<Relation> relations = new List<Relation>(); HashSet<long> relationIds = new HashSet<long>(); foreach (OsmGeo osmGeo in res) { IList<Relation> relationsFor = this.GetRelationsFor(osmGeo); foreach (Relation relation in relationsFor) { if (!relationIds.Contains(relation.Id.Value)) { relations.Add(relation); relationIds.Add(relation.Id.Value); } } } // recursively add all relations containing other relations as a member. do { res.AddRange(relations); // add previous relations-list. List<Relation> newRelations = new List<Relation>(); foreach (OsmGeo osmGeo in relations) { IList<Relation> relationsFor = this.GetRelationsFor(osmGeo); foreach (Relation relation in relationsFor) { if (!relationIds.Contains(relation.Id.Value)) { newRelations.Add(relation); relationIds.Add(relation.Id.Value); } } } relations = newRelations; } while (relations.Count > 0); if (filter != null) { List<OsmGeo> filtered = new List<OsmGeo>(); foreach (OsmGeo geo in res) { if (filter.Evaluate(geo)) { filtered.Add(geo); } } } return res; }
/// <summary> /// Converts a placemark into an osm object. /// </summary> /// <param name="placemark"></param> /// <returns></returns> private void ConvertPlacemark(OsmSharp.Xml.Kml.v2_0_response.Placemark placemark) { for (int idx = 0; idx < placemark.Items.Length; idx++) { switch (placemark.ItemsElementName[idx]) { case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType1.LineString: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertLineString(placemark.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.LineString)); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType1.MultiGeometry: this.ConvertMultiGeometry(placemark.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.MultiGeometry); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType1.MultiLineString: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertMultiLineString(placemark.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.MultiLineString)); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType1.MultiPoint: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertMultiPoint(placemark.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.MultiPoint)); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType1.MultiPolygon: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertMultiPolygon(placemark.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.MultiPolygon)); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType1.Point: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertPoint(placemark.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.Point)); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType1.Polygon: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertPolygon(placemark.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.Polygon)); break; } } }
/// <summary> /// Conversts a placemark and all it's contents to osm elements. /// </summary> /// <param name="placemark"></param> /// <returns></returns> private void ConvertPlacemark(OsmSharp.Xml.Kml.v2_1.PlacemarkType placemark) { this.ConvertGeometry(placemark.Item1); }
/// <summary> /// Converts a multigeometry to osm objects. /// </summary> /// <param name="multiGeometry"></param> /// <returns></returns> private void ConvertMultiGeometry(OsmSharp.Xml.Kml.v2_0.MultiGeometry multiGeometry) { for (int idx = 0; idx < multiGeometry.Items.Length; idx++) { switch (multiGeometry.ItemsElementName[idx]) { case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType.LineString: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertLineString(multiGeometry.Items[idx] as OsmSharp.Xml.Kml.v2_0.LineString)); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType.MultiGeometry: this.ConvertMultiGeometry(multiGeometry.Items[idx] as OsmSharp.Xml.Kml.v2_0.MultiGeometry); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType.MultiLineString: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertMultiLineString(multiGeometry.Items[idx] as OsmSharp.Xml.Kml.v2_0.MultiLineString)); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType.MultiPoint: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertMultiPoint(multiGeometry.Items[idx] as OsmSharp.Xml.Kml.v2_0.MultiPoint)); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType.MultiPolygon: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertMultiPolygon(multiGeometry.Items[idx] as OsmSharp.Xml.Kml.v2_0.MultiPolygon)); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType.Point: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertPoint(multiGeometry.Items[idx] as OsmSharp.Xml.Kml.v2_0.Point)); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType.Polygon: this.GeometryCollection.Add( KmlGeoStreamSource.ConvertPolygon(multiGeometry.Items[idx] as OsmSharp.Xml.Kml.v2_0.Polygon)); break; } } }
/// <summary> /// Converts the multi geometry to multi osm objects. /// </summary> /// <param name="multiGeometry"></param> /// <returns></returns> private void ConvertMultiGeometry(OsmSharp.Xml.Kml.v2_1.MultiGeometryType multiGeometry) { foreach (OsmSharp.Xml.Kml.v2_1.GeometryType geo in multiGeometry.Items) { this.ConvertGeometry(geo); } }
/// <summary> /// Converts a geometry to a list of osm objects. /// </summary> /// <param name="geometry"></param> /// <returns></returns> private void ConvertGeometry(OsmSharp.Xml.Kml.v2_1.GeometryType geometry) { if (geometry is OsmSharp.Xml.Kml.v2_1.PointType) { this.GeometryCollection.Add( KmlGeoStreamSource.ConvertPoint(geometry as OsmSharp.Xml.Kml.v2_1.PointType)); } else if (geometry is OsmSharp.Xml.Kml.v2_1.LineStringType) { this.GeometryCollection.Add( KmlGeoStreamSource.ConvertLineString(geometry as OsmSharp.Xml.Kml.v2_1.LineStringType)); } else if (geometry is OsmSharp.Xml.Kml.v2_1.LinearRingType) { this.GeometryCollection.Add( KmlGeoStreamSource.ConvertLinearRing(geometry as OsmSharp.Xml.Kml.v2_1.LinearRingType)); } else if (geometry is OsmSharp.Xml.Kml.v2_1.PolygonType) { this.GeometryCollection.Add( KmlGeoStreamSource.ConvertPolygon(geometry as OsmSharp.Xml.Kml.v2_1.PolygonType)); } else if (geometry is OsmSharp.Xml.Kml.v2_1.MultiGeometryType) { this.ConvertMultiGeometry(geometry as OsmSharp.Xml.Kml.v2_1.MultiGeometryType); } }
/// <summary> /// Reads a kml v2.0 object into corresponding geometries. /// </summary> /// <param name="kml"></param> private void ConvertKml(OsmSharp.Xml.Kml.v2_0.kml kml) { this.GeometryCollection.Clear(); if (kml.Item is OsmSharp.Xml.Kml.v2_0.Document) { this.ConvertDocument(kml.Item as OsmSharp.Xml.Kml.v2_0.Document); } else if (kml.Item is OsmSharp.Xml.Kml.v2_0.Folder) { this.ConvertFolder(kml.Item as OsmSharp.Xml.Kml.v2_0.Folder); } else if (kml.Item is OsmSharp.Xml.Kml.v2_0.Placemark) { this.ConvertPlacemark(kml.Item as OsmSharp.Xml.Kml.v2_0.Placemark); } }
/// <summary> /// Converts a folder into an osm object. /// </summary> /// <param name="folder"></param> /// <returns></returns> private void ConvertFolder(OsmSharp.Xml.Kml.v2_0_response.Folder folder) { for (int idx = 0; idx < folder.Items.Length; idx++) { switch (folder.ItemsElementName[idx]) { case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType2.Document: this.ConvertDocument(folder.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.Document); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType2.Folder: this.ConvertFolder(folder.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.Folder); break; case OsmSharp.Xml.Kml.v2_0_response.ItemsChoiceType2.Placemark: this.ConvertPlacemark(folder.Items[idx] as OsmSharp.Xml.Kml.v2_0_response.Placemark); break; } } }
/// <summary> /// Converts all the features into osm elements. /// </summary> /// <param name="featureType"></param> private void ConvertFeatures(OsmSharp.Xml.Kml.v2_1.FeatureType[] featureType) { foreach (OsmSharp.Xml.Kml.v2_1.FeatureType feature in featureType) { this.ConvertFeature(feature); } }
/// <summary> /// Converts a feature and all it's contents to osm elements. /// </summary> /// <param name="feature"></param> /// <returns></returns> private void ConvertFeature(OsmSharp.Xml.Kml.v2_1.FeatureType feature) { if (feature is OsmSharp.Xml.Kml.v2_1.ContainerType) { this.ConvertContainer(feature as OsmSharp.Xml.Kml.v2_1.ContainerType); } else if (feature is OsmSharp.Xml.Kml.v2_1.PlacemarkType) { this.ConvertPlacemark(feature as OsmSharp.Xml.Kml.v2_1.PlacemarkType); } }
/// <summary> /// Converts a document into osm elements. /// </summary> /// <param name="document"></param> /// <returns></returns> private void ConvertDocument(OsmSharp.Xml.Kml.v2_0.Document document) { for (int idx = 0; idx < document.Items.Length; idx++) { switch (document.ItemsElementName[idx]) { case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType3.Document: this.ConvertDocument(document.Items[idx] as OsmSharp.Xml.Kml.v2_0.Document); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType3.Folder: this.ConvertFolder(document.Items[idx] as OsmSharp.Xml.Kml.v2_0.Folder); break; case OsmSharp.Xml.Kml.v2_0.ItemsChoiceType3.Placemark: this.ConvertPlacemark(document.Items[idx] as OsmSharp.Xml.Kml.v2_0.Placemark); break; } } }
/// <summary> /// Converts the given OsmSharp feature into an NTS feature. /// </summary> /// <param name="feature"></param> /// <returns></returns> public static Feature Convert(OsmSharp.Geo.Features.Feature feature) { if (feature == null) { throw new ArgumentNullException("feature"); } var geometryFactory = new NetTopologySuite.Geometries.GeometryFactory(); if(feature.Geometry is OsmSharp.Geo.Geometries.Polygon) { // a polygon. var polygon = (feature.Geometry as OsmSharp.Geo.Geometries.Polygon); var holes = polygon.Holes.Select((hole) => { return (ILinearRing)geometryFactory.CreateLinearRing(hole.Coordinates.Select((coordinate) => { return new Coordinate(coordinate.Longitude, coordinate.Latitude); }).ToArray()); }).ToArray(); var shell = geometryFactory.CreateLinearRing(polygon.Ring.Coordinates.Select((coordinate) => { return new Coordinate(coordinate.Longitude, coordinate.Latitude); }).ToArray()); return new Feature(geometryFactory.CreatePolygon(shell, holes), OsmSharpToNTSFeatureConvertor.Convert(feature.Attributes)); } else if (feature.Geometry is OsmSharp.Geo.Geometries.LineairRing) { // a lineair ring. var lineairRing = (feature.Geometry as OsmSharp.Geo.Geometries.LineairRing); var coordinates = lineairRing.Coordinates.Select((coordinate) => { return new Coordinate(coordinate.Longitude, coordinate.Latitude); }); return new Feature(geometryFactory.CreateLinearRing(coordinates.ToArray()), OsmSharpToNTSFeatureConvertor.Convert(feature.Attributes)); } else if (feature.Geometry is OsmSharp.Geo.Geometries.LineString) { // a line string. var lineString = (feature.Geometry as OsmSharp.Geo.Geometries.LineString); var coordinates = lineString.Coordinates.Select((coordinate) => { return new Coordinate(coordinate.Longitude, coordinate.Latitude); }); return new Feature(geometryFactory.CreateLineString(coordinates.ToArray()), OsmSharpToNTSFeatureConvertor.Convert(feature.Attributes)); } else if (feature.Geometry is OsmSharp.Geo.Geometries.Point) { // a point. var point = (feature.Geometry as OsmSharp.Geo.Geometries.Point); return new Feature(geometryFactory.CreatePoint(new Coordinate(point.Coordinate.Longitude, point.Coordinate.Latitude)), OsmSharpToNTSFeatureConvertor.Convert(feature.Attributes)); } else if (feature.Geometry is OsmSharp.Geo.Geometries.MultiLineString) { // a multi line string. throw new NotSupportedException("A MultiLineString is not supported."); } else if (feature.Geometry is OsmSharp.Geo.Geometries.MultiPoint) { // a multi point. throw new NotSupportedException("A MultiPoint is not supported."); } else if (feature.Geometry is OsmSharp.Geo.Geometries.MultiPolygon) { // a multi polygon. throw new NotSupportedException("A MultiPolygon is not supported."); } throw new ArgumentOutOfRangeException("Geometry not recognized: {0}", feature.ToString()); }