Learn R Programming

⚠️There's a newer version (0.6.0) of this package.Take me there.

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

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

distance

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

random set of points

lawn_random(n = 2)
#> <FeatureCollection>
#>   Bounding box: -31.3 3.2 155.0 67.6
#>   No. features: 2
#>   No. points: 4
#>   Properties: NULL
lawn_random(n = 5)
#> <FeatureCollection>
#>   Bounding box: -50.6 -78.2 68.1 52.2
#>   No. features: 5
#>   No. points: 10
#>   Properties: NULL

random features with geojson-random

Points

gr_point(2)
#> <FeatureCollection>
#>   Bounding box: -89.0 -7.1 123.4 -0.7
#>   No. features: 2
#>   No. points: 4
#>   Properties: NULL

Positions

gr_position()
#> [1]  -7.546582 -66.863360

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: NULL

sample from a FeatureCollection

dat <- 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        100
lawn_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        300
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        200
#> 3        100

extent

lawn_extent(lawn_data$points_average)
#> [1] 10.71579 59.90478 10.80643 59.93162

within

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

buffer

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

view

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()

Contributors

Meta

Additional disclaimer

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

Copy Link

Version

Install

install.packages('lawn')

Monthly Downloads

31

Version

0.5.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Scott Chamberlain

Last Published

February 1st, 2019

Functions in lawn (0.5.0)

lawn_boolean_disjoint

Boolean crosses
lawn_boolean_overlap

Boolean overlap
lawn_coordall

Get all coordinates from any GeoJSON object, returning an array of coordinate arrays.
lawn_boolean_pointonline

Boolean overlap
lawn_boolean_within

Boolean within
lawn_boolean_crosses

Boolean crosses
lawn_coordeach

Iterate over property objects in any GeoJSON object
lawn_filter

Filter a FeatureCollection by a given property and value
lawn_flatten

Flatten
lawn_boolean_contains

Boolean contains
as.turf

Coerce to turf.js javascript function call
lawn_center_of_mass

Center of mass
lawn_kinks

Get points at all self-intersections of a polygon
lawn_deviation

Standard deviation of a field among points within polygons
lawn_bearing

Get geographic bearing between two points
lawn_bbox_polygon

Make a polygon from a bounding box
lawn_destination

Calculate destination point
lawn_feature

Create a Feature
lawn_line_distance

Measure a linestring
lawn_min

Minimum value of a field among points within polygons
lawn_bezier

Curve a linestring
lawn_average

Average of a field among points within polygons
lawn_multilinestring

Create a multilinestring
lawn_difference

Difference
lawn_dissolve

Dissolves a FeatureCollection of polygons based on a property. Note that multipart features within the collection are not supported
lawn_extent

Get a bounding box
lawn_explode

Explode vertices to points
lawn_geosjontype

Enforce expectations about types of GeoJSON objects.
lawn_centroid

Centroid
lawn_buffer

Buffer a feature
lawn_polygon

Create a polygon
lawn_bbox

Make a bounding box from a polygon
lawn_collectionof

Enforce expectations about types of FeatureCollection inputs
lawn_boolean_clockwise

Boolean clockwise
lawn_distance

Distance between two points
lawn_center

Get center point
lawn_convex

Convex hull polygon
lawn_concave

Concave hull polygon
lawn_circle

circle
lawn_envelope

Calculate envelope around features
lawn_combine

Combine singular features into plural versions
lawn_polygonize

Polygonizes multi-linestring's to polygons
lawn_getcoord

Unwrap a coordinate from a Feature with a Point geometry, or a single coordinate.
lawn_tag

Spatial join of points and polygons
lawn_idw

IDW
lawn_hex_grid

Create a HexGrid
lawn_merge

Merge polygons
lawn_midpoint

Get a point midway between two points
lawn_truncate

Truncate
lawn_sum

Sum of a field among points within polygons
lawn_collect

Collect method
lawn_max

Maximum value of a field among points within polygons
lawn_simplify

Simplify GeoJSON data
lawn_flip

Flip x,y to y,x, and vice versa
lawn_featureof

Enforce expectations about types of Feature inputs
lawn_count

Count number of points within polygons
lawn_featureeach

Iterate over features in any GeoJSON object
lawn_geometrycollection

Create a geometrycollection
lawn_line_slice_along

Slice a line given two points
lawn_data

Data for use in examples
lawn_union

Merge polygons
lawn_isolines

Generate Isolines
lawn_featurecollection

Create a FeatureCollection
lawn_line_offset

Offset a linestring
lawn_jenks

Get Jenks breaks
lawn_inside

Does a point reside inside a polygon
lawn_median

Median value of a field among points within polygons
lawn_linestring

Create a linestring
lawn_intersect

Intersection
view

Visualize geojson
lawn_size

Expand a bounding box
lawn_line_slice

Slice a line given two points
lawn_point

Create a point
lawn_point_grid

Create a PointGrid
lawn_multipoint

MultiPoint
lawn_planepoint

Calculate a Planepoint
lawn_nearest

Get nearest point
lawn_random

Generate random data
lawn_transform_rotate

Rotate a GeoJSON feature
lawn_quantile

Calculate quantiles
lawn_point_on_line

Get closest point on linestring to reference point
lawn_reclass

Reclassify properties data
lawn_square

Calculate a square bounding box
lawn_remove

Remove things from a FeatureCollection
lawn_multipolygon

Create a multipolygon
lawn_point_on_surface

Get a point on the surface of a feature
lawn_propeach

Iterate over property objects in any GeoJSON object
lawn_transform_scale

Scale a GeoJSON feature
lawn_square_grid

Create a SquareGrid
print-methods

Lawn print methods to provide summary view
type_sum

Type summary
lawn_pt2line_distance

Minimum distance between a point and a lineString
lawn_unkinkpolygon

Unkink polygon
lawn_transform_translate

Translate a GeoJSON feature
lawn_rewind

Rewind
lawn_sample

Return features from FeatureCollection at random
lawn_variance

Variance of a field among points within polygons
lawn_tesselate

Tesselate
lawn_tin

Create a Triangulated Irregular Network
lawn_triangle_grid

Create a TriangleGrid
lawn_within

Return points that fall within polygons
%>%

Pipe operator
lawn-package

R client for turf.js for geospatial analysis
lawn_along

Get a point at a distance along a line
as_feature

Convert a FeatureCollection to a Feature
lawn_area

Calculate the area of a polygon or group of polygons
lawn_aggregate

Calculate a series of aggregations for a set of points within polygons
georandom

Return a FeatureCollection with N number of features with random coordinates
data-types

Description of GeoJSON data types
as.feature

Coerce character strings or JSON to GeoJSON Feature
lawn-defunct

Defunct functions in lawn