C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)_第1頁
C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)_第2頁
C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)_第3頁
C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)_第4頁
C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)_第5頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

第C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)目錄前言:一、界面設計二、效果展示三、代碼邏輯總結:

前言:

之前我們使用對List將數(shù)據(jù)封裝進KML經(jīng)緯度文件中,今天我們來學習一下如何將經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)讀出來,并保存在變量中,這個變量可以是list也可以是數(shù)組,只要能儲存數(shù)據(jù)就可以,我們對KML文件中的Point數(shù)據(jù)下面的coordinates數(shù)據(jù)讀出來即可?。?!

一、界面設計

設計了兩個選項,可以選擇不同的效果進行提取經(jīng)緯度數(shù)據(jù),第一代表可以把數(shù)據(jù)提取到TXT文本文件中,而第二表示不會生成TXT文件只在文本框中展示你的提取數(shù)據(jù),可以看后面的代碼邏輯,有一個函數(shù)是專門負責數(shù)據(jù)的提取,是使用XML讀取的形式讀取指定的標簽數(shù)據(jù)

二、效果展示

目前只展示了不會導出TXT文件的效果,只是對數(shù)據(jù)展示在文本框中。你們也可以按照自己的需求接著寫接著添加自己想要的功能,后面有代碼邏輯,代碼也有注解,不至于不能懂,有啥問題評論區(qū)評論

三、代碼邏輯

使用的是XML文件讀取的形式,利用XML的節(jié)點的方式,對數(shù)據(jù)的標簽遍歷得到,對應的標簽,再對指定的coordinates標簽的數(shù)據(jù)進行提取并賦值,從而實現(xiàn)提取KML文件中的經(jīng)緯度的數(shù)據(jù)效果。

//自定義類的函數(shù)

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

usingSystem.Xml;

namespaceFileConversion

publicclassDataExtract

///summary

///對指定路徑的kml文件,經(jīng)緯度讀取,并返回經(jīng)緯度集合MapConfig

////summary

///paramname="Path"文件路徑名/param

///returns/returns

publicListStringMapConfigs(stringfilename)

ListStringmapConfigs=newListString//實例化list集合

stringdestPath=filename;//賦值文件路徑

if(destPath==null)//判斷路徑是否為空

MessageBox.Show("路徑為空");

returnnull;

XmlDocumentxmldoc=newXmlDocument();

xmldoc.Load(destPath);//加載kml文件

XmlElementroot=xmldoc.DocumentElement;

XmlNodeListxmlmark=root.GetElementsByTagName("coordinates");//獲取coordinates節(jié)點

inti=0;

foreach(XmlNodexmlmarkNodeinxmlmark)//遍歷所有coordinates節(jié)點

if(xmlmarkNode.Name=="coordinates")

i++;

stringmapConfig="";//實例化

stringstr=xmlmarkNode.InnerText;//獲取節(jié)點的值

if(str.Equals("")||str.Contains(",")==false)

MessageBox.Show("第"+i.ToString()+"行數(shù)據(jù)有誤,將返回NULL值","錯誤");

returnnull;

string[]strings=str.Split(',');//對節(jié)點的值字符串進行分割

mapConfig+=strings[0]+",";//經(jīng)度值

mapConfig+=strings[1]+",";//緯度值

mapConfig+=strings[2];//

mapConfigs.Add(mapConfig);//添加在list中

returnmapConfigs;

catch

MessageBox.Show("文件加載失敗或coordinates節(jié)點數(shù)據(jù)獲取失敗","錯誤");

returnnull;//注:kml文件如果使用wps或者word打開會再運行本程序會報錯,文本打開運行不會報錯

//上面是我們需要調(diào)用的類

//這個是我們界面設計調(diào)用的類

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.IO;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

namespaceFileConversion

publicpartialclassForm4:Form

publicForm4()

InitializeComponent();

publicstringKmlPath="";

privatevoidbutton6_Click(objectsender,EventArgse)

OpenFileDialogopenFile=newOpenFileDialog();

openFile.Filter="KML文件(*.kml)|*.kml|所有文件|*.*";

if(openFile.ShowDialog()!=DialogResult.OK)//打開文件是否點擊了取消

return;

KmlPath=openFile.FileName;

textBox1.Text=KmlPath;

privatevoidbutton7_Click(objectsender,EventArgse)

DataExtractdata=newDataExtract();//自定義的函數(shù),復制kml文件經(jīng)緯度數(shù)據(jù)提取

if(KmlPath.Equals("")==true)

MessageBox.Show("選擇文件之后才能導出");

else

if(checkBox1.Checked==truecheckBox2.Checked==false||checkBox1.Checked==truecheckBox2.Checked==true)

Liststringlist=data.MapConfigs(KmlPath);

stringlocalFilePath="";//文件路徑

SaveFileDialogsave=newSaveFileDialog();

save.Filter="Txt(*.txt)|*.txt";//設置文件類型

save.RestoreDirectory=true;//保存對話框是否記憶上次打開的目錄

if(save.ShowDialog()==DialogResult.OK)//點了保存按鈕進入

localFilePath=save.FileName.ToString();//獲得文件路徑

stringfileName=localFilePath.Substring(localFilePath.LastIndexOf("\")+1);//獲取文件名,不帶路徑

//FileStreamfile=newFileStream(localFilePath,FileMode.Create);

foreach(stringkmlinlist)

textBox2.Text+=kml+"\r\n";

File.AppendAllText(localFilePath,kml+"\r\n");

elseif(checkBox1.Checked==falsecheckBox2.Checked==true)

Liststringlist=da

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論