Skip to content

Instantly share code, notes, and snippets.

@kadyb
Last active September 19, 2023 20:28
Show Gist options
  • Save kadyb/fa9346ef5aafa5b28b7d73163265c3cc to your computer and use it in GitHub Desktop.
Save kadyb/fa9346ef5aafa5b28b7d73163265c3cc to your computer and use it in GitHub Desktop.
Download, load and plot Google Buildings data in R
library(sf)
library(leaflet)
library(data.table)
## Morocco buildings (1.21 GB)
url = "https://data.humdata.org/dataset/c6059279-4521-4b39-8b18-d43aedc012c3/resource/6af2ee44-1807-4fb7-a647-a42fc7ffbff0/download/open_buildings_v3_morocco_epicenter.csv.gz"
df = fread(url, nThread = 4, header = TRUE, sep = ",")
df = st_as_sf(df, crs = "EPSG:4326", wkt = "geometry") # convert to spatial object
write_sf(df, "morocco.gpkg") # save as geopackage
## select one city by extent
long_range = c(-10.08, -10.04)
lat_range = c(28.97, 29.01)
idx = df$longitude %between% long_range & df$latitude %between% lat_range
idx = which(idx) # 35395 buildings
## interactive map
leaflet(df[idx, ]) |>
addPolygons(smoothFactor = 0, weight = 2, color = "black") |>
addProviderTiles(providers$Esri.WorldImagery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment