gpt4 book ai didi

xml - 使用 R 的 XML 包从头开始构建具有完整命名空间信息的 KML

转载 作者:行者123 更新时间:2023-12-04 16:52:16 25 4
gpt4 key购买 nike

我使用 KML {raster} 将光栅文件写入 KML,并尝试使用 MAPC2MAPC 将它们转换为在移动设备上使用。
但是,MAPC2MAPC 显然不适用于这些导出的 KML。我注意到我导出的 KML 和 Google Earth 导出的 KML 之间的唯一区别(由 MAPC2MAPC 毫无问题地转换)是命名空间定义。然后我手动修复了 kml 中的命名空间并且 MAPC2MAPC 正在工作。现在我尝试在 R 中以编程方式修复命名空间,但没有成功。

默认 KML 的顶部节点如下所示:

<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">

KML {raster} 导出之一,如:
<kml xmlns="http://www.opengis.net/kml/2.2">

我试图解析 KML 并添加命名空间,使第二个看起来像第一个顶级节点,但它不起作用..

在此先感谢您的指点!

最佳答案

试试这个:

library(XML)
# just a sample kml file... needs to be well-formed
z <-
'<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Folder>
<name>Some Name</name>
<Placemark>
<name>Jackson</name>
<Polygon><outerBoundaryIs><LinearRing><coordinates>-91.151982,44.136728 -91.152627,44.158372 -91.152677,44.170618 -91.152732,44.183928 -91.152372,44.187182 -91.15244,44.187307 -91.152495,44.208702 -91.152892,44.222784 -91.153829,44.24758 -91.165786,44.247429 -91.165936,44.2546 -91.166159,44.272973 -91.166311,44.303743 -91.166469,44.306118 -91.166056,44.30943 -91.165995,44.33161 -91.16614,44.334932 -91.166128,44.335102 -91.16648,44.339077 -91.165909,44.343314 -91.166149,44.34968 -91.165604,44.37626 -91.165545,44.378794 -91.164813,44.418752 -91.164768,44.42248 -91.164873,44.451251 -91.164858,44.45132 -91.164915,44.451544 -91.165155,44.473148 -91.165187,44.480189 -91.165197,44.482528 -91.165636,44.496874 -91.1657,44.498476 -91.16588,44.509756 -91.165916,44.528032 -91.165696,44.550109 -91.165666,44.565612 -91.165408,44.567894 -91.165713,44.567989 -91.165619,44.596987 -91.130969,44.596692 -91.125674,44.596666 -91.119816,44.596285 -91.114632,44.596343 -91.043609,44.596609 -91.026595,44.596426 -91.009307,44.59663 -90.992352,44.596619 -90.990775,44.596447 -90.972986,44.596516 -90.94268,44.596582 -90.92235,44.596293 -90.922637,44.591081 -90.922579,44.580701 -90.922618,44.543798 -90.922775,44.512564 -90.923104,44.509838 -90.801852,44.509573 -90.80194,44.458502 -90.801949,44.429518 -90.801928,44.422301 -90.764289,44.422345 -90.760977,44.422336 -90.72215,44.422293 -90.719005,44.422213 -90.705154,44.422289 -90.680591,44.422218 -90.619465,44.422127 -90.558746,44.42221 -90.497221,44.422613 -90.436472,44.422546 -90.397588,44.423279 -90.31605,44.424673 -90.317397,44.337429 -90.31766,44.291998 -90.317938,44.249963 -90.318107,44.248791 -90.312035,44.248758 -90.31257,44.240933 -90.3125,44.194598 -90.312225,44.19244 -90.312083,44.187037 -90.31249,44.184046 -90.312575,44.155284 -90.435728,44.161022 -90.461889,44.16082 -90.488949,44.160944 -90.49033,44.16076 -90.546446,44.160284 -90.553421,44.160309 -90.591977,44.160163 -90.653047,44.159585 -90.672674,44.159319 -90.774673,44.159056 -90.792783,44.158862 -90.817517,44.158692 -90.817713,44.158644 -90.887873,44.158429 -90.888823,44.158484 -90.8929,44.158323 -90.906152,44.158316 -90.912893,44.154445 -90.916033,44.153313 -90.919834,44.150576 -90.922496,44.147593 -90.927157,44.14646 -90.930865,44.14648 -90.936741,44.147456 -90.939122,44.147337 -90.942552,44.144407 -90.945184,44.146673 -90.947547,44.147332 -90.948945,44.146008 -90.950352,44.143296 -90.952723,44.141954 -90.956902,44.141823 -90.958111,44.140704 -90.958909,44.137424 -90.962404,44.136491 -90.9641,44.137405 -90.966729,44.136248 -90.96894,44.133218 -90.97016,44.129621 -90.971909,44.128667 -90.976753,44.129869 -90.978639,44.127998 -90.978398,44.12666 -90.97364,44.124266 -90.971168,44.122493 -90.967644,44.119582 -90.968004,44.118533 -90.970286,44.117542 -90.973565,44.116895 -90.972764,44.070775 -91.023637,44.071134 -91.04639,44.071139 -91.057154,44.071141 -91.090641,44.071239 -91.091691,44.071192 -91.111766,44.071094 -91.151768,44.071014 -91.151843,44.079656 -91.151901,44.095396 -91.151975,44.114529 -91.151982,44.136728</coordinates></LinearRing></outerBoundaryIs></Polygon>
</Placemark>
</Folder>
</Document>
</kml>'
xmlDoc <- xmlInternalTreeParse(z)

# this is the important bit...
ns <- c(gx="http://www.google.com/kml/ext/2.2",
kml="http://www.opengis.net/kml/2.2",
atom="http://www.w3.org/2005/Atom")
ensureNamespace(xmlDoc, ns)
# save it...
saveXML(xmlDoc,"sample.kml")

这会添加 gx:atom: 命名空间,但不会添加 kml: 命名空间(因为它已经存在)。所以我不确定它是否可以在移动设备上运行,但可以尝试一下。

关于xml - 使用 R 的 XML 包从头开始构建具有完整命名空间信息的 KML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21632399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com