# geojson package classes

#### Scott Chamberlain and Jeroen Ooms

#### 2019-01-30

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

- `point()` - Point
- `multipoint()` - MultiPoint
- `linestring()` - LineString
- `multilinestring()` - MultiLineString
- `polygon()` - Polygon
- `multipolygon()` - MultiPolygon
- `feature()` - Feature
- `featurecollection()` - FeatureCollection
- `geometrycollection()` - GeometryCollection

The following are some examples of their usage.

    library("geojson")

## point

    (x <- point('{ "type": "Point", "coordinates": [100.0, 0.0] }'))
    #> <Point>
    #>   coordinates:  [100,0]
    class(x)
    #> [1] "geopoint" "geojson"
    attributes(x)
    #> $class
    #> [1] "geopoint" "geojson"
    #>
    #> $coords
    #> [1] "[100,0]"

## multipoint

    multipoint('{"type": "MultiPoint", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')
    #> <MultiPoint>
    #>   coordinates:  [[100,0],[101,1]]

## linestring

    linestring('{ "type": "LineString", "coordinates": [ [100.0, 0.0], [101.0, 1.0] ] }')
    #> <LineString>
    #>   coordinates:  [[100,0],[101,1]]

## multilinestring

    str <- '{ "type": "MultiLineString",
      "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]]]

## 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)
    #> <Polygon>
    #>   no. lines:  1
    #>   no. holes:  0
    #>   no. nodes / line:  5
    #>   coordinates:  [[[100,0],[100,1],[101,1],[101,0],[100,0]]]

## 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)
    #> <MultiPolygon>
    #>   no. polygons:  2
    #>   coordinates:  [[[[102,2],[103,2],[103,3],[102,3],[102,2]]],[[[100,0],[101,0],[101,1] ...

## feature

From `geopoint` class

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

From character string

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

## featurecollection

From feature

    pt %>% feature() %>% featurecollection()
    #> <FeatureCollection>
    #>   type:  FeatureCollection
    #>   no. features:  1
    #>   features (1st 5):  Point

From string

    file <- system.file("examples", 'featurecollection1.geojson', package = "geojson")
    str <- paste0(readLines(file), collapse = " ")
    featurecollection(str)
    #> <FeatureCollection>
    #>   type:  FeatureCollection
    #>   no. features:  1
    #>   features (1st 5):  GeometryCollection

## 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