C#实现KML生成(支持点、线、面接口)

本文档介绍了如何使用C#编程语言实现KML文件的生成,支持点、线、面三种几何类型,并提供了一个实例,包括构造函数、KmlPlacemark类和CreateKml静态方法,帮助开发者快速创建定制化的KML文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#实现KML生成(支持点、线、面接口):
点击下载:接口和代码下载地址

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace GenerateKml
{
    [XmlRoot("kml", Namespace = "http://www.opengis.net/kml/2.2")]
    public class Kml
    {
        [XmlElement("Document")]
        public KmlDocument document { get; set; }

        // constructor
        public Kml()
        {
        }

        public Kml(KmlDocument doc)
        {
            document = doc;
        }

        public void GenerateKmlFile(string KmlFileName)
        {
            using (FileStream fs = new FileStream(KmlFileName, FileMode.Create, FileAccess.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
                {
                    XmlSerializer serializer = new XmlSerializer(this.GetType());
                    serializer.Serialize(sw, this);
                }
            }
        }
        /// <summary>
        /// coordinates lon,lat,alt 经纬高度多个用;隔开
        /// </summary>
        public class dataFormat
        {
            public string type { get; set; }
            public string coordinates { get; set; }

            public string name { get; set; }

            public string description { get; set; }
            public string styleUrl { get; set; }

        }
       /// <summary>
       /// 调用此方法
       /// </summary>
       /// <param name="xmlname">xml名称</param>
       /// <param name="filename">导出文件名</param>
       /// <param name="lst">数据</param>
       /// <returns></returns>
        public static int CreateKml(string xmlname,string filename,List<dataFormat> lst)
        {
            List<KmlPlacemark> ls = new List<KmlPlacemark>();
            foreach (dataFormat d in lst)
            {
                string type = d.type;

                string coords = d.coordinates.Trim();

                string styleUrl = d.styleUrl;

                string name = d.name;
                if (string.IsNullOrEmpty(name))
                {
                    name = "";
                }
                string description = d.description;
                if (string.IsNullOrEmpty(description))
                {
                    description = "";
                }

                if (type == "point")
                {
                    if (string.IsNullOrEmpty(styleUrl))
                    {
                        styleUrl = "#downArrowIcon";
                    }
                    KmlPoint p = new KmlPoint(coords);
                    KmlPlacemark placemark = new KmlPlacemark(name, description, styleUrl, p, null, null);
                    ls.Add(placemark);
                }
                else if (type == "line")
                {
                    if (string.IsNullOrEmpty(styleUrl))
                    {
                        styleUrl = "#blue";
                    }
                    KmlLineString line = new KmlLineString(coords.Split(';').ToList());
                    KmlPlacemark placemark = new KmlPlacemark(name, description, styleUrl, null, line, null);
                    ls.Add(placemark);
                }
                else if (type == "polygon")
                {
                    if (string.IsNullOrEmpty(styleUrl))
                    {
                        styleUrl = "#blue";
                    }
                    KmlPolygon polygon = new KmlPolygon(coords.Split(';').ToList());
                    KmlPlacemark placemark = new KmlPlacemark(name, description, styleUrl, null, null, polygon);
                    ls.Add(placemark);
                }

            }
            try
            {
                KmlDocument document = new KmlDocument(xmlname, filename, ls);
                Kml kml = new Kml(document);
                kml.GenerateKmlFile("a.kml");
                return 1;
            }
            catch (Exception e)
            {
                return 0;
            }
           
        }
        //测试
        public static void Test()
        {
            List<dataFormat> l = new List<dataFormat>();
            dataFormat d = new dataFormat();
            d.name = "点";
            d.type = "point";
            d.styleUrl = "#downArrowIcon";
            d.coordinates = "116, 40, 0";
            d.description = "点的描述";
            l.Add(d);
            dataFormat d1 = new dataFormat();
            d1.name = "线";
            d1.type = "line";
            d1.styleUrl = "#blue";
            d1.coordinates = "116,40,0;116,39,0;116,36,0";
            d1.description = "线的描述";
            l.Add(d1);
            dataFormat d3 = new dataFormat();
            d3.name = "面";
            d3.type = "polygon";
            d3.styleUrl = "#poly1";
            d3.coordinates = "-122.0857412771483,37.42227033155257,17;-122.0858169768481,37.42231408832346,17;-122.085852582875,37.42230337469744,17;-122.0858799945639,37.42225686138789,17;-122.0858860101409,37.4222311076138,17;-122.0858069157288,37.42220250173855,17;-122.0858379542653,37.42214027058678,17;-122.0856732640519,37.42208690214408,17;-122.0856022926407,37.42214885429042,17;-122.0855902778436,37.422128290487,17;-122.0855841672237,37.42208171967246,17;-122.0854852065741,37.42210455874995,17;-122.0855067264352,37.42214267949824,17;-122.0854430712915,37.42212783846172,17;-122.0850990714904,37.42251282407603,17;-122.0856769818632,37.42281815323651,17;-122.0860162273783,37.42244918858722,17;-122.0857260327004,37.42229239604253,17;-122.0857412771483,37.42227033155257,17";
            d3.description = "面的描述";
            l.Add(d3);
            CreateKml("测试","a",l);
        }
    }
}

如果需要点、线、面kml的接口,请私信我。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易GIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值