/// <summary> /// Compares what is in the complete list against the objects in the reference source. /// </summary> /// <param name="expected"></param> /// <param name="actual"></param> private void Compare(MemoryDataSource expected, List <CompleteOsmGeo> actual) { List <CompleteOsmGeo> exectedList = new List <CompleteOsmGeo>(); foreach (Node node in expected.GetNodes()) { CompleteNode completeNode = CompleteNode.CreateFrom(node); if (completeNode != null) { exectedList.Add(completeNode); } } foreach (Way way in expected.GetWays()) { CompleteWay completeWay = CompleteWay.CreateFrom(way, expected); if (completeWay != null) { exectedList.Add(completeWay); } } foreach (Relation relation in expected.GetRelations()) { CompleteRelation completeRelation = CompleteRelation.CreateFrom(relation, expected); if (completeRelation != null) { exectedList.Add(completeRelation); } } ComparisonHelpers.CompareComplete(exectedList, actual); }
/// <summary> /// Interprets an OSM-object and returns the correctponding geometry. /// </summary> /// <param name="simpleOsmGeo"></param> /// <param name="data"></param> /// <returns></returns> public virtual GeometryCollection Interpret(OsmGeo simpleOsmGeo, IDataSourceReadOnly data) { switch (simpleOsmGeo.Type) { case OsmGeoType.Node: return(this.Interpret(CompleteNode.CreateFrom(simpleOsmGeo as Node))); case OsmGeoType.Way: return(this.Interpret(CompleteWay.CreateFrom(simpleOsmGeo as Way, data))); case OsmGeoType.Relation: return(this.Interpret(CompleteRelation.CreateFrom(simpleOsmGeo as Relation, data))); } throw new ArgumentOutOfRangeException(); }
/// <summary> /// Translates the given OSM objects into corresponding geometries. /// </summary> /// <param name="scene"></param> /// <param name="projection"></param> /// <param name="source"></param> /// <param name="osmGeo"></param> public virtual void Translate(Scene2D scene, IProjection projection, IDataSourceReadOnly source, OsmGeo osmGeo) { switch (osmGeo.Type) { case OsmGeoType.Node: this.Translate(scene, projection, CompleteNode.CreateFrom(osmGeo as Node)); break; case OsmGeoType.Way: this.Translate(scene, projection, CompleteWay.CreateFrom(osmGeo as Way, source)); break; case OsmGeoType.Relation: this.Translate(scene, projection, CompleteRelation.CreateFrom(osmGeo as Relation, source)); break; } }
public void Scene2DSimpleSerializeDeserializeTest() { // create the MapCSS image source. var imageSource = new MapCSSDictionaryImageSource(); // load mapcss style interpreter. var mapCSSInterpreter = new MapCSSInterpreter( Assembly.GetExecutingAssembly().GetManifestResourceStream( "OsmSharp.UI.Test.Unittests.Data.MapCSS.test.mapcss"), imageSource); // initialize the data source. var xmlSource = new XmlOsmStreamSource( Assembly.GetExecutingAssembly().GetManifestResourceStream( "OsmSharp.UI.Test.Unittests.Data.test.osm")); IEnumerable <OsmGeo> dataSource = xmlSource; MemoryDataSource source = MemoryDataSource.CreateFrom(xmlSource); // get data. var scene = new Scene2DSimple(); var projection = new WebMercator(); GeoCoordinateBox box = null; foreach (var osmGeo in dataSource) { CompleteOsmGeo completeOsmGeo = null; switch (osmGeo.Type) { case OsmGeoType.Node: completeOsmGeo = CompleteNode.CreateFrom(osmGeo as Node); break; case OsmGeoType.Way: completeOsmGeo = CompleteWay.CreateFrom(osmGeo as Way, source); break; case OsmGeoType.Relation: completeOsmGeo = CompleteRelation.CreateFrom(osmGeo as Relation, source); break; } // update box. if (completeOsmGeo != null) { if (box == null) { box = completeOsmGeo.BoundingBox; } else if (completeOsmGeo.BoundingBox != null) { box = box + completeOsmGeo.BoundingBox; } } // translate each object into scene object. mapCSSInterpreter.Translate(scene, projection, source, osmGeo as OsmGeo); } // create the stream. var stream = new MemoryStream(); scene.Serialize(stream, false); // deserialize the stream. IScene2DPrimitivesSource sceneSource = Scene2DSimple.Deserialize(stream, false); if (box != null) { // query both and get the same results. int counter = 100; var rand = new Random(); while (counter > 0) { var queryBox = new GeoCoordinateBox( box.GenerateRandomIn(rand), box.GenerateRandomIn(rand)); var zoomFactor = (float)projection.ToZoomFactor(15); View2D testView = View2D.CreateFromBounds( projection.LatitudeToY(queryBox.MaxLat), projection.LongitudeToX(queryBox.MinLon), projection.LatitudeToY(queryBox.MinLat), projection.LongitudeToX(queryBox.MaxLon)); var testScene = new Scene2DSimple(); sceneSource.Get(testScene, testView, zoomFactor); // var resultIndex = new HashSet<Scene2DPrimitive>(testScene.Get(testView, zoomFactor)); // var resultReference = new HashSet<Scene2DPrimitive>(scene.Get(testView, zoomFactor)); //Assert.AreEqual(resultReference.Count, resultIndex.Count); //foreach (var data in resultIndex) //{ // Assert.IsTrue(resultReference.Contains(data)); //} //foreach (var data in resultReference) //{ // Assert.IsTrue(resultIndex.Contains(data)); //} counter--; } } }
/// <summary> /// Moves to the next object. /// </summary> /// <returns></returns> public override bool MoveNext() { if (!_cachingDone) { // first seek & cache. this.Seek(); this.CacheRelations(); _cachingDone = true; } while (_simpleSource.MoveNext()) { // there is data. OsmGeo currentSimple = _simpleSource.Current(); if (currentSimple.Id == 198214128 || currentSimple.Id == 1014892489) { System.Diagnostics.Debug.WriteLine(""); } switch (currentSimple.Type) { case OsmGeoType.Node: // create complete version. _current = CompleteNode.CreateFrom(currentSimple as Node); if (_nodesToInclude.Contains(currentSimple.Id.Value)) { // node needs to be cached. _dataCache.AddNode(currentSimple as Node); _nodesToInclude.Remove(currentSimple.Id.Value); } break; case OsmGeoType.Way: // create complete way. _current = CompleteWay.CreateFrom(currentSimple as Way, _dataCache); if (_waysToInclude.Contains(currentSimple.Id.Value)) { // keep the way because it is needed later on. _dataCache.AddWay(currentSimple as Way); _waysToInclude.Remove(currentSimple.Id.Value); } else { // only report that the nodes have been used when the way can be let-go. Way way = currentSimple as Way; if (way.Nodes != null) { // report usage. way.Nodes.ForEach(x => this.ReportNodeUsage(x)); } } break; case OsmGeoType.Relation: // create complate relation. _current = CompleteRelation.CreateFrom(currentSimple as Relation, _dataCache); if (!_relationsToInclude.Contains(currentSimple.Id.Value)) { // only report relation usage when the relation can be let go. Relation relation = currentSimple as Relation; if (relation.Members != null) { foreach (RelationMember member in relation.Members) { switch (member.MemberType.Value) { case OsmGeoType.Node: this.ReportNodeUsage(member.MemberId.Value); break; case OsmGeoType.Way: this.ReportWayUsage(member.MemberId.Value); break; case OsmGeoType.Relation: this.ReportRelationUsage(member.MemberId.Value); break; } } } } break; } if (_current != null) { // only return complete objects. return(true); } } return(false); }