Example #1
0
        public void KmlFileURLTest()
        {
            GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance;
            HttpWebRequest  req  = (HttpWebRequest)WebRequest.Create(new Uri("http://112.74.67.213:6080/huayu/TestData/raw/master/online/Polygontwo_LayerToKML.kml"));
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            var             kml  = KmlProvider.FromKml(resp.GetResponseStream());

            Assert.IsNotNull(kml);
            var fullExtent = kml.GetExtents();

            Assert.IsNotNull(fullExtent);
            List <string> ids = kml.GetObjectIDsInViewForSList(fullExtent);

            Assert.AreEqual(ids[0], "ID_00000");
            FeatureDataRow dr = kml.GetFeature(ids[0]);

            Assert.IsNotNull(dr);
            Assert.IsNotNull(dr.ItemArray);
            Assert.AreEqual(dr.ItemArray[0], "ID_00000");
            Assert.AreEqual(dr.ItemArray[1], "PolyStyle00");

            SharpKml.Dom.Placemark placemark = (SharpKml.Dom.Placemark)dr.ItemArray[2];
            Assert.IsNotNull(placemark);
            Assert.AreEqual(placemark.Id, "ID_00000");
            Assert.AreEqual(placemark.Name, "a");
            Assert.AreEqual(placemark.Description.Text.Trim().Length, 1095);
            Assert.AreEqual(placemark.StyleUrl.ToString(), "#PolyStyle00");
            Assert.IsNull(placemark.Visibility);
            Assert.IsNull(placemark.Open);
            Assert.IsNull(placemark.Address);
            Assert.IsNotNull(placemark.Snippet);
            Assert.IsNotNull(dr.Geometry);

            Assert.IsInstanceOf(typeof(GeometryCollection), dr.Geometry);
            GeometryCollection gc = (GeometryCollection)dr.Geometry;

            Assert.AreEqual(gc.NumGeometries, 3);
            Func <List <Coordinate>, string> gwl = (list) =>
            {
                var result = "";
                foreach (var item in list)
                {
                    result += $"{item.X},{item.Y}  ";
                }

                return(result);
            };

            for (int i = 0; i < gc.NumGeometries; i++)
            {
                IGeometry geo = gc.GetGeometryN(i);
                Console.WriteLine($"{i}:{geo.GeometryType},{gwl(geo.Coordinates.ToList())}");
            }
            Assert.IsNotNull(dr.Table);
            Assert.AreEqual(dr.IsFeatureGeometryNull(), false);
            Assert.AreEqual(kml.GetFeatureCount(), 2);
            Console.WriteLine(string.Format("kml.GetFeatureCount():{0}", kml.GetFeatureCount()));
            Console.WriteLine(string.Format("kml.GetExtents():{0}", kml.GetExtents()));
        }
Example #2
0
        public void TestConstruction()
        {
            KmlProvider p = null;

            Assert.DoesNotThrow(() => p = KmlProvider.FromKml(TestUtility.GetPathToTestFile("KML_Samples.kml")));

            Assert.IsNotNull(p);
            Assert.IsTrue(p.ConnectionID.StartsWith("KML Samples"));
        }
Example #3
0
        public void KmlFileTest2()
        {
            GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance;
            //var kml = KmlProvider.FromKmz(@"C:\Workspace\huas\snotelwithlabels.kmz");
            var kml = KmlProvider.FromKml(@"C:\Workspace\huas\2010上海50KM毅行.kml");

            //var kml = KmlProvider.FromKmz(@"C:\Workspace\huas\test2_MapToKML.kmz");
            //var kml = KmlProvider.FromKmz(@"C:\Workspace\huas\Polygontwo_LayerToKML.kmz");
            Assert.IsNotNull(kml);
            var folder = kml.GetRoot();
            Dictionary <string, TypeAndStyle> dic = new Dictionary <string, TypeAndStyle>();

            kml.GetObjectIDsInViewForSList(kml.GetExtents()).ForEach(x =>
            {
                var dr      = kml.GetFeature(x);
                var style   = kml.GetKmlStyle(dr);
                var styleid = (string)dr["StyleUrl"];
                var place   = (Placemark)dr.ItemArray[2];
                //Console.WriteLine($"styleid:{styleid},{place.StyleUrl.ToString()}");
                if (style != null && !string.IsNullOrEmpty(styleid) && !dic.ContainsKey(styleid))
                {
                    dic.Add(styleid, new TypeAndStyle(kml.GetGeometryType(place?.Id), style));
                }
            });

            Console.WriteLine($"styleCount:{dic.Count}");
            dic.ToList().ForEach(x =>
            {
                VectorStyle style = x.Value.Style;
                Console.WriteLine($"styleid:{x.Key},type:{x.Value.Type}");
                if (x.Value.Type.Equals("esriGeometryPolygon"))
                {
                    var fill    = (SolidBrush)style.Fill;
                    var outline = style.Outline;
                    Console.WriteLine($"color:{fill.Color.R},{fill.Color.G},{fill.Color.B},{fill.Color.A},  " +
                                      $"outline:{outline.Color.R},{outline.Color.G},{outline.Color.B},{outline.Color.A}, width:{outline.Width}");
                }
                else if (x.Value.Type.Equals("esriGeometryPolyline"))
                {
                    var line = style.Line;
                    Console.WriteLine($"linecolor:{line.Color.R},{line.Color.G},{line.Color.B},{line.Color.A}, width:{line.Width}, offset:{style.LineOffset}");
                }
                else if (x.Value.Type.Equals("esriGeometryPoint"))
                {
                    var url = kml.GetIconUrl(x.Key);
                    Console.WriteLine($"url:{url}");
                    if (!string.IsNullOrEmpty(url) && !url.Contains("http"))
                    {
                        var image = kml.GetImageFromKmz(url);
                        if (image != null)
                        {
                            Console.WriteLine($"image height:{image.Height} width:{image.Width} ");
                        }
                    }
                }
            });
        }
