Exemple #1
0
        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 = xmlDataProcessorSource.PullToCollection();

            // 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();
        }
        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--;
                }
            }
        }
        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]);
        }