public void KmlFileTest() { DateTime dt1 = DateTime.Now; GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance; //var kml = KmlProvider.FromKml("/WorkSpace/huas/Polygontwo_LayerToKML.kml"); //var kml = KmlProvider.FromKml(@"C:\Workspace\huas\2010上海50KM毅行.kml"); var kml = KmlProvider.FromKmz(@"C:\Workspace\huas\snotelwithlabels.kmz"); DateTime dt2 = DateTime.Now; Assert.IsNotNull(kml); Assert.AreNotEqual(kml.GetFeatureCount(), 0); Console.WriteLine(string.Format("kml.GetFeatureCount():{0}", kml.GetFeatureCount())); Console.WriteLine(string.Format("kml.GetExtents():{0}", kml.GetExtents())); DateTime dt3 = DateTime.Now; TimeSpan span1 = dt2 - dt1; TimeSpan span2 = dt3 - dt1; Console.WriteLine($"dt2 - dt1:{span1.Seconds},dt3 - dt1:{span2.Seconds}"); List <string> ids = kml.GetObjectIDsInViewForSList(kml.GetExtents()); int polylineCount = 0; int pointCount = 0; foreach (var id in ids) { var dr = kml.GetFeature(id); var place = (Placemark)dr.ItemArray[2]; if (dr.Geometry.GeometryType.Equals("MultiLineString") || dr.Geometry.GeometryType.Equals("LineString")) { polylineCount++; Console.WriteLine($"LineString:{place.Id},{place.Name},{kml.GetGeometryType(place.Id)}"); } if (dr.Geometry.GeometryType.Equals("Point") || dr.Geometry.GeometryType.Equals("MultiPoint")) { pointCount++; Console.WriteLine($"Point:{place.Id},{place.Name}"); } //Console.WriteLine(gwl(dr.Geometry.Coordinates.ToList())); //Console.WriteLine("------------------------------------------------------"); } kml.GetFolders().ForEach(folder => { Console.WriteLine($"folder:{folder.Id},{folder.Name},{folder.Features.Count()}"); }); //Assert.AreEqual(kml.GetFolders().Count, 6); //Assert.AreEqual(polylineCount,3); Console.WriteLine($"polylineCount:{polylineCount}"); //Assert.AreEqual(pointCount, 24); Console.WriteLine($"pointCount:{pointCount}"); }
public void KmlFileTest3() { GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance; //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"); var kml = KmlProvider.FromKmz(GetStreamFromUrl(new Uri("http://112.74.67.213:6080/huayu/TestData/raw/master/online/Polygontwo_LayerToKML.kmz"))); Assert.IsNotNull(kml); List <string> ids = kml.GetObjectIDsInViewForSList(kml.GetExtents()); int polylineCount = 0; int pointCount = 0; int polygoncount = 0; int styleCount = 0; foreach (var id in ids) { var dr = kml.GetFeature(id); var style = kml.GetKmlStyle(dr); Console.WriteLine($"styleid:{(string)dr["StyleUrl"]},"); var place = (Placemark)dr.ItemArray[2]; if (style != null) { styleCount++; } if (dr.Geometry.GeometryType.Equals("MultiLineString") || dr.Geometry.GeometryType.Equals("LineString")) { polylineCount++; Console.WriteLine($"LineString:{place.Id},{place.Name},{kml.GetGeometryType(place.Id)}"); } if (dr.Geometry.GeometryType.Equals("Point") || dr.Geometry.GeometryType.Equals("MultiPoint")) { pointCount++; Console.WriteLine($"Point:{place.Id},{place.Name},{kml.GetGeometryType(place.Id)}"); } if (dr.Geometry.GeometryType.Equals("Polygon") || dr.Geometry.GeometryType.Equals("MultiPolygon") || dr.Geometry.GeometryType.Equals("GeometryCollection")) { polygoncount++; Console.WriteLine($"Point:{place.Id},{place.Name},{kml.GetGeometryType(place.Id)}"); } } Console.WriteLine($"pointCount:{pointCount},polylineCount:{polylineCount},polygoncount:{polygoncount},"); Console.WriteLine($"ids:{ids.Count},styleCount:{styleCount}"); kml.GetFolders().ForEach(folder => { Console.WriteLine($"folder:{folder.Id},{folder.Name},{folder.Features.Count()}"); }); }
public void KmzFileTest() { GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance; GeoAPI.GeometryServiceProvider.Instance = NetTopologySuite.NtsGeometryServices.Instance; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://112.74.67.213:6080/huayu/TestData/raw/master/snotelwithlabels.kmz")); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); var kml = KmlProvider.FromKmz(resp.GetResponseStream()); Assert.IsNotNull(kml); Assert.AreNotEqual(kml.GetFeatureCount(), 0); Console.WriteLine(string.Format("kml.GetFeatureCount():{0}", kml.GetFeatureCount())); Console.WriteLine(string.Format("kml.GetExtents():{0}", kml.GetExtents())); }
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); }
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); }