Search code examples
croplarge-dataqgisvector-graphics

Crop large GeoJson layer using a standard machine


I need to extract a portion of a large GeoJson layer. I've tried multiple times with QGis on a 16Gb Ram intel i7 11th gen processor with no result. After about one hour if the program does not crash, I am able to load the file in QGis but no way of cropping it using the Clip vector function.

The layer in question is publicly available at https://github.com/microsoft/KenyaNigeriaBuildingFootprints (the file is kenya.geojson.zip) ... and the extent I need is 36.753446978,36.821255765,-1.330521423,-1.295234032 [EPSG:4326]


Solution

  • Here is how you can approach that with R.

    Get the data

    options(timeout=max(6000, getOption("timeout")))
    url <- "https://minedbuildings.blob.core.windows.net/africa/kenya.geojsonl.zip"
    download.file(url, basename(url), mode="wb")
    f <- unzip("kenya.geojsonl.zip")
    f 
    #"./kenya.geojsonl"
    

    Read a subset by specifying an extent. This takes a while because geojson is a rather inefficient format.

    library(terra)
    e <- ext(36.753446978, 36.821255765, -1.330521423, -1.295234032)
    
    ## with timer
    system.time(v <- vect("kenya.geojsonl", extent=e))
    #   user  system elapsed 
    #1390.03    8.28 1409.11 
    
    v
    # class       : SpatVector 
    # geometry    : polygons 
    # dimensions  : 18159, 0  (geometries, attributes)
    # extent      : 36.75303, 36.8228, -1.331356, -1.29447  (xmin, xmax, ymin, ymax)
    # source      : kenya.geojsonl
    # coord. ref. : lon/lat WGS 84 (EPSG:4326) 
    

    And then perhaps:

    writeVector(v, "buildings.shp")