示例#1
0
        /// <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 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--;
                }
            }
        }
示例#3
0
        public void View2DTestCreateFrom()
        {
            View2D view = View2D.CreateFrom(0, 0, 200, 200, 1, false, false);

            // test result.
            Assert.AreEqual(view.LeftTop[0], -100);
            Assert.AreEqual(view.RightTop[0], 100);
            Assert.AreEqual(view.RightBottom[1], -100);
            Assert.AreEqual(view.RightTop[1], 100);
            Assert.IsTrue(view.Contains(0, 0));
            double[] topLeft = view.FromViewPort(1000, 1000, 0, 0);
            Assert.AreEqual(topLeft[0], -100);
            Assert.AreEqual(topLeft[1], -100);
            double[] bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            Assert.AreEqual(bottomRight[0], 100);
            Assert.AreEqual(bottomRight[1], 100);

            double[] viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.RightTop[1]);
            Assert.AreEqual(0, viewTopLeft[0]);
            Assert.AreEqual(1000, viewTopLeft[1]);
            double[] viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            Assert.AreEqual(1000, viewBottomRight[0]);
            Assert.AreEqual(0, viewBottomRight[1]);

            view = View2D.CreateFrom(0, 0, 200, 200, 1, true, false);

            // test result.
            Assert.AreEqual(view.LeftTop[0], 100);
            Assert.AreEqual(view.RightTop[0], -100);
            Assert.AreEqual(view.RightBottom[1], -100);
            Assert.AreEqual(view.RightTop[1], 100);
            Assert.IsTrue(view.Contains(0, 0));
            topLeft = view.FromViewPort(1000, 1000, 0, 0);
            Assert.AreEqual(topLeft[0], 100);
            Assert.AreEqual(topLeft[1], -100);
            bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            Assert.AreEqual(bottomRight[0], -100);
            Assert.AreEqual(bottomRight[1], 100);

            viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]);
            Assert.AreEqual(0, viewTopLeft[0]);
            Assert.AreEqual(1000, viewTopLeft[1]);
            viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            Assert.AreEqual(1000, viewBottomRight[0]);
            Assert.AreEqual(0, viewBottomRight[1]);

            view = View2D.CreateFrom(0, 0, 200, 200, 1, false, true);

            // test result.
            Assert.AreEqual(view.LeftTop[0], -100);
            Assert.AreEqual(view.RightTop[0], 100);
            Assert.AreEqual(view.RightBottom[1], 100);
            Assert.AreEqual(view.RightTop[1], -100);
            Assert.IsTrue(view.Contains(0, 0));
            topLeft = view.FromViewPort(1000, 1000, 0, 0);
            Assert.AreEqual(topLeft[0], -100);
            Assert.AreEqual(topLeft[1], 100);
            bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            Assert.AreEqual(bottomRight[0], 100);
            Assert.AreEqual(bottomRight[1], -100);

            viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]);
            Assert.AreEqual(0, viewTopLeft[0]);
            Assert.AreEqual(1000, viewTopLeft[1]);
            viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            Assert.AreEqual(1000, viewBottomRight[0]);
            Assert.AreEqual(0, viewBottomRight[1]);

            view = View2D.CreateFrom(0, 0, 200, 200, 1, true, true);

            // test result.
            Assert.AreEqual(view.LeftTop[0], 100);
            Assert.AreEqual(view.RightTop[0], -100);
            Assert.AreEqual(view.RightBottom[1], 100);
            Assert.AreEqual(view.RightTop[1], -100);
            Assert.IsTrue(view.Contains(0, 0));
            topLeft = view.FromViewPort(1000, 1000, 0, 0);
            Assert.AreEqual(topLeft[0], 100);
            Assert.AreEqual(topLeft[1], 100);
            bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            Assert.AreEqual(bottomRight[0], -100);
            Assert.AreEqual(bottomRight[1], -100);

            viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]);
            Assert.AreEqual(0, viewTopLeft[0]);
            Assert.AreEqual(1000, viewTopLeft[1]);
            viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            Assert.AreEqual(1000, viewBottomRight[0]);
            Assert.AreEqual(0, viewBottomRight[1]);

            view = View2D.CreateFromBounds(100, -100, -100, 100);

            // test result.
            Assert.AreEqual(view.LeftTop[0], -100);
            Assert.AreEqual(view.RightTop[0], 100);
            Assert.AreEqual(view.RightBottom[1], -100);
            Assert.AreEqual(view.RightTop[1], 100);
            Assert.IsTrue(view.Contains(0, 0));

            view = View2D.CreateFromBounds(-100, 100, 100, -100);

            // test result.
            Assert.AreEqual(view.LeftTop[0], 100);
            Assert.AreEqual(view.RightTop[0], -100);
            Assert.AreEqual(view.RightBottom[1], 100);
            Assert.AreEqual(view.RightTop[1], -100);
            Assert.IsTrue(view.Contains(0, 0));
        }
