/// <summary> /// Fills the given simple scene with objects inside the given view and for the given zoomFactor. /// </summary> /// <param name="scene"></param> /// <param name="view"></param> /// <param name="zoomFactor"></param> public void Get(Scene2DSimple scene, View2D view, float zoomFactor) { lock (_stream) { // make sure there is only synchonous access to the stream. // check what is in the non-simplified scene. IScene2DPrimitivesSource simpleSource = this.GetNonSimplifiedStream(); simpleSource.Get(scene, view, zoomFactor); // check what index this zoomfactor is for. int idx = this.SearchForScene(zoomFactor); if (idx >= 0) // the index was found. { if (!_loadedScenes.TryGetValue(idx, out simpleSource)) // the scene was not found. { long position = _index.SceneIndexes [idx]; _stream.Seek(position, SeekOrigin.Begin); LimitedStream stream = new LimitedStream(_stream); //Scene2DRTreeSerializer serializer = new Scene2DRTreeSerializer(true); //simpleSource = new Scene2DPrimitivesSource(serializer.Deserialize(stream)); OsmSharp.UI.Renderer.Scene.Storage.Styled.Scene2DStyledSerializer serializer = new Styled.Scene2DStyledSerializer(); simpleSource = serializer.Deserialize(stream, true); _loadedScenes.Add(idx, simpleSource); } simpleSource.Get(scene, view, zoomFactor); OsmSharp.Logging.Log.TraceEvent("Scene2DLayeredSource", System.Diagnostics.TraceEventType.Verbose, string.Format("Deserialized from scene at zoom {0} and idx {1} synchronized.", zoomFactor, idx)); } } }
/// <summary> /// Builds the scene. /// </summary> /// <param name="map"></param> /// <param name="zoomFactor"></param> /// <param name="center"></param> /// <param name="view"></param> private void BuildScene(Map map, float zoomFactor, GeoCoordinate center, View2D view) { // build the boundingbox. var viewBox = view.OuterBox; var box = new GeoCoordinateBox(map.Projection.ToGeoCoordinates(viewBox.Min [0], viewBox.Min [1]), map.Projection.ToGeoCoordinates(viewBox.Max [0], viewBox.Max [1])); var zoomLevel = (int)map.Projection.ToZoomLevel(zoomFactor); if (_lastBox != null && _lastBox.Contains(box) && zoomLevel == _lastZoom) { return; } _lastBox = box; _lastZoom = zoomLevel; // reset the scene. if (_scene2DSimple == null) { _scene2DSimple = new Scene2DSimple(); } _scene2DSimple.Clear(); // get from the index. this.Scene.BackColor = SimpleColor.FromKnownColor(KnownColor.White).Value; lock (_sync) { _index.Get(_scene2DSimple, view, zoomFactor); } }
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--; } } }