1

我有一个 kml 文件,它的解压版本。它有数千个带有 XML 标记的图层,其中许多图层名称重复。

我想使用sf::st_read. 诀窍是st_read一次读取一个图层,并且需要一个图层名称。如果它们是唯一的,我很乐意遍历获取的图层名称st_layers(),但它们不是。

是否有另一种方法来指定所需的图层,或者是否有一种方法可以批量重命名具有唯一 ID 的所有图层?

谢谢。

根据下面接受的答案添加一些颜色。最初,我尝试使用“read_xml”来编辑<name>节点,但似乎没有找到它们。

我下载了 KMZ 文件,将其加载到 Google 地球中,然后将其另存为 KML 文件(“Reports.kml”)。这是我的第一个错误。生成的 KML 是制表符分隔的,这使read_xml. read_xml它是有效的 XML,但即使st_功能有效,也无法正确识别标签。最好unzip在 KMZ 文件上使用。以下是使用 Google-Earth 保存的版本发生的情况:

layers<-st_layers("reports.kml")

data_frame(name=layers$name, type=flatten_chr(layers$geomtype)) %>%
  count(name, type, sort=TRUE)
# A tibble: 1,358 x 3
#            name  type     n
#           <chr> <chr> <int>
# 1     July 2006          25
# 2  October 2006          25
# 3   August 2008          20
# 4     July 2009          19
# 5   August 2005          18
# 6   August 2007          18
# 7 November 2006          18
# 8  October 2004          17
# 9   August 2000          16
#10 November 2012          16
# ... with 1,348 more rows

kml<-read_xml("reports.kml")

xml_find_all(kml, ".//Folder/name")
# {xml_nodeset (0)}

没有什么!但是有一些东西:

xml_children(kml)
# {xml_nodeset (1)}
# [1] <Folder>\n  <name>Reports</name>\n  <open>1</open>\n  <Folder>\n    
# <name>Class A</name>\n  ...

解压后的 KMZ 会发生以下情况:

download.file(url="http://www.bfro.net/app/AllReportsKMZ.aspx",
                  destfile = "AllBFROReports.kmz",
                  mode="wb")
unzip("AllBFROReports.kmz",junkpaths = TRUE) #creates "doc.kml"


layers <- st_layers("doc.kml")

data_frame(name=layers$name, type=flatten_chr(layers$geomtype)) %>%
  count(name, type, sort=TRUE)
# # A tibble: 1,376 x 3
# name  type     n
# <chr> <chr> <int>
#   1     July 2006          25
# 2  October 2006          25
# 3   August 2008          20
# 4     July 2009          19
# 5   August 2005          18
# 6   August 2007          18
# 7 November 2006          18
# 8  October 2004          17
# 9   August 2000          16
# 10 November 2012          16
# # ... with 1,366 more rows

st_layers是一样的,但是现在节点已经正确找到了!

kml <- read_xml("doc.kml")
xml_find_all(kml, ".//Folder/name")
{xml_nodeset (3874)}
[1] <name>June 2000</name>
  [2] <name> 1995</name>
  [3] <name>February 2004</name>
  [4] <name>June 2004</name>
  [5] <name>February 2004</name>
  [6] <name>April 2008</name>
  [7] <name>July 2009</name>
  [8] <name>September 1981 and 1982</name>
  [9] <name>July 1999</name>
  [10] <name>November 1983</name>
  [11] <name>October 2000</name>
  [12] <name>August 1993</name>
  [13] <name> 79, 80, 99</name>
  [14] <name> 1978</name>
  [15] <name>November 1980</name>
  [16] <name>January 1997</name>
  [17] <name> 1990</name>
  [18] <name>December 1996</name>
  [19] <name> 2000</name>
  [20] <name> 2001</name>
  ...

现在,下面提供的答案就像一个魅力!

4

1 回答 1

2

一些 XML 手术就可以解决问题。

首先,显示问题:

library(sf)
library(xml2)
library(tidyverse)

layers <- st_layers("AllBFROReports.kml")

data_frame(name=layers$name, type=flatten_chr(layers$geomtype)) %>%
  count(name, type, sort=TRUE)
## # A tibble: 1,376 x 3
##             name  type     n
##            <chr> <chr> <int>
##  1     July 2006          25
##  2  October 2006          25
##  3   August 2008          20
##  4     July 2009          19
##  5   August 2005          18
##  6   August 2007          18
##  7 November 2006          18
##  8  October 2004          17
##  9   August 2000          16
## 10 November 2012          16
## # ... with 1,366 more rows

啊。一个非常卑鄙的人制作了那个文件。

现在,以“原始”形式阅读它:

kml <- read_xml("AllBFROReports.kml")

为每个层名称添加一个顺序索引号:

idx <- 0
xml_find_all(kml, ".//Folder/name") %>%
  walk(~{
    idx <<- idx + 1
    xml_text(.x) <- sprintf("%s-%s", idx, xml_text(.x))
  })

创建一个新文件:

write_xml(kml, "AllBFROReports-unique.kml")

证明它有效:

layers2 <- st_layers("AllBFROReports-unique.kml")

data_frame(name=layers2$name, type=flatten_chr(layers2$geomtype)) %>%
  count(name, type, sort=TRUE)
## # A tibble: 3,874 x 3
##                  name     type     n
##                 <chr>    <chr> <int>
##  1        1-June 2000              1
##  2   10-November 1983              1
##  3 100-September 1992              1
##  4  1000-October 1987              1
##  5  1001-October 1987              1
##  6  1002-October 1979              1
##  7     1003-June 1993 3D Point     1
##  8         1004- 1982 3D Point     1
##  9         1005- 1982 3D Point     1
## 10   1006-August 1977 3D Point     1
## # ... with 3,864 more rows

用新的索引名称读入一层:

st_read("AllBFROReports-unique.kml", layer = "10-November 1983")
## Reading layer `10-November 1983' from data source `/Users/bob/Desktop/AllBFROReports-unique.kml' using driver `KML'
## Simple feature collection with 2 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XYZ
## bbox:           xmin: -86.4677 ymin: 34.9484 xmax: -86.4441 ymax: 34.9637
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
于 2018-01-13T13:23:54.673 回答