Example #4
0
        public void TestConstruction()
        {
            KmlProvider p = null;

            Assert.DoesNotThrow(() => p = KmlProvider.FromKml(System.IO.Path.Combine("TestData", "KML_Samples.kml")));

            Assert.IsNotNull(p);
            Assert.IsTrue(p.ConnectionID.StartsWith("KML Samples"));
        }
Example #5
0
        public void TestHttp(string kmlWebResource, string description)
        {
            var m = new Map(new Size(500, 300));

            m.BackColor = Color.White;

            KmlProvider p = null;

            var request = WebRequest.Create(kmlWebResource);

            using (var response = request.GetResponse())
            {
                if (response.ContentType == "None" || response.ContentType.Contains("vnd.google-earth.kml"))
                {
                    var s = response.GetResponseStream();
                    p = KmlProvider.FromKml(s);
                }

                else if (response.ContentType.Contains("vnd.google-earth.kmz"))
                {
                    p = KmlProvider.FromKmz(response.GetResponseStream(), "doc.kml");
                }
            }

            var l = new VectorLayer(p.ConnectionID, p);

            l.Theme = p.GetKmlTheme();
            m.Layers.Add(l);

            // Takes ages to render
//            var labelLayer = new LabelLayer("Labels");
//            labelLayer.DataSource = p;
//            labelLayer.LabelColumn = "Name";
//            m.Layers.Add(labelLayer);

            ZoomToExtentsWithMargin(m);

            var disclaimer = new Disclaimer();

            disclaimer.Text   = kmlWebResource;
            disclaimer.Anchor = MapDecorationAnchor.LeftTop;
            m.Decorations.Add(disclaimer);

            using (var img = m.GetMap())
                img.Save(System.IO.Path.Combine(UnitTestsFixture.GetImageDirectory(this), description + ".png"), ImageFormat.Png);
        }
Example #6
0
        public void TestGetMap()
        {
            var m = new Map(new Size(500, 300));

            m.BackColor = Color.White;

            var p = KmlProvider.FromKml(TestUtility.GetPathToTestFile("KML_Samples.kml"));
            var l = new VectorLayer(p.ConnectionID, p);

            l.Theme = p.GetKmlTheme();

            m.Layers.Add(l);
            m.ZoomToExtents();
            m.Zoom *= 1.1;

            using (var img = m.GetMap())
                img.Save(System.IO.Path.Combine(UnitTestsFixture.GetImageDirectory(this), "KmlProviderImage.png"), ImageFormat.Png);
        }
Example #7
0
        public void TestGetMap()
        {
            var m = new Map(new Size(500, 300));

            m.BackColor = Color.White;

            var p = KmlProvider.FromKml(@"TestData\KML_Samples.kml");
            var l = new VectorLayer(p.ConnectionID, p);

            l.Theme = p.GetKmlTheme();

            m.Layers.Add(l);
            m.ZoomToExtents();
            m.Zoom *= 1.1;

            var img = m.GetMap();

            img.Save("KmlProviderImage.png", ImageFormat.Png);
        }
Example #8
0
        public void TestKmlStyles(string testDataFile, string description)
        {
            // NB additional symbols at http://kml4earth.appspot.com/icons.html
            var m = new Map(new Size(500, 300));

            m.BackColor = Color.AliceBlue;

            KmlProvider p;

            if (testDataFile.ToLower().EndsWith(".kml"))
            {
                p = KmlProvider.FromKml(TestUtility.GetPathToTestFile(testDataFile));
            }
            else
            {
                p = KmlProvider.FromKmz(TestUtility.GetPathToTestFile(testDataFile));
            }

            var l = new VectorLayer(p.ConnectionID, p);

            l.Theme = p.GetKmlTheme();

            m.Layers.Add(l);

            var labelLayer = new LabelLayer("Labels");

            labelLayer.DataSource          = p;
            labelLayer.LabelStringDelegate = (fdr) => ((Placemark)fdr["Object"]).Name;
            labelLayer.Style.Font          = new Font(labelLayer.Style.Font.FontFamily, 8f);
            m.Layers.Add(labelLayer);

            ZoomToExtentsWithMargin(m);

            var disclaimer = new Disclaimer();

            disclaimer.Text   = $"{description} \nOpen **{testDataFile}** in GoogleEarth for comparison";
            disclaimer.Anchor = MapDecorationAnchor.LeftTop;
            m.Decorations.Add(disclaimer);

            using (var img = m.GetMap())
                img.Save(System.IO.Path.Combine(UnitTestsFixture.GetImageDirectory(this), testDataFile + ".png"), ImageFormat.Png);
        }
Example #9
0
        public void KmlFileTest4()
        {
            var kml = KmlProvider.FromKml(GetStreamFromUrl(new Uri("http://112.74.67.213:6080/huayu/TestData/raw/master/online/googletest.kml")));

            Assert.IsNotNull(kml);
        }