geojson_read: Read geojson or other formats from a local file or a URL

Description Usage Arguments Details Value Linting GeoJSON File size See Also Examples

View source: R/geojson_read.R

Description

Read geojson or other formats from a local file or a URL

Usage

1
2
3
4
5
6
7
8
geojson_read(
  x,
  parse = FALSE,
  what = "list",
  stringsAsFactors = FALSE,
  query = NULL,
  ...
)

Arguments

x

(character) Path to a local file or a URL.

parse

(logical) To parse geojson to data.frame like structures if possible. Default: FALSE

what

(character) What to return. One of "list", "sp" (for Spatial class), or "json". Default: "list". "list" "and" sp run through package sf. if "json", returns json as character class

stringsAsFactors

Convert strings to Factors? Default FALSE.

query

(character) A SQL query, see also postgis

...

Further args passed on to sf::st_read()

Details

This function supports various geospatial file formats from a URL, as well as local kml, shp, and geojson file formats.

Value

various, depending on what's chosen in what parameter

Linting GeoJSON

If you're having trouble rendering GeoJSON files, ensure you have a valid GeoJSON file by running it through the package geojsonlint, which has a variety of different GeoJSON linters.

File size

We previously used file_to_geojson() in this function, leading to file size problems; this should no longer be a concern, but let us know if you run into file size problems

See Also

topojson_read(), geojson_write() postgis

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## Not run: 
# From a file
file <- system.file("examples", "california.geojson", package = "geojsonio")
(out <- geojson_read(file))
geojson_read(file)

# From a URL
url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
geojson_read(url)
geojson_read(url, parse = TRUE)

# Use as.location first if you want
geojson_read(as.location(file))

# output a SpatialClass object
## read kml
file <- system.file("examples", "norway_maple.kml", package = "geojsonio")
geojson_read(as.location(file), what = "sp")
## read geojson
file <- system.file("examples", "california.geojson", package = "geojsonio")
geojson_read(as.location(file), what = "sp")
## read geojson from a url
url <- "https://raw.githubusercontent.com/glynnbird/usstatesgeojson/master/california.geojson"
geojson_read(url, what = "sp")
## read from a shape file
file <- system.file("examples", "bison.zip", package = "geojsonio")
dir <- tempdir()
unzip(file, exdir = dir)
shpfile <- list.files(dir, pattern = ".shp", full.names = TRUE)
geojson_read(shpfile, what = "sp")

x <- "https://raw.githubusercontent.com/johan/world.geo.json/master/countries.geo.json"
geojson_read(x, what = "sp")
geojson_read(x, what = "list")

utils::download.file(x, destfile = basename(x))
geojson_read(basename(x), what = "sp")

# from a Postgres database - your Postgres instance must be running
## MAKE SURE to run the setup in the postgis manual file first!
if (requireNamespace("DBI") && requireNamespace("RPostgres")) {
library(DBI)
conn <- tryCatch(dbConnect(RPostgres::Postgres(), dbname = 'postgistest'), 
 error = function(e) e)
if (inherits(conn, "PqConnection")) {
  state <- "SELECT row_to_json(fc)
   FROM (SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features
   FROM (SELECT 'Feature' As type
     , ST_AsGeoJSON(lg.geog)::json As geometry
     , row_to_json((SELECT l FROM (SELECT loc_id, loc_name) As l
       )) As properties
    FROM locations As lg   ) As f )  As fc;"
  json <- geojson_read(conn, query = state, what = "json")
  map_leaf(json)
 }
}

## End(Not run)

geojsonio documentation built on Jan. 15, 2021, 3:33 p.m.