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")
<- point('{ "type": "Point", "coordinates": [100.0, 0.0] }'))
(x #> <Point>
#> coordinates: [100,0]
class(x)
#> [1] "geopoint" "geojson"
attributes(x)
#> $class
#> [1] "geopoint" "geojson"
#>
#> $coords
#> [1] "[100,0]"
multipoint('{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')
#> <MultiPoint>
#> coordinates: [[100,0],[101,1]]
linestring('{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')
#> <LineString>
#> coordinates: [[100,0],[101,1]]
<- '{ "type": "MultiLineString",
str "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] }'
multilinestring(str)
#> <MultiLineString>
#> no. lines: 2
#> no. nodes / line: 2, 2
#> coordinates: [[[100,0],[101,1]],[[102,2],[103,3]]]
<- '{ "type": "Polygon",
str "coordinates": [
[ [100.0, 0.0], [100.0, 1.0], [101.0, 1.0], [101.0, 0.0], [100.0, 0.0] ]
]
}'
polygon(str)
#> <Polygon>
#> no. lines: 1
#> no. holes: 0
#> no. nodes / line: 5
#> coordinates: [[[100,0],[100,1],[101,1],[101,0],[100,0]]]
<- '{ "type": "MultiPolygon",
str "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)
#> <MultiPolygon>
#> no. polygons: 2
#> coordinates: [[[[102,2],[103,2],[103,3],[102,3],[102,2]]],[[[100,0],[101,0],[101,1] ...
From geopoint
class
<- point('{ "type": "Point", "coordinates": [100.0, 0.0] }')
pt feature(pt)
#> <Feature>
#> type: Point
#> coordinates: [100,0]
From character string
<- "{ \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"type\": \"Point\", \"coordinates\": [100.0, 0.0] } }"
str feature(str)
#> <Feature>
#> type: Point
#> coordinates: [100,0]
From feature
%>% feature() %>% featurecollection()
pt #> <FeatureCollection>
#> type: FeatureCollection
#> no. features: 1
#> features (1st 5): Point
From string
<- system.file("examples", 'featurecollection1.geojson', package = "geojson")
file <- paste0(readLines(file), collapse = " ")
str featurecollection(str)
#> <FeatureCollection>
#> type: FeatureCollection
#> no. features: 1
#> features (1st 5): 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)
#> <GeometryCollection>
#> geometries (n): 2
#> geometries (geometry / length):
#> Point / 2
#> LineString / 2