lawn
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.
Install
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
Count number of points within polygons
lawn_count(polygons = lawn_data$polygons_count, points = lawn_data$points_count)
#> <FeatureCollection>
#> Bounding box: -112.1 46.6 -112.0 46.6
#> No. features: 2
#> No. points: 20
#> Properties: NULLaverage
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: NULLdistance
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.15958random set of points
lawn_random(n = 2)
#> <FeatureCollection>
#> Bounding box: -60.9 43.4 59.9 57.9
#> No. features: 2
#> No. points: 4
#> Properties: NULLlawn_random(n = 5)
#> <FeatureCollection>
#> Bounding box: -137.6 -77.2 167.4 85.8
#> No. features: 5
#> No. points: 10
#> Properties: NULLrandom features with geojson-random
Points
gr_point(2)
#> <FeatureCollection>
#> Bounding box: -80.6 71.6 -60.2 88.1
#> No. features: 2
#> No. points: 4
#> Properties: NULLPositions
gr_position()
#> [1] 24.779369 6.632737Polygons
gr_polygon(n = 1, vertices = 5, max_radial_length = 5)
#> <FeatureCollection>
#> Bounding box: 129.8 55.9 136.0 59.8
#> No. features: 1
#> No. points: 12
#> Properties: NULLsample from a FeatureCollection
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: NULLlawn_sample(dat, 2)
#> <FeatureCollection>
#> Bounding box: 10.7 59.9 10.8 59.9
#> No. features: 2
#> No. points: 4
#> Properties: NULLlawn_sample(dat, 3)
#> <FeatureCollection>
#> Bounding box: 10.7 59.9 10.8 59.9
#> No. features: 3
#> No. points: 6
#> Properties: NULLextent
lawn_extent(lawn_data$points_average)
#> [1] 10.71579 59.90478 10.80643 59.93162within
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: NULLbuffer
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")
#> <FeatureCollection>
#> Bounding box: -112.1 46.6 -112.0 46.6
#> No. features: 1
#> No. points: 74
#> Properties: NULLview
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()Meta
- Please report any issues or bugs.
- License: MIT
- Get citation information for
lawnin R doingcitation(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.