我正在尝试从 R(Microsoft Office 建筑足迹)中的此链接读取文件。它应该是一个 geojson 文件。
解压文件后,我得到一个 csv。我如何在 R 中读取这个文件。
有 python 代码来读取该文件,但我无法在 R 中复制它。 https://github.com/microsoft/GlobalMLBuildingFootprints/blob/main/scripts/make-gis-Friendly.py
事实证明,GeoJSON 文件非常容易阅读和理解,并且您拥有的 csv 文件基本上是 GeoJSON 文件的所有子元素,没有换行。所以我们提供了包装,一切都很顺利:
pacman::p_load(tidyverse, leaflet)
file_path = "part-00138-7b73ad6b-e7c8-4e22-9fa2-01d140ca911d.c000.csv"
# in an excellent turn of events, it turns out geojson files are easy to read
readLines(file_path) %>%
paste(collapse = ", ") %>%
{paste0('{"type": "FeatureCollection",
"features": [', ., "]}")} -> j
# plot the map
leaflet() %>%
addTiles() %>%
addGeoJSON(j) %>%
setView(lng = 5.366252941303844, lat = 53.40141145330422, zoom = 11)
来源:Wikipedia(为我提供了一个很好的 GeoJSON 示例!)