从R

时间:2018-04-01 14:36:13

标签: r gis kml

我有一个填充了几何的数据库。我可以用

将它们提取为KML元素
require("RPostgreSQL")   
kml <- dbGetQuery(con, "SELECT parcel_id, ST_AsKML(version_perimeter) FROM fm_version")

然后我得到像

这样的东西
<Polygon><outerBoundaryIs><LinearRing><coordinates>-86.091271000000006,13.564622999999999 -86.091190999999995,13.564641999999999 -86.091098000000002,13.564653 -86.090960999999993,13.564734 -86.090782000000004,13.564819 -86.090694999999997,13.564823000000001 -86.090575999999999,13.564802 -86.090479999999999,13.564812999999999 -86.090399000000005,13.564826 -86.090208000000004,13.564868000000001 -86.090145000000007,13.564902999999999 -86.090083000000007,13.564971 -86.090036999999995,13.565058000000001 -86.089911999999998,13.565139 -86.089574999999996,13.565313 -86.089530999999994,13.565332 -86.089367999999993,13.565509 -86.089113999999995,13.56565 -86.088814999999997,13.565659999999999 -86.088769999999997,13.565678999999999 -86.088757000000001,13.565716999999999 -86.088763,13.565771 -86.088815999999994,13.565837999999999 -86.088939999999994,13.566008 -86.088987000000003,13.566153 -86.089055999999999,13.566273000000001 -86.089115000000007,13.566421999999999 -86.089149000000006,13.566490999999999 -86.089225999999996,13.566660000000001 -86.089245000000005,13.566666 -86.089388,13.566637 -86.089560000000006,13.566571 -86.089625999999996,13.566551 -86.089679000000004,13.566523 -86.089770999999999,13.566473 -86.089844999999997,13.566409999999999 -86.089969999999994,13.566292000000001 -86.090057000000002,13.566188 -86.090097,13.566041 -86.090132999999994,13.565981000000001 -86.090132999999994,13.565981000000001 -86.090232999999998,13.565937 -86.090371000000005,13.565830999999999 -86.090445000000003,13.565605 -86.090444000000005,13.565495 -86.090459999999993,13.565455 -86.090491999999998,13.565329 -86.090514999999996,13.565205000000001 -86.090479000000002,13.565159 -86.090508,13.565142 -86.090541000000002,13.565136000000001 -86.090744999999998,13.565045 -86.091149999999999,13.564912 -86.091303999999994,13.564830000000001 -86.091261000000003,13.564686 -86.091271000000006,13.564622999999999</coordinates></LinearRing></outerBoundaryIs></Polygon>

现在我需要一个完整的KML文件,我可以将其导入Google Fusion Tables。 如何从R?

导出填充了这些元素的完整KML文件

1 个答案:

答案 0 :(得分:2)

由于它已经采用KML元素格式的形式,您只需添加所需的标题即可将其转换为KML。怎么样?

kml_polygon成为从postGIS查询返回的kml字符串。

library(XML)

kml_content <-paste0('<kml xmlns="http://www.opengis.net/kml/2.2">
                       <Document>
                        <Folder>
                          <name>MyKML</name>
                         <Placemark>
                          <name>PointName</name>', kml_polygon, 
                         '</Placemark>
                        </Folder>
                       </Document>
                      </kml>')
xmlDoc <- xmlInternalTreeParse(kml_content)

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)

saveXML(xmlDoc,"mykml.kml")