KML(Keyhole Markup Language,Keyhole 标记语言)最初是由Google 旗下的Keyhole 公司开发和维护的一种基于XML 的标记语言,利用XML 语法格式描述地理空间数据(如点、线、面、多边形和模型等),适合网络环境下的地理信息协作与共享。2008 年4月,KML的最新版本2.2 被OGC 宣布为开放地理信息编码标准,并改由OGC 维护和发展。
KML文件可以让你将任意图片,三维模型,柱状图加载到GoogleEarth中。
动态生成KML文件的c#代码如下:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml.Serialization; namespace GBQ.Lidar.AMS.KMLPro { public class kml { public object lockObj = new object(); public static Utils.Logger logger = new Utils.Logger("NLog.config"); [XmlElement("Document")] public KmlDocument Document { get; set; } public kml() { } public kml(KmlDocument doc) { this.Document = doc; } public void GenerateKmlFile(string path, string fileName) { try { if (path == null) { logger.Info("路径为空,无法创建kml文件"); return; } //判断文件夹是否存在 if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } using (var fs = new FileStream(path + "\\" + fileName, FileMode.Create, FileAccess.ReadWrite)) { using (var sw = new StreamWriter(fs, System.Text.Encoding.UTF8)) { XmlSerializer serializer = new XmlSerializer(GetType()); serializer.Serialize(sw, this); } } } catch(Exception ex) { logger.Error("存储KML文件时发生异常.", ex); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace GBQ.Lidar.AMS.KMLPro { public class KmlDocument { [XmlElement("Placemark")] public List<KmlPlacemark> PlaceMarkers { get; set; } public KmlDocument() { } public KmlDocument( List<KmlPlacemark> folderList) { PlaceMarkers = folderList; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GBQ.Lidar.AMS.KMLPro { public class KmlLine { private List<KmlPoint> _pointList; public KmlLine() { _pointList = new List<KmlPoint>(); } public void Add(KmlPoint point) { _pointList.Add(point); } public override string ToString() { return _pointList.Aggregate(string.Empty, (current, point) => current + (point + " ")); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace GBQ.Lidar.AMS.KMLPro { public class KmlLineString { [XmlElement("extrude")] public string Extrude { get; set; } [XmlElement("tessellate")] public string Tessellate { get; set; } [XmlElement("altitudeMode")] public string AltitudeMode { get; set; } [XmlElement("coordinates")] public string Coordinates { get; set; } public KmlLineString() { } public KmlLineString(KmlLine line) { AltitudeMode = "relativeToGround"; Coordinates = line.ToString(); this.Extrude = "1"; this.Tessellate = "1"; } public KmlLineString(string tessel, KmlLine line) { AltitudeMode = "relativeToGround"; Coordinates = line.ToString(); this.Extrude = "1"; this.Tessellate = "1"; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace GBQ.Lidar.AMS.KMLPro { public class KmlPlacemark { [XmlElement("name")] public string Name { get; set; } [XmlElement("Style")] public KmlStyle kmlStyle { get; set; } [XmlElement("Polygon")] public Polygon polygon { get; set; } public KmlPlacemark() { } public KmlPlacemark(string name, KmlStyle kmlStyle,Polygon polygon) { this.Name = name; this.kmlStyle = kmlStyle; this.polygon = polygon; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace GBQ.Lidar.AMS.KMLPro { public class KmlPoint { public double B; public double L; public double H; public DateTime time; public KmlPoint() { } public KmlPoint(double b, double l, double h) { B = l; L = b; H = h; time = DateTime.Now; } public KmlPoint(double b, double l, double h, DateTime time) { B = l; L = b; H = h; this.time = time; } public override string ToString() { return L.ToString("F6") + "," + B.ToString("F6") + "," + H.ToString("F2"); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace GBQ.Lidar.AMS.KMLPro { public class KmlStyle { [XmlElement("LineStyle")] public KmlLineStyle lineStyle { get; set; } [XmlElement("PolyStyle")] public KmlPolyStyle polyStyle { get; set; } public KmlStyle() { } public KmlStyle( string lineColor, string lineWidth,string polyColor) { lineStyle = new KmlLineStyle(lineColor, lineWidth); polyStyle = new KmlPolyStyle(polyColor); } public class KmlLineStyle { [XmlElement("color")] public string Color { get; set; } [XmlElement("width")] public string Width { get; set; } public KmlLineStyle() { } public KmlLineStyle(string color, string width) { Color = color; Width = width; } } public class KmlPolyStyle { [XmlElement("color")] public string Color { get; set; } public KmlPolyStyle() { } public KmlPolyStyle(string color) { this.Color = color; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace GBQ.Lidar.AMS.KMLPro { public class KmlStyleMap { private static readonly string[,] styleTable = { {"normal", "#green"}, {"highlight", "#red"} }; [XmlAttribute("id")] public string ID { get; set; } [XmlElement("Pair")] public List<Pair> pairs { get; set; } public KmlStyleMap() { } public KmlStyleMap(string id) { ID = id; pairs = GetPairList(); } public static List<Pair> GetPairList() { List<Pair> rtn = new List<Pair>(); for (int i = 0; i < styleTable.GetLength(0); i++) { Pair pair = new Pair(styleTable[i, 0], styleTable[i, 1]); rtn.Add(pair); } return rtn; } public class Pair { [XmlElement("key")] public string Key { get; set; } [XmlElement("styleUrl")] public string StyleUrl { get; set; } public Pair() { } public Pair(string key, string url) { Key = key; StyleUrl = url; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace GBQ.Lidar.AMS.KMLPro { public class Polygon { [XmlElement("extrude")] public string extrude { get; set; } [XmlElement("altitudeMode")] public string altitudeMode { get; set; } [XmlElement("outerBoundaryIs")] public OuterBoundaryIs outerBoundaryIs { get; set; } public Polygon() { } public Polygon(string extrude,string altitudeMode,OuterBoundaryIs outerBoundaryIs) { this.extrude = extrude; this.altitudeMode = altitudeMode; this.outerBoundaryIs = outerBoundaryIs; } } public class OuterBoundaryIs { [XmlElement("LinearRing")] public LinearRing linearRing { get; set; } public OuterBoundaryIs() { } public OuterBoundaryIs(LinearRing linearRing) { this.linearRing = linearRing; } } public class LinearRing { [XmlElement("coordinates")] public string coordinates { get; set; } public LinearRing() { } public LinearRing(string coordinates) { this.coordinates = coordinates; } } }
调用:
调用是直接复制的项目里的方法,没有用的信息可以自动过滤。
/// <summary> /// 生成kml文件 /// </summary> /// <param name="kmlPonts"></param> /// <param name="path"></param> /// <param name="fileName"></param> /// <param name="docName"></param> /// <param name="placeMarkName"></param> /// <param name="placeMarkDes"></param> private void GeneratorKML(List<KmlPoint> kmlPonts, string path, string fileName, string docName, string placeMarkName, string placeMarkDes,double zoom ) { try { if (kmlPonts.Count <= 0) return; List<GBQ.Lidar.AMS.KML3D.KmlPlacemark> kmlPlacemarks = new List<KML3D.KmlPlacemark>(); for (int i = 0; i < kmlPonts.Count; i++) { GBQ.Lidar.AMS.KML3D.KmlLine kmlLine = new KML3D.KmlLine(); GBQ.Lidar.AMS.KML3D.KmlPoint kmlPoint = new GBQ.Lidar.AMS.KML3D.KmlPoint(kmlPonts[i].L, kmlPonts[i].B, kmlPonts[i].H * zoom); kmlLine.Add(kmlPoint); GBQ.Lidar.AMS.KML3D.KmlStyle kmlStyle = new KML3D.KmlStyle(GetColor(alpha, maxColorValue, kmlPoint.H, zoom), width); GBQ.Lidar.AMS.KML3D.KmlLineString kmlLineString = new GBQ.Lidar.AMS.KML3D.KmlLineString(kmlLine); GBQ.Lidar.AMS.KML3D.KmlPlacemark kmlPlacemark = new GBQ.Lidar.AMS.KML3D.KmlPlacemark(placeMarkName, placeMarkDes, "#yellowLineGreenPoly", kmlStyle, kmlLineString); kmlPlacemarks.Add(kmlPlacemark); } List<KML3D.KmlScreenOverlay> screenOverlays = new List<KML3D.KmlScreenOverlay>(); GBQ.Lidar.AMS.KML3D.KmlDocument kmlDocument = new GBQ.Lidar.AMS.KML3D.KmlDocument(docName, kmlPlacemarks); GBQ.Lidar.AMS.KML3D.kml kmlRoot = new GBQ.Lidar.AMS.KML3D.kml(kmlDocument); DateTime currentTime = DateTime.Now; kmlRoot.GenerateKmlFile(path, fileName); } catch(Exception ex) { logger.Error("写入kml文件时发生异常,请注意!"); } }
生成后的KML文件:
<?xml version="1.0" encoding="utf-8"?> <kml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <lockObj /> <Document> <Placemark> <name>NO2</name> <Style> <LineStyle> <color>ef000000</color> <width>0</width> </LineStyle> <PolyStyle> <color>ef000000</color> </PolyStyle> </Style> <Polygon> <extrude>1</extrude> <altitudeMode>relativeToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates>117.00189,31.00191,556 117.00295,31.00262,556 </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> <Placemark> <name>NO2</name> <Style> <LineStyle> <color>ef000000</color> <width>0</width> </LineStyle> <PolyStyle> <color>ef000000</color> </PolyStyle> </Style> <Polygon> <extrude>1</extrude> <altitudeMode>relativeToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates>117.00295,31.00262,556 117.00346,31.004,556 </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> <Placemark> <name>NO2</name> <Style> <LineStyle> <color>ef000000</color> <width>0</width> </LineStyle> <PolyStyle> <color>ef000000</color> </PolyStyle> </Style> <Polygon> <extrude>1</extrude> <altitudeMode>relativeToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates>117.00346,31.004,556 117.00424,31.00529,556 </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> <Placemark> <name>NO2</name> <Style> <LineStyle> <color>ef000000</color> <width>0</width> </LineStyle> <PolyStyle> <color>ef000000</color> </PolyStyle> </Style> <Polygon> <extrude>1</extrude> <altitudeMode>relativeToGround</altitudeMode> <outerBoundaryIs> <LinearRing> <coordinates>117.00424,31.00529,556 117.00533,31.00576,556 </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> </Document> </kml>
使用此方法生成的kml加载到googleEarth中的样式。
原文地址:https://www.cnblogs.com/engyue/p/11159176.html
时间: 2024-11-05 03:11:29