rgeojson

Convert .csv.gz to GeoJSON with R


I am trying to read the file from this link in R(Microsoft Office building footprint). It is supposed to be a geojson file.

https://minedbuildings.blob.core.windows.net/global-buildings/2023-06-27/global-buildings.geojsonl/RegionName%3DNetherlands/quadkey%3D120200313/part-00138-7b73ad6b-e7c8-4e22-9fa2-01d140ca911d.c000.csv.gz

After unzipping the file, I get a csv. How do I read this file in R.

There's python code to read the file but I am not able to replicated this in R. https://github.com/microsoft/GlobalMLBuildingFootprints/blob/main/scripts/make-gis-friendly.py


Solution

  • So it turns out GeoJSON files are (fortunately) very easy to read and understand, and the csv file you have is basically all of the child elements of the GeoJSON file, without the wrapping. So we provide that wrapping, and everything works out fine:

    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)
    

    map of netherlands Credit: Wikipedia (for providing a good example of GeoJSON for me!)