public void TestSimpleWebMercatorZoomLevel() { var mercator = new WebMercator(); for (int orignalLevel = 0; orignalLevel < 20; orignalLevel++) { double zoomFactor = mercator.ToZoomFactor(orignalLevel); double zoomLevel = mercator.ToZoomLevel(zoomFactor); Assert.AreEqual(orignalLevel, zoomLevel, 0.001); } }
/// <summary> /// Creates the stream processor associated with this command. /// </summary> /// <returns></returns> public override ProcessorBase CreateProcessor() { // mapCSS stream. Stream mapCSSStream = (new FileInfo(this.MapCSS)).OpenRead(); MapCSSFile mapCSSFile = MapCSSFile.FromStream(mapCSSStream); // scene stream. Stream sceneStream = (new FileInfo(this.SceneFile)).Open(FileMode.OpenOrCreate); // create web mercator. IProjection projection = new WebMercator(); // create scene. Scene2D scene = new Scene2D(projection, this.ZoomLevelCutoffs.ToList()); return new ProcessorCompleteTarget(new StyleOsmStreamSceneStreamTarget( new MapCSSInterpreter(mapCSSFile, new MapCSSDictionaryImageSource()), scene, projection, sceneStream)); }
public void TestEmptyCSS() { // create 'test' objects. Node node1 = new Node(); node1.Id = 1; node1.Latitude = 1; node1.Longitude = 1; Node node2 = new Node(); node2.Id = 2; node2.Latitude = 2; node2.Longitude = 2; Way way = new Way(); way.Id = 1; way.Nodes = new List<long>(); way.Nodes.Add(1); way.Nodes.Add(2); // create the datasource. MemoryDataSource dataSource = new MemoryDataSource(); dataSource.AddNode(node1); dataSource.AddNode(node2); dataSource.AddWay(way); // create the projection and scene objects. var mercator = new WebMercator(); Scene2D scene = new Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); // create the interpreter. MapCSSInterpreter interpreter = new MapCSSInterpreter(string.Empty, new MapCSSDictionaryImageSource()); interpreter.Translate(scene, mercator, dataSource, node1); interpreter.Translate(scene, mercator, dataSource, node2); interpreter.Translate(scene, mercator, dataSource, way); // test the scene contents. Assert.AreEqual(0, scene.Count); Assert.AreEqual(SimpleColor.FromKnownColor(KnownColor.Black).Value, scene.BackColor); }
public void TestSimpleWebMercator() { // TODO: stabalize the webmercator projection numerically for lower zoom levels (0-9). var mercator = new WebMercator(); for (int zoomLevel = 10; zoomLevel <= 25; zoomLevel++) { var tile = Tile.CreateAroundLocation(new GeoCoordinate(0, 0), zoomLevel); var topleft = mercator.ToPixel(tile.Box.TopLeft); var bottomright = mercator.ToPixel(tile.Box.BottomRight); var scaleFactor = mercator.ToZoomFactor(zoomLevel); Assert.AreEqual(-256, (topleft[0] - bottomright[0]) * scaleFactor, 0.01); Assert.AreEqual(-256, (topleft[1] - bottomright[1]) * scaleFactor, 0.01); } var coordinate = new GeoCoordinate(51.26337, 4.78739); var projected = mercator.ToPixel(coordinate); var reProjected = mercator.ToGeoCoordinates(projected[0], projected[1]); Assert.AreEqual(coordinate.Longitude, reProjected.Longitude, 0.0001); Assert.AreEqual(coordinate.Latitude, reProjected.Latitude, 0.0001); }
public void TestMapCSSSimpleEval() { MemoryDataSource source = new MemoryDataSource( Node.Create(1, 0, 0), Node.Create(2, 1, 0), Node.Create(3, 0, 1), Way.Create(1, new TagsCollection( Tag.Create("width", "10")), 1, 2, 3, 1)); // test closed way. string css = "way { " + " width: eval(\"tag('width')\"); " + " color: green; " + "} "; // create the projection and scene objects. var mercator = new WebMercator(); Scene2D scene = new Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); // create the projection and scene objects. scene = new Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); // create the interpreter. MapCSSInterpreter interpreter = new MapCSSInterpreter(css, new MapCSSDictionaryImageSource()); interpreter.Translate(scene, mercator, source, source.GetWay(1)); // test the scene contents. Assert.AreEqual(1, scene.Count); Primitive2D primitive = scene.Get(0); Assert.IsInstanceOf<Line2D>(primitive); Line2D line = (primitive as Line2D); Assert.AreEqual(10, line.Width); }
public void TestMapCSSArea() { MemoryDataSource source = new MemoryDataSource( Node.Create(1, 0, 0), Node.Create(2, 1, 0), Node.Create(3, 0, 1), Way.Create(1, new TagsCollection( Tag.Create("area", "yes")), 1, 2, 3, 1)); // test closed way. string css = "area { " + " fill-color: black; " + "} "; // create the projection and scene objects. var mercator = new WebMercator(); Scene2D scene = new Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); // create the projection and scene objects. scene = new Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); // create the interpreter. MapCSSInterpreter interpreter = new MapCSSInterpreter(css, new MapCSSDictionaryImageSource()); interpreter.Translate(scene, mercator, source, source.GetWay(1)); // test the scene contents. Assert.AreEqual(1, scene.Count); Primitive2D primitive = scene.Get(0); Assert.IsInstanceOf<Polygon2D>(primitive); }
public void TestCanvasJOSMSettingsCSS() { // create CSS. string css = "canvas { " + "background-color: white; " + "default-points: true; " + // adds default points for every node (color: black, size: 2). "default-lines: true; " + // adds default lines for every way (color: red, width: 1). "} "; // create 'test' objects. Node node1 = new Node(); node1.Id = 1; node1.Latitude = 1; node1.Longitude = 1; Node node2 = new Node(); node2.Id = 2; node2.Latitude = 2; node2.Longitude = 2; Way way = new Way(); way.Id = 1; way.Nodes = new List<long>(); way.Nodes.Add(1); way.Nodes.Add(2); // create the datasource. MemoryDataSource dataSource = new MemoryDataSource(); dataSource.AddNode(node1); dataSource.AddNode(node2); dataSource.AddWay(way); // create the projection and scene objects. var mercator = new WebMercator(); Scene2D scene = new Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); // create the interpreter. MapCSSInterpreter interpreter = new MapCSSInterpreter(css, new MapCSSDictionaryImageSource()); interpreter.Translate(scene, mercator, dataSource, node1); interpreter.Translate(scene, mercator, dataSource, node2); interpreter.Translate(scene, mercator, dataSource, way); // test the scene contents. Assert.AreEqual(3, scene.Count); Assert.AreEqual(SimpleColor.FromKnownColor(KnownColor.White).Value, scene.BackColor); // test the scene point 1. Primitive2D primitive = scene.Get(0); Assert.IsNotNull(primitive); Assert.IsInstanceOf<Primitive2D>(primitive); Point2D pointObject = primitive as Point2D; Assert.AreEqual(2, pointObject.Size); Assert.AreEqual(SimpleColor.FromKnownColor(KnownColor.Black).Value, pointObject.Color); Assert.AreEqual(mercator.LongitudeToX(1), pointObject.X); Assert.AreEqual(mercator.LatitudeToY(1), pointObject.Y); // test the scene point 2. primitive = scene.Get(1); Assert.IsNotNull(primitive); Assert.IsInstanceOf<Point2D>(primitive); pointObject = primitive as Point2D; Assert.AreEqual(2, pointObject.Size); Assert.AreEqual(SimpleColor.FromKnownColor(KnownColor.Black).Value, pointObject.Color); Assert.AreEqual(mercator.LongitudeToX(2), pointObject.X); Assert.AreEqual(mercator.LatitudeToY(2), pointObject.Y); // test the scene line 2. primitive = scene.Get(2); Assert.IsNotNull(primitive); Assert.IsInstanceOf<Line2D>(primitive); Line2D line = primitive as Line2D; Assert.AreEqual(1, line.Width); Assert.AreEqual(SimpleColor.FromKnownColor(KnownColor.Red).Value, line.Color); Assert.IsNotNull(line.X); Assert.IsNotNull(line.Y); Assert.AreEqual(2, line.X.Length); Assert.AreEqual(2, line.Y.Length); Assert.AreEqual(mercator.LongitudeToX(1), line.X[0]); Assert.AreEqual(mercator.LatitudeToY(1), line.Y[0]); Assert.AreEqual(mercator.LongitudeToX(2), line.X[1]); Assert.AreEqual(mercator.LatitudeToY(2), line.Y[1]); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.sampleControl1.Center = new double[] { 534463.21f, 6633094.69f }; //this.sampleControl1.Center = new float[] { 0f, 0f }; this.sampleControl1.ZoomFactor = 1; // initialize a test-scene. var scene2D = new Scene2DSimple(); scene2D.BackColor = Color.White.ToArgb(); scene2D.AddPoint(float.MinValue, float.MaxValue, 0, 0, Color.Blue.ToArgb(), 1); bool fill = false; int color = Color.White.ToArgb(); int width = 1; scene2D.AddPolygon(float.MinValue, float.MaxValue, new double[] { 50, -80, 70 }, new double[] { 20, -10, -70 }, color, width, fill); // load test osm file. Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream( "OsmSharp.WinForms.UI.Sample.test.osm"); var xmlDataProcessorSource = new XmlOsmStreamSource(stream); ICollection<OsmGeo> osmList = new List<OsmGeo>(xmlDataProcessorSource); // build a scene using spherical mercator. IProjection sphericalMercator = new WebMercator(); var nodes = new Dictionary<long, GeoCoordinate>(); foreach (OsmGeo simpleOsmGeo in osmList) { if (simpleOsmGeo is Node) { var simplenode = (simpleOsmGeo as Node); double[] point = sphericalMercator.ToPixel( simplenode.Latitude.Value, simplenode.Longitude.Value); nodes.Add(simplenode.Id.Value, new GeoCoordinate(simplenode.Latitude.Value, simplenode.Longitude.Value)); scene2D.AddPoint(float.MinValue, float.MaxValue, (float)point[0], (float)point[1], Color.Yellow.ToArgb(), 2); } else if (simpleOsmGeo is Way) { var way = (simpleOsmGeo as Way); var x = new List<double>(); var y = new List<double>(); if (way.Nodes != null) { for (int idx = 0; idx < way.Nodes.Count; idx++) { GeoCoordinate nodeCoords; if (nodes.TryGetValue(way.Nodes[idx], out nodeCoords)) { x.Add((float) sphericalMercator.LongitudeToX(nodeCoords.Longitude)); y.Add((float) sphericalMercator.LatitudeToY(nodeCoords.Latitude)); } } } if (x.Count > 0) { scene2D.AddLine(float.MinValue, float.MaxValue, x.ToArray(), y.ToArray(), Color.Blue.ToArgb(), 2); } } } this.sampleControl1.Scene = scene2D; this.sampleControl1.Invalidate(); }
/// <summary> /// Tests rendering the given serialized scene. /// </summary> /// <param name="stream"></param> /// <param name="box"></param> /// <param name="testCount"></param> public static void TestRenderScene(Stream stream, GeoCoordinateBox box, int testCount) { WebMercator projection = new WebMercator(); // build a map. Map map = new Map(); IScene2DPrimitivesSource sceneSource = Scene2DLayered.Deserialize(stream, true); LayerScene layerScene = map.AddLayerScene(sceneSource); // build the target to render to. Bitmap imageTarget = new Bitmap(TargetWidth, TargetHeight); Graphics target = Graphics.FromImage(imageTarget); target.SmoothingMode = SmoothingMode.HighQuality; target.PixelOffsetMode = PixelOffsetMode.HighQuality; target.CompositingQuality = CompositingQuality.HighQuality; target.InterpolationMode = InterpolationMode.HighQualityBicubic; MapRenderer<Graphics> mapRenderer = new MapRenderer<Graphics>( new GraphicsRenderer2D()); // render the map. PerformanceInfoConsumer performanceInfo = new PerformanceInfoConsumer("Scene2DLayeredRendering"); performanceInfo.Start(); performanceInfo.Report("Rendering {0} random images...", testCount); while (testCount > 0) { // randomize view. int zoom = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(10) + 10; GeoCoordinate center = box.GenerateRandomIn(); View2D view = mapRenderer.Create(TargetWidth, TargetHeight, map, (float)projection.ToZoomFactor(zoom), center, false, true); layerScene.ViewChanged(map, (float)projection.ToZoomFactor(zoom), center, view); mapRenderer.Render(target, map, view); if (WriteResults) { imageTarget.Save(Guid.NewGuid().ToString() + ".png", ImageFormat.Png); } testCount--; } performanceInfo.Stop(); }
/// <summary> /// Renders the given data onto a 100x100 image using bounds around null-island (1,1,-1,-1) and the MapCSS definition. /// </summary> /// <param name="dataSource"></param> /// <param name="mapCSS"></param> /// <returns></returns> private Bitmap Render(IDataSourceReadOnly dataSource, string mapCSS) { // create projection. WebMercator projection = new WebMercator(); double[] topLeft = projection.ToPixel(new Math.Geo.GeoCoordinate(1, 1)); double[] bottomRight = projection.ToPixel(new Math.Geo.GeoCoordinate(-1, -1)); // create view (this comes down to (1,1,-1,-1) for a size of 100x100). View2D view = View2D.CreateFromBounds(bottomRight[1], topLeft[0], topLeft[1], bottomRight[0]); //View2D view = View2D.CreateFrom(0, 0, 100, 100, 1.0 / 200.0, false, true); // create graphics Bitmap rendering = new Bitmap(100, 100); Graphics renderingGraphics = Graphics.FromImage(rendering); renderingGraphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; renderingGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; renderingGraphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; renderingGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // create renderer. GraphicsRenderer2D graphicsRenderer = new GraphicsRenderer2D(); MapRenderer<Graphics> renderer = new MapRenderer<Graphics>(graphicsRenderer); // create map. OsmSharp.UI.Map.Map map = new OsmSharp.UI.Map.Map(); map.AddLayer(new LayerOsm(dataSource, new MapCSSInterpreter(mapCSS), projection)); // notify the map that there was a view change! map.ViewChanged((float)view.CalculateZoom(100, 100), new Math.Geo.GeoCoordinate(0, 0), view); // ... and finally do the actual rendering. renderer.Render(Graphics.FromImage(rendering), map, view); //rendering.Save(@"c:\temp\rendering.bmp"); return rendering; }
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 Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); var projection = new WebMercator(); GeoCoordinateBox box = null; foreach (var osmGeo in dataSource) { ICompleteOsmGeo completeOsmGeo = null; switch (osmGeo.Type) { case OsmGeoType.Node: completeOsmGeo = osmGeo as Node; if(completeOsmGeo.Tags == null) { // make sure every node has a tags collection. completeOsmGeo.Tags = new TagsCollection(); } 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. TagsCollectionBase metaTags = new TagsCollection(); metaTags.Add("SomeTestKey", "SomeTestValue"); var stream = new MemoryStream(); scene.Serialize(stream, true, metaTags); // deserialize the stream. metaTags = null; stream.Seek(0, SeekOrigin.Begin); IPrimitives2DSource sceneSource = Scene2D.Deserialize(stream, true, out metaTags); // test meta tags. Assert.IsTrue(metaTags.ContainsKeyValue("SomeTestKey", "SomeTestValue")); 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 Scene2D(new OsmSharp.Math.Geo.Projections.WebMercator(), 16); IEnumerable<Primitive2D> primitives = sceneSource.Get(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--; } } }
public void TestMapCSSClosedWay() { // tests map css interpretation of a closed way marked as an area. MemoryDataSource source = new MemoryDataSource( Node.Create(1, 0, 0), Node.Create(2, 1, 0), Node.Create(3, 0, 1), Way.Create(1, new SimpleTagsCollection( Tag.Create("area", "yes")), 1, 2, 3, 1)); // test closed way. string css = "way[area] { " + " fill-color: black; " + "} "; // create the projection and scene objects. var mercator = new WebMercator(); Scene2D scene = new Scene2DSimple(); // create the interpreter. MapCSSInterpreter interpreter = new MapCSSInterpreter(css, new MapCSSDictionaryImageSource()); interpreter.Translate(scene, mercator, source, source.GetWay(1)); // test the scene contents. Assert.AreEqual(1, scene.Count); List<IScene2DPrimitive> primitives = scene.Get(0); Assert.IsNotNull(primitives); Assert.AreEqual(1, primitives.Count); IScene2DPrimitive primitive = primitives[0]; Assert.IsInstanceOf<Polygon2D>(primitive); }
public void TestCanvasSettingsCSS() { // create CSS. string css = "canvas { " + "fill-color: green; " + "} "; // create 'test' objects. Node node1 = new Node(); node1.Id = 1; node1.Latitude = 1; node1.Longitude = 1; Node node2 = new Node(); node2.Id = 2; node2.Latitude = 2; node2.Longitude = 2; Way way = new Way(); way.Id = 1; way.Nodes = new List<long>(); way.Nodes.Add(1); way.Nodes.Add(2); // create the datasource. MemoryDataSource dataSource = new MemoryDataSource(); dataSource.AddNode(node1); dataSource.AddNode(node2); dataSource.AddWay(way); // create the projection and scene objects. var mercator = new WebMercator(); Scene2D scene = new Scene2DSimple(); // create the interpreter. MapCSSInterpreter interpreter = new MapCSSInterpreter(css, new MapCSSDictionaryImageSource()); interpreter.Translate(scene, mercator, dataSource, node1); interpreter.Translate(scene, mercator, dataSource, node2); interpreter.Translate(scene, mercator, dataSource, way); // test the scene contents. Assert.AreEqual(0, scene.Count); Assert.AreEqual(SimpleColor.FromKnownColor(KnownColor.Green).Value, scene.BackColor); }