Client for 'Turfjs' for 'Geospatial' Analysis

Client for 'Turfjs' (< http://turfjs.org>) for 'geospatial' analysis. The package revolves around using 'GeoJSON' data. Functions are included for creating 'GeoJSON' data objects, measuring aspects of 'GeoJSON', and combining, transforming, and creating random 'GeoJSON' data objects.


lawn is an R wrapper for the Javascript library turf.js. In addition, we have a few functions to interface with the geojson-random and geojsonhint Javascript libraries, for generating random GeoJSON objects and linting GeoJSON, respectively.

The latest release of lawn is available from CRAN. To install:

install.packages("lawn")

To install the development version:

install.packages("devtools")
devtools::install_github("ropensci/lawn")
library("lawn")

Count number of points within polygons

lawn_count(lawn_data$polygons_count, lawn_data$points_count, 'population')
#> <FeatureCollection>
#>   Bounding box: -112.1 46.6 -112.0 46.6
#>   No. features: 2
#>   No. points: 20
#>   Properties: 
#>     values count
#> 1 200, 600     2
#> 2              0

Average value of a field for a set of points within a set of polygons

lawn_average(polygons = lawn_data$polygons_average, points = lawn_data$points_average, 'population')
#> <FeatureCollection>
#>   Bounding box: 10.7 59.9 10.9 59.9
#>   No. features: 2
#>   No. points: 20
#>   Properties: 
#>          values average
#> 1 200, 600, 100     300
#> 2      200, 300     250

Define two points

from <- '{
 "type": "Feature",
 "properties": {},
 "geometry": {
   "type": "Point",
   "coordinates": [-75.343, 39.984]
 }
}'
to <- '{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [-75.534, 39.123]
  }
}'

Calculate distance, default units is kilometers (km)

lawn_distance(from, to)
#> [1] 97.15958
lawn_random(n = 2)
#> <FeatureCollection>
#>   Bounding box: -63.0 -12.4 168.8 45.2
#>   No. features: 2
#>   No. points: 4
#>   Properties: NULL
lawn_random(n = 5)
#> <FeatureCollection>
#>   Bounding box: -171.4 -50.5 135.5 51.6
#>   No. features: 5
#>   No. points: 10
#>   Properties: NULL

Points

gr_point(2)
#> <FeatureCollection>
#>   Bounding box: -81.8 -58.5 170.6 43.8
#>   No. features: 2
#>   No. points: 4
#>   Properties: NULL

Positions

gr_position()
#> [1]  46.78765 -69.55507

Polygons

gr_polygon(n = 1, vertices = 5, max_radial_length = 5)
#> <FeatureCollection>
#>   Bounding box: -28.2 20.3 -20.6 29.0
#>   No. features: 1
#>   No. points: 12
#>   Properties: NULL
dat <- lawn_data$points_average
lawn_sample(dat, 1)
#> <FeatureCollection>
#>   Bounding box: 10.8 59.9 10.8 59.9
#>   No. features: 1
#>   No. points: 2
#>   Properties: 
#>   population
#> 1        300
lawn_sample(dat, 2)
#> <FeatureCollection>
#>   Bounding box: 10.8 59.9 10.8 59.9
#>   No. features: 2
#>   No. points: 4
#>   Properties: 
#>   population
#> 1        300
#> 2        200
lawn_sample(dat, 3)
#> <FeatureCollection>
#>   Bounding box: 10.7 59.9 10.8 59.9
#>   No. features: 3
#>   No. points: 6
#>   Properties: 
#>   population
#> 1        600
#> 2        300
#> 3        200
lawn_extent(lawn_data$points_average)
#> [1] 10.71579 59.90478 10.80643 59.93162
lawn_within(lawn_data$points_within, lawn_data$polygons_within)
#> <FeatureCollection>
#>   Bounding box: -46.6 -23.6 -46.6 -23.6
#>   No. features: 2
#>   No. points: 4
#>   Properties: NULL
dat <- '{
 "type": "Feature",
 "properties": {},
 "geometry": {
     "type": "Polygon",
     "coordinates": [[
       [-112.072391,46.586591],
       [-112.072391,46.61761],
       [-112.028102,46.61761],
       [-112.028102,46.586591],
       [-112.072391,46.586591]
     ]]
   }
}'
lawn_buffer(dat, 1, "miles")
#> <Feature>
#>   Type: Polygon
#>   Bounding box: -112.1 46.6 -112.0 46.6
#>   No. points: 74
#>   Properties: NULL

