You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中读取微软建筑足迹.csv.gz并转换为GeoJSON

在R中读取并处理微软全球建筑足迹CSV文件

微软的全球建筑足迹文件以压缩CSV格式分发,其中geometry字段存储为GeoJSON字符串,下面是完整的R处理流程,对应你提到的Python脚本功能:

1. 读取压缩CSV文件

无需手动解压,直接用readr包读取.gz文件,支持本地文件或远程地址:

library(readr)
library(sf)
library(jsonlite)

# 替换为你的文件本地路径或远程地址
file_source <- "你的压缩CSV文件路径/地址"
buildings_df <- read_csv(file_source)

2. 解析GeoJSON几何列并转换为空间对象

CSV中的geometry是字符串格式的GeoJSON,我们需要将其转换为R的空间几何类型(sfc),再生成sf对象:

# 解析每个GeoJSON字符串为空间几何
buildings_df$geometry <- lapply(buildings_df$geometry, fromJSON) %>% 
  st_as_sfc()

# 转换为sf空间数据框
buildings_sf <- st_as_sf(buildings_df)

3. 展开Properties属性列(对应Python脚本的字段提取)

若CSV包含properties列(存储为JSON字符串),可以将其展开为单独的属性字段:

# 解析Properties为数据框并合并
properties_df <- lapply(buildings_df$properties, fromJSON) %>% 
  bind_rows()

buildings_df <- bind_cols(buildings_df, properties_df) %>% 
  select(-properties) # 删除原Properties列

# 重新生成包含所有属性的sf对象
buildings_sf <- st_as_sf(buildings_df)

4. 保存为GIS兼容格式

处理完成后,可保存为GeoPackage(推荐,无字段名长度限制)或Shapefile:

# 保存为GeoPackage
st_write(buildings_sf, "netherlands_buildings.gpkg", delete_layer = TRUE)

# 或保存为Shapefile(注意字段名需≤10字符)
st_write(buildings_sf, "netherlands_buildings.shp", delete_layer = TRUE)

常见问题提示

  • 若远程地址读取速度慢,建议先下载文件到本地再处理;
  • 若遇到几何解析错误,可检查geometry列是否存在格式异常的行,用dplyr::filter筛选后单独排查;
  • sf包默认使用WGS84坐标系(EPSG:4326),与微软建筑足迹的坐标系一致。

内容的提问来源于stack exchange,提问作者89_Simple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:48:23