解析KML文件并提取coordinates中的经纬度坐标信息
2018-01-03 11:14
387 查看
从googleEarh导出的kml文件
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>
<!-- Begin Style Definitions -->
<Style id="line1">
<LineStyle>
<color>FF808080</color>
<width>1</width>
</LineStyle>
</Style>
<Folder>
<name>Line Features</name>
<description>Line Features</description>
<Placemark>
<description>Unclassified Line Feature</description>
<styleUrl>#line1</styleUrl>
<LineString>
<coordinates>
115.9676728167,40.4840735806,0
115.9537842111,40.4823857889,0
115.9563131444,40.4703987611,0
115.9551753833,40.4662973972,0
115.9554458000,40.4636396361,0
115.9397187972,40.4622803889,0
115.9405904556,40.4562066889,0
115.9408371611,40.4549313694,0
115.9422346222,40.4538901583,0
115.9448324639,40.4514630028,0
115.9574561306,40.4524817889,0
115.9619367944,40.4513946639,0
</coordinates>
</LineString>
</Placemark>
</Folder>
</Document>
</kml>
做工程中遇到需要解析kml文件并提取其中的经纬度坐标信息的需求,从网上查了一圈资料发现都不好用,干脆自己写,用正则表达式regex和split()方法实现了坐标信息提取功能。
主要遇到的问题是coordinates中的InnerText文本中有\t \n \r \f 或多空格问题,需要将这些因素排除。
\f 匹配一个换页符
\n 匹配一个换行符
\r 匹配一个回车符
\t 匹配一个制表符
这些符号用正则表达式字符替换方式直接清除
Regex reg = new Regex("\f|\n|\r|\t");
string modified = reg.Replace(tt, "");
多空格问题用split的StringSplitOptions.RemoveEmptyEntries来清除
string[] ss = modified.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//清除空格
这样就能得到纯粹的x,y,z字符串数组了.OK。
***********************************************************************************************************************************************
想了2种思路,目前第1种思路已经测试通过了,第2种思路懒得写了,哪位如果有兴趣实现了的话告诉我一声喔,
,也算是饮水思源啦。
************************************************************************************************************************************************
代码如下:
private void KMLFileLoadButton_Click(object sender, EventArgs e)
{// 读取googleEarth的导出的kml/kmz路径文件
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "KML文件(*.kml)|*.kml|所有文件|*.*";
if (dlg.ShowDialog() != DialogResult.OK)
return;
string destPath = dlg.FileName;//CopyToRelavitePath(dlg.FileName);
if (destPath == null)
return;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(destPath);
XmlElement root = xmldoc.DocumentElement;
/////规范命名空间
XmlNode _document = root.GetElementsByTagName("Document")[0];
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
if (_document== null || _document.Attributes["xmlns"]==null)
{
nsmgr.AddNamespace("ns", root.Attributes["xmlns"].Value);
}
else
{
nsmgr.AddNamespace("ns", _document.Attributes["xmlns"].Value);
}
NGlbLine geo = new NGlbLine(3, false);
XmlNodeList xmlmark = root.GetElementsByTagName("Placemark");
for (int m = 0; m < xmlmark.Count; m++)
{
XmlNodeList xmlmarkChilds = xmlmark[m].ChildNodes;
for (int n = 0; n < xmlmarkChilds.Count; n++)
{
XmlNode node = xmlmarkChilds
;
if (node.Name == "LineString" || node.Name == "LineRing")
{
XmlNode coordsNode = node.FirstChild;
while (coordsNode != null && coordsNode.Name != "coordinates")
{
coordsNode = coordsNode.NextSibling;
}
if (coordsNode == null)
continue;
// 思路1 :用正则表达式去除字符串首位的制表符、换行符等符号,然后用' '来划分为string[]
string tt = coordsNode.InnerText;
Regex reg = new Regex("\f|\n|\r|\t");
string modified = reg.Replace(tt, "");
string[] ss = modified.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//清除空格
for (int cc = 0; cc < ss.Length; cc++)
{
string[] aa = ss[cc].Split(',');
if (aa.Length == 3)
geo.AddPoint(Convert.ToDouble(aa[0]), Convert.ToDouble(aa[1]), Convert.ToDouble(aa[2]));
else if (aa.Length == 2)
geo.AddPoint(Convert.ToDouble(aa[0]), Convert.ToDouble(aa[1]), 0);
}
// 思路2 :用regex正则表达式匹配 类似 115.9676728167,40.4840735806,0的样式字段
}
}
}
.............................................
}
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document>
<!-- Begin Style Definitions -->
<Style id="line1">
<LineStyle>
<color>FF808080</color>
<width>1</width>
</LineStyle>
</Style>
<Folder>
<name>Line Features</name>
<description>Line Features</description>
<Placemark>
<description>Unclassified Line Feature</description>
<styleUrl>#line1</styleUrl>
<LineString>
<coordinates>
115.9676728167,40.4840735806,0
115.9537842111,40.4823857889,0
115.9563131444,40.4703987611,0
115.9551753833,40.4662973972,0
115.9554458000,40.4636396361,0
115.9397187972,40.4622803889,0
115.9405904556,40.4562066889,0
115.9408371611,40.4549313694,0
115.9422346222,40.4538901583,0
115.9448324639,40.4514630028,0
115.9574561306,40.4524817889,0
115.9619367944,40.4513946639,0
</coordinates>
</LineString>
</Placemark>
</Folder>
</Document>
</kml>
做工程中遇到需要解析kml文件并提取其中的经纬度坐标信息的需求,从网上查了一圈资料发现都不好用,干脆自己写,用正则表达式regex和split()方法实现了坐标信息提取功能。
主要遇到的问题是coordinates中的InnerText文本中有\t \n \r \f 或多空格问题,需要将这些因素排除。
\f 匹配一个换页符
\n 匹配一个换行符
\r 匹配一个回车符
\t 匹配一个制表符
这些符号用正则表达式字符替换方式直接清除
Regex reg = new Regex("\f|\n|\r|\t");
string modified = reg.Replace(tt, "");
多空格问题用split的StringSplitOptions.RemoveEmptyEntries来清除
string[] ss = modified.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//清除空格
这样就能得到纯粹的x,y,z字符串数组了.OK。
***********************************************************************************************************************************************
想了2种思路,目前第1种思路已经测试通过了,第2种思路懒得写了,哪位如果有兴趣实现了的话告诉我一声喔,
,也算是饮水思源啦。
************************************************************************************************************************************************
代码如下:
private void KMLFileLoadButton_Click(object sender, EventArgs e)
{// 读取googleEarth的导出的kml/kmz路径文件
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "KML文件(*.kml)|*.kml|所有文件|*.*";
if (dlg.ShowDialog() != DialogResult.OK)
return;
string destPath = dlg.FileName;//CopyToRelavitePath(dlg.FileName);
if (destPath == null)
return;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(destPath);
XmlElement root = xmldoc.DocumentElement;
/////规范命名空间
XmlNode _document = root.GetElementsByTagName("Document")[0];
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
if (_document== null || _document.Attributes["xmlns"]==null)
{
nsmgr.AddNamespace("ns", root.Attributes["xmlns"].Value);
}
else
{
nsmgr.AddNamespace("ns", _document.Attributes["xmlns"].Value);
}
NGlbLine geo = new NGlbLine(3, false);
XmlNodeList xmlmark = root.GetElementsByTagName("Placemark");
for (int m = 0; m < xmlmark.Count; m++)
{
XmlNodeList xmlmarkChilds = xmlmark[m].ChildNodes;
for (int n = 0; n < xmlmarkChilds.Count; n++)
{
XmlNode node = xmlmarkChilds
;
if (node.Name == "LineString" || node.Name == "LineRing")
{
XmlNode coordsNode = node.FirstChild;
while (coordsNode != null && coordsNode.Name != "coordinates")
{
coordsNode = coordsNode.NextSibling;
}
if (coordsNode == null)
continue;
// 思路1 :用正则表达式去除字符串首位的制表符、换行符等符号,然后用' '来划分为string[]
string tt = coordsNode.InnerText;
Regex reg = new Regex("\f|\n|\r|\t");
string modified = reg.Replace(tt, "");
string[] ss = modified.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);//清除空格
for (int cc = 0; cc < ss.Length; cc++)
{
string[] aa = ss[cc].Split(',');
if (aa.Length == 3)
geo.AddPoint(Convert.ToDouble(aa[0]), Convert.ToDouble(aa[1]), Convert.ToDouble(aa[2]));
else if (aa.Length == 2)
geo.AddPoint(Convert.ToDouble(aa[0]), Convert.ToDouble(aa[1]), 0);
}
// 思路2 :用regex正则表达式匹配 类似 115.9676728167,40.4840735806,0的样式字段
}
}
}
.............................................
}
相关文章推荐
- 解析KML文件 , 获取Coordinates标签中的经纬度坐标值
- OGRE中mesh文件信息解析及提取之一
- GPS信息中提取经纬度坐标信息
- Java实现解析dcm医学影像文件并提取文件信息的方法示例
- 解析静态库(Lib)文件,提取出所有函数信息,组织成自定义格式文件
- C++提取文件中信息的方法
- 兄弟连LinuxStudyNote7.1.1_用户和用户组管理(1)-用户配置文件(1)-用户信息文件/etc/passwd详细解析与注意事项
- WKT文件解析 以及各个投影坐标的wkt参数查询
- js+html空间数据编码问题--以姓名为例(代码设涉及文件读取,文本数字提取,特别是文本x,y坐标的提取)
- 将坐标集合datatable 存为googleearth可打开的kml文件
- 纯Java环境解析apk文件信息
- javaweb_day7(JDBC)的配置信息提取到配置文件
- java 经纬度解析地址地理位置信息
- 2018-08-05 期 MapReduce实现每个单词在每个文件中坐标信息统计
- android中用HTTP请求将经纬度解析为具体地址街道信息!
- Android中音乐文件的信息详解【安卓源码解析二】
- python爬虫 如何解析json文件 json文件的解析提取和jsonpath的应用
- FTP服务器LIST指令获取的文件列表信息解析(UNIX格式)
- VBS 批量重命名文件&提取文件路径信息
- 栅格坐标信息文件 TFW或JGW的解释