栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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);
                }
            }
        }
        /// 
        /// coordinates lon,lat,alt 经纬高度多个用;隔开
        /// 
        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; }

        }
       /// 
       /// 调用此方法
       /// 
       /// xml名称
       /// 导出文件名
       /// 数据
       /// 
        public static int CreateKml(string xmlname,string filename,List lst)
        {
            List ls = new List();
            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 l = new List();
            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的接口,请私信我。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/908492.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号