示例#4
0
        public void View2DTestCreateFrom()
        {
            double x, y;
            var    view = View2D.CreateFrom(0, 0, 200, 200, 1, false, false);

            // test result.
            Assert.AreEqual(view.LeftTop[0], -100);
            Assert.AreEqual(view.RightTop[0], 100);
            Assert.AreEqual(view.RightBottom[1], -100);
            Assert.AreEqual(view.RightTop[1], 100);
            Assert.IsTrue(view.Contains(0, 0));
            var fromMatrix = view.CreateFromViewPort(1000, 1000);

            fromMatrix.Apply(0, 0, out x, out y);
            // var topLeft = view.FromViewPort(1000, 1000, 0, 0);
            Assert.AreEqual(x, -100);
            Assert.AreEqual(y, -100);
            fromMatrix.Apply(1000, 1000, out x, out y);
            // var bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            Assert.AreEqual(x, 100);
            Assert.AreEqual(y, 100);

            var toMatrix = view.CreateToViewPort(1000, 1000);

            //var viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.RightTop[1]);
            toMatrix.Apply(view.LeftTop[0], view.RightTop[1], out x, out y);
            Assert.AreEqual(0, x);
            Assert.AreEqual(1000, y);
            // var viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            toMatrix.Apply(view.RightBottom[0], view.RightBottom[1], out x, out y);
            Assert.AreEqual(1000, x);
            Assert.AreEqual(0, y);

            view = View2D.CreateFrom(0, 0, 200, 200, 1, true, false);

            // test result.
            Assert.AreEqual(view.LeftTop[0], 100);
            Assert.AreEqual(view.RightTop[0], -100);
            Assert.AreEqual(view.RightBottom[1], -100);
            Assert.AreEqual(view.RightTop[1], 100);
            Assert.IsTrue(view.Contains(0, 0));
            fromMatrix = view.CreateFromViewPort(1000, 1000);
            // topLeft = view.FromViewPort(1000, 1000, 0, 0);
            fromMatrix.Apply(0, 0, out x, out y);
            Assert.AreEqual(x, 100);
            Assert.AreEqual(y, -100);
            // bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            fromMatrix.Apply(1000, 1000, out x, out y);
            Assert.AreEqual(x, -100);
            Assert.AreEqual(y, 100);

            toMatrix = view.CreateToViewPort(1000, 1000);
            // viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]);
            toMatrix.Apply(view.LeftTop[0], view.LeftTop[1], out x, out y);
            Assert.AreEqual(0, x);
            Assert.AreEqual(1000, y);
            // viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            toMatrix.Apply(view.RightBottom[0], view.RightBottom[1], out x, out y);
            Assert.AreEqual(1000, x);
            Assert.AreEqual(0, y);

            view = View2D.CreateFrom(0, 0, 200, 200, 1, false, true);

            // test result.
            Assert.AreEqual(view.LeftTop[0], -100);
            Assert.AreEqual(view.RightTop[0], 100);
            Assert.AreEqual(view.RightBottom[1], 100);
            Assert.AreEqual(view.RightTop[1], -100);
            Assert.IsTrue(view.Contains(0, 0));
            fromMatrix = view.CreateFromViewPort(1000, 1000);
            // topLeft = view.FromViewPort(1000, 1000, 0, 0);
            fromMatrix.Apply(0, 0, out x, out y);
            Assert.AreEqual(x, -100);
            Assert.AreEqual(y, 100);
            // bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            fromMatrix.Apply(1000, 1000, out x, out y);
            Assert.AreEqual(x, 100);
            Assert.AreEqual(y, -100);

            toMatrix = view.CreateToViewPort(1000, 1000);
            // viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]);
            toMatrix.Apply(view.LeftTop[0], view.LeftTop[1], out x, out y);
            Assert.AreEqual(0, x);
            Assert.AreEqual(1000, y);
            // viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            toMatrix.Apply(view.RightBottom[0], view.RightBottom[1], out x, out y);
            Assert.AreEqual(1000, x);
            Assert.AreEqual(0, y);

            view = View2D.CreateFrom(0, 0, 200, 200, 1, true, true);

            // test result.
            Assert.AreEqual(view.LeftTop[0], 100);
            Assert.AreEqual(view.RightTop[0], -100);
            Assert.AreEqual(view.RightBottom[1], 100);
            Assert.AreEqual(view.RightTop[1], -100);
            Assert.IsTrue(view.Contains(0, 0));
            fromMatrix = view.CreateFromViewPort(1000, 1000);
            // topLeft = view.FromViewPort(1000, 1000, 0, 0);
            fromMatrix.Apply(0, 0, out x, out y);
            Assert.AreEqual(x, 100);
            Assert.AreEqual(y, 100);
            // bottomRight = view.FromViewPort(1000, 1000, 1000, 1000);
            fromMatrix.Apply(1000, 1000, out x, out y);
            Assert.AreEqual(x, -100);
            Assert.AreEqual(y, -100);

            toMatrix = view.CreateToViewPort(1000, 1000);
            // viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]);
            toMatrix.Apply(view.LeftTop[0], view.LeftTop[1], out x, out y);
            Assert.AreEqual(0, x);
            Assert.AreEqual(1000, y);
            // viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]);
            toMatrix.Apply(view.RightBottom[0], view.RightBottom[1], out x, out y);
            Assert.AreEqual(1000, x);
            Assert.AreEqual(0, y);

            view = View2D.CreateFromBounds(100, -100, -100, 100);

            // test result.
            Assert.AreEqual(view.LeftTop[0], -100);
            Assert.AreEqual(view.RightTop[0], 100);
            Assert.AreEqual(view.RightBottom[1], -100);
            Assert.AreEqual(view.RightTop[1], 100);
            Assert.IsTrue(view.Contains(0, 0));

            view = View2D.CreateFromBounds(-100, 100, 100, -100);

            // test result.
            Assert.AreEqual(view.LeftTop[0], 100);
            Assert.AreEqual(view.RightTop[0], -100);
            Assert.AreEqual(view.RightBottom[1], 100);
            Assert.AreEqual(view.RightTop[1], -100);
            Assert.IsTrue(view.Contains(0, 0));
        }