Skip to content

Instantly share code, notes, and snippets.

@sckott
Created March 6, 2015 17:35
Show Gist options
  • Save sckott/ce796d7672347f33ab61 to your computer and use it in GitHub Desktop.
Save sckott/ce796d7672347f33ab61 to your computer and use it in GitHub Desktop.

geojsonio has two ways to validate geojson (specifically geojson, not json structure itself). Via a web API from geojsonlint, or locally using the Mapbox's javascript geojsonhint library (included in the package).

Both can act directly on many different inputs, including character strings, lists, data.frame's, all the sp class spatial objects.

Install and load

install_github("ropensci/geojsonio")
library("geojsonio")

geojsonlint.com - web API

good and bad, from character string

# good
validate('{"type": "Point", "coordinates": [-100, 80]}')
#> [1] TRUE
# bad
validate('{"type": "Rhombus", "coordinates": [[1, 2], [3, 4], [5, 6]]}')
#> [1] TRUE

from a URL

url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
validate(as.location(url))
#> [1] FALSE
#> attr(,"err")
#> [1] "lexical error: invalid char in json text.\n                                       https://raw.githubusercontent.c\n                     (right here) ------^\n"

bad FeatureCollection

x <- jsonlite::minify('{ "type": "FeatureCollection" }')
validate(x)
#> [1] TRUE

geojsonhint - local

good and bad, from character string

# good
lint('{"type": "Point", "coordinates": [-100, 80]}')
#> [1] "valid"
# bad
lint('{"type": "Rhombus", "coordinates": [[1, 2], [3, 4], [5, 6]]}')
#> $message
#> [1] "The type Rhombus is unknown"
#> 
#> $line
#> [1] 1

from a URL

url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
lint(as.location(url))
#> [1] "valid"

bad FeatureCollection

x <- jsonlite::minify('{ "type": "FeatureCollection" }')
lint(x)
#> $message
#> [1] "\"features\" property required"
#> 
#> $line
#> [1] 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment