網站首頁 編程語言 正文
前言:
之前我們使用對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("選擇你需要的項");
}
}
}
}
}
總結:
這篇文章比較簡單,里面也已經寫好了方法讓我們調用就可以了,界面制作比較簡單,但是是一個比較實用的一個小工具。
原文鏈接:https://juejin.cn/post/7134679792607035399
相關推薦
- 2023-06-13 python調試過程中多顏色輸出方式_python
- 2022-03-17 C#表達式樹Expression動態創建表達式_C#教程
- 2022-04-27 前端實現滑動按鈕AJAX與后端交互的示例代碼_AJAX相關
- 2023-02-07 Hive數據去重的兩種方式?(distinct和group?by)_數據庫其它
- 2022-10-30 淺析pytest?鉤子函數?之初始鉤子和引導鉤子_python
- 2022-12-02 AVFoundation?AVCaptureSession媒體捕捉_IOS
- 2022-01-04 圖片網絡地址轉base64和文件
- 2022-08-13 Redis中String字符串sdshdr結構體的講解
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支