从 R 中的 Google Earth KML 文件中提取详细信息

克里斯

我正在尝试从 Google Earth kml 文件中的一系列位置获取详细信息。

获取 id 和坐标有效,但对于位置名称(位于描述的第一个表格单元格(td 标记)中),当我为所有位置执行此操作时,它为所有位置返回相同的值(斯特拉特福Road - 第一个位置的名称)。

library(sf)
library(tidyverse)
library(rvest)

removeHtmlTags <- function(htmlString) {
  return(gsub("<.*?>", "", htmlString))
}
getHtmlTableCells<- function(htmlString) {
  # Convert html to html doc
  htmldoc <- read_html(htmlString)
  # get html for each cell (i.e. within <td></td>)
  table_cells_with_tags <- html_nodes(htmldoc, "td")
  # remove the html tags (<td></td>)
  return(removeHtmlTags(table_cells_with_tags))[1]
}

download.file("https://www.dropbox.com/s/ohipb477kqrqtlz/AQMS_2019.kml?dl=1","aqms.kml")
locations <- st_read("aqms.kml", stringsAsFactors = FALSE) %>%
  rename(id = Name) %>%
  mutate(latitude = st_coordinates(geometry)[,1],
         longitiude = st_coordinates(geometry)[,2],
         name = getHtmlTableCells(Description)[1]) %>%
  st_drop_geometry()

现在,如果我在特定位置使用该函数并获取第一个表格单元格 (td),则它可以工作,返回 Stratford Road 和 Selly Oak 作为第一个,如下所示。

getHtmlTableCells(locations$Description[1])[1]
getHtmlTableCells(locations$Description[2])[1]

我究竟做错了什么?

克里斯

read_html未矢量化 - 它不接受不同 html 的矢量进行解析。我们可以apply对向量的每个元素进行函数处理:

locations <- st_read("aqms.kml", stringsAsFactors = FALSE) 

locations %>%
  rename(id = Name) %>%
  mutate(latitude = st_coordinates(geometry)[,1],
         longitiude = st_coordinates(geometry)[,2],
         name = sapply(Description, function(x) getHtmlTableCells(x)[1])) %>%
  st_drop_geometry()

#>     latitude longitiude                      name
#> 1  -1.871622   52.45920            Stratford Road
#> 2  -1.934559   52.44513  Selly Oak (Bristol Road)
#> 3  -1.830070   52.43771              Acocks Green
#> 4  -1.898731   52.48180               Colmore Row
#> 5  -1.896764   52.48607        St Chads Queensway
#> 6  -1.891955   52.47990     Moor Street Queensway
#> 7  -1.918173   52.48138       Birmingham Ladywood
#> 8  -1.902121   52.47675       Lower Severn Street
#> 9  -1.786413   52.56815                  New Hall
#> 10 -1.874989   52.47609 Birmingham A4540 Roadside

或者,由于您在函数中无论如何都使用了正则表达式,因此您可以使用stringr::str_extract来提取您的文本(已经矢量化)。

library(sf)
library(tidyverse)

locations <- st_read("aqms.kml", stringsAsFactors = FALSE) %>%
  rename(id = Name) %>%
  mutate(latitude = st_coordinates(geometry)[,1],
         longitiude = st_coordinates(geometry)[,2],
         name = str_extract(Description, '(?<=Location</td> <td>)[^<]+')) %>%
  st_drop_geometry()

其中(?<=Location</td> <td>)对于一个回顾后位置先于我们的td标签的名字,并[^<]+匹配任何东西下面的下一个标签名称

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用KML在Google Earth中绘制圆顶

如何使用python将kml文件加载到Google Earth中

将整个企业的文件夹中的多级KML文件自动添加到Google Earth中

用于KML Google Earth的XML的SQL查询

如何将Google Earth KML文件添加到html iframe?

从R中的.kml提取CDATA标记的值

在Google Earth应用程序上不显示KML

R:从 nlme fit 中的详细输出中提取信息

使用ezdxf从dxf文件中的块引用中提取HATCH详细信息

在 Google Earth Engine 中提取(裁剪)

.txt文件gps数据在Google Earth中不可读

如何通过“中”参数从Google Maps API下载KML文件?

如何提取文件中薪资最高的员工的详细信息?

使用R处理Google Earth Engine数据

在Python中提取多个图像文件名中的详细信息,并将其添加为数据集的标签

在64位系统中安装Google Earth

Google Earth Engine中的栅格比例值

从R中的NetCDF文件中提取特定于站点的信息

如何在Google Earth Engine数据中提取或过滤日期

使用Google Earth API查找地形信息

从“ cowplot”中的组合图中提取单个图的详细信息以进行单元测试

在 Android 中从联系人中提取详细信息时获取错误数据

从 Django 中的完整性错误中提取详细信息

.kml文件中带有地标的Google Maps Polygon(使用geoxml3和kmllayer)

在Android版Google Maps API中仅显示kml文件的一部分

在Google Chrome中显示HTTP错误详细信息

使用Swift在Google Map中获取位置详细信息

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

从文件中读取详细信息