C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)
前言:
之前我們使用對(duì)List將數(shù)據(jù)封裝進(jìn)KML經(jīng)緯度文件中,今天我們來(lái)學(xué)習(xí)一下如何將經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)讀出來(lái),并保存在變量中,這個(gè)變量可以是list也可以是數(shù)組,只要能儲(chǔ)存數(shù)據(jù)就可以,我們對(duì)KML文件中的Point數(shù)據(jù)下面的coordinates數(shù)據(jù)讀出來(lái)即可?。?!
一、界面設(shè)計(jì)
設(shè)計(jì)了兩個(gè)選項(xiàng),可以選擇不同的效果進(jìn)行提取經(jīng)緯度數(shù)據(jù),第一代表可以把數(shù)據(jù)提取到TXT文本文件中,而第二表示不會(huì)生成TXT文件只在文本框中展示你的提取數(shù)據(jù),可以看后面的代碼邏輯,有一個(gè)函數(shù)是專門負(fù)責(zé)數(shù)據(jù)的提取,是使用XML讀取的形式讀取指定的標(biāo)簽數(shù)據(jù)
二、效果展示
目前只展示了不會(huì)導(dǎo)出TXT文件的效果,只是對(duì)數(shù)據(jù)展示在文本框中。你們也可以按照自己的需求接著寫接著添加自己想要的功能,后面有代碼邏輯,代碼也有注解,不至于不能懂,有啥問(wèn)題評(píng)論區(qū)評(píng)論
三、代碼邏輯
使用的是XML文件讀取的形式,利用XML的節(jié)點(diǎn)的方式,對(duì)數(shù)據(jù)的標(biāo)簽遍歷得到,對(duì)應(yīng)的標(biāo)簽,再對(duì)指定的coordinates標(biāo)簽的數(shù)據(jù)進(jìn)行提取并賦值,從而實(shí)現(xiàn)提取KML文件中的經(jīng)緯度的數(shù)據(jù)效果。
//自定義類的函數(shù) 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> /// 對(duì)指定路徑的kml文件,經(jīng)緯度讀取,并返回經(jīng)緯度集合MapConfig /// </summary> /// <param name="Path">文件路徑名</param> /// <returns></returns> public List<String> MapConfigs(string filename) { List<String> mapConfigs = new List<String>();//實(shí)例化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節(jié)點(diǎn) int i = 0; foreach (XmlNode xmlmarkNode in xmlmark)//遍歷所有coordinates節(jié)點(diǎn) { if (xmlmarkNode.Name == "coordinates") { i++; string mapConfig = "";//實(shí)例化 string str = xmlmarkNode.InnerText;//獲取節(jié)點(diǎn)的值 if (str.Equals("") || str.Contains(",") == false) { MessageBox.Show("第"+i.ToString()+"行數(shù)據(jù)有誤,將返回NULL值","錯(cuò)誤"); return null; } string[] strings = str.Split(',');//對(duì)節(jié)點(diǎn)的值字符串進(jìn)行分割 mapConfig+= strings[0]+",";//經(jīng)度值 mapConfig+= strings[1]+",";//緯度值 mapConfig+= strings[2];// mapConfigs.Add(mapConfig);//添加在list中 } } return mapConfigs; } catch { MessageBox.Show("文件加載失敗或coordinates節(jié)點(diǎn)數(shù)據(jù)獲取失敗", "錯(cuò)誤"); return null;//注:kml文件如果使用wps或者word打開(kāi)會(huì)再運(yùn)行本程序會(huì)報(bào)錯(cuò),文本打開(kāi)運(yùn)行不會(huì)報(bào)錯(cuò) } } } } ? ? //上面是我們需要調(diào)用的類 ?? //這個(gè)是我們界面設(shè)計(jì)調(diào)用的類 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)//打開(kāi)文件是否點(diǎn)擊了取消 return; KmlPath = openFile.FileName; textBox1.Text = KmlPath; } ? private void button7_Click(object sender, EventArgs e) { DataExtract data = new DataExtract();//自定義的函數(shù),復(fù)制kml文件經(jīng)緯度數(shù)據(jù)提取 if (KmlPath.Equals("") == true) { MessageBox.Show("選擇文件之后才能導(dǎo)出"); } 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"; //設(shè)置文件類型 save.RestoreDirectory = true; //保存對(duì)話框是否記憶上次打開(kāi)的目錄 if (save.ShowDialog() == DialogResult.OK)//點(diǎn)了保存按鈕進(jìn)入 { 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("選擇你需要的項(xiàng)"); } } } } }
總結(jié):
這篇文章比較簡(jiǎn)單,里面也已經(jīng)寫好了方法讓我們調(diào)用就可以了,界面制作比較簡(jiǎn)單,但是是一個(gè)比較實(shí)用的一個(gè)小工具。
到此這篇關(guān)于C#如何提取經(jīng)緯度文件中經(jīng)緯度數(shù)據(jù)的文章就介紹到這了,更多相關(guān)C#提取經(jīng)緯度數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#Windows窗體設(shè)計(jì)之ContextMenuStrip(鼠標(biāo)右擊菜單)的使用
這篇文章主要介紹了C#Windows窗體設(shè)計(jì)之ContextMenuStrip(鼠標(biāo)右擊菜單)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07常用.NET工具(包括.NET可再發(fā)行包2.0)下載
常用.NET工具(包括.NET可再發(fā)行包2.0)下載...2007-03-03Unity Shader實(shí)現(xiàn)素描風(fēng)格的渲染
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)素描風(fēng)格的渲染,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能(2)(窗體應(yīng)用)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01詳解C#中對(duì)于接口的實(shí)現(xiàn)方式(隱式接口和顯式接口)
這篇文章主要介紹了詳解C#中對(duì)于接口的實(shí)現(xiàn)方式(隱式接口和顯式接口),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12解析在內(nèi)部循環(huán)中Continue外部循環(huán)的使用詳解
本篇文章是對(duì)在內(nèi)部循環(huán)中Continue外部循環(huán)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05