文章目录
1、简介
https://developers.google.cn/kml/documentation/kmzarchives?hl=zh-cn
1.1 kml简介
KML 是一种文件格式,用于在地球浏览器(例如 Google 地球、Google 地图和 Google 地图移动版)中显示地理数据。KML 使用包含嵌套的元素和属性的结构(基于标记),并符合 XML 标准。所有标记都区分大小写,并且必须与 KML 参考中列出的完全一样。该参考指出了哪些标记是可选的。在给定元素内,标记必须按照参考中列出的顺序显示。
Google 地球和 Google 地图可以直接读取 KML 和 KMZ 文件,并可将文件保存为 KMZ 格式。默认情况下,主 KML 文件名为 doc.kml。
KMZ 文件包含主 KML 文件以及0个或多个用 ZIP 格式打包成一个单元的支持文件(称为归档)。
一个KMZ文件的内容结构可能如下:
1.2 功能点
1.2.1 地标
地标是 Google 地球中最常用的地图项之一。它使用黄色图钉作为图标,在地球表面标记位置。最简单的地标只包含一个 元素,用于指定地标的地点。您可以指定地标的名称和自定义图标,还可以为地标添加其他几何元素。
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>爱看书的杨小羊</name>
<description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
</kml>
- 3d地球上加载kml文件显示如下:
1.2.2 地面叠加层
通过地面叠加层,您可在 Google 地球的地形上“叠加”图片。 元素包含指向叠加图片 .jpg 文件的链接。
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Folder>
<name>爱看书的杨小羊</name>
<description>Examples of ground overlays</description>
<GroundOverlay>
<name>Large-scale overlay on terrain</name>
<description>Overlay shows Mount Etna erupting on July 13th, 2001.</description>
<Icon>
<href>http://developers.google.com/kml/documentation/images/etna.jpg</href>
</Icon>
<LatLonBox>
<north>37.91904192681665</north>
<south>37.46543388598137</south>
<east>15.35832653742206</east>
<west>14.60128369746704</west>
<rotation>-0.1556640799496235</rotation>
</LatLonBox>
</GroundOverlay>
</Folder>
</kml>
- 3d地球上加载kml文件显示如下:
1.2.3 路径
可以在 Google 地球中创建多种不同类型的路径,并且可轻松地利用您的数据充分发挥创造力。在 KML 中,路径是用 元素创建的。
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Paths</name>
<description>Examples of paths. Note that the tessellate tag is by default
set to 0. If you want to create tessellated lines, they must be authored
(or edited) directly in KML.</description>
<Style id="yellowLineGreenPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Placemark>
<name>爱看书的杨小羊</name>
<description>Transparent green wall with yellow outlines</description>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates> -112.2550785337791,36.07954952145647,2357
-112.2549277039738,36.08117083492122,2357
-112.2552505069063,36.08260761307279,2357
-112.2564540158376,36.08395660588506,2357
-112.2580238976449,36.08511401044813,2357
-112.2595218489022,36.08584355239394,2357
-112.2608216347552,36.08612634548589,2357
-112.262073428656,36.08626019085147,2357
-112.2633204928495,36.08621519860091,2357
-112.2644963846444,36.08627897945274,2357
-112.2656969554589,36.08649599090644,2357 </coordinates>
</LineString>
</Placemark>
</Document>
</kml>
- 3d地球上加载kml文件显示如下:
1.2.4 多边形
可以使用多边形来创建简单的建筑物和其他形状。
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>爱看书的杨小羊</name>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-77.05788457660967,38.87253259892824,100
-77.05465973756702,38.87291016281703,100
-77.05315536854791,38.87053267794386,100
-77.05552622493516,38.868757801256,100
-77.05844056290393,38.86996206506943,100
-77.05788457660967,38.87253259892824,100
</coordinates>
</LinearRing>
</outerBoundaryIs>
<innerBoundaryIs>
<LinearRing>
<coordinates>
-77.05668055019126,38.87154239798456,100
-77.05542625960818,38.87167890344077,100
-77.05485125901024,38.87076535397792,100
-77.05577677433152,38.87008686581446,100
-77.05691162017543,38.87054446963351,100
-77.05668055019126,38.87154239798456,100
</coordinates>
</LinearRing>
</innerBoundaryIs>
</Polygon>
</Placemark>
</kml>
- 3d地球上加载kml文件显示如下:
2、下载和编译
https://gdal.org/drivers/vector/libkml.html
https://github.com/libkml/libkml
https://github.com/google/libkml
git clone https://github.com/libkml/libkml.git
cd libkml
mkdir build
cd build
cmake ..
生成vs工程文件如下:
vs工程文件打开如下:
编译成功如下:
3、C++测试
<?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" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Folder>
<name>tab.kml</name>
<Folder>
<name>tab1</name>
<Folder>
<name>123</name>
<open>1</open>
<Folder>
<name>12</name>
<name>未命名矩形1</name>
</Folder>
</Folder>
<Folder>
<name>MIF测试</name>
</Folder>
</Folder>
<Folder>
<name>tab2</name>
</Folder>
</Folder>
</kml>
编写C++代码遍历Folder对象,如下:
#include <iostream>
#include <string>
#include <iomanip>
#include "boost/scoped_ptr.hpp"
#include "kml/dom.h"
#include "kml/engine.h"
#include "kml/base/file.h"
#include "kml/dom/folder.h"
#include "kml/dom/kml_cast.h"
using kmlengine::KmlFile;
using kmlengine::KmlFilePtr;
using std::cout;
using std::endl;
void TraverseFolders(const kmldom::FolderPtr folderParent, int level) {
const kmldom::ContainerPtr container = kmldom::AsContainer(folderParent);
for (size_t j = 0; j < container->get_feature_array_size(); ++j) {
kmldom::ElementPtr feature = container->get_feature_array_at(j);
if (feature->Type() == kmldom::Type_Folder) {
kmldom::FolderPtr folder = kmldom::AsFolder(feature);
std::cout << std::setw(level*2) << std::setfill('\t') << level << ", Folder: " << folder->get_name() << std::endl;
TraverseFolders(folder, level + 1);
}
}
}
int main(int argc, char** argv) {
// Read the file content.
std::string file_data;
if (!kmlbase::File::ReadFileToString("d:\\doc.kml", &file_data)) {
cout << argv[1] << " read failed" << endl;
return 1;
}
// Parse it.
std::string errors;
KmlFilePtr kml_file = KmlFile::CreateFromParse(file_data, &errors);
if (!kml_file) {
cout << errors;
return 1;
}
// 获取KML文件的根元素
kmldom::ElementPtr root = kml_file->get_root();
kmldom::KmlPtr kml = kmldom::AsKml(root);
kmldom::FeaturePtr feature = kml->get_feature();
kmldom::FolderPtr folder = kmldom::AsFolder(feature);
TraverseFolders(folder, 0);
return 0;
}
4、Python测试
Simplekml 是一个 python 包,它使您能够以尽可能少的工作量生成 KML。
4.1 安装库
安装simplekml库如下:
pip install simplekml
4.2 测试1
import simplekml
kml = simplekml.Kml()
kml.newpoint(name="Kirstenbosch", coords=[(18.432314,-33.988862)])
kml.save("yxy.kml")
生成yxy.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 id="1">
<Placemark id="3">
<name>Kirstenbosch</name>
<Point id="2">
<coordinates>18.432314,-33.988862,0.0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
4.2 测试2
import simplekml
kml = simplekml.Kml()
fol = kml.newfolder(name='A Folder')
pnt = fol.newpoint()
kml.save("yxy_Folder.kml")
生成yxy_Folder.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 id="1">
<Folder id="2">
<name>A Folder</name>
<Placemark id="4">
<Point id="3">
<coordinates>0.0, 0.0, 0.0</coordinates>
</Point>
</Placemark>
</Folder>
</Document>
</kml>
4.3 测试3
import simplekml
kml = simplekml.Kml()
ground = kml.newgroundoverlay(name='GroundOverlay')
ground.icon.href = 'http://simplekml.googlecode.com/hg/samples/resources/smile.png'
ground.gxlatlonquad.coords = [(18.410524,-33.903972),(18.411429,-33.904171),
(18.411757,-33.902944),(18.410850,-33.902767)]
# or
#ground.latlonbox.north = -33.902828
#ground.latlonbox.south = -33.904104
#ground.latlonbox.east = 18.410684
#ground.latlonbox.west = 18.411633
#ground.latlonbox.rotation = -14
kml.save("yxy_GroundOverlay.kml")
生成yxy_GroundOverlay.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 id="1">
<GroundOverlay id="2">
<name>GroundOverlay</name>
<Icon id="3">
<href>http://simplekml.googlecode.com/hg/samples/resources/smile.png</href>
</Icon>
<gx:LatLonQuad>
<coordinates>18.410524,-33.903972,0.0 18.411429,-33.904171,0.0 18.411757,-33.902944,0.0 18.41085,-33.902767,0.0</coordinates>
</gx:LatLonQuad>
</GroundOverlay>
</Document>
</kml>
第三届机器人、人工智能与智能控制国际会议(RAIIC 2024)将于2024年7月5-7日中国·绵阳举行。 RAIIC 2024是汇聚业界和学术界的顶级论坛,会议将邀请国内外著名专家就以传播机器人、人工智能与智能控制领域的技术进步、研究成果和应用做专题报告,同时进行学术交流。诚邀国内外相关高校和科研院所的科研人员、企业工程技术人员等参加会议。
大会网站:更多会议详情
时间地点:中国-绵阳|2024年7月5-7日
由河南省科学院、河南大学、郑州航空工业管理学院主办,河南省产学研人工智能研究院、河南大学人工智能学院、郑州航空工业管理学院计算机学院承办的第四届人工智能,大数据与算法国际学术会议 (CAIBDA 2024)将于2024年7月5-7日于中国郑州隆重举行。CAIBDA 2024致力于为人工智能,大数据与算法等相关领域的学者,工程师和从业人员提供一个分享最新研究成果的平台。
大会网站:更多会议详情
时间地点:中国-郑州|2024年7月5-7日
2024第四届人工智能、自动化与高性能计算国际会议(AIAHPC 2024)将于2024年7月19-21日在中国·珠海召开。
大会网站:更多会议详情
时间地点:中国珠海-中山大学珠海校区|2024年7月19-21日
结语
如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;
╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地
//(ㄒoㄒ)//,就在评论处留言,作者继续改进;
o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;
(✿◡‿◡)
感谢各位大佬童鞋们的支持!
( ´ ▽´ )ノ ( ´ ▽´)っ!!!