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.
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)
#> <MultiPolygon>
#> no. polygons: 2
#> coordinates: [[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]],[[[10 ...
From geopoint
class
pt <- point('{ "type": "Point", "coordinates": [100.0, 0.0] }')
feature(pt)
#> <Feature>
#> type: Point
#> coordinates: [100.0,0.0]
From character string
From feature
pt %>% feature() %>% featurecollection()
#> <FeatureCollection>
#> type: FeatureCollection
#> no. features: 1
#> features (1st 5): Point
From string
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