lawn includes a tiny helper function for visualizing geojson.

view(lawn_data$points_average)

Or during process of manipulating geojson, view at mostly any time.

Here, we sample at random two points from the same dataset just viewed.

lawn_sample(lawn_data$points_average, 2) %>% view()

  • Please report any issues or bugs.
  • License: MIT
  • Get citation information for lawn in R doing citation(package = 'lawn')
  • Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

News

lawn 0.3.0

  • Updated to using turf.js v3.5.2 (#49) (#50), with the following significant changes. Defunct functions (these methods were removed from turf, so made sense to remove them here): lawn_aggregate, lawn_jenks, lawn_quantile, lawn_reclass, lawn_size. New functions (these methods were added to turf): lawn_circle, lawn_collect, lawn_bbox, lawn_feature, lawn_tesselate. New data functions to create geojson: lawn_multipoint, lawn_multipolygon, lawn_multilinestring, lawn_geometrycollection.
  • as.turf gains S3 methods for multipoint, multipolygon, multilinestring, and geometrycollection.
  • lawn_featurecollection gains a default S3 method to error gracefully on classes not supported, and gains S3 methods for multipoint, multipolygon, multilinestring, and geometrycollection.
  • lawn_erase renamed to lawn_difference, following turf changes
  • lawn_average parameter name changes: field to in_field, outputField to out_field
  • lawn_count loses parameter countField, and gains parameters in_field, and out_field (with default 'count')
  • lawn_deviation parameter name changes: inField to in_field, outField to out_field (with default 'deviation')
  • lawn_max parameter out_field with new default value 'max'
  • lawn_median parameter out_field with new default value 'median'
  • lawn_min parameter out_field with new default value 'min'
  • lawn_sum parameter out_field with new default value 'sum'
  • lawn_variance parameter out_field with new default value 'variance'
  • all aggregatation/math functions (lawn_average, lawn_count, lawn_deviation, lawn_max, lawn_median, lawn_min, lawn_sum, lawn_variance) use turf.collect internally, same method that's used in lawn_collect
  • Fixed example for lawn_tag() that wasn't working. Also, poly_id parameter changed to field, and containing_polyid changed to out_field. (#30)
  • lawn_merge uses new turf method turf.union internally, but no user facing changes
  • Changed x parameter to coordinates in lawn_point for consistency with similar methods
  • Changed rings parameter to coordinates in lawn_polygon for consistency with similar methods
  • Internal changes to lawn_remove following turf changes, but there should be no user facing changes
  • Fixed bug in lawn_buffer() (#53)
  • Fixed bug in lawn_random(). num_vertices parameter wasn't working (#44)

lawn 0.1.7

  • Fixes tests (#46)

lawn 0.1.6

  • Fixes expectations in test suite on failure for new V8 version (#45)
  • Updated geojsonhint Javascript library (v1.2.0) (#42)

lawn 0.1.4

  • Added a code of conduct
  • Implemented print methods for most output objects for easier comprehension (#26)
  • Import all non-base R functions, now from methods, stats, and utils (#40)

lawn 0.1.0

  • released to CRAN

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("lawn")

0.3.0 by Scott Chamberlain, 6 months ago


https://github.com/ropensci/lawn


Report a bug at http://www.github.com/ropensci/lawn/issues


Browse source code at https://github.com/cran/lawn


Authors: Scott Chamberlain [aut, cre], Jeff Hollister [aut], Morgan Herlocker [cph]


Documentation:   PDF Manual  


Task views: Analysis of Spatial Data


MIT + file LICENSE license


Imports methods, stats, utils, V8, jsonlite, magrittr

Suggests testthat, knitr, rmarkdown, leaflet, covr

Enhances maps, geojsonio


See at CRAN