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:
To install the development version:
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 0Average 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 250Define 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_random(n = 2)
#> <FeatureCollection>
#> Bounding box: -31.3 3.2 155.0 67.6
#> No. features: 2
#> No. points: 4
#> Properties: NULLlawn_random(n = 5)
#> <FeatureCollection>
#> Bounding box: -50.6 -78.2 68.1 52.2
#> No. features: 5
#> No. points: 10
#> Properties: NULLPoints
gr_point(2)
#> <FeatureCollection>
#> Bounding box: -89.0 -7.1 123.4 -0.7
#> No. features: 2
#> No. points: 4
#> Properties: NULLPositions
Polygons
gr_polygon(n = 1, vertices = 5, max_radial_length = 5)
#> <FeatureCollection>
#> Bounding box: 76.8 -88.4 80.6 -83.6
#> No. features: 1
#> No. points: 12
#> Properties: NULLdat <- lawn_data$points_average
lawn_sample(dat, 1)
#> <FeatureCollection>
#> Bounding box: 10.7 59.9 10.7 59.9
#> No. features: 1
#> No. points: 2
#> Properties:
#> population
#> 1 100lawn_sample(dat, 2)
#> <FeatureCollection>
#> Bounding box: 10.8 59.9 10.8 59.9
#> No. features: 2
#> No. points: 4
#> Properties:
#> population
#> 1 200
#> 2 300lawn_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 200
#> 3 100lawn_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: NULLdat <- '{
"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: NULLlawn includes a tiny helper function for visualizing geojson.

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 in R doing citation(package = 'lawn')Portions of this code have been contributed by Jeff Hollister, US EPA. As such, that code is subjec to the following disclaimer: https://www.epa.gov/home/github-contribution-disclaimer