如何在R中正确读取KML文件,或将集总变量分离为列

时间:2018-06-09 14:34:38

标签: kml sf

我使用以下内容读入KML文件:

clinics = st_read(dsn = "Data/clinics-kml.kml","CLINICS")

但是,我的所有变量(坐标除外)都被归为Description下的1列(见下面的链接)。

将变量分开的最佳方法是什么?或者,有没有办法正确导入KML文件以避免此问题? You may view the screenshot of the problem here.

2 个答案:

答案 0 :(得分:1)

问题(或可能不是问题)是 Description 列有一个html表作为每个观察值的字符串。如果您想解析该html字符串并获得漂亮的表格(例如在创建交互式网络地图时),那很好。但是,如果您只想要内部数据,那可能会很头疼。

因此,可以按照以下步骤在R中完成所有过程:

  1. 从互联网下载KML文件
  2. 解压缩下载的文件
  3. 将KML文件作为空间对象读取
  4. 获取每个观察值的属性
  5. 将每个观察的属性绑定为新列

所有代码均已注释,如下所示:

library(tidyverse)
library(sf)
library(mapview)
library(rvest)
library(httr)

# 1) Download the kml file
moh_chas_clinics <- GET("https://data.gov.sg/dataset/31e92629-980d-4672-af33-cec147c18102/download",
                        write_disk(here::here("moh_chas_clinics.zip"), overwrite = TRUE))

# 2) Unzip the downloaded zip file 
unzip(here::here("moh_chas_clinics.zip"))

# 3) Read the KML file as a Spatial object
moh_chas_clinics <- read_sf(here::here("chas-clinics-kml.kml"))

# Watch data
moh_chas_clinics %>%
  glimpse()

# See map
mapview(moh_chas_clinics)

# 4) Get the attributes for each observation

# Option a) Using a simple lapply
attributes <- lapply(X = 1:nrow(moh_chas_clinics), 
                     FUN = function(x) {

                       moh_chas_clinics %>% 
                         slice(x) %>%
                         pull(Description) %>%
                         read_html() %>%
                         html_node("table") %>%
                         html_table(header = TRUE, trim = TRUE, dec = ".", fill = TRUE) %>%
                         as_tibble(.name_repair = ~ make.names(c("Attribute", "Value"))) %>% 
                         pivot_wider(names_from = Attribute, values_from = Value)

                     })

# Option b) Using a Parallel lapply (faster)
future::plan("multisession")

attributes <- future.apply::future_lapply(X = 1:nrow(moh_chas_clinics), 
                                          FUN = function(x) {

                                            moh_chas_clinics %>% 
                                              slice(x) %>%
                                              pull(Description) %>%
                                              read_html() %>%
                                              html_node("table") %>%
                                              html_table(header = TRUE, trim = TRUE, dec = ".", fill = TRUE) %>%
                                              as_tibble(.name_repair = ~ make.names(c("Attribute", "Value"))) %>% 
                                              pivot_wider(names_from = Attribute, values_from = Value)

                                          })

# 5) Bind the attributes to each observation as new columns
moh_chas_clinics_attr <- 
  moh_chas_clinics %>%
  bind_cols(bind_rows(attributes)) %>%
  select(-Description)

# Watch new data
moh_chas_clinics_attr %>%
  glimpse()

# New map
mapview(moh_chas_clinics_attr, 
        zcol = "CLINIC_PROGRAMME_CODE", 
        layer.name = "Clinic Programme Code")

以最终地图为例,显示一个点的所有属性并用“诊所程序代码”进行着色:

enter image description here

答案 1 :(得分:0)

通过使用QGIS将KML转换为SHP,找出了另一种方法。然后将其作为SHP读入R。