Skip to content
Prev 29103 / 29559 Next

Convert geojson file to R

Ah ha, its a file with no line endings. Count the lines, zero:

$ wc -l countrymasks.geojson
0 countrymasks.geojson

Although adding a line ending to the file still produces the error.
Hmm. Even running it through `json_pp` to get it on multiple lines
results in the error:


$ more countrymasks_pp.geojson
{
   "features" : [
      {
         "geometry" : {
            "coordinates" : [
...
Reading layer `countrymasks_pp' from data source
  `/nobackup/rowlings/Downloads/SO/countrymasks_pp.geojson' using
driver `GeoJSON'
Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet,  :
  attempt to set index 210/210 in SET_STRING_ELT

Weirdly weird, `ogrinfo` thinks there's 210 features, but `st_read`
with my "query" fix gets 214...


$ ogrinfo -so -al countrymasks_pp.geojson
INFO: Open of `countrymasks_pp.geojson'
      using driver `GeoJSON' successful.

Layer name: countrymasks_pp
Geometry: Unknown (any)
Feature Count: 210   <--- 210 features
Reading query `select * from countrymasks_pp where 1 = 1'
from data source
`/nobackup/rowlings/Downloads/SO/countrymasks_pp.geojson' using driver
`GeoJSON'
Simple feature collection with 214 features and 15 fields
Geometry type: MULTIPOLYGON


I now notice some of the rows have incomplete property sets, for
example Paraguay has:

        "properties" : {
            "ISIPEDIA" : "PRY",
            "NAME" : "Paraguay",
            "an_crop" : "t",
            "an_range" : "t",
            "asap0_id" : 178,
            "asap_cntry" : "f",
            "g1_units" : 17,
            "isocode" : "PY",
            "km2_crop" : 80032,
            "km2_rang2" : 20908,
            "km2_tot" : 399367,
            "name0" : "Paraguay",
            "name0_shr" : "Paraguay"
         },

but Palestine has:

         "properties" : {
            "ISIPEDIA" : "PSE",
            "NAME" : "Palestine, State of",
            "isocode" : "PS",
            "km2_crop" : 84,
            "km2_rang2" : 3423,
            "km2_tot" : 6224,
            "name0" : "Palestine, State of"
         }

Then there "Caribbean island small states" which has a vector in its properties:

       "properties" : {
            "ISIPEDIA" : "CSID",
            "NAME" : "Caribbean island small states",
            "country_codes" : [
               "BRB",
               "DMA",
               "VIR",
               "BHS",
               "GRD",
               "ATG",
               "ANT",
               "LCA",
               "BLZ",
               "CYM",
               "VCT"
            ]

I wonder if these irregularities are causing odd problems with the
various gdal ways of parsing this...

Ugh. Let's all use geopackages....

B

On Tue, Nov 29, 2022 at 11:34 AM Edzer Pebesma
<edzer.pebesma at uni-muenster.de> wrote: