C#提取经纬度数据

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#提取经纬度数据

IC00   2022-09-29 我要评论

前言:

之前我们使用对List将数据封装进KML经纬度文件中,今天我们来学习一下如何将经纬度文件中的经纬度数据读出来,并保存在变量中,这个变量可以是list也可以是数组,只要能储存数据就可以,我们对KML文件中的Point数据下面的coordinates数据读出来即可!!!

一、界面设计

设计了两个选项,可以选择不同的效果进行提取经纬度数据,第一代表可以把数据提取到TXT文本文件中,而第二表示不会生成TXT文件只在文本框中展示你的提取数据,可以看后面的代码逻辑,有一个函数是专门负责数据的提取,是使用XML读取的形式读取指定的标签数据

二、效果展示

目前只展示了不会导出TXT文件的效果,只是对数据展示在文本框中。你们也可以按照自己的需求接着写接着添加自己想要的功能,后面有代码逻辑,代码也有注解,不至于不能懂,有啥问题评论区评论

三、代码逻辑

使用的是XML文件读取的形式,利用XML的节点的方式,对数据的标签遍历得到,对应的标签,再对指定的coordinates标签的数据进行提取并赋值,从而实现提取KML文件中的经纬度的数据效果。

//自定义类的函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
​
namespace FileConversion
{
    public class DataExtract
    {
        /// <summary>
        /// 对指定路径的kml文件,经纬度读取,并返回经纬度集合MapConfig
        /// </summary>
        /// <param name="Path">文件路径名</param>
        /// <returns></returns>
        public List<String> MapConfigs(string filename)
        {
            List<String> mapConfigs = new List<String>();//实例化list集合
           
            string destPath = filename;//赋值文件路径
            if (destPath == null)//判断路径是否为空
            {
                MessageBox.Show("路径为空");
                return null;
            }   
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load(destPath);//加载kml文件
                XmlElement root = xmldoc.DocumentElement;
                XmlNodeList xmlmark = root.GetElementsByTagName("coordinates");//获取coordinates节点
                int i = 0;
                foreach (XmlNode xmlmarkNode in xmlmark)//遍历所有coordinates节点
                {
                    if (xmlmarkNode.Name == "coordinates")
                    {
                        i++;
                        string mapConfig = "";//实例化
                        string str = xmlmarkNode.InnerText;//获取节点的值
                        if (str.Equals("") || str.Contains(",") == false)
                        {
                            MessageBox.Show("第"+i.ToString()+"行数据有误,将返回NULL值","错误");
                            return null;
                        }
                        string[] strings = str.Split(',');//对节点的值字符串进行分割
                        mapConfig+= strings[0]+",";//经度值
                        mapConfig+= strings[1]+",";//纬度值
                        mapConfig+= strings[2];//
                        mapConfigs.Add(mapConfig);//添加在list中
                    }
                }
                return mapConfigs;
            }
            catch
            {
                MessageBox.Show("文件加载失败或coordinates节点数据获取失败", "错误");
                return null;//注:kml文件如果使用wps或者word打开会再运行本程序会报错,文本打开运行不会报错
            }
            
        }
    }
}
​
​
//上面是我们需要调用的类
​​
//这个是我们界面设计调用的类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
​
namespace FileConversion
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }
        public string KmlPath = "";
        private void button6_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "KML文件(*.kml)|*.kml|所有文件|*.*";
            if (openFile.ShowDialog() != DialogResult.OK)//打开文件是否点击了取消
                return;
            KmlPath = openFile.FileName;
            textBox1.Text = KmlPath;
        }
​
        private void button7_Click(object sender, EventArgs e)
        {
            DataExtract data = new DataExtract();//自定义的函数,复制kml文件经纬度数据提取
            if (KmlPath.Equals("") == true)
            {
                MessageBox.Show("选择文件之后才能导出");
            }
            else
            {
                if (checkBox1.Checked == true && checkBox2.Checked == false || checkBox1.Checked == true && checkBox2.Checked == true)
                {
                    List<string> list = data.MapConfigs(KmlPath);
                    string localFilePath = "";//文件路径
                    SaveFileDialog save = new SaveFileDialog();
                    save.Filter = "Txt(*.txt)|*.txt";        //设置文件类型  
                    save.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录 
                    if (save.ShowDialog() == DialogResult.OK)//点了保存按钮进入 
                    {
                        localFilePath = save.FileName.ToString(); //获得文件路径 
                        string fileName = localFilePath.Substring(localFilePath.LastIndexOf("\") + 1); //获取文件名,不带路径
                        //FileStream file = new FileStream(localFilePath, FileMode.Create);
                        foreach (string kml in list)
                        {
                            textBox2.Text += kml + "\r\n";
                            File.AppendAllText(localFilePath, kml + "\r\n");
                        }
​
                    }
                }
                else if (checkBox1.Checked == false && checkBox2.Checked == true)
                {
                    List<string> list = data.MapConfigs(KmlPath);
                    foreach (string kml in list)
                    {
                        textBox2.Text += kml + "\r\n";
                    }
                }
                else
                {
                    MessageBox.Show("选择你需要的项");
                }
            }
        }
    }
}

总结:

这篇文章比较简单,里面也已经写好了方法让我们调用就可以了,界面制作比较简单,但是是一个比较实用的一个小工具。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们