geojson package classes

knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE,
  warning = FALSE,
  message = FALSE
)

The geojson package has a function to create a GeoJSON class matching all the GeoJSON data types:

The following are some examples of their usage.

library("geojson")

point

(x <- point('{ "type": "Point", "coordinates": [100.0, 0.0] }'))
class(x)
attributes(x)

multipoint

multipoint('{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')

linestring

linestring('{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')

multilinestring

str <- '{ "type": "MultiLineString",
  "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }'
multilinestring(str)

polygon

str <- '{ "type": "Polygon",
 "coordinates": [
   [ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ]
   ]
}'
polygon(str)

multipolygon

str <- '{ "type": "MultiPolygon",
  "coordinates": [
   [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
   [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
   [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
  ]
}'
multipolygon(str)

feature

From geopoint class

pt <- point('{ "type": "Point", "coordinates": [100.0, 0.0] }')
feature(pt)

From character string

str <- "{ \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [100.0, 0.0] } }"
feature(str)

featurecollection

From feature

pt %>% feature() %>% featurecollection()

From string

file <- system.file("examples", 'featurecollection1.geojson', package = "geojson")
str <- paste0(readLines(file), collapse = " ")
featurecollection(str)

geometrycollection

str <- '{
 "type": "GeometryCollection",
 "geometries": [
   {
     "type": "Point",
     "coordinates": [100.0, 0.0]
   },
   {
     "type": "LineString",
     "coordinates": [ [101.0, 0.0], [102.0, 1.0] ]
   }
  ]
}'
geometrycollection(str)


Try the geojson package in your browser

Any scripts or data that you put into this service are public.

geojson documentation built on July 2, 2020, 2:37 a.m.