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:
point()
- Pointmultipoint()
- MultiPointlinestring()
- LineStringmultilinestring()
- MultiLineStringpolygon()
- Polygonmultipolygon()
- MultiPolygonfeature()
- Featurefeaturecollection()
- FeatureCollectiongeometrycollection()
- GeometryCollectionThe following are some examples of their usage.
library("geojson")
(x <- point('{ "type": "Point", "coordinates": [100.0, 0.0] }')) class(x) attributes(x)
multipoint('{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')
linestring('{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')
str <- '{ "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }' multilinestring(str)
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)
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)
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)
From feature
pt %>% feature() %>% featurecollection()
From string
file <- system.file("examples", 'featurecollection1.geojson', package = "geojson") str <- paste0(readLines(file), collapse = " ") featurecollection(str)
str <- '{ "type": "GeometryCollection", "geometries": [ { "type": "Point", "coordinates": [100.0, 0.0] }, { "type": "LineString", "coordinates": [ [101.0, 0.0], [102.0, 1.0] ] } ] }' geometrycollection(str)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.