├── .Rbuildignore ├── .gitattributes ├── .github ├── CONTRIBUTING.md ├── issue_template.md ├── pull_request_template.md └── workflows │ └── R-check.yml ├── .gitignore ├── DESCRIPTION ├── LICENSE ├── LICENSE.md ├── Makefile ├── NAMESPACE ├── NEWS.md ├── R ├── along.R ├── area.R ├── as.feature.R ├── as.turf.R ├── as_feature.R ├── average.R ├── bbox_polygon.R ├── bearing.R ├── bezier.R ├── boolean_clockwise.R ├── boolean_contains.R ├── boolean_crosses.R ├── boolean_disjoint.R ├── boolean_overlap.R ├── boolean_pointonline.R ├── boolean_within.R ├── buffer.R ├── center.R ├── centerofmass.R ├── centroid.R ├── check_leaflet.R ├── circle.R ├── collect.R ├── collectionof.R ├── combine.R ├── concave.R ├── convex.R ├── coordeach.R ├── cordall.R ├── count.R ├── data-types.R ├── defunct-deprecated.R ├── destination.R ├── deviation.R ├── dissolve.R ├── distance.R ├── envelope.R ├── erase.R ├── explode.R ├── extent.R ├── featurecollection.R ├── featureeach.R ├── featureof.R ├── filter.R ├── flatten.R ├── flip.R ├── geojson_random.R ├── geojsontype.R ├── geometrycollection.R ├── getcoord.R ├── hex_grid.R ├── idw.R ├── inside.R ├── intersect.R ├── is_type.R ├── isolines.R ├── kinks.R ├── lawn-package.R ├── lawn_bbox.R ├── lawn_feature.R ├── line_distance.R ├── line_offset.R ├── line_slice.R ├── line_slice_along.R ├── linestring.R ├── max.R ├── median.R ├── merge.R ├── midpoint.R ├── min.R ├── multilinestring.R ├── multipoint.R ├── multipolygon.R ├── nearest.R ├── onLoad.R ├── pipe.R ├── planepoint.R ├── point.R ├── point_grid.R ├── point_on_line.R ├── point_on_surface.R ├── point_pt2line_distance.R ├── polygon.R ├── polygonize.R ├── print_methods.R ├── propeach.R ├── random.R ├── remove.R ├── rewind.R ├── sample.R ├── simplify.R ├── square.R ├── square_grid.R ├── sum.R ├── tag.R ├── tbldf.R ├── tesselate.R ├── tin.R ├── transform_rotate.R ├── transform_scale.R ├── transform_translate.R ├── triangle_grid.R ├── truncate.R ├── union.R ├── unkinkpolygon.R ├── variance.R ├── view.R ├── within.R └── zzz.R ├── README-NOT.md ├── README.Rmd ├── README.md ├── codemeta.json ├── cran-comments.md ├── data └── lawn_data.rda ├── inst ├── examples │ └── isolines_data.json ├── ignore │ ├── cordreduce.R │ ├── propreduce.R │ └── stuff.R └── js │ ├── LICENSE-clone │ ├── LICENSE-geojson-random │ ├── LICENSE-geojsonhint │ ├── LICENSE-simple-statistics │ ├── LICENSE-turfjs │ ├── README.md │ ├── cloner.js │ ├── geojson_random.js │ ├── geojsonhint-v201.js │ ├── simple-statistics.js │ ├── turf-invariant.js │ ├── turf-meta.js │ └── turf516.js ├── man-roxygen ├── lint.R └── math.R ├── man ├── as.feature.Rd ├── as.turf.Rd ├── as_feature.Rd ├── data-types.Rd ├── figures │ ├── map1.png │ └── map2.png ├── georandom.Rd ├── lawn-defunct.Rd ├── lawn-package.Rd ├── lawn_aggregate.Rd ├── lawn_along.Rd ├── lawn_area.Rd ├── lawn_average.Rd ├── lawn_bbox.Rd ├── lawn_bbox_polygon.Rd ├── lawn_bearing.Rd ├── lawn_bezier.Rd ├── lawn_boolean_clockwise.Rd ├── lawn_boolean_contains.Rd ├── lawn_boolean_crosses.Rd ├── lawn_boolean_disjoint.Rd ├── lawn_boolean_overlap.Rd ├── lawn_boolean_pointonline.Rd ├── lawn_boolean_within.Rd ├── lawn_buffer.Rd ├── lawn_center.Rd ├── lawn_center_of_mass.Rd ├── lawn_centroid.Rd ├── lawn_circle.Rd ├── lawn_collect.Rd ├── lawn_collectionof.Rd ├── lawn_combine.Rd ├── lawn_concave.Rd ├── lawn_convex.Rd ├── lawn_coordall.Rd ├── lawn_coordeach.Rd ├── lawn_count.Rd ├── lawn_data.Rd ├── lawn_destination.Rd ├── lawn_deviation.Rd ├── lawn_difference.Rd ├── lawn_dissolve.Rd ├── lawn_distance.Rd ├── lawn_envelope.Rd ├── lawn_explode.Rd ├── lawn_extent.Rd ├── lawn_feature.Rd ├── lawn_featurecollection.Rd ├── lawn_featureeach.Rd ├── lawn_featureof.Rd ├── lawn_filter.Rd ├── lawn_flatten.Rd ├── lawn_flip.Rd ├── lawn_geometrycollection.Rd ├── lawn_geosjontype.Rd ├── lawn_getcoord.Rd ├── lawn_hex_grid.Rd ├── lawn_idw-defunct.Rd ├── lawn_inside.Rd ├── lawn_intersect.Rd ├── lawn_isolines.Rd ├── lawn_jenks.Rd ├── lawn_kinks.Rd ├── lawn_line_distance.Rd ├── lawn_line_offset.Rd ├── lawn_line_slice.Rd ├── lawn_line_slice_along.Rd ├── lawn_linestring.Rd ├── lawn_max.Rd ├── lawn_median.Rd ├── lawn_merge.Rd ├── lawn_midpoint.Rd ├── lawn_min.Rd ├── lawn_multilinestring.Rd ├── lawn_multipoint.Rd ├── lawn_multipolygon.Rd ├── lawn_nearest.Rd ├── lawn_planepoint.Rd ├── lawn_point.Rd ├── lawn_point_grid.Rd ├── lawn_point_on_feature.Rd ├── lawn_point_on_line.Rd ├── lawn_polygon.Rd ├── lawn_polygonize.Rd ├── lawn_propeach.Rd ├── lawn_pt2line_distance.Rd ├── lawn_quantile.Rd ├── lawn_random.Rd ├── lawn_reclass.Rd ├── lawn_remove.Rd ├── lawn_rewind.Rd ├── lawn_sample.Rd ├── lawn_simplify.Rd ├── lawn_size.Rd ├── lawn_square.Rd ├── lawn_square_grid.Rd ├── lawn_sum.Rd ├── lawn_tag.Rd ├── lawn_tesselate.Rd ├── lawn_tin.Rd ├── lawn_transform_rotate.Rd ├── lawn_transform_scale.Rd ├── lawn_transform_translate.Rd ├── lawn_triangle_grid.Rd ├── lawn_truncate.Rd ├── lawn_union.Rd ├── lawn_unkinkpolygon.Rd ├── lawn_variance.Rd ├── lawn_within.Rd ├── pipe.Rd ├── print-methods.Rd ├── type_sum.Rd └── view.Rd ├── paper.bib ├── paper.md ├── tests ├── test-all.R └── testthat │ ├── helper-lawn.R │ ├── test-along.R │ ├── test-area.R │ ├── test-as.turf.R │ ├── test-average.R │ ├── test-bbox_polygon.R │ ├── test-bearing.R │ ├── test-bezier.R │ ├── test-buffer.R │ ├── test-center.R │ ├── test-centroid.R │ ├── test-check4leaflet.R │ ├── test-collect.R │ ├── test-combine.R │ ├── test-concave.R │ ├── test-convex.R │ ├── test-count.R │ ├── test-destination.R │ ├── test-deviation.R │ ├── test-distance.R │ ├── test-envelope.R │ ├── test-erase.R │ ├── test-explode.R │ ├── test-extent.R │ ├── test-featurecollection.R │ ├── test-filter.R │ ├── test-flip.R │ ├── test-geojson_random.R │ ├── test-hex_grid.R │ ├── test-inside.R │ ├── test-intersect.R │ ├── test-isolines.R │ ├── test-kinks.R │ ├── test-line_distance.R │ ├── test-line_slice.R │ ├── test-linestring.R │ ├── test-lint.R │ ├── test-max.R │ ├── test-median.R │ ├── test-merge.R │ ├── test-midpoint.R │ ├── test-min.R │ ├── test-nearest.R │ ├── test-onload.R │ ├── test-planepoint.R │ ├── test-point.R │ ├── test-point_grid.R │ ├── test-point_on_line.R │ ├── test-point_on_surface.R │ ├── test-polygon.R │ ├── test-random.R │ ├── test-remove.R │ ├── test-sample.R │ ├── test-simplify.R │ ├── test-square.R │ ├── test-square_grid.R │ ├── test-sum.R │ ├── test-tag.R │ ├── test-tin.R │ ├── test-transform_rotate.R │ ├── test-transform_scale.R │ ├── test-transform_translate.R │ ├── test-triangle_grid.R │ ├── test-union.R │ ├── test-unkinkpolygon.R │ ├── test-variance.R │ ├── test-view.R │ └── test-within.R └── vignettes ├── img ├── map1.png └── map2.png └── lawn.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | .travis.yml 4 | appveyor.yml 5 | README.Rmd 6 | README.md 7 | inst/img 8 | CONTRIBUTING.md 9 | ^\.httr-oauth$ 10 | man-roxygen 11 | .V8history 12 | cran-comments.md 13 | ^CODE_OF_CONDUCT\.md$ 14 | ^cran-comments\.md$ 15 | ^paper\.bib$ 16 | ^paper\.md$ 17 | ^generate\.sh$ 18 | ^codemeta\.json$ 19 | .github 20 | ^Makefile$ 21 | ^LICENSE\.md$ 22 | vignettes/ 23 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | inst/js/* linguist-vendored 2 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | CONTRIBUTING 2 | ============ 3 | 4 | ### Bugs? 5 | 6 | * Submit an issue on [the Issues page](https://github.com/ropensci/lawn/issues) 7 | 8 | ### Code contributions 9 | 10 | * Fork this repo to your Github account 11 | * Clone your version on your account down to your machine from your account, e.g,. `git clone https://github.com//lawn.git` 12 | * Make sure to track progress upstream (i.e., on our version of `lawn` at `ropensci/lawn`) by doing `git remote add upstream https://github.com/ropensci/lawn.git`. Before making changes make sure to pull changes in from upstream by doing either `git fetch upstream` then merge later or `git pull upstream` to fetch and merge in one step 13 | * Make your changes (bonus points for making changes on a new branch) 14 | * If you are adding a new function, or changing functionality of a function, include new tests, and make sure those pass before submittint the PR. 15 | * Push up to your account 16 | * Submit a pull request to home base at `ropensci/lawn` 17 | 18 | ### Questions? Use [our discussion forum](http://discuss.ropensci.org/) 19 | -------------------------------------------------------------------------------- /.github/issue_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |
Session Info 4 | 5 | ```r 6 | 7 | ``` 8 |
9 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Description 4 | 5 | 6 | ## Related Issue 7 | 10 | 11 | ## Example 12 | 14 | 15 | 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .DS_Store 5 | .V8history 6 | .httr-oauth 7 | generate.sh 8 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: lawn 2 | Title: Client for 'Turfjs' for 'Geospatial' Analysis 3 | Description: Client for 'Turfjs' () for 4 | 'geospatial' analysis. The package revolves around using 'GeoJSON' 5 | data. Functions are included for creating 'GeoJSON' data objects, 6 | measuring aspects of 'GeoJSON', and combining, transforming, 7 | and creating random 'GeoJSON' data objects. 8 | Type: Package 9 | Version: 0.6.0 10 | License: MIT + file LICENSE 11 | Authors@R: c( 12 | person("Scott", "Chamberlain", role = c("aut", "cre"), 13 | email = "myrmecocystus@gmail.com", 14 | comment = c(ORCID = "0000-0003-1444-9135")), 15 | person("Jeff", "Hollister", role = "aut", 16 | email = "hollister.jeff@epa.gov"), 17 | person("Morgan", "Herlocker", role = "cph"), 18 | person("rOpenSci", role = "fnd", comment = "https://ropensci.org") 19 | ) 20 | URL: https://github.com/ropensci/lawn (devel) 21 | https://docs.ropensci.org/lawn/ (docs) 22 | BugReports: https://github.com/ropensci/lawn/issues 23 | LazyData: true 24 | Roxygen: list(markdown = TRUE) 25 | Encoding: UTF-8 26 | Language: en-US 27 | Imports: 28 | V8, 29 | jsonlite, 30 | magrittr 31 | Suggests: 32 | testthat, 33 | leaflet 34 | Enhances: 35 | maps, 36 | geojsonio 37 | RoxygenNote: 7.1.1 38 | X-schema.org-applicationCategory: Geospatial 39 | X-schema.org-keywords: geojson, turf, geospatial, conversion, data, GeoJSON 40 | X-schema.org-isPartOf: https://ropensci.org 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2020 2 | COPYRIGHT HOLDER: lawn authors 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2020 lawn authors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | PACKAGE := $(shell grep '^Package:' DESCRIPTION | sed -E 's/^Package:[[:space:]]+//') 2 | RSCRIPT = Rscript --no-init-file 3 | 4 | install: doc build 5 | R CMD INSTALL . && rm *.tar.gz 6 | 7 | build: 8 | R CMD build . 9 | 10 | doc: 11 | ${RSCRIPT} -e "devtools::document()" 12 | 13 | eg: 14 | ${RSCRIPT} -e "devtools::run_examples()" 15 | 16 | test: 17 | ${RSCRIPT} -e "devtools::test()" 18 | 19 | check: build 20 | _R_CHECK_CRAN_INCOMING_=FALSE R CMD CHECK --as-cran --no-manual `ls -1tr ${PACKAGE}*gz | tail -n1` 21 | @rm -f `ls -1tr ${PACKAGE}*gz | tail -n1` 22 | @rm -rf ${PACKAGE}.Rcheck 23 | 24 | check_windows: 25 | ${RSCRIPT} -e "devtools::check_win_devel(); devtools::check_win_release()" 26 | 27 | readme: 28 | ${RSCRIPT} -e "knitr::knit('README.Rmd')" 29 | -------------------------------------------------------------------------------- /R/area.R: -------------------------------------------------------------------------------- 1 | #' Calculate the area of a polygon or group of polygons 2 | #' 3 | #' @export 4 | #' 5 | #' @param input A [data-Feature] or [data-FeatureCollection] of polygons 6 | #' @template lint 7 | #' @family measurements 8 | #' @return A numeric in square meters 9 | #' @examples 10 | #' lawn_area(lawn_data$poly) 11 | #' lawn_area(lawn_data$multipoly) 12 | lawn_area <- function(input, lint = FALSE) { 13 | input <- convert(input) 14 | lawnlint(input, lint) 15 | if (lint) { 16 | is_type(input, type_top = c("Feature", "FeatureCollection")) 17 | } 18 | ct$eval(sprintf("var area = turf.area(%s);", input)) 19 | ct$get("area") 20 | } 21 | -------------------------------------------------------------------------------- /R/as_feature.R: -------------------------------------------------------------------------------- 1 | #' Convert a FeatureCollection to a Feature 2 | #' 3 | #' @export 4 | #' @param x A [data-FeatureCollection]. 5 | #' @details If there are more than one feature within the featurecollection, 6 | #' each feature is split out into a separate feature, returned in a list. 7 | #' Each feature is assigned a class matching it's GeoJSON data type 8 | #' (e.g., point, polygon, linestring). 9 | #' @seealso [as.feature] , which is similarly named, but has a different 10 | #' purpose 11 | #' @examples 12 | #' as_feature(lawn_random()) 13 | #' # as_feature(lawn_random("polygons")) 14 | as_feature <- function(x) { 15 | if (inherits(x, "featurecollection")) { 16 | ct$eval(sprintf("var x = %s;", convert(x))) 17 | tmp <- ct$get("x.features", simplifyVector = FALSE) 18 | tmp <- lapply(tmp, function(z) { 19 | structure(z, class = tolower(z$geometry$type)) 20 | }) 21 | if (length(tmp) == 1) { 22 | tmp[[1]] 23 | } else { 24 | tmp 25 | } 26 | } else { 27 | x 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /R/average.R: -------------------------------------------------------------------------------- 1 | #' Average of a field among points within polygons 2 | #' 3 | #' Calculate the average value of a field for a set of [data-Point]s 4 | #' within a set of [data-Polygon]s 5 | #' 6 | #' @export 7 | #' 8 | #' @param polygons A [data-FeatureCollection] of [data-Polygon]'s 9 | #' @param points A [data-FeatureCollection] of [data-Point]'s 10 | #' @param in_field (character) The field in the points feature from which to 11 | #' pull values to average. 12 | #' @param out_field (character) The field in polygons to put results of the 13 | #' averages. 14 | #' @template lint 15 | #' @family aggregations 16 | #' @return Polygons with the value of out_field set to the calculated averages 17 | #' @examples \dontrun{ 18 | #' # using data in the package 19 | #' cat(lawn_data$points_average) 20 | #' cat(lawn_data$polygons_average) 21 | #' lawn_average(polygons = lawn_data$polygons_average, 22 | #' points = lawn_data$points_average, 'population') 23 | #' } 24 | lawn_average <- function(polygons, points, in_field, out_field = 'average', 25 | lint = FALSE) { 26 | 27 | lawnlint(list(polygons, points), lint) 28 | if (lint) { 29 | is_type(polygons, type_top = "FeatureCollection") 30 | is_type(points, type_top = "FeatureCollection") 31 | } 32 | calc_math("mean", convert(polygons), convert(points), in_field, out_field) 33 | } 34 | -------------------------------------------------------------------------------- /R/bbox_polygon.R: -------------------------------------------------------------------------------- 1 | #' Make a polygon from a bounding box 2 | #' 3 | #' Takes a bbox and returns an equivalent polygon [data-Polygon]. 4 | #' 5 | #' @export 6 | #' @param bbox An array of bounding box coordinates in the form: 7 | #' `[xLow, yLow, xHigh, yHigh]`. 8 | #' @family measurements 9 | #' @return A [data-Polygon] representation of the bounding box. 10 | #' @examples 11 | #' bbox <- c(0, 0, 10, 10) 12 | #' lawn_bbox_polygon(bbox) 13 | #' @examples \dontrun{ 14 | #' lawn_bbox_polygon(bbox) %>% view 15 | #' lawn_bbox_polygon(c(1, 3, 5, 50)) %>% view 16 | #' } 17 | lawn_bbox_polygon <- function(bbox) { 18 | all_numeric(bbox) 19 | ct$eval(sprintf("var bbp = turf.bboxPolygon(%s);", convert(bbox))) 20 | structure(ct$get("bbp"), class = "polygon") 21 | } 22 | 23 | lawn_bbox <- function(x) { 24 | lawnlint(x, lint) 25 | ct$eval(sprintf("var bbp = turf.bbox(%s);", convert(x))) 26 | structure(ct$get("bbp"), class = "polygon") 27 | } 28 | 29 | all_numeric <- function(x) { 30 | if (!all(vapply(x, is.numeric, logical(1)))) { 31 | stop("All values must be numeric", call. = FALSE) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /R/bearing.R: -------------------------------------------------------------------------------- 1 | #' Get geographic bearing between two points 2 | #' 3 | #' Takes two [data-Point]'s and finds the geographic bearing 4 | #' between them. 5 | #' 6 | #' @export 7 | #' @param start Starting [data-Feature] with a single [data-Point] 8 | #' @param end Ending [data-Feature] with a single [data-Point] 9 | #' @template lint 10 | #' @family measurements 11 | #' @return A numeric value of the bearing in degrees. 12 | #' @examples 13 | #' start <- '{ 14 | #' "type": "Feature", 15 | #' "properties": { 16 | #' "marker-color": "#f00" 17 | #' }, 18 | #' "geometry": { 19 | #' "type": "Point", 20 | #' "coordinates": [-75.343, 39.984] 21 | #' } 22 | #' }' 23 | #' 24 | #' end <- '{ 25 | #' "type": "Feature", 26 | #' "properties": { 27 | #' "marker-color": "#0f0" 28 | #' }, 29 | #' "geometry": { 30 | #' "type": "Point", 31 | #' "coordinates": [-75.534, 39.123] 32 | #' } 33 | #' }' 34 | #' lawn_bearing(start, end) 35 | lawn_bearing <- function(start, end, lint = FALSE) { 36 | start <- convert(start) 37 | end <- convert(end) 38 | lawnlint(list(start, end), lint) 39 | if (lint) { 40 | is_type(start, type_top = "Feature", type_lower = "Point") 41 | is_type(end, type_top = "Feature", type_lower = "Point") 42 | } 43 | ct$eval(sprintf("var bear = turf.bearing(%s, %s);", start, end)) 44 | ct$get("bear") 45 | } 46 | -------------------------------------------------------------------------------- /R/bezier.R: -------------------------------------------------------------------------------- 1 | #' Curve a linestring 2 | #' 3 | #' Takes a [data-LineString] and returns a curved version 4 | #' by applying a Bezier http://en.wikipedia.org/wiki/Bezier_spline 5 | #' spline algorithm. 6 | #' 7 | #' @export 8 | #' @param line A [data-Feature] with a single [data-LineString] 9 | #' @param resolution Time in milliseconds between points 10 | #' @param sharpness A measure of how curvy the path should be between splines 11 | #' @template lint 12 | #' @family transformations 13 | #' @return A [data-LineString] curved line. 14 | #' @examples 15 | #' pts <- '[ 16 | #' [-21.964416, 64.148203], 17 | #' [-21.956176, 64.141316], 18 | #' [-21.93901, 64.135924], 19 | #' [-21.927337, 64.136673] 20 | #' ]' 21 | #' lawn_bezier(lawn_linestring(pts)) 22 | #' lawn_bezier(lawn_linestring(pts), 9000L) 23 | #' lawn_bezier(lawn_linestring(pts), 9000L, 0.65) 24 | #' @examples \dontrun{ 25 | #' lawn_bezier(lawn_linestring(pts)) %>% view 26 | #' lawn_featurecollection(list(lawn_linestring(pts), 27 | #' lawn_bezier(lawn_linestring(pts)))) %>% view 28 | #' } 29 | lawn_bezier <- function(line, resolution = 10000L, sharpness = 0.85, 30 | lint = FALSE) { 31 | 32 | line <- convert(line) 33 | lawnlint(line, lint) 34 | if (lint) is_type(line, type_top = "Feature", type_lower = "LineString") 35 | ct$eval(sprintf("var bz = turf.bezierSpline(%s, {resolution:%s, sharpness:%s});", 36 | line, resolution, sharpness)) 37 | structure(ct$get("bz"), class = "linestring") 38 | } 39 | -------------------------------------------------------------------------------- /R/boolean_clockwise.R: -------------------------------------------------------------------------------- 1 | #' Boolean clockwise 2 | #' 3 | #' @export 4 | #' @param line line [data-Feature]<([data-LineString])> 5 | #' @template lint 6 | #' @family boolean functions 7 | #' @return a logical (`TRUE`/`FALSE`) 8 | #' @examples 9 | #' l1 <- '[[0,0],[1,1],[1,0],[0,0]]' 10 | #' l2 <- '[[0,0],[1,0],[1,1],[0,0]]' 11 | #' lawn_boolean_clockwise(lawn_linestring(l1)) 12 | #' lawn_boolean_clockwise(lawn_linestring(l2)) 13 | lawn_boolean_clockwise <- function(line, lint = FALSE) { 14 | lawnlint(line, lint) 15 | ct$eval(sprintf("var bcw = turf.booleanClockwise(%s);", convert(line))) 16 | ct$get("bcw") 17 | } 18 | -------------------------------------------------------------------------------- /R/boolean_contains.R: -------------------------------------------------------------------------------- 1 | #' Boolean contains 2 | #' 3 | #' @export 4 | #' @param feature1,feature2 any [data-Geometry]/[data-Feature] objects 5 | #' @template lint 6 | #' @family boolean functions 7 | #' @return a logical (`TRUE`/`FALSE`) 8 | #' @examples 9 | #' l1 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 10 | #' pt1 <- '[1, 2]' 11 | #' lawn_boolean_contains(feature1=lawn_linestring(l1), feature2=lawn_point(pt1)) 12 | lawn_boolean_contains <- function(feature1, feature2, lint = FALSE) { 13 | lawnlint(list(feature1, feature2), lint) 14 | ct$eval(sprintf("var bcon = turf.booleanContains(%s, %s);", 15 | convert(feature1), convert(feature2))) 16 | ct$get("bcon") 17 | } 18 | -------------------------------------------------------------------------------- /R/boolean_crosses.R: -------------------------------------------------------------------------------- 1 | #' Boolean crosses 2 | #' 3 | #' @export 4 | #' @param feature1,feature2 any [data-Geometry]/[data-Feature] objects 5 | #' @template lint 6 | #' @family boolean functions 7 | #' @return a logical (`TRUE`/`FALSE`) 8 | #' @examples 9 | #' l1 <- '[[-2, 2], [4, 2]]' 10 | #' l2 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 11 | #' lawn_boolean_crosses(lawn_linestring(l1), lawn_linestring(l2)) 12 | lawn_boolean_crosses <- function(feature1, feature2, lint = FALSE) { 13 | lawnlint(list(feature1, feature2), lint) 14 | ct$eval(sprintf("var bcr = turf.booleanCrosses(%s, %s);", 15 | convert(feature1), convert(feature2))) 16 | ct$get("bcr") 17 | } 18 | -------------------------------------------------------------------------------- /R/boolean_disjoint.R: -------------------------------------------------------------------------------- 1 | #' Boolean crosses 2 | #' 3 | #' @export 4 | #' @param feature1,feature2 any [data-Geometry]/[data-Feature] objects 5 | #' @template lint 6 | #' @family boolean functions 7 | #' @return a logical (`TRUE`/`FALSE`) 8 | #' @examples 9 | #' pt1 <- '[2, 2]' 10 | #' l1 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 11 | #' lawn_boolean_disjoint(lawn_point(pt1), lawn_linestring(l1)) 12 | lawn_boolean_disjoint <- function(feature1, feature2, lint = FALSE) { 13 | lawnlint(list(feature1, feature2), lint) 14 | ct$eval(sprintf("var bdj = turf.booleanDisjoint(%s, %s);", 15 | convert(feature1), convert(feature2))) 16 | ct$get("bdj") 17 | } 18 | -------------------------------------------------------------------------------- /R/boolean_overlap.R: -------------------------------------------------------------------------------- 1 | #' Boolean overlap 2 | #' 3 | #' @export 4 | #' @param feature1,feature2 any data-Geometry/data-Feature( 5 | #' data-LineString|data-MultiLineString|data-Polygon|data-MultiPolygon) objects 6 | #' @template lint 7 | #' @family boolean functions 8 | #' @return a logical (`TRUE`/`FALSE`) 9 | #' @examples 10 | #' poly1 <- "[[[0,0],[0,5],[5,5],[5,0],[0,0]]]" 11 | #' poly2 <- "[[[1,1],[1,6],[6,6],[6,1],[1,1]]]" 12 | #' poly3 <- "[[[10,10],[10,15],[15,15],[15,10],[10,10]]]" 13 | #' lawn_boolean_overlap(lawn_polygon(poly1), lawn_polygon(poly2)) 14 | #' lawn_boolean_overlap(lawn_polygon(poly2), lawn_polygon(poly3)) 15 | lawn_boolean_overlap <- function(feature1, feature2, lint = FALSE) { 16 | lawnlint(list(feature1, feature2), lint) 17 | ct$eval(sprintf("var bover = turf.booleanOverlap(%s, %s);", 18 | convert(feature1), convert(feature2))) 19 | ct$get("bover") 20 | } 21 | -------------------------------------------------------------------------------- /R/boolean_pointonline.R: -------------------------------------------------------------------------------- 1 | #' Boolean overlap 2 | #' 3 | #' @export 4 | #' @param point any [data-Geometry]/[data-Feature] data-Point 5 | #' @param linestring any [data-Geometry]/[data-Feature] data-LineString 6 | #' @param ignoreEndVertices (logical) whether to ignore the start and end 7 | #' vertices. Default: `FALSE 8 | #' @template lint 9 | #' @family boolean functions 10 | #' @return a logical (`TRUE`/`FALSE`) 11 | #' @examples 12 | #' l1 <- "[[-1, -1],[1, 1],[1.5, 2.2]]" 13 | #' lawn_boolean_pointonline(lawn_point("[0, 0]"), lawn_linestring(l1)) 14 | lawn_boolean_pointonline <- function(point, linestring, 15 | ignoreEndVertices = FALSE, lint = FALSE) { 16 | 17 | lawnlint(list(point, linestring), lint) 18 | ct$eval(sprintf("var bpol = turf.booleanPointOnLine(%s, %s, %s);", 19 | convert(point), convert(linestring), 20 | convert(ignoreEndVertices))) 21 | ct$get("bpol") 22 | } 23 | -------------------------------------------------------------------------------- /R/boolean_within.R: -------------------------------------------------------------------------------- 1 | #' Boolean within 2 | #' 3 | #' returns `TRUE` if the first geometry is completely within the second 4 | #' geometry 5 | #' 6 | #' @export 7 | #' @param feature1,feature2 any [data-Geometry]/[data-Feature] objects 8 | #' @template lint 9 | #' @family boolean functions 10 | #' @return a logical (`TRUE`/`FALSE`) 11 | #' @examples 12 | #' pt1 <- '[1, 2]' 13 | #' l1 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 14 | #' lawn_boolean_within(lawn_point(pt1), lawn_linestring(l1)) 15 | lawn_boolean_within <- function(feature1, feature2, lint = FALSE) { 16 | lawnlint(list(feature1, feature2), lint) 17 | ct$eval(sprintf("var bdj = turf.booleanWithin(%s, %s);", 18 | convert(feature1), convert(feature2))) 19 | ct$get("bdj") 20 | } 21 | -------------------------------------------------------------------------------- /R/center.R: -------------------------------------------------------------------------------- 1 | #' Get center point 2 | #' 3 | #' Takes a [data-FeatureCollection] and returns the 4 | #' absolute center point of all features. 5 | #' 6 | #' @export 7 | #' @param features Input features, as a [data-Feature] or 8 | #' [data-FeatureCollection] 9 | #' @param properties A list of properties. Default: `NULL` 10 | #' @template lint 11 | #' @family measurements 12 | #' @return A [data-Point] feature at the absolute center point of 13 | #' all input features. 14 | #' @examples 15 | #' lawn_center(lawn_data$points_average) 16 | #' lawn_center(lawn_data$points_average, properties = list( 17 | #' foo = "bar", hello = "world")) 18 | #' @examples \dontrun{ 19 | #' lawn_center(lawn_data$points_average) %>% view 20 | #' lawn_featurecollection(lawn_data$points_average) %>% view 21 | #' lawn_center(lawn_data$points_average) %>% view 22 | #' } 23 | lawn_center <- function(features, properties = NULL, lint = FALSE) { 24 | features <- convert(features) 25 | lawnlint(features, lint) 26 | if (lint) is_type(features, type_top = c("Feature", "FeatureCollection")) 27 | ct$eval(sprintf("var cent = turf.center(%s, %s);", features, toj(properties))) 28 | structure(ct$get("cent"), class = "point") 29 | } 30 | -------------------------------------------------------------------------------- /R/centerofmass.R: -------------------------------------------------------------------------------- 1 | #' Center of mass 2 | #' 3 | #' Takes a [data-Feature] or a [data-FeatureCollection] 4 | #' and returns its center of mass using formula 5 | #' https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon 6 | #' 7 | #' @export 8 | #' @param x a [data-Feature] or [data-FeatureCollection] 9 | #' @template lint 10 | #' @family measurements 11 | #' @return a [data-Feature]<([data-Point])> 12 | #' @examples 13 | #' x <- '{ 14 | #' "type": "Feature", 15 | #' "properties": {}, 16 | #' "geometry": { 17 | #' "type": "Polygon", 18 | #' "coordinates": [[ 19 | #' [-112.072391,46.586591], 20 | #' [-112.072391,46.61761], 21 | #' [-112.028102,46.61761], 22 | #' [-112.028102,46.586591], 23 | #' [-112.072391,46.586591] 24 | #' ]] 25 | #' } 26 | #' }' 27 | #' lawn_center_of_mass(x) 28 | #' 29 | #' lawn_center_of_mass(lawn_data$polygons_average) 30 | lawn_center_of_mass <- function(x, lint = FALSE) { 31 | lawnlint(x, lint) 32 | if (lint) is_type(x, type_top = c("Feature", "FeatureCollection")) 33 | ct$eval(sprintf('var out = turf.centerOfMass(%s);', convert(x))) 34 | as.f(ct$get("out")) 35 | } 36 | -------------------------------------------------------------------------------- /R/centroid.R: -------------------------------------------------------------------------------- 1 | #' Centroid 2 | #' 3 | #' Takes one or more features and calculates the centroid using the arithmetic 4 | #' mean of all vertices. This lessens the effect of small islands and artifacts 5 | #' when calculating the centroid of a set of polygons. 6 | #' 7 | #' @export 8 | #' 9 | #' @param features Input features, as a [data-Feature] or 10 | #' [data-FeatureCollection] 11 | #' @param properties A list of properties. Default: `NULL` 12 | #' @template lint 13 | #' @family measurements 14 | #' @return a [data-Feature]<([data-Point])> - centroid of the input features 15 | #' @examples 16 | #' poly <- '{ 17 | #' "type": "Feature", 18 | #' "properties": {}, 19 | #' "geometry": { 20 | #' "type": "Polygon", 21 | #' "coordinates": [[ 22 | #' [105.818939,21.004714], 23 | #' [105.818939,21.061754], 24 | #' [105.890007,21.061754], 25 | #' [105.890007,21.004714], 26 | #' [105.818939,21.004714] 27 | #' ]] 28 | #' } 29 | #' }' 30 | #' lawn_centroid(features = poly) 31 | #' lawn_centroid(features = as.feature(poly)) 32 | #' lawn_centroid(features = poly, properties = list(foo = "bar")) 33 | lawn_centroid <- function(features, properties = NULL, lint = FALSE) { 34 | fts <- convert(features) 35 | lawnlint(fts, lint) 36 | assert(fts, c('character', 'feature', 'featurecollection', 'json')) 37 | if (lint) is_type(fts, type_top = c("Feature", "FeatureCollection")) 38 | ct$eval(sprintf("var ctr = turf.centroid(%s, %s);", fts, toj(properties))) 39 | structure(ct$get("ctr"), class = "point") 40 | } 41 | -------------------------------------------------------------------------------- /R/check_leaflet.R: -------------------------------------------------------------------------------- 1 | # check for leaflet 2 | check4leaflet <- function() { 3 | if (!requireNamespace("leaflet", quietly = TRUE)) { 4 | stop("Please install leaflet", call. = FALSE) 5 | } else { 6 | invisible(TRUE) 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /R/circle.R: -------------------------------------------------------------------------------- 1 | #' circle 2 | #' 3 | #' Takes a [data-Point] and calculates the circle polygon given 4 | #' a radius in degrees, radians, miles, or kilometers; and steps 5 | #' for precision 6 | #' 7 | #' @export 8 | #' @param center The center, a [data-Feature]<([data-Point])> 9 | #' @param radius (integer) Radius of the circle. 10 | #' @param steps (integer) Number of steps. 11 | #' @param units (character) Miles, kilometers (default), degrees, or radians 12 | #' @template lint 13 | #' @family assertions 14 | #' @return a [data-Feature]<([data-Polygon])> 15 | #' @examples 16 | #' pt <- '{ 17 | #' "type": "Feature", 18 | #' "properties": { 19 | #' "marker-color": "#0f0" 20 | #' }, 21 | #' "geometry": { 22 | #' "type": "Point", 23 | #' "coordinates": [-75.343, 39.984] 24 | #' } 25 | #' }' 26 | #' 27 | #' lawn_circle(pt, radius = 5, steps = 10) 28 | #' @examples \dontrun{ 29 | #' lawn_circle(pt, radius = 5, steps = 10) %>% view 30 | #' lawn_circle(pt, radius = 4, steps = 10) %>% view 31 | #' lawn_circle(pt, radius = 3, steps = 10) %>% view 32 | #' lawn_circle(pt, radius = 10, steps = 10) %>% view 33 | #' lawn_circle(pt, radius = 5, steps = 5) %>% view 34 | #' lawn_circle(pt, radius = 5, steps = 4) %>% view 35 | #' } 36 | lawn_circle <- function(center, radius, steps = FALSE, units = "kilometers", 37 | lint = FALSE) { 38 | lawnlint(center, lint) 39 | if (lint) is_type(center, type_top = "Feature") 40 | ct$eval(sprintf("var xx = turf.circle(%s, %s, {steps:%s, units:'%s'});", 41 | convert(center), radius, tolower(steps), units)) 42 | structure(ct$get("xx"), class = "polygon") 43 | } 44 | -------------------------------------------------------------------------------- /R/collectionof.R: -------------------------------------------------------------------------------- 1 | #' Enforce expectations about types of FeatureCollection inputs 2 | #' 3 | #' @export 4 | #' @param x a [data-FeatureCollection] for which features will be judged. 5 | #' required 6 | #' @param type (character) expected GeoJSON type. required. 7 | #' @param name (character) name of calling function. required. 8 | #' @template lint 9 | #' @family invariant 10 | #' @return nothing if no problems - error message if a problem 11 | #' @examples 12 | #' # all okay 13 | #' cat(lawn_data$points_count) 14 | #' lawn_collectionof(lawn_data$points_count, 'Point', 'stuff') 15 | #' 16 | #' # error 17 | #' # lawn_collectionof(lawn_data$points_count, 'Polygon', 'stuff') 18 | lawn_collectionof <- function(x, type, name, lint = FALSE) { 19 | lawnlint(x, lint) 20 | if (lint) is_type(x, type_top = "FeatureCollection") 21 | ct$eval(sprintf("var gt = turfinvariant.collectionOf(%s, '%s', '%s');", 22 | convert(x), type, name)) 23 | ct$get("gt") 24 | } 25 | -------------------------------------------------------------------------------- /R/cordall.R: -------------------------------------------------------------------------------- 1 | #' Get all coordinates from any GeoJSON object, returning an array 2 | #' of coordinate arrays. 3 | #' 4 | #' Takes any [data-GeoJSON] and returns an array of 5 | #' coordinate arrays 6 | #' 7 | #' @export 8 | #' @param x any [data-GeoJSON] object 9 | #' @template lint 10 | #' @return matrix of coordinates, where each row in the matrix is a 11 | #' coordinate pair 12 | #' @examples 13 | #' lawn_point(c(-74.5, 40)) %>% lawn_coordall() 14 | #' 15 | #' rings <- list(list( 16 | #' c(-2.275543, 53.464547), 17 | #' c(-2.275543, 53.489271), 18 | #' c(-2.215118, 53.489271), 19 | #' c(-2.215118, 53.464547), 20 | #' c(-2.275543, 53.464547) 21 | #' )) 22 | #' lawn_polygon(rings) %>% lawn_coordall() 23 | lawn_coordall <- function(x, lint = FALSE) { 24 | lawnlint(x, lint) 25 | ct$eval(sprintf("var cdall = turfmeta.coordAll(%s);", convert(x))) 26 | ct$get("cdall") 27 | } 28 | -------------------------------------------------------------------------------- /R/count.R: -------------------------------------------------------------------------------- 1 | #' Count number of points within polygons 2 | #' 3 | #' Calculates the number of [data-Point]'s that fall within the set 4 | #' of [data-Polygon]'s 5 | #' 6 | #' @export 7 | #' 8 | #' @template math 9 | #' @template lint 10 | #' @family aggregations 11 | #' @return a [data-FeatureCollection] 12 | #' @examples \dontrun{ 13 | #' # using data in the package 14 | #' cat(lawn_data$points_count) 15 | #' cat(lawn_data$polygons_count) 16 | #' lawn_count(lawn_data$polygons_count, lawn_data$points_count, 'population') 17 | #' } 18 | lawn_count <- function(polygons, points, in_field, out_field = 'count', 19 | lint = FALSE) { 20 | 21 | lawnlint(list(polygons, points), lint) 22 | if (lint) { 23 | is_type(polygons, "FeatureCollection", "Polygon") 24 | is_type(points, "FeatureCollection", "Point") 25 | } 26 | ct$eval(sprintf("var fc = turf.collect(%s, %s, '%s', 'values');", 27 | convert(polygons), convert(points), in_field)) 28 | ct$eval(sprintf("fc.features.forEach(function (feature) { 29 | feature.properties.%s = feature.properties.values.length; 30 | });", out_field)) 31 | as.fc(ct$get("fc")) 32 | } 33 | -------------------------------------------------------------------------------- /R/distance.R: -------------------------------------------------------------------------------- 1 | #' Distance between two points 2 | #' 3 | #' Calculates the distance between two [data-Point]s in degress, 4 | #' radians, miles, or kilometers. Uses the 5 | #' Haversine formula (http://en.wikipedia.org/wiki/Haversine_formula) 6 | #' to account for global curvature. 7 | #' 8 | #' @export 9 | #' 10 | #' @param from Origin [data-Feature]<([data-Point])> 11 | #' @param to Destination [data-Feature]<([data-Point])> 12 | #' @param units (character) Can be degrees, radians, miles, or kilometers 13 | #' (default). 14 | #' @template lint 15 | #' @family measurements 16 | #' @return Single numeric value 17 | #' @examples 18 | #' from <- '{ 19 | #' "type": "Feature", 20 | #' "properties": {}, 21 | #' "geometry": { 22 | #' "type": "Point", 23 | #' "coordinates": [-75.343, 39.984] 24 | #' } 25 | #' }' 26 | #' to <- '{ 27 | #' "type": "Feature", 28 | #' "properties": {}, 29 | #' "geometry": { 30 | #' "type": "Point", 31 | #' "coordinates": [-75.534, 39.123] 32 | #' } 33 | #' }' 34 | #' lawn_distance(from, to) 35 | lawn_distance <- function(from, to, units = 'kilometers', lint = FALSE) { 36 | from <- convert(from) 37 | to <- convert(to) 38 | lawnlint(list(from, to), lint) 39 | if (lint) { 40 | is_type(from, "Feature", "Point") 41 | is_type(to, "Feature", "Point") 42 | } 43 | ct$eval(sprintf('var point1 = %s;', from)) 44 | ct$eval(sprintf('var point2 = %s;', to)) 45 | ct$eval(sprintf("var avg = turf.distance(point1, point2, {units:'%s'});", 46 | units)) 47 | ct$get("avg") 48 | } 49 | -------------------------------------------------------------------------------- /R/explode.R: -------------------------------------------------------------------------------- 1 | #' Explode vertices to points 2 | #' 3 | #' Takes a feature or set of features and returns all positions as points 4 | #' 5 | #' @export 6 | #' @param input [data-Feature] or [data-FeatureCollection] 7 | #' @template lint 8 | #' @return a [data-FeatureCollection] of points 9 | #' @examples 10 | #' poly <- '{ 11 | #' "type": "Feature", 12 | #' "properties": {}, 13 | #' "geometry": { 14 | #' "type": "Polygon", 15 | #' "coordinates": [[ 16 | #' [177.434692, -17.77517], 17 | #' [177.402076, -17.779093], 18 | #' [177.38079, -17.803937], 19 | #' [177.40242, -17.826164], 20 | #' [177.438468, -17.824857], 21 | #' [177.454948, -17.796746], 22 | #' [177.434692, -17.77517] 23 | #' ]] 24 | #' } 25 | #' }' 26 | #' lawn_explode(poly) 27 | #' @examples \dontrun{ 28 | #' lawn_data$polygons_average %>% view 29 | #' lawn_explode(lawn_data$polygons_average) %>% view 30 | #' lawn_data$polygons_within %>% view 31 | #' lawn_explode(lawn_data$polygons_within) %>% view 32 | #' } 33 | lawn_explode <- function(input, lint = FALSE) { 34 | input <- convert(input) 35 | lawnlint(input, lint) 36 | if (lint) is_type(input, type_top = c("Feature", "FeatureCollection")) 37 | ct$eval(sprintf("var exp = turf.explode(%s);", input)) 38 | as.fc(ct$get("exp")) 39 | } 40 | -------------------------------------------------------------------------------- /R/extent.R: -------------------------------------------------------------------------------- 1 | #' Get a bounding box 2 | #' 3 | #' Calculates the extent of all input features in a FeatureCollection, 4 | #' and returns a bounding box. The returned bounding box is of the form 5 | #' (west, south, east, north). 6 | #' 7 | #' @export 8 | #' @param input A [data-Feature] or [data-FeatureCollection] 9 | #' @template lint 10 | #' @family measurements 11 | #' @return A bounding box, numeric vector of length 4, in 12 | #' `[ minX, minY, maxX, maxY ]` order 13 | #' @examples 14 | #' # From a FeatureCollection 15 | #' cat(lawn_data$points_average) 16 | #' lawn_extent(lawn_data$points_average) 17 | #' 18 | #' # From a Feature 19 | #' dat <- '{ 20 | #' "type": "Feature", 21 | #' "properties": {}, 22 | #' "geometry": { 23 | #' "type": "Polygon", 24 | #' "coordinates": [[ 25 | #' [-112.072391,46.586591], 26 | #' [-112.072391,46.61761], 27 | #' [-112.028102,46.61761], 28 | #' [-112.028102,46.586591], 29 | #' [-112.072391,46.586591] 30 | #' ]] 31 | #' } 32 | #' }' 33 | #' lawn_extent(dat) 34 | lawn_extent <- function(input, lint = FALSE) { 35 | input <- convert(input) 36 | lawnlint(input, lint) 37 | if (lint) is_type(input, type_top = c("Feature", "FeatureCollection")) 38 | ct$eval(sprintf("var bbox = turf.bbox(%s);", input)) 39 | ct$get("bbox") 40 | } 41 | -------------------------------------------------------------------------------- /R/featureeach.R: -------------------------------------------------------------------------------- 1 | #' Iterate over features in any GeoJSON object 2 | #' 3 | #' @export 4 | #' @param x any [data-GeoJSON] object 5 | #' @param fun a Javascript function. if not given, returns self 6 | #' @template lint 7 | #' @return matrix of coordinates, where each row in the matrix is a 8 | #' coordinate pair 9 | #' @examples 10 | #' x <- "{ type: 'Feature', geometry: null, properties: { foo: 1, bar: 3 } }" 11 | #' 12 | #' # don't apply any function, identity essentially 13 | #' lawn_featureeach(x) 14 | #' 15 | #' lawn_featureeach(lawn_data$points_count) 16 | #' 17 | #' # appply a function callback 18 | #' lawn_featureeach(lawn_data$points_count, "z.geometry") 19 | #' lawn_featureeach(lawn_data$points_count, "z.geometry.type") 20 | #' lawn_featureeach(lawn_data$points_count, "z.properties") 21 | #' lawn_featureeach(lawn_data$points_count, "z.properties.population") 22 | 23 | lawn_featureeach <- function(x, fun = NULL, lint = FALSE) { 24 | lawnlint(x, lint) 25 | ct$eval( 26 | sprintf(" 27 | var peach = []; 28 | turfmeta.featureEach(%s, function(z) { 29 | res = %s; 30 | peach.push(res); 31 | });", convert(x), fun %||% 'z' 32 | ) 33 | ) 34 | as.list(ct$get("peach")) 35 | } 36 | -------------------------------------------------------------------------------- /R/featureof.R: -------------------------------------------------------------------------------- 1 | #' Enforce expectations about types of Feature inputs 2 | #' 3 | #' @export 4 | #' @param x a [data-Feature] with an expected geometry type. required. 5 | #' @param type (character) expected GeoJSON type. required. 6 | #' @param name (character) name of calling function. required. 7 | #' @template lint 8 | #' @family invariant 9 | #' @return nothing if no problems - error message if a problem 10 | #' @examples 11 | #' # all okay 12 | #' x <- "{ type: 'Feature', properties: {}, geometry: { type: 'Point', 13 | #' coordinates: [10, 50] } }" 14 | #' lawn_featureof(x, 'Point', 'foobar') 15 | #' 16 | #' # error 17 | #' # lawn_featureof(x, 'MultiPoint', 'foobar') 18 | lawn_featureof <- function(x, type, name, lint = FALSE) { 19 | lawnlint(x, lint) 20 | ct$eval(sprintf("var gt = turfinvariant.featureOf(%s, '%s', '%s');", 21 | convert(x), type, name)) 22 | ct$get("gt") 23 | } 24 | -------------------------------------------------------------------------------- /R/filter.R: -------------------------------------------------------------------------------- 1 | #' Filter a FeatureCollection by a given property and value 2 | #' 3 | #' @export 4 | #' 5 | #' @param features A [data-FeatureCollection] 6 | #' @param key (character) The property on which to filter. 7 | #' @param value (character) The value of that property on which to filter. 8 | #' @template lint 9 | #' @family data functions 10 | #' @return S filtered [data-FeatureCollection] with only 11 | #' features that match input key and value. 12 | #' @examples 13 | #' cat(lawn_data$filter_features) 14 | #' lawn_filter(features = lawn_data$filter_features, key = 'species', 15 | #' value = 'oak') 16 | #' lawn_filter(lawn_data$filter_features, 'species', 'maple') 17 | #' lawn_filter(lawn_data$filter_features, 'species', 'redwood') 18 | lawn_filter <- function(features, key, value, lint = FALSE) { 19 | lawnlint(features, lint) 20 | ct$eval(sprintf('var key = "%s";', key)) 21 | ct$eval(sprintf('var value = "%s";', value)) 22 | ct$eval(' 23 | fun = function(cc, k, v) { 24 | var nfc = turf.featureCollection([]); 25 | for (var i = 0; i < cc.features.length; i++) { 26 | if(cc.features[i].properties[k] === v) { 27 | nfc.features.push(cc.features[i]); 28 | } 29 | } 30 | return nfc; 31 | } 32 | ') 33 | ct$eval(sprintf("var filtered = fun(%s, key, value);", features)) 34 | as.fc(ct$get("filtered")) 35 | } 36 | -------------------------------------------------------------------------------- /R/flatten.R: -------------------------------------------------------------------------------- 1 | #' Flatten 2 | #' 3 | #' Flattens any GeoJSON to a FeatureCollection 4 | #' 5 | #' @export 6 | #' @param x any valid GeoJSON with multi-geometry [data-Feature]'s 7 | #' @template lint 8 | #' @family misc 9 | #' @return a [data-FeatureCollection] 10 | #' @examples 11 | #' x <- '{"type":"MultiPolygon","coordinates":[ 12 | #' [[[102,2],[103,2],[103,3],[102,3],[102,2]]], 13 | #' [[[100,0],[101,0],[101,1],[100,1],[100,0]], 14 | #' [[100.2,0.2],[100.2,0.8],[100.8,0.8],[100.8,0.2],[100.2,0.2]]] 15 | #' ] 16 | #' }' 17 | #' lawn_flatten(x) 18 | #' lawn_flatten(x, TRUE) 19 | lawn_flatten <- function(x, lint = FALSE) { 20 | lawnlint(x, lint) 21 | ct$eval(sprintf('var out = turf.flatten(%s);', convert(x))) 22 | ct$get("out") 23 | } 24 | -------------------------------------------------------------------------------- /R/flip.R: -------------------------------------------------------------------------------- 1 | #' Flip x,y to y,x, and vice versa 2 | #' 3 | #' @export 4 | #' @param input [data-Feature] or [data-FeatureCollection] 5 | #' @template lint 6 | #' @return A [data-Feature] or [data-FeatureCollection] 7 | #' @examples 8 | #' # a point 9 | #' serbia <- '{ 10 | #' "type": "Feature", 11 | #' "properties": {"color": "red"}, 12 | #' "geometry": { 13 | #' "type": "Point", 14 | #' "coordinates": [20.566406, 43.421008] 15 | #' } 16 | #' }' 17 | #' lawn_flip(serbia) 18 | #' 19 | #' # a featurecollection 20 | #' pts <- lawn_random("points") 21 | #' lawn_flip(pts) 22 | #' @examples \dontrun{ 23 | #' lawn_data$points_average %>% view 24 | #' lawn_flip(lawn_data$points_average) %>% view 25 | #' lawn_data$polygons_average %>% view 26 | #' lawn_flip(lawn_data$polygons_average) %>% view 27 | #' } 28 | lawn_flip <- function(input, lint = FALSE) { 29 | input <- convert(input) 30 | lawnlint(input, lint) 31 | if (lint) is_type(input, type_top = c("Feature", "FeatureCollection")) 32 | ct$eval(sprintf("var flp = turf.flip(%s);", input)) 33 | structure(ct$get("flp"), class = tolower(ct$get("flp.type"))) 34 | } 35 | -------------------------------------------------------------------------------- /R/geojsontype.R: -------------------------------------------------------------------------------- 1 | #' Enforce expectations about types of GeoJSON objects. 2 | #' 3 | #' @export 4 | #' @param x value of any [data-GeoJSON] object. required. 5 | #' @param type expected GeoJSON type. required. 6 | #' @param name name of calling function. required. 7 | #' @template lint 8 | #' @family invariant 9 | #' @return nothing if no problems - error message if a problem 10 | #' @examples 11 | #' # all okay 12 | #' x <- "{ type: 'Point', coordinates: [10, 50] }" 13 | #' lawn_geosjontype(x, 'Point', 'fooBar') 14 | #' 15 | #' # error 16 | #' # lawn_geosjontype(x, 'Polygon', 'fooBar') 17 | lawn_geosjontype <- function(x, type, name, lint = FALSE) { 18 | lawnlint(x, lint) 19 | ct$eval(sprintf("var gt = turfinvariant.geojsonType(%s, '%s', '%s');", 20 | convert(x), type, name)) 21 | ct$get("gt") 22 | } 23 | -------------------------------------------------------------------------------- /R/geometrycollection.R: -------------------------------------------------------------------------------- 1 | #' Create a geometrycollection 2 | #' 3 | #' @export 4 | #' @param coordinates A list of GeoJSON geometries, or in json. 5 | #' @param properties A list of properties. 6 | #' @family data functions 7 | #' @return A [data-GeometryCollection] feature. 8 | #' @examples 9 | #' x <- list( 10 | #' list( 11 | #' type = "Point", 12 | #' coordinates = list( 13 | #' list(100, 0) 14 | #' ) 15 | #' ), 16 | #' list( 17 | #' type = "LineString", 18 | #' coordinates = list( 19 | #' list(100, 0), 20 | #' list(102, 1) 21 | #' ) 22 | #' ) 23 | #' ) 24 | #' lawn_geometrycollection(x) 25 | #' lawn_geometrycollection(x, 26 | #' properties = list(city = 'Los Angeles', population = 400)) 27 | #' 28 | #' x <- '[ 29 | #' { 30 | #' "type": "Point", 31 | #' "coordinates": [100.0, 0.0] 32 | #' }, 33 | #' { 34 | #' "type": "LineString", 35 | #' "coordinates": [ [101.0, 0.0], [102.0, 1.0] ] 36 | #' } 37 | #' ]' 38 | #' lawn_geometrycollection(x) 39 | lawn_geometrycollection <- function(coordinates, properties = NULL) { 40 | ct$eval(sprintf("var geocoll = turf.geometryCollection(%s, %s);", 41 | convert(coordinates), toj(properties))) 42 | as.gc(ct$get("geocoll")) 43 | } 44 | -------------------------------------------------------------------------------- /R/getcoord.R: -------------------------------------------------------------------------------- 1 | #' Unwrap a coordinate from a Feature with a Point geometry, 2 | #' or a single coordinate. 3 | #' 4 | #' @export 5 | #' @param x any [data-GeoJSON] object 6 | #' @template lint 7 | #' @return matrix of coordinates, where each row in the matrix is a 8 | #' coordinate pair 9 | #' @examples 10 | #' x <- "{ type: 'Point', coordinates: [10, 50] }" 11 | #' lawn_getcoord(x) 12 | #' 13 | #' library(jsonlite) 14 | #' x <- fromJSON(lawn_data$points_count, FALSE)$features 15 | #' lawn_getcoord(x[[1]]) 16 | #' lawn_getcoord(x[[2]]) 17 | #' lawn_getcoord(x[[1]]$geometry) 18 | #' lawn_getcoord(x[[1]]$geometry$coordinates) 19 | #' 20 | #' # fails 21 | #' # lawn_getcoord(x[[1]]$geometry$coordinates[[1]]) 22 | 23 | lawn_getcoord <- function(x, lint = FALSE) { 24 | lawnlint(x, lint) 25 | ct$eval(sprintf("var coord = turfinvariant.getCoord(%s);", convert(x))) 26 | ct$get("coord") 27 | } 28 | -------------------------------------------------------------------------------- /R/hex_grid.R: -------------------------------------------------------------------------------- 1 | #' Create a HexGrid 2 | #' 3 | #' Takes a bounding box and a cell size in degrees and returns a 4 | #' [data-FeatureCollection] of flat-topped hexagons 5 | #' ([data-Polygon] features) aligned in an "odd-q" vertical 6 | #' grid as described in Hexagonal Grids 7 | #' http://www.redblobgames.com/grids/hexagons/ 8 | #' 9 | #' @export 10 | #' 11 | #' @param extent (numeric) Extent in `[minX, minY, maxX, maxY]` order. 12 | #' @param cellWidth (integer) Width of each cell. 13 | #' @param units (character) Units to use for cellWidth, one of 'miles' or 14 | #' 'kilometers'. 15 | #' @family interpolation 16 | #' @return A [data-FeatureCollection] grid of points. 17 | #' @examples 18 | #' lawn_hex_grid(c(-96,31,-84,40), 50, 'miles') 19 | #' lawn_hex_grid(c(-96,31,-84,40), 30, 'miles') 20 | lawn_hex_grid <- function(extent, cellWidth, units) { 21 | ct$eval(sprintf("var hg = turf.hexGrid(%s, %s, {units:'%s'});", 22 | toj(extent), cellWidth, units)) 23 | as.fc(ct$get("hg")) 24 | } 25 | -------------------------------------------------------------------------------- /R/idw.R: -------------------------------------------------------------------------------- 1 | #' IDW 2 | #' 3 | #' DEFUNCT 4 | #' 5 | #' @export 6 | #' @param ... ignored 7 | #' @rdname lawn_idw-defunct 8 | lawn_idw <- function(...) .Defunct() 9 | -------------------------------------------------------------------------------- /R/kinks.R: -------------------------------------------------------------------------------- 1 | #' Get points at all self-intersections of a polygon 2 | #' 3 | #' @export 4 | #' @param input Feature of features. 5 | #' @template lint 6 | #' @examples 7 | #' poly <- '{ 8 | #' "type": "Feature", 9 | #' "properties": {}, 10 | #' "geometry": { 11 | #' "type": "Polygon", 12 | #' "coordinates": [[ 13 | #' [-12.034835, 8.901183], 14 | #' [-12.060413, 8.899826], 15 | #' [-12.03638, 8.873199], 16 | #' [-12.059383, 8.871418], 17 | #' [-12.034835, 8.901183] 18 | #' ]] 19 | #' } 20 | #' }' 21 | #' lawn_kinks(poly) 22 | #' # lint input object 23 | #' # lawn_kinks(poly, TRUE) 24 | #' @examples \dontrun{ 25 | #' poly %>% view 26 | #' lawn_kinks(poly) %>% view 27 | #' } 28 | lawn_kinks <- function(input, lint = FALSE) { 29 | lawnlint(input, lint) 30 | ct$eval(sprintf("var exp = turf.kinks(%s);", convert(input))) 31 | ct$get("exp") 32 | } 33 | -------------------------------------------------------------------------------- /R/lawn_bbox.R: -------------------------------------------------------------------------------- 1 | #' Make a bounding box from a polygon 2 | #' 3 | #' Takes a polygon [data-Polygon] and returns a bbox 4 | #' 5 | #' @export 6 | #' @param x A FeatureCollection of [data-Polygon] features. 7 | #' @template lint 8 | #' @family measurements 9 | #' @return A bounding box. 10 | #' @examples 11 | #' bbox <- c(0, 0, 10, 10) 12 | #' lawn_bbox(lawn_bbox_polygon(bbox)) 13 | lawn_bbox <- function(x, lint = FALSE) { 14 | lawnlint(x, lint) 15 | ct$eval(sprintf("var bbp = turf.bbox(%s);", convert(x))) 16 | ct$get("bbp") 17 | } 18 | -------------------------------------------------------------------------------- /R/lawn_feature.R: -------------------------------------------------------------------------------- 1 | #' Create a Feature 2 | #' 3 | #' @export 4 | #' @param geometry (character/json) Any geojson geometry. 5 | #' @param properties (list) list of properties, must be named 6 | #' @template lint 7 | #' @family data functions 8 | #' @examples \dontrun{ 9 | #' # points 10 | #' ## single point 11 | #' pt <- '{"type":"Point","coordinates":[-75.343,39.984]}' 12 | #' lawn_feature(pt) 13 | #' 14 | #' ## with properties 15 | #' lawn_feature(pt, properties = list(foo = "bar")) 16 | #' 17 | #' ## many points in a list 18 | #' pts <- list( 19 | #' lawn_point(c(-75.343, 39.984))$geometry, 20 | #' lawn_point(c(-75.833, 39.284))$geometry, 21 | #' lawn_point(c(-75.534, 39.123))$geometry 22 | #' ) 23 | #' lapply(pts, lawn_feature) 24 | #' } 25 | lawn_feature <- function(geometry, properties = c(), lint = FALSE) { 26 | lawnlint(geometry, lint) 27 | ct$eval(sprintf("var feet = turf.feature(%s, %s);", 28 | convert(geometry), convert(properties))) 29 | as.f(ct$get("feet")) 30 | } 31 | -------------------------------------------------------------------------------- /R/line_distance.R: -------------------------------------------------------------------------------- 1 | #' Measure a linestring 2 | #' 3 | #' Takes a [data-LineString] and measures its length in 4 | #' the specified units. Uses turf/length internally as 5 | #' lineDistance was deprecated 6 | #' 7 | #' @export 8 | #' @param line Line to measure, a [data-Feature]<([data-LineString])>, 9 | #' or [data-FeatureCollection]<([data-LineString])> 10 | #' @param units Can be degrees, radians, miles, or kilometers. 11 | #' @template lint 12 | #' @family measurements 13 | #' @return Length of the input line (numeric). 14 | #' @examples 15 | #' line <- '{ 16 | #' "type": "Feature", 17 | #' "properties": {}, 18 | #' "geometry": { 19 | #' "type": "LineString", 20 | #' "coordinates": [ 21 | #' [-77.031669, 38.878605], 22 | #' [-77.029609, 38.881946], 23 | #' [-77.020339, 38.884084], 24 | #' [-77.025661, 38.885821], 25 | #' [-77.021884, 38.889563], 26 | #' [-77.019824, 38.892368] 27 | #' ] 28 | #' } 29 | #' }' 30 | #' lawn_line_distance(line, 'kilometers') 31 | #' lawn_line_distance(line, 'miles') 32 | #' lawn_line_distance(line, 'radians') 33 | #' lawn_line_distance(line, 'degrees') 34 | lawn_line_distance <- function(line, units, lint = FALSE) { 35 | line <- convert(line) 36 | lawnlint(line, lint) 37 | assert(units, "character") 38 | if (lint) is_type(line, type_top = c("Feature", "FeatureCollection")) 39 | ct$eval(sprintf("var env = turf.length(%s, {units:'%s'});", line, units)) 40 | ct$get("env") 41 | } 42 | -------------------------------------------------------------------------------- /R/line_offset.R: -------------------------------------------------------------------------------- 1 | #' Offset a linestring 2 | #' 3 | #' Takes a [data-LineString] and returns a [data-LineString] at offset by 4 | #' the specified distance. 5 | #' 6 | #' @export 7 | #' @param line Line to measure, a [data-LineString]. 8 | #' @param distance (integer/numeric) Distance along the line. 9 | #' @param units Can be degrees, radians, miles, kilometers, inches, 10 | #' yards, meters 11 | #' @template lint 12 | #' @return a [data-LineString] 13 | #' @examples 14 | #' line <- '{ 15 | #' "type": "Feature", 16 | #' "properties": { 17 | #' "stroke": "#F00" 18 | #' }, 19 | #' "geometry": { 20 | #' "type": "LineString", 21 | #' "coordinates": [[-83, 30], [-84, 36], [-78, 41]] 22 | #' } 23 | #' }' 24 | #' 25 | #' lawn_line_offset(line, 2, 'miles') 26 | #' lawn_line_offset(line, 200, 'miles') 27 | #' lawn_line_offset(line, 0.5, 'radians') 28 | #' lawn_line_offset(line, 4, 'yards') 29 | #' 30 | #' 31 | #' line <- '{ 32 | #' "type": "LineString", 33 | #' "coordinates": [[-83, 30], [-84, 36], [-78, 41]] 34 | #' }' 35 | #' lawn_line_offset(line, 4, 'yards') 36 | lawn_line_offset <- function(line, distance, units, lint = FALSE) { 37 | line <- convert(line) 38 | lawnlint(line, lint) 39 | assert(distance, c('numeric', 'integer')) 40 | assert(units, "character") 41 | ct$eval(sprintf("var off = turf.lineOffset(%s, %s, {units:'%s'});", 42 | line, distance, units)) 43 | as.f(ct$get("off")) 44 | } 45 | -------------------------------------------------------------------------------- /R/linestring.R: -------------------------------------------------------------------------------- 1 | #' Create a linestring 2 | #' 3 | #' @export 4 | #' @param coordinates A list of positions. 5 | #' @param properties A list of properties. 6 | #' @family data functions 7 | #' @return A [data-Feature]<([data-LineString])> 8 | #' @examples 9 | #' linestring1 <- '[ 10 | #' [-21.964416, 64.148203], 11 | #' [-21.956176, 64.141316], 12 | #' [-21.93901, 64.135924], 13 | #' [-21.927337, 64.136673] 14 | #' ]' 15 | #' linestring2 <- '[ 16 | #' [-21.929054, 64.127985], 17 | #' [-21.912918, 64.134726], 18 | #' [-21.916007, 64.141016], 19 | #' [-21.930084, 64.14446] 20 | #' ]' 21 | #' lawn_linestring(linestring1) 22 | #' lawn_linestring(linestring2) 23 | #' 24 | #' pts <- list( 25 | #' c(-21.964416, 64.148203), 26 | #' c(-21.956176, 64.141316), 27 | #' c(-21.93901, 64.135924), 28 | #' c(-21.927337, 64.136673) 29 | #' ) 30 | #' lawn_linestring(pts, properties = list(name = 'line1', distance = 145)) 31 | #' 32 | #' # completely non-sensical, but gets some data quickly 33 | #' pts <- lawn_random()$features$geometry$coordinates 34 | #' lawn_linestring(pts) 35 | lawn_linestring <- function(coordinates, properties = NULL) { 36 | # TODO: need to find way to fail well when unacceptable items passed in 37 | ct$eval(sprintf("var poly = turf.lineString(%s, %s);", convert(coordinates), 38 | toj(properties))) 39 | as.l(ct$get("poly")) 40 | } 41 | -------------------------------------------------------------------------------- /R/max.R: -------------------------------------------------------------------------------- 1 | #' Maximum value of a field among points within polygons 2 | #' 3 | #' Calculates the maximum value of a field for a set of [data-Point]'s 4 | #' within a set of [data-Polygon]'s. 5 | #' 6 | #' @export 7 | #' @template math 8 | #' @template lint 9 | #' @family aggregations 10 | #' @return A FeatureCollection of [data-Polygon] features with 11 | #' properties listed as `out_field`. 12 | #' @examples \dontrun{ 13 | #' poly <- lawn_data$polygons_average 14 | #' pt <- lawn_data$points_average 15 | #' lawn_max(poly, pt, 'population') 16 | #' } 17 | lawn_max <- function(polygons, points, in_field, out_field = "max", 18 | lint = FALSE) { 19 | 20 | lawnlint(list(polygons, points), lint) 21 | calc_math("max", convert(polygons), convert(points), in_field, out_field) 22 | } 23 | -------------------------------------------------------------------------------- /R/median.R: -------------------------------------------------------------------------------- 1 | #' Median value of a field among points within polygons 2 | #' 3 | #' Calculates the **median** value of a field for a set of 4 | #' [data-Point]'s within a set of [data-Polygon]'s. 5 | #' 6 | #' @export 7 | #' @template math 8 | #' @template lint 9 | #' @family aggregations 10 | #' @return A FeatureCollection of [data-Polygon] features with 11 | #' properties listed as `out_field`. 12 | #' @examples \dontrun{ 13 | #' poly <- lawn_data$polygons_average 14 | #' pt <- lawn_data$points_average 15 | #' lawn_median(polygons=poly, points=pt, in_field='population') 16 | #' } 17 | lawn_median <- function(polygons, points, in_field, out_field = "median", 18 | lint = FALSE) { 19 | 20 | lawnlint(list(polygons, points), lint) 21 | calc_math("median", convert(polygons), convert(points), in_field, out_field) 22 | } 23 | -------------------------------------------------------------------------------- /R/midpoint.R: -------------------------------------------------------------------------------- 1 | #' Get a point midway between two points 2 | #' 3 | #' Takes two [data-Point]'s and returns a point midway 4 | #' between them 5 | #' 6 | #' @export 7 | #' @param pt1 First [data-Feature]<([data-Point])> 8 | #' @param pt2 Second [data-Feature]<([data-Point])> 9 | #' @template lint 10 | #' @family measurements 11 | #' @return A [data-Feature]<([data-Point])> midway between pt1 and pt2 12 | #' @examples 13 | #' pt1 <- '{ 14 | #' "type": "Feature", 15 | #' "properties": {}, 16 | #' "geometry": { 17 | #' "type": "Point", 18 | #' "coordinates": [144.834823, -37.771257] 19 | #' } 20 | #' }' 21 | #' pt2 <- '{ 22 | #' "type": "Feature", 23 | #' "properties": {}, 24 | #' "geometry": { 25 | #' "type": "Point", 26 | #' "coordinates": [145.14244, -37.830937] 27 | #' } 28 | #' }' 29 | #' lawn_midpoint(pt1, pt2) 30 | #' @examples \dontrun{ 31 | #' lawn_midpoint(pt1, pt2) %>% view 32 | #' lawn_featurecollection(list( 33 | #' lawn_point(jsonlite::fromJSON(pt1)$geometry$coordinates), 34 | #' lawn_point(jsonlite::fromJSON(pt2)$geometry$coordinates), 35 | #' structure(lawn_midpoint(pt1, pt2), class = "point") 36 | #' )) %>% view 37 | #' } 38 | lawn_midpoint <- function(pt1, pt2, lint = FALSE) { 39 | pt1 <- convert(pt1) 40 | pt2 <- convert(pt2) 41 | lawnlint(list(pt1, pt2), lint) 42 | if (lint) { 43 | is_type(pt1, type_top = "Feature", type_lower = "Point") 44 | is_type(pt2, type_top = "Feature", type_lower = "Point") 45 | } 46 | ct$eval(sprintf("var md = turf.midpoint(%s, %s);", pt1, pt2)) 47 | structure(ct$get("md"), class = "point") 48 | } 49 | -------------------------------------------------------------------------------- /R/min.R: -------------------------------------------------------------------------------- 1 | #' Minimum value of a field among points within polygons 2 | #' 3 | #' Calculates the minimum value of a field for a set of [data-Point]'s 4 | #' within a set of [data-Polygon]'s 5 | #' 6 | #' @export 7 | #' @template math 8 | #' @template lint 9 | #' @family aggregations 10 | #' @return A FeatureCollection of [data-Polygon] features with 11 | #' properties listed as `out_field`. 12 | #' @examples \dontrun{ 13 | #' poly <- lawn_data$polygons_average 14 | #' pt <- lawn_data$points_average 15 | #' lawn_min(poly, pt, 'population') 16 | #' } 17 | lawn_min <- function(polygons, points, in_field, out_field = "min", lint = FALSE) { 18 | lawnlint(list(polygons, points), lint) 19 | calc_math("min", convert(polygons), convert(points), in_field, out_field) 20 | } 21 | -------------------------------------------------------------------------------- /R/multilinestring.R: -------------------------------------------------------------------------------- 1 | #' Create a multilinestring 2 | #' 3 | #' @export 4 | #' @param coordinates A list of positions. 5 | #' @param properties A list of properties. 6 | #' @family data functions 7 | #' @return A [data-Feature]<([data-MultiLineString])> 8 | #' @examples 9 | #' mlstr <- '[ 10 | #' [ 11 | #' [-21.964416, 64.148203], 12 | #' [-21.956176, 64.141316], 13 | #' [-21.93901, 64.135924], 14 | #' [-21.927337, 64.136673] 15 | #' ], 16 | #' [ 17 | #' [-21.929054, 64.127985], 18 | #' [-21.912918, 64.134726], 19 | #' [-21.916007, 64.141016], 20 | #' [-21.930084, 64.14446] 21 | #' ] 22 | #' ]' 23 | #' lawn_multilinestring(mlstr) 24 | #' 25 | #' lawn_multilinestring(mlstr, 26 | #' properties = list(name = 'line1', distance = 145)) 27 | #' 28 | #' # Make a FeatureCollection 29 | #' lawn_featurecollection(lawn_multilinestring(mlstr)) 30 | #' 31 | #' @examples \dontrun{ 32 | #' lawn_featurecollection(lawn_multilinestring(mlstr)) %>% view 33 | #' } 34 | lawn_multilinestring <- function(coordinates, properties = NULL) { 35 | ct$eval(sprintf("var xx = turf.multiLineString(%s, %s);", 36 | convert(coordinates), toj(properties))) 37 | as.ml(ct$get("xx")) 38 | } 39 | -------------------------------------------------------------------------------- /R/multipoint.R: -------------------------------------------------------------------------------- 1 | #' MultiPoint 2 | #' 3 | #' Create a multipoint 4 | #' 5 | #' @export 6 | #' @param coordinates A list of point pairs, either as a list or json, of the 7 | #' form e.g. \code{list(c(longitude, latitude), c(longitude, latitude))} or 8 | #' as JSON e.g. \code{[[longitude, latitude], [longitude, latitude]]}. 9 | #' @param properties A list of properties. Default: `NULL` 10 | #' @family data functions 11 | #' @return A [data-Feature]<([data-MultiPoint])> 12 | #' @examples 13 | #' lawn_multipoint(list(c(-74.5, 40), c(-77.5, 45))) 14 | #' lawn_multipoint("[[-74.5,40],[-77.5,45]]") 15 | #' identical( 16 | #' lawn_multipoint(list(c(-74.5, 40), c(-77.5, 45))), 17 | #' lawn_multipoint("[[-74.5,40],[-77.5,45]]") 18 | #' ) 19 | #' lawn_multipoint("[[-74.5,40],[-77.5,45]]", 20 | #' properties = list(city = 'Boston', population = 400)) 21 | #' 22 | #' # Make a FeatureCollection 23 | #' lawn_featurecollection( 24 | #' lawn_multipoint(list(c(-74.5, 40), c(-77.5, 45))) 25 | #' ) 26 | lawn_multipoint <- function(coordinates, properties = NULL) { 27 | ct$eval(sprintf("var pt = turf.multiPoint(%s, %s);", 28 | convert(coordinates), toj(properties))) 29 | as.mp(ct$get("pt")) 30 | } 31 | -------------------------------------------------------------------------------- /R/multipolygon.R: -------------------------------------------------------------------------------- 1 | #' Create a multipolygon 2 | #' 3 | #' @export 4 | #' @param coordinates A list of LinearRings, or in json. 5 | #' @param properties A list of properties. 6 | #' @family data functions 7 | #' @return A [data-Feature]<([data-MultiPolygon])> 8 | #' @examples 9 | #' rings <- list( 10 | #' list(list( 11 | #' c(-2.27, 53.46), 12 | #' c(-2.27, 53.48), 13 | #' c(-2.21, 53.48), 14 | #' c(-2.21, 53.46), 15 | #' c(-2.27, 53.46) 16 | #' )), 17 | #' list(list( 18 | #' c(-4.27, 55.46), 19 | #' c(-4.27, 55.48), 20 | #' c(-4.21, 55.48), 21 | #' c(-4.21, 55.46), 22 | #' c(-4.27, 55.46) 23 | #' )) 24 | #' ) 25 | #' lawn_multipolygon(rings) 26 | #' lawn_multipolygon(rings, properties = list(name = 'poly1', population = 400)) 27 | #' 28 | #' x <- '[ 29 | #' [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], 30 | #' [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], 31 | #' [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]] 32 | #' ]' 33 | #' lawn_multipolygon(x) 34 | #' 35 | #' lawn_multipolygon("[[[[0,0],[0,10],[10,10],[10,0],[0,0]]]]") 36 | #' 37 | #' # Make a FeatureCollection 38 | #' lawn_featurecollection(lawn_multipolygon(rings)) 39 | #' 40 | #' @examples \dontrun{ 41 | #' lawn_featurecollection(lawn_multipolygon(rings)) %>% view 42 | #' } 43 | lawn_multipolygon <- function(coordinates, properties = NULL) { 44 | ct$eval(sprintf("var mpoly = turf.multiPolygon(%s, %s);", 45 | convert(coordinates), toj(properties))) 46 | as.mpol(ct$get("mpoly")) 47 | } 48 | -------------------------------------------------------------------------------- /R/onLoad.R: -------------------------------------------------------------------------------- 1 | #' @importFrom V8 v8 2 | ct <- NULL 3 | rand <- NULL 4 | ht <- NULL 5 | 6 | .onLoad <- function(libname, pkgname){ 7 | ct <<- V8::v8(); 8 | ct$source(system.file("js/turf516.js", package = pkgname)) 9 | ct$source(system.file("js/turf-meta.js", package = pkgname)) 10 | ct$source(system.file("js/turf-invariant.js", package = pkgname)) 11 | ct$source(system.file("js/cloner.js", package = pkgname)) 12 | ct$source(system.file("js/simple-statistics.js", package = pkgname)) 13 | 14 | rand <<- V8::v8(); 15 | rand$source(system.file("js/geojson_random.js", package = pkgname)) 16 | 17 | ht <<- V8::v8(); 18 | ht$source(system.file("js/geojsonhint-v201.js", package = pkgname)) 19 | } 20 | -------------------------------------------------------------------------------- /R/pipe.R: -------------------------------------------------------------------------------- 1 | #' Pipe operator 2 | #' 3 | #' @name %>% 4 | #' @rdname pipe 5 | #' @keywords internal 6 | #' @export 7 | #' @importFrom magrittr %>% 8 | #' @usage lhs \%>\% rhs 9 | NULL 10 | -------------------------------------------------------------------------------- /R/planepoint.R: -------------------------------------------------------------------------------- 1 | #' Calculate a Planepoint 2 | #' 3 | #' Takes a triangular plane as a [data-Polygon] and a 4 | #' [data-Point] within that triangle and returns the 5 | #' z-value at that point. 6 | #' 7 | #' @export 8 | #' @param pt The Point for which a z-value will be calculated. 9 | #' @param triangle A Polygon feature with three vertices. 10 | #' @template lint 11 | #' @family interpolation 12 | #' @return The z-value for pt (numeric). 13 | #' @details The Polygon needs to have properties a, b, and c that define 14 | #' the values at its three corners. 15 | #' @examples 16 | #' pt <- lawn_point(c(-75.3221, 39.529)) 17 | #' triangle <- '{ 18 | #' "type": "Feature", 19 | #' "properties": { 20 | #' "a": 11, 21 | #' "b": 122, 22 | #' "c": 44 23 | #' }, 24 | #' "geometry": { 25 | #' "type": "Polygon", 26 | #' "coordinates": [[ 27 | #' [-75.1221, 39.57], 28 | #' [-75.58, 39.18], 29 | #' [-75.97, 39.86], 30 | #' [-75.1221, 39.57] 31 | #' ]] 32 | #' } 33 | #' }' 34 | #' lawn_planepoint(pt, triangle) 35 | lawn_planepoint <- function(pt, triangle, lint = FALSE) { 36 | pt <- convert(pt) 37 | triangle <- convert(triangle) 38 | lawnlint(list(pt, triangle), lint) 39 | ct$eval(sprintf("var planept = turf.planepoint(%s, %s);", pt, triangle)) 40 | ct$get("planept") 41 | } 42 | -------------------------------------------------------------------------------- /R/point.R: -------------------------------------------------------------------------------- 1 | #' Create a point 2 | #' 3 | #' @export 4 | #' @param coordinates A pair of points in a vector, list or json, of the form 5 | #' \code{e.g., c(longitude,latitude)}. 6 | #' @param properties A list of properties. Default: NULL 7 | #' @family data functions 8 | #' @return A [data-Feature]<([data-Point])> 9 | #' @examples 10 | #' lawn_point(c(-74.5, 40)) 11 | #' lawn_point(list(-74.5, 40)) 12 | #' lawn_point('[-74.5, 40]') 13 | #' lawn_point(c(-74.5, 40), properties = list(name = 'poly1', population = 400)) 14 | #' 15 | #' # Make a FeatureCollection 16 | #' lawn_featurecollection(lawn_point(c(-74.5, 40))) 17 | lawn_point <- function(coordinates, properties = NULL) { 18 | ct$eval(sprintf("var pt = turf.point(%s, %s);", 19 | convert(coordinates), toj(properties))) 20 | structure(ct$get("pt"), class = "point") 21 | } 22 | -------------------------------------------------------------------------------- /R/point_grid.R: -------------------------------------------------------------------------------- 1 | #' Create a PointGrid 2 | #' 3 | #' Takes a bounding box and a cell depth and returns a set of 4 | #' [data-Point]'s in a grid 5 | #' 6 | #' @export 7 | #' 8 | #' @param extent (numeric) Extent in `[minX, minY, maxX, maxY]` order. 9 | #' @param cellSide (integer) the distance between points 10 | #' @param units (character) Units to use for cellWidth, one of 'miles' or 11 | #' 'kilometers' (default). 12 | #' @param mask (logical) if passed a Polygon or MultiPolygon, the grid 13 | #' Points will be created only inside it 14 | #' @family interpolation 15 | #' @return [data-FeatureCollection] grid of points. 16 | #' @note parameters `centered` and `bboxIsMask` removed 17 | #' @examples 18 | #' lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles') 19 | #' lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles') 20 | #' lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles') 21 | lawn_point_grid <- function(extent, cellSide, units = "kilometers", 22 | mask = NULL) { 23 | 24 | assert(extent, c('numeric', 'integer')) 25 | assert(cellSide, c('numeric', 'integer')) 26 | assert(units, 'character') 27 | if (!is.null(mask)) { 28 | ct$eval(sprintf("var options = {units:'%s', mask:%s};", units, 29 | convert(mask))) 30 | } else { 31 | ct$eval(sprintf("var options = {units:'%s'};", units)) 32 | } 33 | ct$eval(sprintf("var pg = turf.pointGrid(%s, %s, options);", 34 | toj(extent), cellSide)) 35 | as.fc(ct$get("pg")) 36 | } 37 | -------------------------------------------------------------------------------- /R/point_on_surface.R: -------------------------------------------------------------------------------- 1 | #' Get a point on the surface of a feature 2 | #' 3 | #' Finds a [data-Point] guaranteed to be on the surface of 4 | #' [data-GeoJSON] object. 5 | #' 6 | #' @export 7 | #' @param x Any [data-GeoJSON] object 8 | #' @return A [data-Feature]<([data-Point])> on the surface of `x` 9 | #' @template lint 10 | #' @family measurements 11 | #' @details What will be returned? 12 | #' \itemize{ 13 | #' \item Given a [data-Polygon], the point will be in the area 14 | #' of the polygon 15 | #' \item Given a [data-LineString], the point will be along 16 | #' the string 17 | #' \item Given a [data-Point], the point will be the same as 18 | #' the input 19 | #' } 20 | #' 21 | #' @examples 22 | #' # polygon 23 | #' x <- lawn_random("polygons") 24 | #' lawn_point_on_feature(x) 25 | #' # point 26 | #' x <- lawn_random("points") 27 | #' lawn_point_on_feature(x) 28 | #' # linestring 29 | #' linestring <- '[ 30 | #' [-21.929054, 64.127985], 31 | #' [-21.912918, 64.134726], 32 | #' [-21.916007, 64.141016], 33 | #' [-21.930084, 64.14446] 34 | #' ]' 35 | #' lawn_point_on_feature(lawn_linestring(linestring)) 36 | lawn_point_on_feature <- function(x, lint = FALSE) { 37 | x <- convert(x) 38 | lawnlint(x, lint) 39 | is_type(x, type_top = c("Feature", "FeatureCollection")) 40 | ct$eval(sprintf("var psf = turf.pointOnFeature(%s);", x)) 41 | structure(ct$get("psf"), class = "point") 42 | } 43 | lawn_point_on_surface <- lawn_point_on_feature 44 | -------------------------------------------------------------------------------- /R/polygon.R: -------------------------------------------------------------------------------- 1 | #' Create a polygon 2 | #' 3 | #' @export 4 | #' @param coordinates A list of LinearRings, or in json. 5 | #' @param properties A list of properties. 6 | #' @family data functions 7 | #' @return A [data-Polygon] feature. 8 | #' @examples 9 | #' rings <- list(list( 10 | #' c(-2.275543, 53.464547), 11 | #' c(-2.275543, 53.489271), 12 | #' c(-2.215118, 53.489271), 13 | #' c(-2.215118, 53.464547), 14 | #' c(-2.275543, 53.464547) 15 | #' )) 16 | #' lawn_polygon(rings) 17 | #' lawn_polygon(rings, properties = list(name = 'poly1', population = 400)) 18 | #' 19 | #' # Make a FeatureCollection 20 | #' lawn_featurecollection(lawn_polygon(rings)) 21 | #' 22 | #' @examples \dontrun{ 23 | #' lawn_featurecollection(lawn_polygon(rings)) %>% view 24 | #' } 25 | lawn_polygon <- function(coordinates, properties = NULL) { 26 | ct$eval(sprintf("var poly = turf.polygon(%s, %s);", 27 | convert(coordinates), toj(properties))) 28 | as.pol(ct$get("poly")) 29 | } 30 | -------------------------------------------------------------------------------- /R/remove.R: -------------------------------------------------------------------------------- 1 | #' Remove things from a FeatureCollection 2 | #' 3 | #' Takes a [data-FeatureCollection] of any type, a property, and a 4 | #' value and returns a [data-FeatureCollection] with features 5 | #' matching that property-value pair removed. 6 | #' 7 | #' @export 8 | #' 9 | #' @param features A set of input features. 10 | #' @param property Property to filter. 11 | #' @param value Value to filter. 12 | #' @template lint 13 | #' @family data functions 14 | #' @return A [data-FeatureCollection]. 15 | #' @examples 16 | #' cat(lawn_data$remove_features) 17 | #' lawn_remove(lawn_data$remove_features, 'marker-color', '#00f') 18 | #' lawn_remove(lawn_data$remove_features, 'marker-color', '#0f0') 19 | lawn_remove <- function(features, property, value, lint = FALSE) { 20 | features <- convert(features) 21 | lawnlint(features, lint) 22 | ct$eval(' 23 | fun = function(cc, k, v) { 24 | var nfc = turf.featureCollection([]); 25 | for (var i = 0; i < cc.features.length; i++) { 26 | if(cc.features[i].properties[k] === v) { 27 | nfc.features.push(cc.features[i]); 28 | } 29 | } 30 | return nfc; 31 | } 32 | ') 33 | ct$eval(sprintf("var rmv = fun(%s, '%s', '%s');", features, property, value)) 34 | as.fc(ct$get("rmv")) 35 | } 36 | -------------------------------------------------------------------------------- /R/rewind.R: -------------------------------------------------------------------------------- 1 | #' Rewind 2 | #' 3 | #' Rewind (Multi)LineString or (Multi)Polygon outer ring counterclockwise 4 | #' and inner rings clockwise (Uses Shoelace Formula) 5 | #' 6 | #' @export 7 | #' @param x A [data-FeatureCollection] or [data-Feature] with 8 | #' Polygon, MultiPolygon, LineString, or MultiLineString 9 | #' @param reverse (logical) enable reverse winding. Default: `FALSE` 10 | #' @param mutate (logical) allows GeoJSON input to be mutated (significant 11 | #' performance increase if true) Default: `FALSE` 12 | #' @template lint 13 | #' @return A [data-FeatureCollection] 14 | #' 15 | #' @examples 16 | #' x <- '{ 17 | #' "type": "Feature", 18 | #' "properties": {}, 19 | #' "geometry": { 20 | #' "type": "Polygon", 21 | #' "coordinates": [ 22 | #' [[121, -29], [138, -29], [138, -18], [121, -18], [121, -29]] 23 | #' ] 24 | #' } 25 | #' }' 26 | #' lawn_rewind(x, TRUE) 27 | #' lawn_rewind(x, mutate = TRUE) 28 | #' lawn_rewind(x, lint = TRUE) 29 | lawn_rewind <- function(x, reverse = FALSE, mutate = FALSE, lint = FALSE) { 30 | if (!is.null(x)) lawnlint(x, lint) 31 | if (lint) is_type(x, type_top = c("FeatureCollection", "Feature")) 32 | ct$eval(sprintf("var res = turf.rewind(%s, {reverse:%s, mutate:%s});", 33 | convert(x), tolower(reverse), tolower(mutate))) 34 | as.fc(ct$get("res")) 35 | } 36 | -------------------------------------------------------------------------------- /R/sample.R: -------------------------------------------------------------------------------- 1 | #' Return features from FeatureCollection at random 2 | #' 3 | #' Takes a [data-FeatureCollection] and returns a 4 | #' [data-FeatureCollection] with given number of features at random. 5 | #' 6 | #' @export 7 | #' @param features A [data-FeatureCollection] 8 | #' @param n (integer) Number of features to generate. 9 | #' @template lint 10 | #' @family data functions 11 | #' @return A [data-FeatureCollection] 12 | #' 13 | #' @examples 14 | #' lawn_sample(lawn_data$points_average, 1) 15 | #' lawn_sample(lawn_data$points_average, 2) 16 | #' lawn_sample(lawn_data$points_average, 3) 17 | lawn_sample <- function(features = NULL, n = 100, lint = FALSE) { 18 | if (!is.null(features)) lawnlint(features, lint) 19 | if (lint) is_type(features, type_top = "FeatureCollection") 20 | ct$eval(sprintf("var pts = turf.sample(%s, %s);", features, n)) 21 | as.fc(ct$get("pts")) 22 | } 23 | -------------------------------------------------------------------------------- /R/square.R: -------------------------------------------------------------------------------- 1 | #' Calculate a square bounding box 2 | #' 3 | #' Takes a bounding box and calculates the minimum square bounding 4 | #' box that would contain the input. 5 | #' 6 | #' @export 7 | #' @param bbox A bounding box. 8 | #' @family measurements 9 | #' @return A square surrounding bbox, numeric vector of length four. 10 | #' @examples 11 | #' bbox <- c(-20, -20, -15, 0) 12 | #' lawn_square(bbox) 13 | #' @examples \dontrun{ 14 | #' sq <- lawn_square(bbox) 15 | #' lawn_featurecollection(list(lawn_bbox_polygon(bbox), 16 | #' lawn_bbox_polygon(sq))) %>% view 17 | #' } 18 | lawn_square <- function(bbox) { 19 | assert(bbox, c('numeric', 'integer')) 20 | ct$eval(sprintf("var sq = turf.square(%s);", convert(bbox))) 21 | ct$get("sq") 22 | } 23 | -------------------------------------------------------------------------------- /R/square_grid.R: -------------------------------------------------------------------------------- 1 | #' Create a SquareGrid 2 | #' 3 | #' Takes a bounding box and a cell depth and returns a set of square 4 | #' [data-Polygon]'s in a grid. 5 | #' 6 | #' @export 7 | #' 8 | #' @param extent (numeric) Extent in `[minX, minY, maxX, maxY]` order. 9 | #' @param cellSide (integer) dimension of each cell. 10 | #' @param units (character) Units to use for cellSide, one of 'miles' or 11 | #' 'kilometers'. 12 | #' @param mask (logical) if passed a Polygon or MultiPolygon, the grid 13 | #' Points will be created only inside it 14 | #' @family interpolation 15 | #' @return [data-FeatureCollection] grid of polygons. 16 | #' @examples \dontrun{ 17 | #' lawn_square_grid(c(-95, 30 ,-85, 40), 30, 'miles') 18 | #' lawn_square_grid(c(-95, 30 ,-85, 40), 10, 'miles') 19 | #' lawn_square_grid(c(-95, 30 ,-85, 40), 3, 'miles') 20 | #' } 21 | lawn_square_grid <- function(extent, cellSide, units = "kilometers", 22 | mask = NULL) { 23 | 24 | assert(extent, c('numeric', 'integer')) 25 | assert(cellSide, c('numeric', 'integer')) 26 | assert(units, 'character') 27 | if (!is.null(mask)) { 28 | ct$eval(sprintf("var options = {units:'%s', mask:%s};", units, 29 | convert(mask))) 30 | } else { 31 | ct$eval(sprintf("var options = {units:'%s'};", units)) 32 | } 33 | ct$eval(sprintf("var sg = turf.squareGrid(%s, %s, options);", 34 | toj(extent), cellSide)) 35 | as.fc(ct$get("sg")) 36 | } 37 | -------------------------------------------------------------------------------- /R/sum.R: -------------------------------------------------------------------------------- 1 | #' Sum of a field among points within polygons 2 | #' 3 | #' Calculates the sum of a field for a set of [data-Point]'s 4 | #' within a set of [data-Polygon]'s. 5 | #' 6 | #' @export 7 | #' @template math 8 | #' @template lint 9 | #' @family aggregations 10 | #' @return A FeatureCollection of [data-Polygon] features with 11 | #' properties listed as `out_field`. 12 | #' @examples \dontrun{ 13 | #' poly <- lawn_data$polygons_average 14 | #' pt <- lawn_data$points_average 15 | #' lawn_sum(poly, pt, 'population') 16 | #' } 17 | lawn_sum <- function(polygons, points, in_field, out_field = "sum", 18 | lint = FALSE) { 19 | 20 | lawnlint(list(polygons, points), lint) 21 | calc_math("sum", convert(polygons), convert(points), in_field, out_field) 22 | } 23 | -------------------------------------------------------------------------------- /R/tesselate.R: -------------------------------------------------------------------------------- 1 | #' Tesselate 2 | #' 3 | #' Tesselates a [data-Polygon] into a 4 | #' [data-FeatureCollection] of 5 | #' triangles using earcut (github.com/mapbox/earcut) 6 | #' 7 | #' @export 8 | #' @param polygon Input [data-Feature]<([data-Polygon])> 9 | #' @template lint 10 | #' @family assertions 11 | #' @return A [data-FeatureCollection] 12 | #' @examples 13 | #' poly <- '{ 14 | #' "type": "Feature", 15 | #' "properties": { 16 | #' "fill": "#0f0" 17 | #' }, 18 | #' "geometry": { 19 | #' "type": "Polygon", 20 | #' "coordinates": [[ 21 | #' [-46.738586, -23.596711], 22 | #' [-46.738586, -23.458207], 23 | #' [-46.560058, -23.458207], 24 | #' [-46.560058, -23.596711], 25 | #' [-46.738586, -23.596711] 26 | #' ]] 27 | #' } 28 | #' }' 29 | #' lawn_tesselate(poly) 30 | #' 31 | #' xx <- jsonlite::fromJSON(lawn_data$polygons_within, FALSE) 32 | #' lawn_tesselate(xx$features[[1]]) 33 | #' @examples \dontrun{ 34 | #' lawn_tesselate(xx$features[[1]]) %>% view 35 | #' lawn_tesselate(poly) %>% view 36 | #' } 37 | lawn_tesselate <- function(polygon, lint = FALSE) { 38 | poly <- convert(polygon) 39 | lawnlint(poly, lint) 40 | if (lint) is_type(polygon, "Feature", "Polygon") 41 | ct$eval(sprintf("var er = turf.tesselate(%s);", poly)) 42 | tmp <- ct$get("er") 43 | structure(tmp, class = tolower(tmp$type)) 44 | } 45 | -------------------------------------------------------------------------------- /R/tin.R: -------------------------------------------------------------------------------- 1 | #' Create a Triangulated Irregular Network 2 | #' 3 | #' Takes a set of [data-Point]'s and the name of a z-value property 4 | #' and creates a Triangulated Irregular Network (TIN). 5 | #' 6 | #' @export 7 | #' @param pt Input points. 8 | #' @param propertyName (character) Name of the property from which to pull z 9 | #' values. This is optional: if not given, then there will be no extra data 10 | #' added to the derived triangles 11 | #' @template lint 12 | #' @family interpolation 13 | #' @return TIN output, as a [data-FeatureCollection]. 14 | #' @details Data returned as a collection of Polygons. These are often used for 15 | #' developing elevation contour maps or stepped heat visualizations. 16 | #' 17 | #' This triangulates the points, as well as adds properties called a, b, and c 18 | #' representing the value of the given propertyName at each of the points that 19 | #' represent the corners of the triangle. 20 | #' @examples 21 | #' pts <- lawn_random(bbox = c(-70, 40, -60, 60)) 22 | #' lawn_tin(pts) 23 | #' @examples \dontrun{ 24 | #' lawn_tin(pts) %>% view 25 | #' lawn_tin(lawn_random(bbox = c(-70, 40, -60, 10))) %>% view 26 | #' } 27 | lawn_tin <- function(pt, propertyName = NULL, lint = FALSE) { 28 | pt <- convert(pt) 29 | lawnlint(pt, lint) 30 | assert(propertyName, 'character') 31 | ct$eval(sprintf("var tin = turf.tin(%s, '%s');", pt, convert(propertyName))) 32 | as.fc(ct$get("tin")) 33 | } 34 | -------------------------------------------------------------------------------- /R/triangle_grid.R: -------------------------------------------------------------------------------- 1 | #' Create a TriangleGrid 2 | #' 3 | #' Takes a bounding box and a cell depth and returns a set of triangular 4 | #' [data-Polygon]'s in a grid. 5 | #' 6 | #' @export 7 | #' 8 | #' @param extent (numeric) Extent in `[minX, minY, maxX, maxY]` order. 9 | #' @param cellSide (integer) dimension of each cell. 10 | #' @param units (character) Units to use for cellSide, one of miles, 11 | #' kilometers, degrees, radians 12 | #' @param mask (logical) if passed a Polygon or MultiPolygon, the grid 13 | #' Points will be created only inside it 14 | #' @family interpolation 15 | #' @return [data-FeatureCollection] grid of [data-Polygon]'s 16 | #' @examples 17 | #' lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles') 18 | #' lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles') 19 | #' lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles') 20 | lawn_triangle_grid <- function(extent, cellSide, units = "kilometers", 21 | mask = NULL) { 22 | 23 | assert(extent, c('numeric', 'integer')) 24 | assert(cellSide, c('numeric', 'integer')) 25 | assert(units, 'character') 26 | if (!is.null(mask)) { 27 | ct$eval(sprintf("var options = {units:'%s', mask:%s};", units, 28 | convert(mask))) 29 | } else { 30 | ct$eval(sprintf("var options = {units:'%s'};", units)) 31 | } 32 | ct$eval(sprintf("var tg = turf.triangleGrid(%s, %s, options);", 33 | toj(extent), cellSide)) 34 | as.fc(ct$get("tg")) 35 | } 36 | -------------------------------------------------------------------------------- /R/truncate.R: -------------------------------------------------------------------------------- 1 | #' Truncate 2 | #' 3 | #' Takes a GeoJSON Feature or FeatureCollection and truncates the precision 4 | #' of the geometry. 5 | #' 6 | #' @export 7 | #' @param x any [data-Feature] or [data-FeatureCollection] 8 | #' @param precision (integer) coordinate decimal precision. default: 6 9 | #' @param coordinates (integer) maximum number of coordinates (primarily used 10 | #' to remove z coordinates). default: 2 11 | #' @param mutate (logical) allows GeoJSON input to be mutated (significant 12 | #' performance increase if true) Default: `FALSE` 13 | #' @template lint 14 | #' @family misc 15 | #' @return a [data-Feature] or [data-FeatureCollection] with truncated geometry 16 | #' @examples 17 | #' cat(lawn_data$filter_features) 18 | #' lawn_coordall(lawn_data$filter_features) 19 | #' lawn_truncate(lawn_data$filter_features, 4) %>% lawn_coordall 20 | #' lawn_truncate(lawn_data$filter_features, 2) %>% lawn_coordall 21 | #' lawn_truncate(lawn_data$filter_features, 4, 1) %>% lawn_coordall 22 | lawn_truncate <- function(x, precision = 6, coordinates = 2, mutate = FALSE, 23 | lint = FALSE) { 24 | 25 | lawnlint(x, lint) 26 | assert(precision, c('numeric', 'integer')) 27 | assert(coordinates, c('numeric', 'integer')) 28 | assert(mutate, "logical") 29 | ct$eval(sprintf( 30 | 'var out = turf.truncate(%s, {precision:%s, coordinates:%s, mutate:%s});', 31 | x, precision, coordinates, tolower(mutate))) 32 | as.fc(ct$get("out")) 33 | } 34 | -------------------------------------------------------------------------------- /R/unkinkpolygon.R: -------------------------------------------------------------------------------- 1 | #' Unkink polygon 2 | #' 3 | #' Takes a kinked polygon and returns a feature collection of polygons 4 | #' that have no kinks. 5 | #' 6 | #' @export 7 | #' @param x A [data-FeatureCollection]<([data-Polygon])> or 8 | #' [data-FeatureCollection]<([data-MultiPolygon])> 9 | #' @template lint 10 | #' @family grids 11 | #' @return a [data-FeatureCollection]<([data-Polygon])> 12 | #' @examples 13 | #' x <- '{ 14 | #' "type": "Feature", 15 | #' "properties": {}, 16 | #' "geometry": { 17 | #' "type": "Polygon", 18 | #' "coordinates": [[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]] 19 | #' } 20 | #' }' 21 | #' lawn_unkinkpolygon(x) 22 | #' view(x) 23 | #' view(lawn_unkinkpolygon(x)) 24 | lawn_unkinkpolygon <- function(x, lint = FALSE) { 25 | lawnlint(x, lint) 26 | if (lint) is_type(x, "FeatureCollection", c("Polygon", "MultiPolygon")) 27 | ct$eval(sprintf('var out = turf.unkinkPolygon(%s);', x)) 28 | as.fc(ct$get("out")) 29 | } 30 | -------------------------------------------------------------------------------- /R/variance.R: -------------------------------------------------------------------------------- 1 | #' Variance of a field among points within polygons 2 | #' 3 | #' Calculates the variance value of a field for a set of [data-Point]'s 4 | #' within a set of [data-Polygon]'s. 5 | #' 6 | #' @export 7 | #' @template math 8 | #' @family aggregations 9 | #' @return A FeatureCollection of [data-Polygon] features with 10 | #' properties listed as `out_field`. 11 | #' @template lint 12 | #' @return A FeatureCollection of [data-Polygon] features with 13 | #' properties listed as `out_field`. 14 | #' @examples \dontrun{ 15 | #' poly <- lawn_data$polygons_average 16 | #' pt <- lawn_data$points_average 17 | #' lawn_variance(poly, pt, 'population') 18 | #' } 19 | lawn_variance <- function(polygons, points, in_field, out_field = "variance", 20 | lint = FALSE) { 21 | 22 | lawnlint(list(polygons, points), lint) 23 | calc_math("variance", convert(polygons), convert(points), in_field, 24 | out_field) 25 | } 26 | -------------------------------------------------------------------------------- /R/within.R: -------------------------------------------------------------------------------- 1 | #' Return points that fall within polygons 2 | #' 3 | #' Takes a set of [data-Point]'s and a set of 4 | #' [data-Polygon]'s and returns points that fall within 5 | #' the polygons. 6 | #' 7 | #' @export 8 | #' @param points [data-FeatureCollection] of points. 9 | #' @param polygons [data-FeatureCollection] of polygons. 10 | #' @template lint 11 | #' @family joins 12 | #' @return Points that land within at least one polygon, as a 13 | #' [data-FeatureCollection]. 14 | #' @examples \dontrun{ 15 | #' cat(lawn_data$points_within) 16 | #' cat(lawn_data$polygons_within) 17 | #' lawn_within(lawn_data$points_within, lawn_data$polygons_within) 18 | #' 19 | #' pt <- '{ 20 | #' "type": "Feature", 21 | #' "properties": {}, 22 | #' "geometry": { 23 | #' "type": "Point", 24 | #' "coordinates": [-90.548630, 14.616599] 25 | #' } 26 | #' }' 27 | #' poly <- lawn_featurecollection(lawn_buffer(pt, 5)) 28 | #' pts <- lawn_featurecollection(lawn_point(c(-90.55, 14.62))) 29 | #' 30 | #' lawn_within(pts, poly) 31 | #' } 32 | lawn_within <- function(points, polygons, lint = FALSE) { 33 | points <- convert(points) 34 | polygons <- convert(polygons) 35 | lawnlint(list(points, polygons), lint) 36 | if (lint) { 37 | is_type(points, "FeatureCollection", "Point") 38 | is_type(polygons, "FeatureCollection", "Polygon") 39 | } 40 | ct$eval(sprintf("var pts = turf.within(%s, %s);", points, polygons)) 41 | as.fc(ct$get("pts")) 42 | } 43 | -------------------------------------------------------------------------------- /README-NOT.md: -------------------------------------------------------------------------------- 1 | lawn 2 | ======= 3 | 4 | [![Project Status: Abandoned – Initial development has started, but there has not yet been a stable, usable release; the project has been abandoned and the author(s) do not intend on continuing development.](https://www.repostatus.org/badges/latest/abandoned.svg)](https://www.repostatus.org/#abandoned) 5 | 6 | 7 | `lawn` is an R wrapper for the Javascript library [turf.js](https://turfjs.org/). In addition, we have a few functions to interface with the [geojson-random](https://github.com/mapbox/geojson-random) and [geojsonhint](https://www.npmjs.com/package/geojsonhint) Javascript libraries, for generating random GeoJSON objects and linting GeoJSON, respectively. 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lawn 2 | 3 | [![Project Status: Abandoned](https://www.repostatus.org/badges/latest/abandoned.svg)](https://www.repostatus.org/#abandoned) 4 | 5 | This repository has been archived. The former README is now in [README-NOT.md](README-NOT.md). 6 | -------------------------------------------------------------------------------- /cran-comments.md: -------------------------------------------------------------------------------- 1 | ## Test environments 2 | 3 | * local OS X install, R 4.0.3 patched 4 | * ubuntu 16.04 (on GitHub Actions), R 4.0.3 5 | * win-builder (devel and release) 6 | 7 | ## R CMD check results 8 | 9 | 0 errors | 0 warnings | 0 notes 10 | 11 | ## Reverse dependencies 12 | 13 | There are no reverse dependencies. 14 | 15 | --- 16 | 17 | This version fixes updates the javascript libraries. 18 | 19 | Thanks! 20 | Scott Chamberlain 21 | -------------------------------------------------------------------------------- /data/lawn_data.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/lawn/e196449f6855862760c2bf8e319b06fa393ebec2/data/lawn_data.rda -------------------------------------------------------------------------------- /inst/js/LICENSE-clone: -------------------------------------------------------------------------------- 1 | Copyright © 2011-2015 Paul Vorbach 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the “Software”), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE 18 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /inst/js/LICENSE-geojson-random: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2017, Tom MacWright 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 11 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 14 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /inst/js/LICENSE-geojsonhint: -------------------------------------------------------------------------------- 1 | Copyright 2017 Mapbox 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. 4 | 5 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 6 | -------------------------------------------------------------------------------- /inst/js/LICENSE-simple-statistics: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2014, Tom MacWright 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND 11 | FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 14 | OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /inst/js/LICENSE-turfjs: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Morgan Herlocker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /man-roxygen/lint.R: -------------------------------------------------------------------------------- 1 | #' @param lint (logical) Lint or not. Uses geojsonhint. Takes up increasing 2 | #' time as the object to get linted increases in size, so probably use by 3 | #' default for small objects, but not for large if you know they are good 4 | #' geojson objects. Default: \code{FALSE} 5 | -------------------------------------------------------------------------------- /man-roxygen/math.R: -------------------------------------------------------------------------------- 1 | #' @param polygons a \code{\link{data-FeatureCollection}} of 2 | #' \code{\link{data-Polygon}} features 3 | #' @param points a \code{\link{data-FeatureCollection}} of 4 | #' \code{\link{data-Point}} features 5 | #' @param in_field (character) the field in input data to analyze 6 | #' @param out_field (character) the field in which to store results 7 | -------------------------------------------------------------------------------- /man/as.feature.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/as.feature.R 3 | \name{as.feature} 4 | \alias{as.feature} 5 | \title{Coerce character strings or JSON to GeoJSON Feature} 6 | \usage{ 7 | as.feature(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{a character string or json class with a GeoJSON object, any 11 | of feature, point, multipoint, linestring, multilinestring, polygon, 12 | or multipolygon. featurecollection and geometrycollection simply returned 13 | without alteration} 14 | 15 | \item{...}{ignored} 16 | } 17 | \value{ 18 | a \code{feature} class object 19 | } 20 | \description{ 21 | Coerce character strings or JSON to GeoJSON Feature 22 | } 23 | \examples{ 24 | poly <- '{ 25 | "type": "Feature", 26 | "properties": {}, 27 | "geometry": { 28 | "type": "Polygon", 29 | "coordinates": [[ 30 | [105.818939,21.004714], 31 | [105.818939,21.061754], 32 | [105.890007,21.061754], 33 | [105.890007,21.004714], 34 | [105.818939,21.004714] 35 | ]] 36 | } 37 | }' 38 | as.feature(poly) 39 | 40 | pt <- '{"type":"Point","coordinates":[-75.343,39.984]}' 41 | as.feature(pt) 42 | 43 | line <- '{ 44 | "type": "LineString", 45 | "coordinates": [ 46 | [-77.031669, 38.878605], 47 | [-77.029609, 38.881946], 48 | [-77.020339, 38.884084], 49 | [-77.025661, 38.885821], 50 | [-77.021884, 38.889563], 51 | [-77.019824, 38.892368] 52 | ] 53 | }' 54 | as.feature(line) 55 | 56 | # returns self if no match - note "Points" is not a GeoJSON type 57 | pt <- '{"type":"Points","coordinates":[-75.343,39.984]}' 58 | as.feature(pt) 59 | } 60 | -------------------------------------------------------------------------------- /man/as.turf.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/as.turf.R 3 | \name{as.turf} 4 | \alias{as.turf} 5 | \title{Coerce to turf.js javascript function call} 6 | \usage{ 7 | as.turf(x, ...) 8 | } 9 | \arguments{ 10 | \item{x}{Input data to be coerced.} 11 | 12 | \item{...}{Ignored.} 13 | } 14 | \description{ 15 | Coerce to turf.js javascript function call 16 | } 17 | \keyword{internal} 18 | -------------------------------------------------------------------------------- /man/as_feature.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/as_feature.R 3 | \name{as_feature} 4 | \alias{as_feature} 5 | \title{Convert a FeatureCollection to a Feature} 6 | \usage{ 7 | as_feature(x) 8 | } 9 | \arguments{ 10 | \item{x}{A \link{data-FeatureCollection}.} 11 | } 12 | \description{ 13 | Convert a FeatureCollection to a Feature 14 | } 15 | \details{ 16 | If there are more than one feature within the featurecollection, 17 | each feature is split out into a separate feature, returned in a list. 18 | Each feature is assigned a class matching it's GeoJSON data type 19 | (e.g., point, polygon, linestring). 20 | } 21 | \examples{ 22 | as_feature(lawn_random()) 23 | # as_feature(lawn_random("polygons")) 24 | } 25 | \seealso{ 26 | \link{as.feature} , which is similarly named, but has a different 27 | purpose 28 | } 29 | -------------------------------------------------------------------------------- /man/figures/map1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/lawn/e196449f6855862760c2bf8e319b06fa393ebec2/man/figures/map1.png -------------------------------------------------------------------------------- /man/figures/map2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/lawn/e196449f6855862760c2bf8e319b06fa393ebec2/man/figures/map2.png -------------------------------------------------------------------------------- /man/lawn-defunct.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/defunct-deprecated.R 3 | \name{lawn-defunct} 4 | \alias{lawn-defunct} 5 | \title{Defunct functions in \pkg{lawn}} 6 | \description{ 7 | \itemize{ 8 | \item \link{lawn_size}: Function removed. The \code{size} 9 | method in turf.js has been removed. 10 | See https://github.com/Turfjs/turf/issues/306 11 | \item \link{lawn_reclass}: Function removed. The \code{reclass} 12 | method in turf.js has been removed. 13 | See https://github.com/Turfjs/turf/issues/306 14 | \item \link{lawn_jenks}: Function removed. The \code{jenks} 15 | method in turf.js has been removed. 16 | See https://github.com/Turfjs/turf/issues/306 17 | \item \link{lawn_quantile}: Function removed. The \code{quantile} 18 | method in turf.js has been removed. 19 | See https://github.com/Turfjs/turf/issues/306 20 | \item \link{lawn_aggregate}: Function removed. The \code{aggregate} 21 | method in turf.js has been removed. 22 | See https://github.com/Turfjs/turf/issues/306 23 | \item \link{lawn_idw}: Function removed. 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /man/lawn-package.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lawn-package.R 3 | \docType{package} 4 | \name{lawn-package} 5 | \alias{lawn-package} 6 | \alias{lawn} 7 | \title{R client for turf.js for geospatial analysis} 8 | \description{ 9 | turf.js uses GeoJSON for all geographic data, and expects the data to be 10 | standard WGS84 (http://en.wikipedia.org/wiki/World_Geodetic_System) 11 | longitude,latitude coordinates. See https://geojson.io/ for a tool to 12 | easily create GeoJSON in a browser. 13 | } 14 | \seealso{ 15 | \link{lawn-defunct} 16 | } 17 | \author{ 18 | Scott Chamberlain 19 | 20 | Jeff Hollister (\email{hollister.jeff@epa.gov}) 21 | } 22 | -------------------------------------------------------------------------------- /man/lawn_aggregate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/defunct-deprecated.R 3 | \name{lawn_aggregate} 4 | \alias{lawn_aggregate} 5 | \title{Calculate a series of aggregations for a set of points within polygons} 6 | \usage{ 7 | lawn_aggregate(...) 8 | } 9 | \description{ 10 | This function is defunct 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/lawn_area.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/area.R 3 | \name{lawn_area} 4 | \alias{lawn_area} 5 | \title{Calculate the area of a polygon or group of polygons} 6 | \usage{ 7 | lawn_area(input, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{input}{A \link{data-Feature} or \link{data-FeatureCollection} of polygons} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | A numeric in square meters 19 | } 20 | \description{ 21 | Calculate the area of a polygon or group of polygons 22 | } 23 | \examples{ 24 | lawn_area(lawn_data$poly) 25 | lawn_area(lawn_data$multipoly) 26 | } 27 | \seealso{ 28 | Other measurements: 29 | \code{\link{lawn_along}()}, 30 | \code{\link{lawn_bbox_polygon}()}, 31 | \code{\link{lawn_bbox}()}, 32 | \code{\link{lawn_bearing}()}, 33 | \code{\link{lawn_center_of_mass}()}, 34 | \code{\link{lawn_center}()}, 35 | \code{\link{lawn_centroid}()}, 36 | \code{\link{lawn_destination}()}, 37 | \code{\link{lawn_distance}()}, 38 | \code{\link{lawn_envelope}()}, 39 | \code{\link{lawn_extent}()}, 40 | \code{\link{lawn_line_distance}()}, 41 | \code{\link{lawn_midpoint}()}, 42 | \code{\link{lawn_point_on_feature}()}, 43 | \code{\link{lawn_pt2line_distance}()}, 44 | \code{\link{lawn_square}()} 45 | } 46 | \concept{measurements} 47 | -------------------------------------------------------------------------------- /man/lawn_bbox.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lawn_bbox.R 3 | \name{lawn_bbox} 4 | \alias{lawn_bbox} 5 | \title{Make a bounding box from a polygon} 6 | \usage{ 7 | lawn_bbox(x, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{A FeatureCollection of \link{data-Polygon} features.} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | A bounding box. 19 | } 20 | \description{ 21 | Takes a polygon \link{data-Polygon} and returns a bbox 22 | } 23 | \examples{ 24 | bbox <- c(0, 0, 10, 10) 25 | lawn_bbox(lawn_bbox_polygon(bbox)) 26 | } 27 | \seealso{ 28 | Other measurements: 29 | \code{\link{lawn_along}()}, 30 | \code{\link{lawn_area}()}, 31 | \code{\link{lawn_bbox_polygon}()}, 32 | \code{\link{lawn_bearing}()}, 33 | \code{\link{lawn_center_of_mass}()}, 34 | \code{\link{lawn_center}()}, 35 | \code{\link{lawn_centroid}()}, 36 | \code{\link{lawn_destination}()}, 37 | \code{\link{lawn_distance}()}, 38 | \code{\link{lawn_envelope}()}, 39 | \code{\link{lawn_extent}()}, 40 | \code{\link{lawn_line_distance}()}, 41 | \code{\link{lawn_midpoint}()}, 42 | \code{\link{lawn_point_on_feature}()}, 43 | \code{\link{lawn_pt2line_distance}()}, 44 | \code{\link{lawn_square}()} 45 | } 46 | \concept{measurements} 47 | -------------------------------------------------------------------------------- /man/lawn_bbox_polygon.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/bbox_polygon.R 3 | \name{lawn_bbox_polygon} 4 | \alias{lawn_bbox_polygon} 5 | \title{Make a polygon from a bounding box} 6 | \usage{ 7 | lawn_bbox_polygon(bbox) 8 | } 9 | \arguments{ 10 | \item{bbox}{An array of bounding box coordinates in the form: 11 | \verb{[xLow, yLow, xHigh, yHigh]}.} 12 | } 13 | \value{ 14 | A \link{data-Polygon} representation of the bounding box. 15 | } 16 | \description{ 17 | Takes a bbox and returns an equivalent polygon \link{data-Polygon}. 18 | } 19 | \examples{ 20 | bbox <- c(0, 0, 10, 10) 21 | lawn_bbox_polygon(bbox) 22 | \dontrun{ 23 | lawn_bbox_polygon(bbox) \%>\% view 24 | lawn_bbox_polygon(c(1, 3, 5, 50)) \%>\% view 25 | } 26 | } 27 | \seealso{ 28 | Other measurements: 29 | \code{\link{lawn_along}()}, 30 | \code{\link{lawn_area}()}, 31 | \code{\link{lawn_bbox}()}, 32 | \code{\link{lawn_bearing}()}, 33 | \code{\link{lawn_center_of_mass}()}, 34 | \code{\link{lawn_center}()}, 35 | \code{\link{lawn_centroid}()}, 36 | \code{\link{lawn_destination}()}, 37 | \code{\link{lawn_distance}()}, 38 | \code{\link{lawn_envelope}()}, 39 | \code{\link{lawn_extent}()}, 40 | \code{\link{lawn_line_distance}()}, 41 | \code{\link{lawn_midpoint}()}, 42 | \code{\link{lawn_point_on_feature}()}, 43 | \code{\link{lawn_pt2line_distance}()}, 44 | \code{\link{lawn_square}()} 45 | } 46 | \concept{measurements} 47 | -------------------------------------------------------------------------------- /man/lawn_boolean_clockwise.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boolean_clockwise.R 3 | \name{lawn_boolean_clockwise} 4 | \alias{lawn_boolean_clockwise} 5 | \title{Boolean clockwise} 6 | \usage{ 7 | lawn_boolean_clockwise(line, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{line}{line \link{data-Feature}<(\link{data-LineString})>} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | a logical (\code{TRUE}/\code{FALSE}) 19 | } 20 | \description{ 21 | Boolean clockwise 22 | } 23 | \examples{ 24 | l1 <- '[[0,0],[1,1],[1,0],[0,0]]' 25 | l2 <- '[[0,0],[1,0],[1,1],[0,0]]' 26 | lawn_boolean_clockwise(lawn_linestring(l1)) 27 | lawn_boolean_clockwise(lawn_linestring(l2)) 28 | } 29 | \seealso{ 30 | Other boolean functions: 31 | \code{\link{lawn_boolean_contains}()}, 32 | \code{\link{lawn_boolean_crosses}()}, 33 | \code{\link{lawn_boolean_disjoint}()}, 34 | \code{\link{lawn_boolean_overlap}()}, 35 | \code{\link{lawn_boolean_pointonline}()}, 36 | \code{\link{lawn_boolean_within}()} 37 | } 38 | \concept{boolean functions} 39 | -------------------------------------------------------------------------------- /man/lawn_boolean_contains.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boolean_contains.R 3 | \name{lawn_boolean_contains} 4 | \alias{lawn_boolean_contains} 5 | \title{Boolean contains} 6 | \usage{ 7 | lawn_boolean_contains(feature1, feature2, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{feature1, feature2}{any \link{data-Geometry}/\link{data-Feature} objects} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | a logical (\code{TRUE}/\code{FALSE}) 19 | } 20 | \description{ 21 | Boolean contains 22 | } 23 | \examples{ 24 | l1 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 25 | pt1 <- '[1, 2]' 26 | lawn_boolean_contains(feature1=lawn_linestring(l1), feature2=lawn_point(pt1)) 27 | } 28 | \seealso{ 29 | Other boolean functions: 30 | \code{\link{lawn_boolean_clockwise}()}, 31 | \code{\link{lawn_boolean_crosses}()}, 32 | \code{\link{lawn_boolean_disjoint}()}, 33 | \code{\link{lawn_boolean_overlap}()}, 34 | \code{\link{lawn_boolean_pointonline}()}, 35 | \code{\link{lawn_boolean_within}()} 36 | } 37 | \concept{boolean functions} 38 | -------------------------------------------------------------------------------- /man/lawn_boolean_crosses.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boolean_crosses.R 3 | \name{lawn_boolean_crosses} 4 | \alias{lawn_boolean_crosses} 5 | \title{Boolean crosses} 6 | \usage{ 7 | lawn_boolean_crosses(feature1, feature2, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{feature1, feature2}{any \link{data-Geometry}/\link{data-Feature} objects} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | a logical (\code{TRUE}/\code{FALSE}) 19 | } 20 | \description{ 21 | Boolean crosses 22 | } 23 | \examples{ 24 | l1 <- '[[-2, 2], [4, 2]]' 25 | l2 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 26 | lawn_boolean_crosses(lawn_linestring(l1), lawn_linestring(l2)) 27 | } 28 | \seealso{ 29 | Other boolean functions: 30 | \code{\link{lawn_boolean_clockwise}()}, 31 | \code{\link{lawn_boolean_contains}()}, 32 | \code{\link{lawn_boolean_disjoint}()}, 33 | \code{\link{lawn_boolean_overlap}()}, 34 | \code{\link{lawn_boolean_pointonline}()}, 35 | \code{\link{lawn_boolean_within}()} 36 | } 37 | \concept{boolean functions} 38 | -------------------------------------------------------------------------------- /man/lawn_boolean_disjoint.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boolean_disjoint.R 3 | \name{lawn_boolean_disjoint} 4 | \alias{lawn_boolean_disjoint} 5 | \title{Boolean crosses} 6 | \usage{ 7 | lawn_boolean_disjoint(feature1, feature2, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{feature1, feature2}{any \link{data-Geometry}/\link{data-Feature} objects} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | a logical (\code{TRUE}/\code{FALSE}) 19 | } 20 | \description{ 21 | Boolean crosses 22 | } 23 | \examples{ 24 | pt1 <- '[2, 2]' 25 | l1 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 26 | lawn_boolean_disjoint(lawn_point(pt1), lawn_linestring(l1)) 27 | } 28 | \seealso{ 29 | Other boolean functions: 30 | \code{\link{lawn_boolean_clockwise}()}, 31 | \code{\link{lawn_boolean_contains}()}, 32 | \code{\link{lawn_boolean_crosses}()}, 33 | \code{\link{lawn_boolean_overlap}()}, 34 | \code{\link{lawn_boolean_pointonline}()}, 35 | \code{\link{lawn_boolean_within}()} 36 | } 37 | \concept{boolean functions} 38 | -------------------------------------------------------------------------------- /man/lawn_boolean_overlap.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boolean_overlap.R 3 | \name{lawn_boolean_overlap} 4 | \alias{lawn_boolean_overlap} 5 | \title{Boolean overlap} 6 | \usage{ 7 | lawn_boolean_overlap(feature1, feature2, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{feature1, feature2}{any data-Geometry/data-Feature( 11 | data-LineString|data-MultiLineString|data-Polygon|data-MultiPolygon) objects} 12 | 13 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 14 | time as the object to get linted increases in size, so probably use by 15 | default for small objects, but not for large if you know they are good 16 | geojson objects. Default: \code{FALSE}} 17 | } 18 | \value{ 19 | a logical (\code{TRUE}/\code{FALSE}) 20 | } 21 | \description{ 22 | Boolean overlap 23 | } 24 | \examples{ 25 | poly1 <- "[[[0,0],[0,5],[5,5],[5,0],[0,0]]]" 26 | poly2 <- "[[[1,1],[1,6],[6,6],[6,1],[1,1]]]" 27 | poly3 <- "[[[10,10],[10,15],[15,15],[15,10],[10,10]]]" 28 | lawn_boolean_overlap(lawn_polygon(poly1), lawn_polygon(poly2)) 29 | lawn_boolean_overlap(lawn_polygon(poly2), lawn_polygon(poly3)) 30 | } 31 | \seealso{ 32 | Other boolean functions: 33 | \code{\link{lawn_boolean_clockwise}()}, 34 | \code{\link{lawn_boolean_contains}()}, 35 | \code{\link{lawn_boolean_crosses}()}, 36 | \code{\link{lawn_boolean_disjoint}()}, 37 | \code{\link{lawn_boolean_pointonline}()}, 38 | \code{\link{lawn_boolean_within}()} 39 | } 40 | \concept{boolean functions} 41 | -------------------------------------------------------------------------------- /man/lawn_boolean_pointonline.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boolean_pointonline.R 3 | \name{lawn_boolean_pointonline} 4 | \alias{lawn_boolean_pointonline} 5 | \title{Boolean overlap} 6 | \usage{ 7 | lawn_boolean_pointonline( 8 | point, 9 | linestring, 10 | ignoreEndVertices = FALSE, 11 | lint = FALSE 12 | ) 13 | } 14 | \arguments{ 15 | \item{point}{any \link{data-Geometry}/\link{data-Feature} data-Point} 16 | 17 | \item{linestring}{any \link{data-Geometry}/\link{data-Feature} data-LineString} 18 | 19 | \item{ignoreEndVertices}{(logical) whether to ignore the start and end 20 | vertices. Default: `FALSE} 21 | 22 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 23 | time as the object to get linted increases in size, so probably use by 24 | default for small objects, but not for large if you know they are good 25 | geojson objects. Default: \code{FALSE}} 26 | } 27 | \value{ 28 | a logical (\code{TRUE}/\code{FALSE}) 29 | } 30 | \description{ 31 | Boolean overlap 32 | } 33 | \examples{ 34 | l1 <- "[[-1, -1],[1, 1],[1.5, 2.2]]" 35 | lawn_boolean_pointonline(lawn_point("[0, 0]"), lawn_linestring(l1)) 36 | } 37 | \seealso{ 38 | Other boolean functions: 39 | \code{\link{lawn_boolean_clockwise}()}, 40 | \code{\link{lawn_boolean_contains}()}, 41 | \code{\link{lawn_boolean_crosses}()}, 42 | \code{\link{lawn_boolean_disjoint}()}, 43 | \code{\link{lawn_boolean_overlap}()}, 44 | \code{\link{lawn_boolean_within}()} 45 | } 46 | \concept{boolean functions} 47 | -------------------------------------------------------------------------------- /man/lawn_boolean_within.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/boolean_within.R 3 | \name{lawn_boolean_within} 4 | \alias{lawn_boolean_within} 5 | \title{Boolean within} 6 | \usage{ 7 | lawn_boolean_within(feature1, feature2, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{feature1, feature2}{any \link{data-Geometry}/\link{data-Feature} objects} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | a logical (\code{TRUE}/\code{FALSE}) 19 | } 20 | \description{ 21 | returns \code{TRUE} if the first geometry is completely within the second 22 | geometry 23 | } 24 | \examples{ 25 | pt1 <- '[1, 2]' 26 | l1 <- '[[1, 1], [1, 2], [1, 3], [1, 4]]' 27 | lawn_boolean_within(lawn_point(pt1), lawn_linestring(l1)) 28 | } 29 | \seealso{ 30 | Other boolean functions: 31 | \code{\link{lawn_boolean_clockwise}()}, 32 | \code{\link{lawn_boolean_contains}()}, 33 | \code{\link{lawn_boolean_crosses}()}, 34 | \code{\link{lawn_boolean_disjoint}()}, 35 | \code{\link{lawn_boolean_overlap}()}, 36 | \code{\link{lawn_boolean_pointonline}()} 37 | } 38 | \concept{boolean functions} 39 | -------------------------------------------------------------------------------- /man/lawn_collectionof.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/collectionof.R 3 | \name{lawn_collectionof} 4 | \alias{lawn_collectionof} 5 | \title{Enforce expectations about types of FeatureCollection inputs} 6 | \usage{ 7 | lawn_collectionof(x, type, name, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{a \link{data-FeatureCollection} for which features will be judged. 11 | required} 12 | 13 | \item{type}{(character) expected GeoJSON type. required.} 14 | 15 | \item{name}{(character) name of calling function. required.} 16 | 17 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 18 | time as the object to get linted increases in size, so probably use by 19 | default for small objects, but not for large if you know they are good 20 | geojson objects. Default: \code{FALSE}} 21 | } 22 | \value{ 23 | nothing if no problems - error message if a problem 24 | } 25 | \description{ 26 | Enforce expectations about types of FeatureCollection inputs 27 | } 28 | \examples{ 29 | # all okay 30 | cat(lawn_data$points_count) 31 | lawn_collectionof(lawn_data$points_count, 'Point', 'stuff') 32 | 33 | # error 34 | # lawn_collectionof(lawn_data$points_count, 'Polygon', 'stuff') 35 | } 36 | \seealso{ 37 | Other invariant: 38 | \code{\link{lawn_featureof}()}, 39 | \code{\link{lawn_geosjontype}()} 40 | } 41 | \concept{invariant} 42 | -------------------------------------------------------------------------------- /man/lawn_coordall.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/cordall.R 3 | \name{lawn_coordall} 4 | \alias{lawn_coordall} 5 | \title{Get all coordinates from any GeoJSON object, returning an array 6 | of coordinate arrays.} 7 | \usage{ 8 | lawn_coordall(x, lint = FALSE) 9 | } 10 | \arguments{ 11 | \item{x}{any \link{data-GeoJSON} object} 12 | 13 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 14 | time as the object to get linted increases in size, so probably use by 15 | default for small objects, but not for large if you know they are good 16 | geojson objects. Default: \code{FALSE}} 17 | } 18 | \value{ 19 | matrix of coordinates, where each row in the matrix is a 20 | coordinate pair 21 | } 22 | \description{ 23 | Takes any \link{data-GeoJSON} and returns an array of 24 | coordinate arrays 25 | } 26 | \examples{ 27 | lawn_point(c(-74.5, 40)) \%>\% lawn_coordall() 28 | 29 | rings <- list(list( 30 | c(-2.275543, 53.464547), 31 | c(-2.275543, 53.489271), 32 | c(-2.215118, 53.489271), 33 | c(-2.215118, 53.464547), 34 | c(-2.275543, 53.464547) 35 | )) 36 | lawn_polygon(rings) \%>\% lawn_coordall() 37 | } 38 | -------------------------------------------------------------------------------- /man/lawn_data.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lawn-package.R 3 | \docType{data} 4 | \name{lawn_data} 5 | \alias{lawn_data} 6 | \title{Data for use in examples} 7 | \format{ 8 | A list of character strings of points or polygons in FeatureCollection or Feature 9 | Geojson formats. 10 | } 11 | \description{ 12 | Data for use in examples 13 | } 14 | \details{ 15 | The data objects included in the list, accessible by name 16 | \itemize{ 17 | \item filter_features - FeatureCollection of points 18 | \item points_average - FeatureCollection of points 19 | \item polygons_average - FeatureCollection of polygons 20 | \item points_count - FeatureCollection of points 21 | \item polygons_count - FeatureCollection of polygons 22 | \item points_within - FeatureCollection of points 23 | \item polygons_within - FeatureCollection of polygons 24 | \item poly - Feature of a single 1 degree by 1 degree polygon 25 | \item multipoly - FeatureCollection of two 1 degree by 1 degree polygons 26 | \item polygons_aggregate - FeatureCollection of Polygons from turf.js examples 27 | \item points_aggregate - FeatureCollection of Points from turf.js examples 28 | } 29 | } 30 | \keyword{datasets} 31 | -------------------------------------------------------------------------------- /man/lawn_explode.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/explode.R 3 | \name{lawn_explode} 4 | \alias{lawn_explode} 5 | \title{Explode vertices to points} 6 | \usage{ 7 | lawn_explode(input, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{input}{\link{data-Feature} or \link{data-FeatureCollection}} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | a \link{data-FeatureCollection} of points 19 | } 20 | \description{ 21 | Takes a feature or set of features and returns all positions as points 22 | } 23 | \examples{ 24 | poly <- '{ 25 | "type": "Feature", 26 | "properties": {}, 27 | "geometry": { 28 | "type": "Polygon", 29 | "coordinates": [[ 30 | [177.434692, -17.77517], 31 | [177.402076, -17.779093], 32 | [177.38079, -17.803937], 33 | [177.40242, -17.826164], 34 | [177.438468, -17.824857], 35 | [177.454948, -17.796746], 36 | [177.434692, -17.77517] 37 | ]] 38 | } 39 | }' 40 | lawn_explode(poly) 41 | \dontrun{ 42 | lawn_data$polygons_average \%>\% view 43 | lawn_explode(lawn_data$polygons_average) \%>\% view 44 | lawn_data$polygons_within \%>\% view 45 | lawn_explode(lawn_data$polygons_within) \%>\% view 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /man/lawn_featureeach.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/featureeach.R 3 | \name{lawn_featureeach} 4 | \alias{lawn_featureeach} 5 | \title{Iterate over features in any GeoJSON object} 6 | \usage{ 7 | lawn_featureeach(x, fun = NULL, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{any \link{data-GeoJSON} object} 11 | 12 | \item{fun}{a Javascript function. if not given, returns self} 13 | 14 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 15 | time as the object to get linted increases in size, so probably use by 16 | default for small objects, but not for large if you know they are good 17 | geojson objects. Default: \code{FALSE}} 18 | } 19 | \value{ 20 | matrix of coordinates, where each row in the matrix is a 21 | coordinate pair 22 | } 23 | \description{ 24 | Iterate over features in any GeoJSON object 25 | } 26 | \examples{ 27 | x <- "{ type: 'Feature', geometry: null, properties: { foo: 1, bar: 3 } }" 28 | 29 | # don't apply any function, identity essentially 30 | lawn_featureeach(x) 31 | 32 | lawn_featureeach(lawn_data$points_count) 33 | 34 | # appply a function callback 35 | lawn_featureeach(lawn_data$points_count, "z.geometry") 36 | lawn_featureeach(lawn_data$points_count, "z.geometry.type") 37 | lawn_featureeach(lawn_data$points_count, "z.properties") 38 | lawn_featureeach(lawn_data$points_count, "z.properties.population") 39 | } 40 | -------------------------------------------------------------------------------- /man/lawn_featureof.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/featureof.R 3 | \name{lawn_featureof} 4 | \alias{lawn_featureof} 5 | \title{Enforce expectations about types of Feature inputs} 6 | \usage{ 7 | lawn_featureof(x, type, name, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{a \link{data-Feature} with an expected geometry type. required.} 11 | 12 | \item{type}{(character) expected GeoJSON type. required.} 13 | 14 | \item{name}{(character) name of calling function. required.} 15 | 16 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 17 | time as the object to get linted increases in size, so probably use by 18 | default for small objects, but not for large if you know they are good 19 | geojson objects. Default: \code{FALSE}} 20 | } 21 | \value{ 22 | nothing if no problems - error message if a problem 23 | } 24 | \description{ 25 | Enforce expectations about types of Feature inputs 26 | } 27 | \examples{ 28 | # all okay 29 | x <- "{ type: 'Feature', properties: {}, geometry: { type: 'Point', 30 | coordinates: [10, 50] } }" 31 | lawn_featureof(x, 'Point', 'foobar') 32 | 33 | # error 34 | # lawn_featureof(x, 'MultiPoint', 'foobar') 35 | } 36 | \seealso{ 37 | Other invariant: 38 | \code{\link{lawn_collectionof}()}, 39 | \code{\link{lawn_geosjontype}()} 40 | } 41 | \concept{invariant} 42 | -------------------------------------------------------------------------------- /man/lawn_flatten.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/flatten.R 3 | \name{lawn_flatten} 4 | \alias{lawn_flatten} 5 | \title{Flatten} 6 | \usage{ 7 | lawn_flatten(x, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{any valid GeoJSON with multi-geometry \link{data-Feature}'s} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | a \link{data-FeatureCollection} 19 | } 20 | \description{ 21 | Flattens any GeoJSON to a FeatureCollection 22 | } 23 | \examples{ 24 | x <- '{"type":"MultiPolygon","coordinates":[ 25 | [[[102,2],[103,2],[103,3],[102,3],[102,2]]], 26 | [[[100,0],[101,0],[101,1],[100,1],[100,0]], 27 | [[100.2,0.2],[100.2,0.8],[100.8,0.8],[100.8,0.2],[100.2,0.2]]] 28 | ] 29 | }' 30 | lawn_flatten(x) 31 | lawn_flatten(x, TRUE) 32 | } 33 | \seealso{ 34 | Other misc: 35 | \code{\link{lawn_truncate}()} 36 | } 37 | \concept{misc} 38 | -------------------------------------------------------------------------------- /man/lawn_flip.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/flip.R 3 | \name{lawn_flip} 4 | \alias{lawn_flip} 5 | \title{Flip x,y to y,x, and vice versa} 6 | \usage{ 7 | lawn_flip(input, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{input}{\link{data-Feature} or \link{data-FeatureCollection}} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | A \link{data-Feature} or \link{data-FeatureCollection} 19 | } 20 | \description{ 21 | Flip x,y to y,x, and vice versa 22 | } 23 | \examples{ 24 | # a point 25 | serbia <- '{ 26 | "type": "Feature", 27 | "properties": {"color": "red"}, 28 | "geometry": { 29 | "type": "Point", 30 | "coordinates": [20.566406, 43.421008] 31 | } 32 | }' 33 | lawn_flip(serbia) 34 | 35 | # a featurecollection 36 | pts <- lawn_random("points") 37 | lawn_flip(pts) 38 | \dontrun{ 39 | lawn_data$points_average \%>\% view 40 | lawn_flip(lawn_data$points_average) \%>\% view 41 | lawn_data$polygons_average \%>\% view 42 | lawn_flip(lawn_data$polygons_average) \%>\% view 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /man/lawn_geosjontype.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/geojsontype.R 3 | \name{lawn_geosjontype} 4 | \alias{lawn_geosjontype} 5 | \title{Enforce expectations about types of GeoJSON objects.} 6 | \usage{ 7 | lawn_geosjontype(x, type, name, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{value of any \link{data-GeoJSON} object. required.} 11 | 12 | \item{type}{expected GeoJSON type. required.} 13 | 14 | \item{name}{name of calling function. required.} 15 | 16 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 17 | time as the object to get linted increases in size, so probably use by 18 | default for small objects, but not for large if you know they are good 19 | geojson objects. Default: \code{FALSE}} 20 | } 21 | \value{ 22 | nothing if no problems - error message if a problem 23 | } 24 | \description{ 25 | Enforce expectations about types of GeoJSON objects. 26 | } 27 | \examples{ 28 | # all okay 29 | x <- "{ type: 'Point', coordinates: [10, 50] }" 30 | lawn_geosjontype(x, 'Point', 'fooBar') 31 | 32 | # error 33 | # lawn_geosjontype(x, 'Polygon', 'fooBar') 34 | } 35 | \seealso{ 36 | Other invariant: 37 | \code{\link{lawn_collectionof}()}, 38 | \code{\link{lawn_featureof}()} 39 | } 40 | \concept{invariant} 41 | -------------------------------------------------------------------------------- /man/lawn_getcoord.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/getcoord.R 3 | \name{lawn_getcoord} 4 | \alias{lawn_getcoord} 5 | \title{Unwrap a coordinate from a Feature with a Point geometry, 6 | or a single coordinate.} 7 | \usage{ 8 | lawn_getcoord(x, lint = FALSE) 9 | } 10 | \arguments{ 11 | \item{x}{any \link{data-GeoJSON} object} 12 | 13 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 14 | time as the object to get linted increases in size, so probably use by 15 | default for small objects, but not for large if you know they are good 16 | geojson objects. Default: \code{FALSE}} 17 | } 18 | \value{ 19 | matrix of coordinates, where each row in the matrix is a 20 | coordinate pair 21 | } 22 | \description{ 23 | Unwrap a coordinate from a Feature with a Point geometry, 24 | or a single coordinate. 25 | } 26 | \examples{ 27 | x <- "{ type: 'Point', coordinates: [10, 50] }" 28 | lawn_getcoord(x) 29 | 30 | library(jsonlite) 31 | x <- fromJSON(lawn_data$points_count, FALSE)$features 32 | lawn_getcoord(x[[1]]) 33 | lawn_getcoord(x[[2]]) 34 | lawn_getcoord(x[[1]]$geometry) 35 | lawn_getcoord(x[[1]]$geometry$coordinates) 36 | 37 | # fails 38 | # lawn_getcoord(x[[1]]$geometry$coordinates[[1]]) 39 | } 40 | -------------------------------------------------------------------------------- /man/lawn_hex_grid.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/hex_grid.R 3 | \name{lawn_hex_grid} 4 | \alias{lawn_hex_grid} 5 | \title{Create a HexGrid} 6 | \usage{ 7 | lawn_hex_grid(extent, cellWidth, units) 8 | } 9 | \arguments{ 10 | \item{extent}{(numeric) Extent in \verb{[minX, minY, maxX, maxY]} order.} 11 | 12 | \item{cellWidth}{(integer) Width of each cell.} 13 | 14 | \item{units}{(character) Units to use for cellWidth, one of 'miles' or 15 | 'kilometers'.} 16 | } 17 | \value{ 18 | A \link{data-FeatureCollection} grid of points. 19 | } 20 | \description{ 21 | Takes a bounding box and a cell size in degrees and returns a 22 | \link{data-FeatureCollection} of flat-topped hexagons 23 | (\link{data-Polygon} features) aligned in an "odd-q" vertical 24 | grid as described in Hexagonal Grids 25 | http://www.redblobgames.com/grids/hexagons/ 26 | } 27 | \examples{ 28 | lawn_hex_grid(c(-96,31,-84,40), 50, 'miles') 29 | lawn_hex_grid(c(-96,31,-84,40), 30, 'miles') 30 | } 31 | \seealso{ 32 | Other interpolation: 33 | \code{\link{lawn_isolines}()}, 34 | \code{\link{lawn_planepoint}()}, 35 | \code{\link{lawn_point_grid}()}, 36 | \code{\link{lawn_square_grid}()}, 37 | \code{\link{lawn_tin}()}, 38 | \code{\link{lawn_triangle_grid}()} 39 | } 40 | \concept{interpolation} 41 | -------------------------------------------------------------------------------- /man/lawn_idw-defunct.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/idw.R 3 | \name{lawn_idw} 4 | \alias{lawn_idw} 5 | \title{IDW} 6 | \usage{ 7 | lawn_idw(...) 8 | } 9 | \arguments{ 10 | \item{...}{ignored} 11 | } 12 | \description{ 13 | DEFUNCT 14 | } 15 | -------------------------------------------------------------------------------- /man/lawn_jenks.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/defunct-deprecated.R 3 | \name{lawn_jenks} 4 | \alias{lawn_jenks} 5 | \title{Get Jenks breaks} 6 | \usage{ 7 | lawn_jenks(...) 8 | } 9 | \description{ 10 | This function is defunct 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/lawn_kinks.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/kinks.R 3 | \name{lawn_kinks} 4 | \alias{lawn_kinks} 5 | \title{Get points at all self-intersections of a polygon} 6 | \usage{ 7 | lawn_kinks(input, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{input}{Feature of features.} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \description{ 18 | Get points at all self-intersections of a polygon 19 | } 20 | \examples{ 21 | poly <- '{ 22 | "type": "Feature", 23 | "properties": {}, 24 | "geometry": { 25 | "type": "Polygon", 26 | "coordinates": [[ 27 | [-12.034835, 8.901183], 28 | [-12.060413, 8.899826], 29 | [-12.03638, 8.873199], 30 | [-12.059383, 8.871418], 31 | [-12.034835, 8.901183] 32 | ]] 33 | } 34 | }' 35 | lawn_kinks(poly) 36 | # lint input object 37 | # lawn_kinks(poly, TRUE) 38 | \dontrun{ 39 | poly \%>\% view 40 | lawn_kinks(poly) \%>\% view 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /man/lawn_line_offset.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/line_offset.R 3 | \name{lawn_line_offset} 4 | \alias{lawn_line_offset} 5 | \title{Offset a linestring} 6 | \usage{ 7 | lawn_line_offset(line, distance, units, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{line}{Line to measure, a \link{data-LineString}.} 11 | 12 | \item{distance}{(integer/numeric) Distance along the line.} 13 | 14 | \item{units}{Can be degrees, radians, miles, kilometers, inches, 15 | yards, meters} 16 | 17 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 18 | time as the object to get linted increases in size, so probably use by 19 | default for small objects, but not for large if you know they are good 20 | geojson objects. Default: \code{FALSE}} 21 | } 22 | \value{ 23 | a \link{data-LineString} 24 | } 25 | \description{ 26 | Takes a \link{data-LineString} and returns a \link{data-LineString} at offset by 27 | the specified distance. 28 | } 29 | \examples{ 30 | line <- '{ 31 | "type": "Feature", 32 | "properties": { 33 | "stroke": "#F00" 34 | }, 35 | "geometry": { 36 | "type": "LineString", 37 | "coordinates": [[-83, 30], [-84, 36], [-78, 41]] 38 | } 39 | }' 40 | 41 | lawn_line_offset(line, 2, 'miles') 42 | lawn_line_offset(line, 200, 'miles') 43 | lawn_line_offset(line, 0.5, 'radians') 44 | lawn_line_offset(line, 4, 'yards') 45 | 46 | 47 | line <- '{ 48 | "type": "LineString", 49 | "coordinates": [[-83, 30], [-84, 36], [-78, 41]] 50 | }' 51 | lawn_line_offset(line, 4, 'yards') 52 | } 53 | -------------------------------------------------------------------------------- /man/lawn_point.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/point.R 3 | \name{lawn_point} 4 | \alias{lawn_point} 5 | \title{Create a point} 6 | \usage{ 7 | lawn_point(coordinates, properties = NULL) 8 | } 9 | \arguments{ 10 | \item{coordinates}{A pair of points in a vector, list or json, of the form 11 | \code{e.g., c(longitude,latitude)}.} 12 | 13 | \item{properties}{A list of properties. Default: NULL} 14 | } 15 | \value{ 16 | A \link{data-Feature}<(\link{data-Point})> 17 | } 18 | \description{ 19 | Create a point 20 | } 21 | \examples{ 22 | lawn_point(c(-74.5, 40)) 23 | lawn_point(list(-74.5, 40)) 24 | lawn_point('[-74.5, 40]') 25 | lawn_point(c(-74.5, 40), properties = list(name = 'poly1', population = 400)) 26 | 27 | # Make a FeatureCollection 28 | lawn_featurecollection(lawn_point(c(-74.5, 40))) 29 | } 30 | \seealso{ 31 | Other data functions: 32 | \code{\link{lawn_featurecollection}()}, 33 | \code{\link{lawn_feature}()}, 34 | \code{\link{lawn_filter}()}, 35 | \code{\link{lawn_geometrycollection}()}, 36 | \code{\link{lawn_linestring}()}, 37 | \code{\link{lawn_multilinestring}()}, 38 | \code{\link{lawn_multipoint}()}, 39 | \code{\link{lawn_multipolygon}()}, 40 | \code{\link{lawn_polygon}()}, 41 | \code{\link{lawn_random}()}, 42 | \code{\link{lawn_remove}()}, 43 | \code{\link{lawn_sample}()} 44 | } 45 | \concept{data functions} 46 | -------------------------------------------------------------------------------- /man/lawn_point_grid.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/point_grid.R 3 | \name{lawn_point_grid} 4 | \alias{lawn_point_grid} 5 | \title{Create a PointGrid} 6 | \usage{ 7 | lawn_point_grid(extent, cellSide, units = "kilometers", mask = NULL) 8 | } 9 | \arguments{ 10 | \item{extent}{(numeric) Extent in \verb{[minX, minY, maxX, maxY]} order.} 11 | 12 | \item{cellSide}{(integer) the distance between points} 13 | 14 | \item{units}{(character) Units to use for cellWidth, one of 'miles' or 15 | 'kilometers' (default).} 16 | 17 | \item{mask}{(logical) if passed a Polygon or MultiPolygon, the grid 18 | Points will be created only inside it} 19 | } 20 | \value{ 21 | \link{data-FeatureCollection} grid of points. 22 | } 23 | \description{ 24 | Takes a bounding box and a cell depth and returns a set of 25 | \link{data-Point}'s in a grid 26 | } 27 | \note{ 28 | parameters \code{centered} and \code{bboxIsMask} removed 29 | } 30 | \examples{ 31 | lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles') 32 | lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles') 33 | lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles') 34 | } 35 | \seealso{ 36 | Other interpolation: 37 | \code{\link{lawn_hex_grid}()}, 38 | \code{\link{lawn_isolines}()}, 39 | \code{\link{lawn_planepoint}()}, 40 | \code{\link{lawn_square_grid}()}, 41 | \code{\link{lawn_tin}()}, 42 | \code{\link{lawn_triangle_grid}()} 43 | } 44 | \concept{interpolation} 45 | -------------------------------------------------------------------------------- /man/lawn_polygon.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/polygon.R 3 | \name{lawn_polygon} 4 | \alias{lawn_polygon} 5 | \title{Create a polygon} 6 | \usage{ 7 | lawn_polygon(coordinates, properties = NULL) 8 | } 9 | \arguments{ 10 | \item{coordinates}{A list of LinearRings, or in json.} 11 | 12 | \item{properties}{A list of properties.} 13 | } 14 | \value{ 15 | A \link{data-Polygon} feature. 16 | } 17 | \description{ 18 | Create a polygon 19 | } 20 | \examples{ 21 | rings <- list(list( 22 | c(-2.275543, 53.464547), 23 | c(-2.275543, 53.489271), 24 | c(-2.215118, 53.489271), 25 | c(-2.215118, 53.464547), 26 | c(-2.275543, 53.464547) 27 | )) 28 | lawn_polygon(rings) 29 | lawn_polygon(rings, properties = list(name = 'poly1', population = 400)) 30 | 31 | # Make a FeatureCollection 32 | lawn_featurecollection(lawn_polygon(rings)) 33 | 34 | \dontrun{ 35 | lawn_featurecollection(lawn_polygon(rings)) \%>\% view 36 | } 37 | } 38 | \seealso{ 39 | Other data functions: 40 | \code{\link{lawn_featurecollection}()}, 41 | \code{\link{lawn_feature}()}, 42 | \code{\link{lawn_filter}()}, 43 | \code{\link{lawn_geometrycollection}()}, 44 | \code{\link{lawn_linestring}()}, 45 | \code{\link{lawn_multilinestring}()}, 46 | \code{\link{lawn_multipoint}()}, 47 | \code{\link{lawn_multipolygon}()}, 48 | \code{\link{lawn_point}()}, 49 | \code{\link{lawn_random}()}, 50 | \code{\link{lawn_remove}()}, 51 | \code{\link{lawn_sample}()} 52 | } 53 | \concept{data functions} 54 | -------------------------------------------------------------------------------- /man/lawn_quantile.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/defunct-deprecated.R 3 | \name{lawn_quantile} 4 | \alias{lawn_quantile} 5 | \title{Calculate quantiles} 6 | \usage{ 7 | lawn_quantile(...) 8 | } 9 | \description{ 10 | This function is defunct 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/lawn_reclass.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/defunct-deprecated.R 3 | \name{lawn_reclass} 4 | \alias{lawn_reclass} 5 | \title{Reclassify properties data} 6 | \usage{ 7 | lawn_reclass(...) 8 | } 9 | \description{ 10 | This function is defunct 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/lawn_rewind.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/rewind.R 3 | \name{lawn_rewind} 4 | \alias{lawn_rewind} 5 | \title{Rewind} 6 | \usage{ 7 | lawn_rewind(x, reverse = FALSE, mutate = FALSE, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{A \link{data-FeatureCollection} or \link{data-Feature} with 11 | Polygon, MultiPolygon, LineString, or MultiLineString} 12 | 13 | \item{reverse}{(logical) enable reverse winding. Default: \code{FALSE}} 14 | 15 | \item{mutate}{(logical) allows GeoJSON input to be mutated (significant 16 | performance increase if true) Default: \code{FALSE}} 17 | 18 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 19 | time as the object to get linted increases in size, so probably use by 20 | default for small objects, but not for large if you know they are good 21 | geojson objects. Default: \code{FALSE}} 22 | } 23 | \value{ 24 | A \link{data-FeatureCollection} 25 | } 26 | \description{ 27 | Rewind (Multi)LineString or (Multi)Polygon outer ring counterclockwise 28 | and inner rings clockwise (Uses Shoelace Formula) 29 | } 30 | \examples{ 31 | x <- '{ 32 | "type": "Feature", 33 | "properties": {}, 34 | "geometry": { 35 | "type": "Polygon", 36 | "coordinates": [ 37 | [[121, -29], [138, -29], [138, -18], [121, -18], [121, -29]] 38 | ] 39 | } 40 | }' 41 | lawn_rewind(x, TRUE) 42 | lawn_rewind(x, mutate = TRUE) 43 | lawn_rewind(x, lint = TRUE) 44 | } 45 | -------------------------------------------------------------------------------- /man/lawn_sample.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/sample.R 3 | \name{lawn_sample} 4 | \alias{lawn_sample} 5 | \title{Return features from FeatureCollection at random} 6 | \usage{ 7 | lawn_sample(features = NULL, n = 100, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{features}{A \link{data-FeatureCollection}} 11 | 12 | \item{n}{(integer) Number of features to generate.} 13 | 14 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 15 | time as the object to get linted increases in size, so probably use by 16 | default for small objects, but not for large if you know they are good 17 | geojson objects. Default: \code{FALSE}} 18 | } 19 | \value{ 20 | A \link{data-FeatureCollection} 21 | } 22 | \description{ 23 | Takes a \link{data-FeatureCollection} and returns a 24 | \link{data-FeatureCollection} with given number of features at random. 25 | } 26 | \examples{ 27 | lawn_sample(lawn_data$points_average, 1) 28 | lawn_sample(lawn_data$points_average, 2) 29 | lawn_sample(lawn_data$points_average, 3) 30 | } 31 | \seealso{ 32 | Other data functions: 33 | \code{\link{lawn_featurecollection}()}, 34 | \code{\link{lawn_feature}()}, 35 | \code{\link{lawn_filter}()}, 36 | \code{\link{lawn_geometrycollection}()}, 37 | \code{\link{lawn_linestring}()}, 38 | \code{\link{lawn_multilinestring}()}, 39 | \code{\link{lawn_multipoint}()}, 40 | \code{\link{lawn_multipolygon}()}, 41 | \code{\link{lawn_point}()}, 42 | \code{\link{lawn_polygon}()}, 43 | \code{\link{lawn_random}()}, 44 | \code{\link{lawn_remove}()} 45 | } 46 | \concept{data functions} 47 | -------------------------------------------------------------------------------- /man/lawn_size.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/defunct-deprecated.R 3 | \name{lawn_size} 4 | \alias{lawn_size} 5 | \title{Expand a bounding box} 6 | \usage{ 7 | lawn_size(...) 8 | } 9 | \description{ 10 | This function is defunct 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/lawn_square.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/square.R 3 | \name{lawn_square} 4 | \alias{lawn_square} 5 | \title{Calculate a square bounding box} 6 | \usage{ 7 | lawn_square(bbox) 8 | } 9 | \arguments{ 10 | \item{bbox}{A bounding box.} 11 | } 12 | \value{ 13 | A square surrounding bbox, numeric vector of length four. 14 | } 15 | \description{ 16 | Takes a bounding box and calculates the minimum square bounding 17 | box that would contain the input. 18 | } 19 | \examples{ 20 | bbox <- c(-20, -20, -15, 0) 21 | lawn_square(bbox) 22 | \dontrun{ 23 | sq <- lawn_square(bbox) 24 | lawn_featurecollection(list(lawn_bbox_polygon(bbox), 25 | lawn_bbox_polygon(sq))) \%>\% view 26 | } 27 | } 28 | \seealso{ 29 | Other measurements: 30 | \code{\link{lawn_along}()}, 31 | \code{\link{lawn_area}()}, 32 | \code{\link{lawn_bbox_polygon}()}, 33 | \code{\link{lawn_bbox}()}, 34 | \code{\link{lawn_bearing}()}, 35 | \code{\link{lawn_center_of_mass}()}, 36 | \code{\link{lawn_center}()}, 37 | \code{\link{lawn_centroid}()}, 38 | \code{\link{lawn_destination}()}, 39 | \code{\link{lawn_distance}()}, 40 | \code{\link{lawn_envelope}()}, 41 | \code{\link{lawn_extent}()}, 42 | \code{\link{lawn_line_distance}()}, 43 | \code{\link{lawn_midpoint}()}, 44 | \code{\link{lawn_point_on_feature}()}, 45 | \code{\link{lawn_pt2line_distance}()} 46 | } 47 | \concept{measurements} 48 | -------------------------------------------------------------------------------- /man/lawn_square_grid.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/square_grid.R 3 | \name{lawn_square_grid} 4 | \alias{lawn_square_grid} 5 | \title{Create a SquareGrid} 6 | \usage{ 7 | lawn_square_grid(extent, cellSide, units = "kilometers", mask = NULL) 8 | } 9 | \arguments{ 10 | \item{extent}{(numeric) Extent in \verb{[minX, minY, maxX, maxY]} order.} 11 | 12 | \item{cellSide}{(integer) dimension of each cell.} 13 | 14 | \item{units}{(character) Units to use for cellSide, one of 'miles' or 15 | 'kilometers'.} 16 | 17 | \item{mask}{(logical) if passed a Polygon or MultiPolygon, the grid 18 | Points will be created only inside it} 19 | } 20 | \value{ 21 | \link{data-FeatureCollection} grid of polygons. 22 | } 23 | \description{ 24 | Takes a bounding box and a cell depth and returns a set of square 25 | \link{data-Polygon}'s in a grid. 26 | } 27 | \examples{ 28 | \dontrun{ 29 | lawn_square_grid(c(-95, 30 ,-85, 40), 30, 'miles') 30 | lawn_square_grid(c(-95, 30 ,-85, 40), 10, 'miles') 31 | lawn_square_grid(c(-95, 30 ,-85, 40), 3, 'miles') 32 | } 33 | } 34 | \seealso{ 35 | Other interpolation: 36 | \code{\link{lawn_hex_grid}()}, 37 | \code{\link{lawn_isolines}()}, 38 | \code{\link{lawn_planepoint}()}, 39 | \code{\link{lawn_point_grid}()}, 40 | \code{\link{lawn_tin}()}, 41 | \code{\link{lawn_triangle_grid}()} 42 | } 43 | \concept{interpolation} 44 | -------------------------------------------------------------------------------- /man/lawn_tesselate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tesselate.R 3 | \name{lawn_tesselate} 4 | \alias{lawn_tesselate} 5 | \title{Tesselate} 6 | \usage{ 7 | lawn_tesselate(polygon, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{polygon}{Input \link{data-Feature}<(\link{data-Polygon})>} 11 | 12 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 13 | time as the object to get linted increases in size, so probably use by 14 | default for small objects, but not for large if you know they are good 15 | geojson objects. Default: \code{FALSE}} 16 | } 17 | \value{ 18 | A \link{data-FeatureCollection} 19 | } 20 | \description{ 21 | Tesselates a \link{data-Polygon} into a 22 | \link{data-FeatureCollection} of 23 | triangles using earcut (github.com/mapbox/earcut) 24 | } 25 | \examples{ 26 | poly <- '{ 27 | "type": "Feature", 28 | "properties": { 29 | "fill": "#0f0" 30 | }, 31 | "geometry": { 32 | "type": "Polygon", 33 | "coordinates": [[ 34 | [-46.738586, -23.596711], 35 | [-46.738586, -23.458207], 36 | [-46.560058, -23.458207], 37 | [-46.560058, -23.596711], 38 | [-46.738586, -23.596711] 39 | ]] 40 | } 41 | }' 42 | lawn_tesselate(poly) 43 | 44 | xx <- jsonlite::fromJSON(lawn_data$polygons_within, FALSE) 45 | lawn_tesselate(xx$features[[1]]) 46 | \dontrun{ 47 | lawn_tesselate(xx$features[[1]]) \%>\% view 48 | lawn_tesselate(poly) \%>\% view 49 | } 50 | } 51 | \seealso{ 52 | Other assertions: 53 | \code{\link{lawn_circle}()}, 54 | \code{\link{lawn_dissolve}()} 55 | } 56 | \concept{assertions} 57 | -------------------------------------------------------------------------------- /man/lawn_triangle_grid.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/triangle_grid.R 3 | \name{lawn_triangle_grid} 4 | \alias{lawn_triangle_grid} 5 | \title{Create a TriangleGrid} 6 | \usage{ 7 | lawn_triangle_grid(extent, cellSide, units = "kilometers", mask = NULL) 8 | } 9 | \arguments{ 10 | \item{extent}{(numeric) Extent in \verb{[minX, minY, maxX, maxY]} order.} 11 | 12 | \item{cellSide}{(integer) dimension of each cell.} 13 | 14 | \item{units}{(character) Units to use for cellSide, one of miles, 15 | kilometers, degrees, radians} 16 | 17 | \item{mask}{(logical) if passed a Polygon or MultiPolygon, the grid 18 | Points will be created only inside it} 19 | } 20 | \value{ 21 | \link{data-FeatureCollection} grid of \link{data-Polygon}'s 22 | } 23 | \description{ 24 | Takes a bounding box and a cell depth and returns a set of triangular 25 | \link{data-Polygon}'s in a grid. 26 | } 27 | \examples{ 28 | lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles') 29 | lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles') 30 | lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles') 31 | } 32 | \seealso{ 33 | Other interpolation: 34 | \code{\link{lawn_hex_grid}()}, 35 | \code{\link{lawn_isolines}()}, 36 | \code{\link{lawn_planepoint}()}, 37 | \code{\link{lawn_point_grid}()}, 38 | \code{\link{lawn_square_grid}()}, 39 | \code{\link{lawn_tin}()} 40 | } 41 | \concept{interpolation} 42 | -------------------------------------------------------------------------------- /man/lawn_truncate.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/truncate.R 3 | \name{lawn_truncate} 4 | \alias{lawn_truncate} 5 | \title{Truncate} 6 | \usage{ 7 | lawn_truncate(x, precision = 6, coordinates = 2, mutate = FALSE, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{any \link{data-Feature} or \link{data-FeatureCollection}} 11 | 12 | \item{precision}{(integer) coordinate decimal precision. default: 6} 13 | 14 | \item{coordinates}{(integer) maximum number of coordinates (primarily used 15 | to remove z coordinates). default: 2} 16 | 17 | \item{mutate}{(logical) allows GeoJSON input to be mutated (significant 18 | performance increase if true) Default: \code{FALSE}} 19 | 20 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 21 | time as the object to get linted increases in size, so probably use by 22 | default for small objects, but not for large if you know they are good 23 | geojson objects. Default: \code{FALSE}} 24 | } 25 | \value{ 26 | a \link{data-Feature} or \link{data-FeatureCollection} with truncated geometry 27 | } 28 | \description{ 29 | Takes a GeoJSON Feature or FeatureCollection and truncates the precision 30 | of the geometry. 31 | } 32 | \examples{ 33 | cat(lawn_data$filter_features) 34 | lawn_coordall(lawn_data$filter_features) 35 | lawn_truncate(lawn_data$filter_features, 4) \%>\% lawn_coordall 36 | lawn_truncate(lawn_data$filter_features, 2) \%>\% lawn_coordall 37 | lawn_truncate(lawn_data$filter_features, 4, 1) \%>\% lawn_coordall 38 | } 39 | \seealso{ 40 | Other misc: 41 | \code{\link{lawn_flatten}()} 42 | } 43 | \concept{misc} 44 | -------------------------------------------------------------------------------- /man/lawn_unkinkpolygon.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/unkinkpolygon.R 3 | \name{lawn_unkinkpolygon} 4 | \alias{lawn_unkinkpolygon} 5 | \title{Unkink polygon} 6 | \usage{ 7 | lawn_unkinkpolygon(x, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{x}{A \link{data-FeatureCollection}<(\link{data-Polygon})> or 11 | \link{data-FeatureCollection}<(\link{data-MultiPolygon})>} 12 | 13 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 14 | time as the object to get linted increases in size, so probably use by 15 | default for small objects, but not for large if you know they are good 16 | geojson objects. Default: \code{FALSE}} 17 | } 18 | \value{ 19 | a \link{data-FeatureCollection}<(\link{data-Polygon})> 20 | } 21 | \description{ 22 | Takes a kinked polygon and returns a feature collection of polygons 23 | that have no kinks. 24 | } 25 | \examples{ 26 | x <- '{ 27 | "type": "Feature", 28 | "properties": {}, 29 | "geometry": { 30 | "type": "Polygon", 31 | "coordinates": [[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]] 32 | } 33 | }' 34 | lawn_unkinkpolygon(x) 35 | view(x) 36 | view(lawn_unkinkpolygon(x)) 37 | } 38 | \concept{grids} 39 | -------------------------------------------------------------------------------- /man/lawn_within.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/within.R 3 | \name{lawn_within} 4 | \alias{lawn_within} 5 | \title{Return points that fall within polygons} 6 | \usage{ 7 | lawn_within(points, polygons, lint = FALSE) 8 | } 9 | \arguments{ 10 | \item{points}{\link{data-FeatureCollection} of points.} 11 | 12 | \item{polygons}{\link{data-FeatureCollection} of polygons.} 13 | 14 | \item{lint}{(logical) Lint or not. Uses geojsonhint. Takes up increasing 15 | time as the object to get linted increases in size, so probably use by 16 | default for small objects, but not for large if you know they are good 17 | geojson objects. Default: \code{FALSE}} 18 | } 19 | \value{ 20 | Points that land within at least one polygon, as a 21 | \link{data-FeatureCollection}. 22 | } 23 | \description{ 24 | Takes a set of \link{data-Point}'s and a set of 25 | \link{data-Polygon}'s and returns points that fall within 26 | the polygons. 27 | } 28 | \examples{ 29 | \dontrun{ 30 | cat(lawn_data$points_within) 31 | cat(lawn_data$polygons_within) 32 | lawn_within(lawn_data$points_within, lawn_data$polygons_within) 33 | 34 | pt <- '{ 35 | "type": "Feature", 36 | "properties": {}, 37 | "geometry": { 38 | "type": "Point", 39 | "coordinates": [-90.548630, 14.616599] 40 | } 41 | }' 42 | poly <- lawn_featurecollection(lawn_buffer(pt, 5)) 43 | pts <- lawn_featurecollection(lawn_point(c(-90.55, 14.62))) 44 | 45 | lawn_within(pts, poly) 46 | } 47 | } 48 | \seealso{ 49 | Other joins: 50 | \code{\link{lawn_inside}()}, 51 | \code{\link{lawn_tag}()} 52 | } 53 | \concept{joins} 54 | -------------------------------------------------------------------------------- /man/pipe.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/pipe.R 3 | \name{\%>\%} 4 | \alias{\%>\%} 5 | \title{Pipe operator} 6 | \usage{ 7 | lhs \%>\% rhs 8 | } 9 | \description{ 10 | Pipe operator 11 | } 12 | \keyword{internal} 13 | -------------------------------------------------------------------------------- /man/type_sum.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/tbldf.R 3 | \name{type_sum} 4 | \alias{type_sum} 5 | \alias{type_sum.default} 6 | \alias{type_sum.character} 7 | \alias{type_sum.Date} 8 | \alias{type_sum.factor} 9 | \alias{type_sum.integer} 10 | \alias{type_sum.logical} 11 | \alias{type_sum.array} 12 | \alias{type_sum.matrix} 13 | \alias{type_sum.numeric} 14 | \alias{type_sum.POSIXt} 15 | \title{Type summary} 16 | \usage{ 17 | type_sum(x) 18 | 19 | \method{type_sum}{default}(x) 20 | 21 | \method{type_sum}{character}(x) 22 | 23 | \method{type_sum}{Date}(x) 24 | 25 | \method{type_sum}{factor}(x) 26 | 27 | \method{type_sum}{integer}(x) 28 | 29 | \method{type_sum}{logical}(x) 30 | 31 | \method{type_sum}{array}(x) 32 | 33 | \method{type_sum}{matrix}(x) 34 | 35 | \method{type_sum}{numeric}(x) 36 | 37 | \method{type_sum}{POSIXt}(x) 38 | } 39 | \description{ 40 | Type summary 41 | } 42 | \keyword{internal} 43 | -------------------------------------------------------------------------------- /paper.bib: -------------------------------------------------------------------------------- 1 | @online{turfjs, 2 | author = {Herlocker, Morgan}, 3 | title = {Turf: Advanced geospatial analysis for browsers and node}, 4 | year = 2016, 5 | url = {http://turfjs.org/}, 6 | urldate = {2016-12-16} 7 | } 8 | 9 | @Manual{r, 10 | title = {R: A Language and Environment for Statistical Computing}, 11 | author = {{R Core Team}}, 12 | organization = {R Foundation for Statistical Computing}, 13 | address = {Vienna, Austria}, 14 | year = {2016}, 15 | url = {https://www.R-project.org/}, 16 | } 17 | 18 | @book{bivand2013asdar, 19 | author={Bivand, Roger S and Pebesma, Edzer and G{\'o}mez-Rubio, Virgilio}, 20 | title={Applied Spatial Data Analysis with R}, 21 | year={2013}, 22 | publisher={Springer}, 23 | edition = 2, 24 | } 25 | 26 | @Manual{rgeos, 27 | title = {rgeos: Interface to Geometry Engine - Open Source (GEOS)}, 28 | author = {Bivand, Roger S. and Rundel, C.}, 29 | year = {2016}, 30 | note = {R package version 0.3-19}, 31 | url = {https://CRAN.R-project.org/package=rgeos}, 32 | } 33 | 34 | @Manual{gdal, 35 | title = {GDAL - Geospatial Data Abstraction Library, Version 1.11.3}, 36 | author = {{GDAL Development Team}}, 37 | organization = {Open Source Geospatial Foundation}, 38 | year = {2015}, 39 | url = {http://www.gdal.org}, 40 | } 41 | 42 | @Manual{geos, 43 | title = {GEOS - Geometry Engine, Open Source}, 44 | author = {GEOS Development Team}, 45 | organization = {Open Source Geospatial Foundation}, 46 | year = {2016}, 47 | url = {https://trac.osgeo.org/geos/}, 48 | } -------------------------------------------------------------------------------- /paper.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: 'lawn: An R client for the Turf Javascript Library for Geospatial Analysis' 3 | tags: 4 | - geospatial 5 | - GeoJSON 6 | - javascript 7 | authors: 8 | - name: Scott Chamberlain 9 | orcid: 0000-0003-1444-9135 10 | affiliation: 11 | - 1 12 | - name: Jeffrey W. Hollister 13 | orcid: 0000-0002-9254-9740 14 | affiliation: 15 | - 2 16 | affiliations: 17 | - name: rOpenSci 18 | index: 1 19 | - name: US Environmental Protection Agency, Atlantic Ecology Division 20 | index: 2 21 | date: 16 December 2016 22 | bibliography: paper.bib 23 | --- 24 | 25 | # Summary 26 | 27 | `lawn` is an R package to provide access to the geospatial analysis capabilities in the `Turf` javascript library [@r; @turfjs]. `Turf` expects data in GeoJSON format. Given that many datasets are now available natively in GeoJSON providing an easier method for conducting geospatial analyses on these datasets in R is desired. Additionally, many existing packages (e.g. `sp` and `rgeos`) do provide similar analysis capabilities; however, they require data be converted to `sp` objects and also have external dependencies (e.g. `gdal` and `geos`) that can be challenging to maintain on some systems [@bivand2013asdar; @rgeos; @gdal; @geos]. To address these challenges and facilitate geospatial analysis of GeoJSON formatted data, we developed the `lawn` package. 28 | 29 | # References -------------------------------------------------------------------------------- /tests/test-all.R: -------------------------------------------------------------------------------- 1 | library("testthat") 2 | test_check("lawn") 3 | -------------------------------------------------------------------------------- /tests/testthat/helper-lawn.R: -------------------------------------------------------------------------------- 1 | skip_on_fedora <- function() { 2 | sysname <- tolower(Sys.info()[["sysname"]]) 3 | if (sysname == "linux") skip("skipping") 4 | invisible(TRUE) 5 | } 6 | -------------------------------------------------------------------------------- /tests/testthat/test-along.R: -------------------------------------------------------------------------------- 1 | context("lawn_along") 2 | 3 | pts <- '[ 4 | [-21.964416, 64.148203], 5 | [-21.956176, 64.141316], 6 | [-21.93901, 64.135924], 7 | [-21.927337, 64.136673] 8 | ]' 9 | a <- lawn_along(lawn_linestring(pts), 1, 'miles') 10 | 11 | test_that("lawn_along works", { 12 | expect_is(a, "point") 13 | expect_is(a$type, "character") 14 | expect_is(a$geometry, "list") 15 | expect_is(a$geometry$type, "character") 16 | expect_is(a$geometry$coordinates, "numeric") 17 | expect_equal(length(a$properties), 0) 18 | }) 19 | 20 | test_that("lawn_along fails correctly", { 21 | expect_error(lawn_along(), class="error") 22 | expect_error(lawn_along(lawn_linestring(pts), units = "miles"), "argument \"distance\" is missing, with no default") 23 | expect_error(lawn_along(lawn_linestring(pts), 1), "argument \"units\" is missing, with no default") 24 | expect_error(lawn_along(lawn_linestring(pts), 1, "stuff")) 25 | expect_error(lawn_along(lawn_linestring(pts), "asdfadf", "miles"), "distance must be of class numeric, integer") 26 | expect_error(lawn_along("adsf", "asdfadf", "miles"), "distance must be of class numeric, integer") 27 | expect_error(lawn_along(5, 1, "miles")) 28 | expect_error(lawn_along(5, 1, "miles", lint = TRUE), "The root of a GeoJSON object must be an object") 29 | }) 30 | -------------------------------------------------------------------------------- /tests/testthat/test-area.R: -------------------------------------------------------------------------------- 1 | context("lawn_area") 2 | 3 | poly <- lawn_data$poly 4 | multi <- lawn_data$multipoly 5 | pt <- lawn_point(c(-71.4226, 41.4945)) 6 | 7 | test_that("lawn_area works", { 8 | skip_on_cran() 9 | skip_on_fedora() 10 | 11 | expect_is(lawn_area(poly), "numeric") 12 | expect_is(lawn_area(multi), "numeric") 13 | expect_is(lawn_area(pt), "integer") 14 | expect_equal(round(lawn_area(poly)/1000000), 12391 ) 15 | expect_equal(round(lawn_area(multi)/1000000), 24779 ) 16 | expect_equal(lawn_area(pt), 0 ) 17 | }) 18 | 19 | test_that("lawn_area fails correctly", { 20 | skip_on_cran() 21 | skip_on_fedora() 22 | 23 | expect_error(lawn_area(), "argument \"input\" is missing, with no default") 24 | }) 25 | -------------------------------------------------------------------------------- /tests/testthat/test-as.turf.R: -------------------------------------------------------------------------------- 1 | context("as.turf") 2 | 3 | rings <- list(list( 4 | c(-2.275543, 53.464547), 5 | c(-2.275543, 53.489271), 6 | c(-2.215118, 53.489271), 7 | c(-2.215118, 53.464547), 8 | c(-2.275543, 53.464547) 9 | )) 10 | 11 | linestring1 <- '[ 12 | [-21.964416, 64.148203], 13 | [-21.956176, 64.141316], 14 | [-21.93901, 64.135924], 15 | [-21.927337, 64.136673] 16 | ]' 17 | 18 | a <- as.turf(lawn_point(c(-74.5, 40))) 19 | b <- as.turf(lawn_polygon(rings)) 20 | d <- as.turf(lawn_linestring(linestring1)) 21 | e <- as.turf(lawn_centroid(lawn_polygon(rings))) # <-- THIS ONE 22 | 23 | # suppressPackageStartupMessages(library("geojsonio", quietly = TRUE)) 24 | # f <- as.turf(geojson_list(c(-99.74,32.45))) 25 | 26 | test_that("as.turf works", { 27 | expect_is(a, "character") 28 | expect_is(b, "character") 29 | expect_is(d, "character") 30 | expect_is(e, "character") 31 | # expect_is(f, "list") 32 | # expect_is(f[[1]], "character") 33 | 34 | expect_true(grepl("turf.point", a)) 35 | expect_true(grepl("turf.polygon", b)) 36 | expect_true(grepl("turf.lineString", d)) 37 | expect_true(grepl("turf.point", e)) 38 | # expect_true(grepl("turf.point", f)) 39 | }) 40 | 41 | test_that("as.turf fails correctly", { 42 | expect_error(as.turf(), "no applicable method") 43 | expect_error(as.turf(""), "no applicable method") 44 | expect_error(as.turf(mtcars), "no applicable method") 45 | }) 46 | -------------------------------------------------------------------------------- /tests/testthat/test-average.R: -------------------------------------------------------------------------------- 1 | context("lawn_average") 2 | 3 | a <- lawn_average(polygons = lawn_data$polygons_average, 4 | points = lawn_data$points_average, 'population') 5 | 6 | b <- lawn_average(polygons = lawn_data$polygons_average, 7 | points = sub("600", "6000", lawn_data$points_average), 'population') 8 | 9 | c <- lawn_average(lawn_data$polygons_average, lawn_data$points_average, "pumpkin") 10 | 11 | test_that("lawn_average works", { 12 | expect_is(a, "featurecollection") 13 | expect_is(a$type, "character") 14 | expect_equal(a$type, "FeatureCollection") 15 | expect_is(a$features, "data.frame") 16 | expect_is(a$features$geometry$type, "character") 17 | expect_is(a$features$geometry$coordinates[[1]], "array") 18 | expect_equal(a$features$properties$average[1], 300) 19 | expect_equal(a$features$properties$average[2], 250) 20 | 21 | expect_equal(b$features$properties$average[1], 2100) 22 | expect_equal(b$features$properties$average[2], 250) 23 | 24 | expect_true(is.na(c$features$properties$average[1])) 25 | }) 26 | 27 | test_that("lawn_average fails correctly", { 28 | expect_error(lawn_average(), 29 | "argument \"in_field\" is missing, with no default") 30 | expect_error(lawn_average(lawn_data$polygons_average), 31 | "argument \"in_field\" is missing, with no default") 32 | expect_error( 33 | lawn_average(lawn_data$polygons_average, lawn_data$points_average), 34 | "argument \"in_field\" is missing, with no default") 35 | expect_error( 36 | lawn_average(lawn_data$polygons_average, "styff", "population")) 37 | }) 38 | -------------------------------------------------------------------------------- /tests/testthat/test-bbox_polygon.R: -------------------------------------------------------------------------------- 1 | context("lawn_bbox_polygon") 2 | 3 | bbox <- c(0, 0, 10, 10) 4 | a <- lawn_bbox_polygon(bbox) 5 | b <- lawn_bbox_polygon(c(5, 5, 10, 10)) 6 | d <- lawn_bbox_polygon(c(-15, -5, 50, 60)) 7 | e <- lawn_bbox_polygon(c(0.1, 1, 0.2, 2)) 8 | 9 | test_that("lawn_bbox_polygon works", { 10 | expect_is(a, "polygon") 11 | expect_is(a$type, "character") 12 | expect_is(a$geometry$type, "character") 13 | expect_equal(a$geometry$type, "Polygon") 14 | expect_is(a$geometry$coordinates, "array") 15 | 16 | expect_is(b, "polygon") 17 | expect_is(b$type, "character") 18 | expect_is(b$geometry$type, "character") 19 | expect_equal(b$geometry$type, "Polygon") 20 | expect_is(b$geometry$coordinates, "array") 21 | 22 | expect_is(d, "polygon") 23 | expect_is(d$type, "character") 24 | expect_is(d$geometry$type, "character") 25 | expect_equal(d$geometry$type, "Polygon") 26 | expect_is(d$geometry$coordinates, "array") 27 | 28 | expect_is(e, "polygon") 29 | expect_is(e$type, "character") 30 | expect_is(e$geometry$type, "character") 31 | expect_equal(e$geometry$type, "Polygon") 32 | expect_is(e$geometry$coordinates, "array") 33 | }) 34 | 35 | test_that("lawn_bbox_polygon fails correctly", { 36 | expect_error(lawn_bbox_polygon(), "argument \"bbox\" is missing, with no default") 37 | expect_error(lawn_bbox_polygon(1)) 38 | expect_error(lawn_bbox_polygon(c("1", 2, 3, 4)), "All values must be numeric") 39 | expect_error(lawn_bbox_polygon(c("a", "b", "c", "d")), "All values must be numeric") 40 | }) 41 | -------------------------------------------------------------------------------- /tests/testthat/test-bearing.R: -------------------------------------------------------------------------------- 1 | context("lawn_bearing") 2 | 3 | start <- '{ 4 | "type": "Feature", 5 | "properties": { 6 | "marker-color": "#f00" 7 | }, 8 | "geometry": { 9 | "type": "Point", 10 | "coordinates": [-75.343, 39.984] 11 | } 12 | }' 13 | 14 | end <- '{ 15 | "type": "Feature", 16 | "properties": { 17 | "marker-color": "#0f0" 18 | }, 19 | "geometry": { 20 | "type": "Point", 21 | "coordinates": [-75.534, 39.123] 22 | } 23 | }' 24 | 25 | test_that("lawn_bearing works", { 26 | expect_is(lawn_bearing(start, end), "numeric") 27 | expect_lt(lawn_bearing(start, end), 0) 28 | }) 29 | 30 | start_bad1 <- '{ 31 | "type": "Feature", 32 | "properties": { 33 | "marker-color": "#f00" 34 | }, 35 | "geometry": { 36 | "type": "Point" 37 | } 38 | }' 39 | 40 | start_bad2 <- '{ 41 | "type": "Feature", 42 | "properties": { 43 | "marker-color": "#f00" 44 | }, 45 | "limousine": { 46 | "type": "Point", 47 | "coordinates": [-75.343, 39.984] 48 | } 49 | }' 50 | 51 | test_that("lawn_bearing linting works correctly", { 52 | expect_error(lawn_bearing(start_bad1, end, lint = TRUE), "Line 1 - \"coordinates\" member required") 53 | expect_error(lawn_bearing(start_bad2, end, lint = TRUE), "Line 1 - \"geometry\" member required") 54 | }) 55 | 56 | test_that("lawn_bearing fails correctly", { 57 | expect_error(lawn_bearing(), "argument \"start\" is missing, with no default") 58 | expect_error(lawn_bearing(start), "argument \"end\" is missing, with no default") 59 | expect_error(lawn_bearing(4, 5)) 60 | }) 61 | -------------------------------------------------------------------------------- /tests/testthat/test-bezier.R: -------------------------------------------------------------------------------- 1 | context("lawn_bezier") 2 | 3 | pts <- '[ 4 | [-21.964416, 64.148203], 5 | [-21.956176, 64.141316], 6 | [-21.93901, 64.135924], 7 | [-21.927337, 64.136673] 8 | ]' 9 | a <- lawn_bezier(lawn_linestring(pts)) 10 | b <- lawn_bezier(lawn_linestring(pts), 9000L) 11 | d <- lawn_bezier(lawn_linestring(pts), 3000L) 12 | 13 | test_that("lawn_bezier works", { 14 | expect_is(a, "linestring") 15 | expect_is(a$type, "character") 16 | expect_is(a$geometry, "list") 17 | expect_is(a$geometry$type, "character") 18 | expect_is(a$geometry$coordinates, "matrix") 19 | expect_equal(length(a$properties), 0) 20 | expect_equal(NROW(a$geometry$coordinates), 500) 21 | 22 | expect_is(b, "linestring") 23 | expect_is(b$type, "character") 24 | expect_is(b$geometry, "list") 25 | expect_is(b$geometry$type, "character") 26 | expect_is(b$geometry$coordinates, "matrix") 27 | expect_equal(length(b$properties), 0) 28 | expect_equal(NROW(b$geometry$coordinates), 450) 29 | 30 | expect_is(d, "linestring") 31 | expect_is(d$type, "character") 32 | expect_is(d$geometry, "list") 33 | expect_is(d$geometry$type, "character") 34 | expect_is(d$geometry$coordinates, "matrix") 35 | expect_equal(length(d$properties), 0) 36 | expect_equal(NROW(d$geometry$coordinates), 150) 37 | }) 38 | 39 | test_that("lawn_bezier fails correctly", { 40 | expect_error(lawn_bezier(), "argument \"line\" is missing, with no default") 41 | expect_error(lawn_bezier(lawn_linestring(pts), "bean")) 42 | expect_error(lawn_bezier(lawn_linestring(pts), sharpness = "bean")) 43 | }) 44 | -------------------------------------------------------------------------------- /tests/testthat/test-buffer.R: -------------------------------------------------------------------------------- 1 | context("lawn_buffer") 2 | 3 | 4 | test_that("lawn_buffer works", { 5 | skip_on_fedora() 6 | 7 | poly <- lawn_data$poly 8 | multi <- lawn_data$multipoly 9 | pt <- lawn_point(c(0, 0)) 10 | pt_bff <- lawn_buffer(pt, 100) 11 | 12 | expect_is(lawn_buffer(poly, 100), "feature") 13 | expect_is(unclass(lawn_buffer(poly, 100)), "list") 14 | expect_is(lawn_buffer(multi, 100), "featurecollection") 15 | expect_is(lawn_buffer(pt, 100), "feature") 16 | expect_is(pt_bff$type, "character") 17 | expect_null(pt_bff$features) 18 | }) 19 | 20 | test_that("lawn_buffer fails correctly", { 21 | skip_on_fedora() 22 | 23 | pt <- lawn_point(c(0, 0)) 24 | 25 | expect_error(lawn_buffer(), "argument \"input\" is missing, with no default") 26 | expect_error(lawn_buffer(dist = 100), "argument \"input\" is missing, with no default") 27 | expect_error(lawn_buffer(pt), "argument \"dist\" is missing, with no default") 28 | expect_null(lawn_buffer(pt, dist = 0)) 29 | expect_null(lawn_buffer(pt, dist = -100)) 30 | }) 31 | -------------------------------------------------------------------------------- /tests/testthat/test-center.R: -------------------------------------------------------------------------------- 1 | context("lawn_center") 2 | 3 | a <- lawn_center(lawn_data$points_average) 4 | 5 | test_that("lawn_center works", { 6 | expect_is(a, "point") 7 | expect_is(unclass(a), "list") 8 | expect_is(a$type, "character") 9 | expect_is(a$geometry, "list") 10 | expect_is(a$geometry$type, "character") 11 | expect_is(a$geometry$coordinates, "numeric") 12 | 13 | expect_lt(lawn_center(lawn_data$filter_features)$geometry$coordinates[1], 0) 14 | expect_lt(lawn_center(lawn_data$polygons_within)$geometry$coordinates[1], 0) 15 | }) 16 | 17 | test_that("lawn_center fails correctly", { 18 | expect_error(lawn_center(), "argument \"features\" is missing, with no default") 19 | expect_error(lawn_center(4)) 20 | expect_error(lawn_center(4, lint = TRUE), 21 | "The root of a GeoJSON object must be an object") 22 | expect_error(lawn_center("a")) 23 | expect_error(lawn_center("a", lint = TRUE), 24 | "invalid char in json text") 25 | }) 26 | -------------------------------------------------------------------------------- /tests/testthat/test-check4leaflet.R: -------------------------------------------------------------------------------- 1 | context("check4leaflet") 2 | 3 | skip_if_not_installed <- function(pkg) { 4 | if (requireNamespace(pkg, quietly = TRUE)) 5 | return() 6 | testthat::skip(paste0(pkg, " not installed")) 7 | } 8 | 9 | test_that("check4leaflet returns TRUE", { 10 | skip_if_not_installed("leaflet") 11 | 12 | expect_true(check4leaflet()) 13 | }) 14 | -------------------------------------------------------------------------------- /tests/testthat/test-collect.R: -------------------------------------------------------------------------------- 1 | context("lawn_collect") 2 | 3 | ex_polys <- lawn_data$polygons_aggregate 4 | ex_pts <- lawn_data$points_aggregate 5 | agg_result <- lawn_collect(ex_polys, ex_pts, 'population', 'stuff') 6 | 7 | test_that("lawn_collect works", { 8 | expect_is(agg_result, "featurecollection") 9 | expect_is(agg_result$type, "character") 10 | expect_is(agg_result$features, "data.frame") 11 | expect_is(agg_result$features$geometry$type, "character") 12 | expect_is(agg_result$features$geometry$coordinates[[1]], "array") 13 | expect_equal(agg_result$features$properties$stuff[[1]][1], 200) 14 | expect_equal(agg_result$features$properties$stuff[[1]][2], 600) 15 | }) 16 | 17 | test_that("lawn_collect fails correctly", { 18 | expect_error(lawn_collect(), "argument \"polygons\" is missing, with no default") 19 | expect_error(lawn_collect(ex_polys), "argument \"points\" is missing, with no default") 20 | expect_error(lawn_collect(ex_polys, ex_pts, 5), "\"out_field\" is missing") 21 | }) 22 | -------------------------------------------------------------------------------- /tests/testthat/test-convex.R: -------------------------------------------------------------------------------- 1 | context("lawn_convex") 2 | 3 | a <- lawn_convex(lawn_data$points_average) 4 | b <- lawn_convex(lawn_data$points_within) 5 | d <- lawn_convex(lawn_data$points_aggregate) 6 | 7 | test_that("lawn_convex works", { 8 | expect_is(a, "polygon") 9 | expect_is(unclass(a), "list") 10 | expect_is(a$type, "character") 11 | expect_is(a$geometry, "list") 12 | expect_is(a$geometry$type, "character") 13 | expect_is(a$geometry$coordinates, "array") 14 | 15 | expect_is(b, "polygon") 16 | expect_is(unclass(b), "list") 17 | expect_equal(length(b$properties), 0) 18 | }) 19 | 20 | test_that("lawn_convex fails correctly", { 21 | expect_error(lawn_convex(), "argument \"input\" is missing, with no default") 22 | expect_error(lawn_convex("stuff")) 23 | expect_error(lawn_convex("stuff", lint = TRUE), "invalid char") 24 | }) 25 | -------------------------------------------------------------------------------- /tests/testthat/test-count.R: -------------------------------------------------------------------------------- 1 | context("lawn_count") 2 | 3 | pts <- lawn_data$points_count 4 | ply <- lawn_data$polygons_count 5 | a <- lawn_count(polygons = ply, points = pts, "population") 6 | 7 | test_that("lawn_count works", { 8 | expect_is(a, "featurecollection") 9 | expect_equal(a$type, "FeatureCollection") 10 | expect_is(a$features, "data.frame") 11 | expect_named(a$features$properties, c("values", "count")) 12 | 13 | expect_equal(sum( 14 | lawn_count(lawn_data$polygons_average, 15 | lawn_data$points_average, "population")$features$properties$count), 5) 16 | }) 17 | 18 | test_that("count fails correctly", { 19 | expect_error(lawn_count(), 20 | "argument \"polygons\" is missing, with no default") 21 | expect_error(lawn_count(polygons = ply, points = 4, "population")) 22 | expect_error( 23 | lawn_count(polygons = ply, points = 4, "population", lint = TRUE), 24 | "Polygons and MultiPolygons should follow the right-hand rule") 25 | }) 26 | -------------------------------------------------------------------------------- /tests/testthat/test-destination.R: -------------------------------------------------------------------------------- 1 | context("lawn_destination") 2 | 3 | pt <- '{ 4 | "type": "Feature", 5 | "properties": { 6 | "marker-color": "#0f0" 7 | }, 8 | "geometry": { 9 | "type": "Point", 10 | "coordinates": [-75.343, 39.984] 11 | } 12 | }' 13 | 14 | a <- lawn_destination(pt, 50, 90, "miles") 15 | b <- lawn_destination(pt, 100, 90, "miles") 16 | d <- lawn_destination(pt, 2, 45, "kilometers") 17 | e <- lawn_destination(pt, 2, 30, "degrees") 18 | 19 | test_that("lawn_destination works", { 20 | expect_is(a, "point") 21 | expect_is(unclass(a), "list") 22 | expect_is(a$type, "character") 23 | expect_is(a$geometry, "list") 24 | expect_is(a$geometry$type, "character") 25 | expect_is(a$geometry$coordinates, "numeric") 26 | 27 | expect_is(b, "point") 28 | expect_is(unclass(b), "list") 29 | expect_equal(length(b$properties), 0) 30 | 31 | expect_equal(round(lawn_distance(pt, a, units = "miles")), 50) 32 | expect_equal(round(lawn_distance(pt, b, units = "miles")), 100) 33 | expect_equal(round(lawn_distance(pt, d, units = "kilometers")), 2) 34 | expect_equal(round(lawn_distance(pt, e, units = "degrees")), 2) 35 | }) 36 | 37 | test_that("lawn_destination fails correctly", { 38 | expect_error(lawn_destination(), "argument \"bearing\" is missing, with no default") 39 | expect_error(lawn_destination(pt, 5), "argument \"bearing\" is missing, with no default") 40 | expect_error(lawn_destination(pt, 5, 5, "things"), "should be one of") 41 | expect_error(lawn_destination(pt, 5, 200, "miles"), "bearing must be between -180 and 180") 42 | }) 43 | -------------------------------------------------------------------------------- /tests/testthat/test-deviation.R: -------------------------------------------------------------------------------- 1 | context("lawn_deviation") 2 | 3 | ex_polys <- lawn_data$polygons_aggregate 4 | ex_pts <- lawn_data$points_aggregate 5 | dev_result <- lawn_deviation(ex_polys, ex_pts, "population") 6 | 7 | test_that("lawn_deviation works", { 8 | expect_is(dev_result, "featurecollection") 9 | expect_is(dev_result$type, "character") 10 | expect_is(dev_result$features, "data.frame") 11 | expect_is(dev_result$features$geometry$type, "character") 12 | expect_is(dev_result$features$geometry$coordinates[[1]], "array") 13 | expect_equal(dev_result$features$properties$deviation[1], sd(c(200, 600, 100)) * sqrt(2/3)) 14 | }) 15 | 16 | test_that("lawn_deviation fails correctly", { 17 | expect_error(lawn_deviation(), "argument \"in_field\" is missing, with no default") 18 | expect_error(lawn_deviation(ex_polys), "argument \"in_field\" is missing, with no default") 19 | expect_error(lawn_deviation(ex_polys, ex_pts), "argument \"in_field\" is missing, with no default") 20 | }) -------------------------------------------------------------------------------- /tests/testthat/test-distance.R: -------------------------------------------------------------------------------- 1 | context("lawn_distance") 2 | 3 | from <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Point", 8 | "coordinates": [-75.343, 39.984] 9 | } 10 | }' 11 | to <- '{ 12 | "type": "Feature", 13 | "properties": {}, 14 | "geometry": { 15 | "type": "Point", 16 | "coordinates": [-75.534, 39.123] 17 | } 18 | }' 19 | 20 | test_that("lawn_distance works", { 21 | expect_is(lawn_distance(from, to), "numeric") 22 | expect_equal(round(lawn_distance(from, to, "kilometers"), 0), 97) 23 | expect_equal(round(lawn_distance(from, to, "miles"), 0), 60) 24 | expect_equal(round(lawn_distance(from, to, "radians"), 2), 0.02) 25 | expect_equal(round(lawn_distance(from, to, "degrees"), 2), 0.87) 26 | }) 27 | 28 | test_that("lawn_distance fails correctly", { 29 | expect_error(lawn_distance(), "missing, with no default") 30 | expect_error(lawn_distance(4, 5)) 31 | expect_error(lawn_distance(4, 5, lint = TRUE), 32 | "The root of a GeoJSON object must be an object") 33 | expect_error(lawn_distance(from, to, "km")) 34 | }) 35 | -------------------------------------------------------------------------------- /tests/testthat/test-envelope.R: -------------------------------------------------------------------------------- 1 | context("lawn_envelope") 2 | 3 | a <- lawn_envelope(lawn_data$points_count) 4 | b <- lawn_envelope(lawn_data$polygons_count) 5 | d <- lawn_envelope(lawn_data$polygons_aggregate) 6 | 7 | test_that("lawn_envelope works", { 8 | expect_is(a, "polygon") 9 | expect_is(unclass(a), "list") 10 | expect_is(a$type, "character") 11 | expect_is(a$geometry, "list") 12 | expect_is(a$geometry$type, "character") 13 | expect_is(a$geometry$coordinates, "array") 14 | 15 | ## this first test doesn't work, maybe a digits problem? 16 | # expect_true(lawn_inside(lawn_point(c(-112.0372, 46.608058)), a)) 17 | expect_true(lawn_inside(lawn_point(c(-112.045955, 46.596264)), a)) 18 | expect_true(lawn_inside(lawn_point(c(-112.072391, 46.586591)), b)) 19 | expect_true(lawn_inside(lawn_point(c(-112.023983, 46.570426)), b)) 20 | expect_true(lawn_inside(lawn_point(c(1.669921, 48.632908)), d)) 21 | }) 22 | 23 | test_that("lawn_envelope fails correctly", { 24 | expect_error(lawn_envelope(), "argument \"fc\" is missing, with no default") 25 | expect_error(lawn_envelope(5)) 26 | expect_error(lawn_envelope(5, lint = TRUE), 27 | "The root of a GeoJSON object must be an object") 28 | }) 29 | -------------------------------------------------------------------------------- /tests/testthat/test-explode.R: -------------------------------------------------------------------------------- 1 | context("lawn_explode") 2 | 3 | poly <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Polygon", 8 | "coordinates": [[ 9 | [177.434692, -17.77517], 10 | [177.402076, -17.779093], 11 | [177.38079, -17.803937], 12 | [177.40242, -17.826164], 13 | [177.438468, -17.824857], 14 | [177.454948, -17.796746], 15 | [177.434692, -17.77517] 16 | ]] 17 | } 18 | }' 19 | a <- lawn_explode(poly) 20 | 21 | test_that("lawn_explode works", { 22 | expect_is(a, "featurecollection") 23 | expect_is(a$type, "character") 24 | expect_is(a$features, "data.frame") 25 | expect_is(a$features$geometry$type, "character") 26 | expect_is(a$features$geometry$coordinates[[1]], "numeric") 27 | expect_equal(a$features$geometry$type[1], "Point") 28 | }) 29 | 30 | test_that("lawn_explode fails correctly", { 31 | expect_error(lawn_explode(), "argument \"input\" is missing, with no default") 32 | expect_error(lawn_explode(5)) 33 | expect_error(lawn_explode(5, TRUE), "Line 0 - The root of a GeoJSON object must be an object") 34 | }) 35 | -------------------------------------------------------------------------------- /tests/testthat/test-extent.R: -------------------------------------------------------------------------------- 1 | context("lawn_extent") 2 | 3 | # From a FeatureCollection 4 | a <- lawn_extent(lawn_data$points_average) 5 | 6 | # From a Feature 7 | dat <- '{ 8 | "type": "Feature", 9 | "properties": {}, 10 | "geometry": { 11 | "type": "Polygon", 12 | "coordinates": [[ 13 | [-112.072391,46.586591], 14 | [-112.072391,46.61761], 15 | [-112.028102,46.61761], 16 | [-112.028102,46.586591], 17 | [-112.072391,46.586591] 18 | ]] 19 | } 20 | }' 21 | b <- lawn_extent(dat) 22 | 23 | test_that("lawn_extent returns correct classes", { 24 | expect_is(a, "numeric") 25 | expect_is(a[1], "numeric") 26 | expect_is(b, "numeric") 27 | expect_is(b[1], "numeric") 28 | }) 29 | 30 | test_that("lawn_extent returns correct values", { 31 | expect_equal(length(a), 4) 32 | expect_lt(a[1], 20) 33 | expect_lt(a[2], 60) 34 | 35 | expect_equal(length(b), 4) 36 | expect_lt(b[1], 0) 37 | expect_lt(b[2], 50) 38 | }) 39 | 40 | test_that("lawn_extent fails correctly", { 41 | expect_error(lawn_extent(), "argument \"input\" is missing, with no default") 42 | expect_error(lawn_extent(5)) 43 | expect_error(lawn_extent("{}", TRUE), "Line 1 - \"type\" member required") 44 | }) 45 | -------------------------------------------------------------------------------- /tests/testthat/test-filter.R: -------------------------------------------------------------------------------- 1 | context("lawn_filter") 2 | 3 | a <- lawn_filter(lawn_data$filter_features, 'species', 'oak') 4 | b <- lawn_filter(lawn_data$filter_features, 'species', 'maple') 5 | d <- lawn_filter(lawn_data$filter_features, 'species', 'redwood') 6 | 7 | test_that("lawn_filter returns the correct classes", { 8 | expect_is(a, "featurecollection") 9 | expect_is(a$type, "character") 10 | expect_is(a$features, "data.frame") 11 | expect_is(a$features$geometry$type, "character") 12 | expect_is(a$features$geometry$coordinates[[1]], "numeric") 13 | expect_equal(a$features$geometry$type[1], "Point") 14 | }) 15 | 16 | test_that("lawn_filter returns the correct values", { 17 | expect_equal(a$type, "FeatureCollection") 18 | expect_equal(d$features$type, "Feature") 19 | expect_equal(d$features$geometry$type, "Point") 20 | }) 21 | 22 | test_that("lawn_filter fails correctly", { 23 | # missing parameter input 24 | expect_error(lawn_filter(), "argument \"key\" is missing, with no default") 25 | # zero length output if no matches 26 | expect_equal(length(lawn_filter(lawn_data$filter_features, 4, 5)$features), 0) 27 | }) 28 | -------------------------------------------------------------------------------- /tests/testthat/test-hex_grid.R: -------------------------------------------------------------------------------- 1 | context("lawn_hex_grid") 2 | 3 | a <- lawn_hex_grid(c(-96, 31, -84, 40), 50, 'miles') 4 | b <- lawn_hex_grid(c(-96, 31, -84, 40), 30, 'miles') 5 | d <- lawn_hex_grid(c(-96, 31, -84, 40), 10, 'miles') 6 | 7 | test_that("lawn_hex_grid returns correct classes", { 8 | expect_is(a, "featurecollection") 9 | expect_is(a$type, "character") 10 | expect_is(a$features, "data.frame") 11 | expect_is(a$features$geometry$type, "character") 12 | expect_is(a$features$geometry$coordinates[[1]], "array") 13 | expect_equal(a$features$geometry$type[1], "Polygon") 14 | }) 15 | 16 | test_that("cellWidth parameter works as expected", { 17 | expect_lt(length(a$features$geometry$coordinates), 18 | length(b$features$geometry$coordinates)) 19 | expect_lt(length(a$features$geometry$coordinates), 20 | length(d$features$geometry$coordinates)) 21 | expect_lt(length(b$features$geometry$coordinates), 22 | length(d$features$geometry$coordinates)) 23 | }) 24 | 25 | test_that("lawn_hex_grid fails correctly", { 26 | # missing arguments 27 | expect_error(lawn_hex_grid(), 28 | "argument \"extent\" is missing, with no default") 29 | # empty featurecollection if bbox is not correct 30 | expect_error(lawn_hex_grid(c(-96, 31, -84), 10, 'miles')) 31 | # can't pass in a character string to cellWidth 32 | expect_error(lawn_hex_grid(c(-96, 31, -84, 40), "the", 'miles')) 33 | # can't pass in a character string to cellWidth 34 | expect_error(lawn_hex_grid(c(-96, 31, -84, 40), 50, 'doesntexist')) 35 | }) 36 | -------------------------------------------------------------------------------- /tests/testthat/test-inside.R: -------------------------------------------------------------------------------- 1 | context("lawn_inside") 2 | 3 | point1 <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Point", 8 | "coordinates": [-111.467285, 40.75766] 9 | } 10 | }' 11 | point2 <- '{ 12 | "type": "Feature", 13 | "properties": {}, 14 | "geometry": { 15 | "type": "Point", 16 | "coordinates": [-111.873779, 40.647303] 17 | } 18 | }' 19 | point3 <- '{ 20 | "type": "Feature", 21 | "properties": {}, 22 | "geometry": { 23 | "type": "Point", 24 | "coordinates": [-111.973879, 40.647303] 25 | } 26 | }' 27 | poly <- '{ 28 | "type": "Feature", 29 | "properties": {}, 30 | "geometry": { 31 | "type": "Polygon", 32 | "coordinates": [[ 33 | [-112.074279, 40.52215], 34 | [-112.074279, 40.853293], 35 | [-111.610107, 40.853293], 36 | [-111.610107, 40.52215], 37 | [-112.074279, 40.52215] 38 | ]] 39 | } 40 | }' 41 | 42 | a <- lawn_inside(point1, poly) 43 | b <- lawn_inside(point2, poly) 44 | d <- lawn_inside(point3, poly) 45 | 46 | test_that("lawn_inside returns correct classes", { 47 | expect_is(a, "logical") 48 | expect_is(b, "logical") 49 | expect_is(d, "logical") 50 | }) 51 | 52 | test_that("lawn_inside returns correct values", { 53 | expect_false(a) 54 | expect_true(b) 55 | expect_true(d) 56 | }) 57 | 58 | test_that("lawn_inside fails correctly", { 59 | expect_error(lawn_inside(), 60 | "argument \"point\" is missing, with no default") 61 | expect_error(lawn_inside("{}", poly)) 62 | expect_error(lawn_inside(point1, point2)) 63 | }) 64 | -------------------------------------------------------------------------------- /tests/testthat/test-isolines.R: -------------------------------------------------------------------------------- 1 | context("lawn_isolines") 2 | 3 | # file <- system.file("examples", "isolines_data.json", package = "lawn") 4 | # pts <- jsonlite::fromJSON(file) 5 | # pts$features$properties <- data.frame(z = round(rnorm(100, mean = 5)), stringsAsFactors = FALSE) 6 | # breaks <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 7 | # a <- lawn_isolines(pts, 'z', 15, breaks) 8 | # b <- lawn_isolines(pts, 'z', 15, breaks) 9 | # d <- lawn_isolines(pts, 'z', 15, breaks) 10 | 11 | # test_that("lawn_isolines returns correct class", { 12 | # expect_is(a, "featurecollection") 13 | # expect_is(a$type, "character") 14 | # expect_is(a$features, "data.frame") 15 | # expect_is(a$features$geometry$type, "character") 16 | # expect_is(a$features$geometry$coordinates, "list") 17 | # expect_is(a$features$geometry$coordinates[[1]], "matrix") 18 | # }) 19 | 20 | # test_that("lawn_isolines returns correct values", { 21 | # expect_equal(a$features$geometry$type[1], "LineString") 22 | # expect_equal(a$features$geometry$type[1], "LineString") 23 | # }) 24 | 25 | # test_that("resolution parameter works as expected", { 26 | # expect_lt(length(lawn_isolines(pts, 'z', 30, breaks)$features$geometry$coordinates), 27 | # length(lawn_isolines(pts, 'z', 50, breaks)$features$geometry$coordinates)) 28 | # }) 29 | 30 | # test_that("lawn_isolines fails correctly", { 31 | # expect_error(lawn_isolines(), "argument \"points\" is missing, with no default") 32 | # }) 33 | -------------------------------------------------------------------------------- /tests/testthat/test-kinks.R: -------------------------------------------------------------------------------- 1 | context("lawn_kinks") 2 | 3 | poly <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Polygon", 8 | "coordinates": [[ 9 | [-12.034835, 8.901183], 10 | [-12.060413, 8.899826], 11 | [-12.03638, 8.873199], 12 | [-12.059383, 8.871418], 13 | [-12.034835, 8.901183] 14 | ]] 15 | } 16 | }' 17 | a <- lawn_kinks(poly) 18 | 19 | test_that("lawn_kinks works", { 20 | expect_is(a, "list") 21 | expect_is(a$features, "data.frame") 22 | expect_is(a$features$type, "character") 23 | expect_is(a$features$geometry, "data.frame") 24 | expect_equivalent(round(a$features$geometry$coordinates[[1]][1]), -12) 25 | }) 26 | 27 | test_that("lawn_kinks fails correctly", { 28 | expect_error(lawn_kinks(), "argument \"input\" is missing, with no default") 29 | expect_error(lawn_kinks(5, TRUE), "Line 0 - The root of a GeoJSON object must be an object") 30 | expect_error(lawn_kinks("adfadfs")) 31 | 32 | bad1 <- '{"type":"Featureadf","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-12.034835,8.901183],[-12.060413,8.899826],[-12.03638,8.873199],[-12.059383,8.871418],[-12.034835,8.901183]]]}}' 33 | expect_error(lawn_kinks(bad1, TRUE), "The type Featureadf is unknown") 34 | 35 | bad2 <- '{"type":"Feature","properties":{},"geometry":{"type":"Polygan","coordinates":[[[-12.034835,8.901183],[-12.060413,8.899826],[-12.03638,8.873199],[-12.059383,8.871418],[-12.034835,8.901183]]]}}' 36 | expect_error(lawn_kinks(bad2, TRUE), "The type Polygan is unknown") 37 | }) 38 | -------------------------------------------------------------------------------- /tests/testthat/test-line_distance.R: -------------------------------------------------------------------------------- 1 | context("lawn_line_distance") 2 | 3 | line <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "LineString", 8 | "coordinates": [ 9 | [-77.031669, 38.878605], 10 | [-77.029609, 38.881946], 11 | [-77.020339, 38.884084], 12 | [-77.025661, 38.885821], 13 | [-77.021884, 38.889563], 14 | [-77.019824, 38.892368] 15 | ] 16 | } 17 | }' 18 | a <- lawn_line_distance(line, 'kilometers') 19 | b <- lawn_line_distance(line, 'miles') 20 | d <- lawn_line_distance(line, 'radians') 21 | e <- lawn_line_distance(line, 'degrees') 22 | 23 | test_that("lawn_line_distance returns correct classes", { 24 | expect_is(a, "numeric") 25 | expect_is(b, "numeric") 26 | expect_is(d, "numeric") 27 | expect_is(e, "numeric") 28 | }) 29 | 30 | test_that("right dimensions", { 31 | expect_lt(d, e) 32 | expect_lt(e, b) 33 | expect_lt(b, a) 34 | expect_equal(length(lawn_line_distance(line, 'miles')), 1) 35 | }) 36 | 37 | test_that("lawn_line_distance fails correctly", { 38 | # missing arguments 39 | expect_error(lawn_line_distance(), 40 | "argument \"line\" is missing, with no default") 41 | # can't pass in a character string to cellWidth 42 | expect_error(lawn_line_distance(line, 'stuff')) 43 | # can't pass in a character string to cellWidth 44 | expect_error(lawn_line_distance("{}", "miles")) 45 | }) 46 | -------------------------------------------------------------------------------- /tests/testthat/test-line_slice.R: -------------------------------------------------------------------------------- 1 | context("lawn_line_slice") 2 | 3 | start <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Point", 8 | "coordinates": [-77.029609, 38.881946] 9 | } 10 | }' 11 | stop <- '{ 12 | "type": "Feature", 13 | "properties": {}, 14 | "geometry": { 15 | "type": "Point", 16 | "coordinates": [-77.021884, 38.889563] 17 | } 18 | }' 19 | line <- '{ 20 | "type": "Feature", 21 | "properties": {}, 22 | "geometry": { 23 | "type": "LineString", 24 | "coordinates": [ 25 | [-77.031669, 38.878605], 26 | [-77.029609, 38.881946], 27 | [-77.020339, 38.884084], 28 | [-77.025661, 38.885821], 29 | [-77.021884, 38.889563], 30 | [-77.019824, 38.892368] 31 | ] 32 | } 33 | }' 34 | 35 | a <- lawn_line_slice(start, stop, line) 36 | 37 | test_that("lawn_line_slice works", { 38 | expect_is(a, "linestring") 39 | expect_is(a$type, "character") 40 | expect_is(a$geometry$coordinates, "matrix") 41 | expect_is(a$geometry$type, "character") 42 | expect_is(a$geometry$coordinates[[1]], "numeric") 43 | }) 44 | 45 | test_that("lawn_line_slice fails correctly", { 46 | expect_error(lawn_line_slice(), "argument \"point1\" is missing, with no default") 47 | expect_error(lawn_line_slice(5, 5, 6, TRUE), "Line 0 - The root of a GeoJSON object must be an object") 48 | }) 49 | -------------------------------------------------------------------------------- /tests/testthat/test-linestring.R: -------------------------------------------------------------------------------- 1 | context("lawn_hex_grid") 2 | 3 | linestring1 <- '[ 4 | [-21.964416, 64.148203], 5 | [-21.956176, 64.141316], 6 | [-21.93901, 64.135924], 7 | [-21.927337, 64.136673] 8 | ]' 9 | linestring2 <- '[ 10 | [-21.929054, 64.127985], 11 | [-21.912918, 64.134726], 12 | [-21.916007, 64.141016], 13 | [-21.930084, 64.14446] 14 | ]' 15 | 16 | a <- lawn_linestring(linestring1) 17 | b <- lawn_linestring(linestring2) 18 | 19 | pts <- list( 20 | c(-21.964416, 64.148203), 21 | c(-21.956176, 64.141316), 22 | c(-21.93901, 64.135924), 23 | c(-21.927337, 64.136673) 24 | ) 25 | 26 | d <- lawn_linestring(pts, properties = list(name = 'line1', distance = 145)) 27 | 28 | test_that("lawn_linestring returns correct classes", { 29 | expect_is(a, "linestring") 30 | expect_is(b, "linestring") 31 | expect_is(d, "linestring") 32 | 33 | expect_is(a$type, "character") 34 | expect_is(a$geometry$coordinates, "matrix") 35 | }) 36 | 37 | test_that("properties parameter works as expected", { 38 | expect_equal(length(a$properties), 0) 39 | expect_equal(length(d$properties), 2) 40 | expect_equal(d$properties$name, "line1") 41 | }) 42 | 43 | test_that("lawn_linestring fails correctly", { 44 | # missing arguments 45 | expect_error(lawn_linestring(), "argument \"coordinates\" is missing, with no default") 46 | }) 47 | -------------------------------------------------------------------------------- /tests/testthat/test-max.R: -------------------------------------------------------------------------------- 1 | context("lawn_max") 2 | 3 | poly <- lawn_data$polygons_average 4 | pt <- lawn_data$points_average 5 | 6 | a <- lawn_max(poly, pt, 'population', 'max') 7 | 8 | test_that("lawn_max returns correct classes", { 9 | expect_is(a, "featurecollection") 10 | expect_is(a$type, "character") 11 | expect_is(a$features, "data.frame") 12 | expect_is(a$features$geometry$type, "character") 13 | expect_is(a$features$geometry$coordinates[[1]], "array") 14 | expect_equal(a$features$geometry$type[1], "Polygon") 15 | }) 16 | 17 | test_that("in_field and out_field parameter works as expected", { 18 | expect_null(lawn_max(poly, pt, 'population2')$features$properties$max) 19 | expect_named(lawn_max(poly, pt, 'population', 'bears')$features$properties, 20 | c('values', 'bears')) 21 | }) 22 | 23 | test_that("lawn_max fails correctly", { 24 | # missing arguments 25 | expect_error(lawn_max(), "argument \"in_field\" is missing, with no default") 26 | # wrong in_field param leads to no ouput for max 27 | expect_null(lawn_max(poly, pt, 'population2')$features$properties$max) 28 | # can't pass in a character string to cellWidth 29 | expect_error(lawn_max(poly, "{}", 'population', 'max')) 30 | # can't pass in a character string to cellWidth 31 | expect_error(lawn_max(poly, "{}", 'population', 'max', lint = TRUE), 32 | 'Line 1 - "type" member required') 33 | }) 34 | -------------------------------------------------------------------------------- /tests/testthat/test-median.R: -------------------------------------------------------------------------------- 1 | context("lawn_median") 2 | 3 | poly <- lawn_data$polygons_average 4 | pt <- lawn_data$points_average 5 | 6 | a <- lawn_median(poly, pt, 'population', 'max') 7 | 8 | test_that("lawn_median returns correct classes", { 9 | expect_is(a, "featurecollection") 10 | expect_is(a$type, "character") 11 | expect_is(a$features, "data.frame") 12 | expect_is(a$features$geometry$type, "character") 13 | expect_is(a$features$geometry$coordinates[[1]], "array") 14 | expect_equal(a$features$geometry$type[1], "Polygon") 15 | }) 16 | 17 | test_that("in_field and out_field parameter works as expected", { 18 | expect_equal( 19 | length( 20 | na.omit( 21 | lawn_median( 22 | poly, pt, 'population2', 'median')$features$properties$median)), 23 | 0 24 | ) 25 | expect_named( 26 | lawn_median(poly, pt, 'population', 'bears')$features$properties, 27 | c('values', 'bears')) 28 | }) 29 | 30 | test_that("lawn_median fails correctly", { 31 | # missing arguments 32 | expect_error(lawn_median(), 33 | "argument \"in_field\" is missing, with no default") 34 | # wrong in_field param leads to no ouput for median 35 | expect_equal( 36 | length( 37 | na.omit( 38 | lawn_median(poly, pt, 39 | 'population2', 'median')$features$properties$median)), 0) 40 | expect_error(lawn_median(poly, "{}", 'population', 'median')) 41 | }) 42 | -------------------------------------------------------------------------------- /tests/testthat/test-merge.R: -------------------------------------------------------------------------------- 1 | context("lawn_merge") 2 | 3 | polygons <- '{ 4 | "type": "FeatureCollection", 5 | "features": [ 6 | { 7 | "type": "Feature", 8 | "properties": { 9 | "fill": "#0f0" 10 | }, 11 | "geometry": { 12 | "type": "Polygon", 13 | "coordinates": [[ 14 | [9.994812, 53.549487], 15 | [10.046997, 53.598209], 16 | [10.117721, 53.531737], 17 | [9.994812, 53.549487] 18 | ]] 19 | } 20 | }, { 21 | "type": "Feature", 22 | "properties": { 23 | "fill": "#00f" 24 | }, 25 | "geometry": { 26 | "type": "Polygon", 27 | "coordinates": [[ 28 | [10.000991, 53.50418], 29 | [10.03807, 53.562539], 30 | [9.926834, 53.551731], 31 | [10.000991, 53.50418] 32 | ]] 33 | } 34 | } 35 | ] 36 | }' 37 | 38 | a <- lawn_merge(polygons) 39 | 40 | test_that("lawn_merge returns correct classes", { 41 | expect_is(a, "polygon") 42 | expect_is(a$type, "character") 43 | expect_is(a$geometry$type, "character") 44 | expect_is(a$geometry$coordinates, "array") 45 | expect_equal(a$geometry$type, "Polygon") 46 | }) 47 | 48 | test_that("lawn_merge fails correctly", { 49 | # missing arguments 50 | expect_error(lawn_merge(), "argument \"fc\" is missing, with no default") 51 | }) 52 | -------------------------------------------------------------------------------- /tests/testthat/test-midpoint.R: -------------------------------------------------------------------------------- 1 | context("lawn_midpoint") 2 | 3 | pt1 <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Point", 8 | "coordinates": [144.834823, -37.771257] 9 | } 10 | }' 11 | pt2 <- '{ 12 | "type": "Feature", 13 | "properties": {}, 14 | "geometry": { 15 | "type": "Point", 16 | "coordinates": [145.14244, -37.830937] 17 | } 18 | }' 19 | 20 | a <- lawn_midpoint(pt1, pt2) 21 | 22 | test_that("lawn_midpoint returns correct classes", { 23 | expect_is(a, "point") 24 | expect_is(a$type, "character") 25 | expect_is(a$geometry$type, "character") 26 | expect_is(a$geometry$coordinates, "numeric") 27 | expect_equal(a$geometry$type, "Point") 28 | }) 29 | 30 | test_that("lawn_midpoint fails correctly", { 31 | # missing arguments 32 | expect_error(lawn_midpoint(), "argument \"pt1\" is missing, with no default") 33 | }) 34 | -------------------------------------------------------------------------------- /tests/testthat/test-min.R: -------------------------------------------------------------------------------- 1 | context("lawn_average") 2 | 3 | poly <- lawn_data$polygons_average 4 | pt <- lawn_data$points_average 5 | a <- lawn_min(poly, pt, 'population', 'min') 6 | 7 | test_that("lawn_average works", { 8 | expect_is(a, "featurecollection") 9 | expect_is(a$type, "character") 10 | expect_equal(a$type, "FeatureCollection") 11 | expect_is(a$features, "data.frame") 12 | expect_is(a$features$geometry$type, "character") 13 | expect_is(a$features$geometry$coordinates[[1]], "array") 14 | expect_equal(a$features$properties$min[1], 100) 15 | expect_equal(a$features$properties$min[2], 200) 16 | }) 17 | 18 | test_that("lawn_average fails correctly", { 19 | expect_error(lawn_min(), "argument \"in_field\" is missing, with no default") 20 | expect_null(lawn_min(poly, pt, 'population2', 'min')$features$properties$min) 21 | }) 22 | -------------------------------------------------------------------------------- /tests/testthat/test-nearest.R: -------------------------------------------------------------------------------- 1 | context("lawn_nearest") 2 | 3 | point <- '{ 4 | "type": "Feature", 5 | "properties": { 6 | "marker-color": "#0f0" 7 | }, 8 | "geometry": { 9 | "type": "Point", 10 | "coordinates": [28.965797, 41.010086] 11 | } 12 | }' 13 | against <- '{ 14 | "type": "FeatureCollection", 15 | "features": [ 16 | { 17 | "type": "Feature", 18 | "properties": {}, 19 | "geometry": { 20 | "type": "Point", 21 | "coordinates": [28.973865, 41.011122] 22 | } 23 | }, { 24 | "type": "Feature", 25 | "properties": {}, 26 | "geometry": { 27 | "type": "Point", 28 | "coordinates": [28.948459, 41.024204] 29 | } 30 | }, { 31 | "type": "Feature", 32 | "properties": {}, 33 | "geometry": { 34 | "type": "Point", 35 | "coordinates": [28.938674, 41.013324] 36 | } 37 | } 38 | ] 39 | }' 40 | 41 | a <- lawn_nearest(point, against) 42 | 43 | test_that("lawn_nearest returns correct classes", { 44 | expect_is(a, "point") 45 | expect_is(a$type, "character") 46 | expect_is(a$geometry$type, "character") 47 | expect_is(a$geometry$coordinates, "numeric") 48 | expect_equal(a$geometry$type, "Point") 49 | }) 50 | 51 | test_that("expected values", { 52 | expect_lt(lawn_nearest(point, lawn_data$points_average)$geometry$coordinates[1], 20) 53 | expect_lt(lawn_nearest(point, lawn_data$points_aggregate)$geometry$coordinates[1], 5) 54 | }) 55 | 56 | test_that("lawn_nearest fails correctly", { 57 | # missing arguments 58 | expect_error(lawn_nearest(), "argument \"point\" is missing, with no default") 59 | }) 60 | -------------------------------------------------------------------------------- /tests/testthat/test-onload.R: -------------------------------------------------------------------------------- 1 | context("onload") 2 | 3 | obj_names <- c('eval', 'assign', 'validate', 'call', 4 | 'reset', 'source', 'console', 'get') 5 | 6 | test_that("onload for turf.js worked", { 7 | expect_is(ct, "V8") 8 | expect_true(all(obj_names %in% ls(ct))) 9 | expect_true(grepl("function", ct$eval("turf.collect"))) 10 | }) 11 | 12 | test_that("onload for geojson-random worked", { 13 | expect_is(rand, "V8") 14 | expect_true(all(obj_names %in% ls(rand))) 15 | expect_true(grepl("function", rand$eval("georandom.point"))) 16 | }) 17 | 18 | test_that("onload for geojsonhint worked", { 19 | expect_is(ht, "V8") 20 | expect_true(all(obj_names %in% ls(ht))) 21 | expect_true(grepl("function", ht$eval("geojsonhint.hint"))) 22 | }) 23 | -------------------------------------------------------------------------------- /tests/testthat/test-planepoint.R: -------------------------------------------------------------------------------- 1 | context("lawn_planepoint") 2 | 3 | pt <- lawn_point(c(-75.3221, 39.529)) 4 | triangle <- '{ 5 | "type": "Feature", 6 | "properties": { 7 | "a": 11, 8 | "b": 122, 9 | "c": 44 10 | }, 11 | "geometry": { 12 | "type": "Polygon", 13 | "coordinates": [[ 14 | [-75.1221, 39.57], 15 | [-75.58, 39.18], 16 | [-75.97, 39.86], 17 | [-75.1221, 39.57] 18 | ]] 19 | } 20 | }' 21 | 22 | a <- lawn_planepoint(pt, triangle) 23 | 24 | test_that("lawn_planepoint returns correct classes", { 25 | expect_is(a, "numeric") 26 | }) 27 | 28 | test_that("give right values", { 29 | expect_lt(lawn_planepoint(lawn_point(c(-75.3221, 39.229)), triangle), 90) 30 | expect_lt(lawn_planepoint(lawn_point(c(-75.3221, 39.129)), triangle), 106) 31 | expect_lt(lawn_planepoint(lawn_point(c(-75.3221, 39.029)), triangle), 125) 32 | }) 33 | 34 | test_that("lawn_planepoint fails correctly", { 35 | # missing arguments 36 | expect_error(lawn_planepoint(), "argument \"pt\" is missing, with no default") 37 | }) 38 | -------------------------------------------------------------------------------- /tests/testthat/test-point.R: -------------------------------------------------------------------------------- 1 | context("lawn_point") 2 | 3 | test_that("lawn_point works", { 4 | a <- lawn_point(c(40, -74.50)) 5 | b <- lawn_point(c(40, -74.50), properties = list(name = 'poly1', population = 400)) 6 | 7 | expect_is(a, "point") 8 | expect_is(unclass(a), "list") 9 | expect_equal(a$type, "Feature") 10 | expect_is(a$geometry, "list") 11 | expect_equal(length(a$properties), 0) 12 | 13 | expect_is(b, "point") 14 | expect_is(unclass(b), "list") 15 | expect_is(b$geometry, "list") 16 | expect_is(b$properties, "list") 17 | expect_equal(b$type, "Feature") 18 | expect_equal(length(b$properties), 2) 19 | }) 20 | 21 | test_that("lawn_point fails correctly", { 22 | expect_error(lawn_point(), 23 | "argument \"coordinates\" is missing, with no default") 24 | expect_error(lawn_point(5)) 25 | expect_error(lawn_point(NULL)) 26 | }) 27 | -------------------------------------------------------------------------------- /tests/testthat/test-point_grid.R: -------------------------------------------------------------------------------- 1 | context("lawn_point_grid") 2 | 3 | a <- lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles') 4 | b <- lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles') 5 | d <- lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles') 6 | 7 | test_that("lawn_point_grid returns correct classes", { 8 | expect_is(a, "featurecollection") 9 | expect_is(a$type, "character") 10 | expect_is(a$features, "data.frame") 11 | expect_is(a$features$geometry$type, "character") 12 | expect_is(a$features$geometry$coordinates, "list") 13 | expect_equal(a$features$geometry$type, "Point") 14 | 15 | expect_is(b, "featurecollection") 16 | expect_is(d, "featurecollection") 17 | }) 18 | 19 | test_that("cellWidth parameter works as expected", { 20 | expect_lt(NROW(a$features$geometry), NROW(b$features$geometry)) 21 | expect_lt(NROW(b$features$geometry), NROW(d$features$geometry)) 22 | expect_lt(NROW(a$features$geometry), NROW(d$features$geometry)) 23 | }) 24 | 25 | test_that("lawn_point_grid fails correctly", { 26 | # missing arguments 27 | expect_error(lawn_point_grid(), "argument \"extent\" is missing, with no default") 28 | # units value not allowed 29 | expect_error(lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'adf')) 30 | }) 31 | -------------------------------------------------------------------------------- /tests/testthat/test-point_on_line.R: -------------------------------------------------------------------------------- 1 | context("lawn_point_on_line") 2 | 3 | line <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "LineString", 8 | "coordinates": [ 9 | [-77.031669, 38.878605], 10 | [-77.029609, 38.881946], 11 | [-77.020339, 38.884084], 12 | [-77.025661, 38.885821], 13 | [-77.021884, 38.889563], 14 | [-77.019824, 38.892368] 15 | ] 16 | } 17 | }' 18 | pt <- '{ 19 | "type": "Feature", 20 | "properties": {}, 21 | "geometry": { 22 | "type": "Point", 23 | "coordinates": [-77.037076, 38.884017] 24 | } 25 | }' 26 | a <- lawn_point_on_line(line, pt) 27 | 28 | test_that("lawn_point_on_line works", { 29 | expect_is(a, "point") 30 | expect_is(a$type, "character") 31 | expect_is(a$geometry, "list") 32 | expect_is(a$geometry$type, "character") 33 | expect_is(a$geometry$coordinates, "numeric") 34 | }) 35 | 36 | test_that("lawn_point_on_line fails correctly", { 37 | expect_error(lawn_point_on_line(), 38 | "argument \"line\" is missing, with no default") 39 | expect_error(lawn_point_on_line(5, TRUE)) 40 | }) 41 | -------------------------------------------------------------------------------- /tests/testthat/test-point_on_surface.R: -------------------------------------------------------------------------------- 1 | context("lawn_point_on_surface") 2 | 3 | # polygon 4 | x <- lawn_random("polygons") 5 | a <- lawn_point_on_surface(x) 6 | 7 | # point 8 | x <- lawn_random("points") 9 | b <- lawn_point_on_surface(x) 10 | 11 | # linestring 12 | linestring <- '[ 13 | [-21.929054, 64.127985], 14 | [-21.912918, 64.134726], 15 | [-21.916007, 64.141016], 16 | [-21.930084, 64.14446] 17 | ]' 18 | d <- lawn_point_on_surface(lawn_linestring(linestring)) 19 | 20 | test_that("lawn_point_on_surface returns correct classes", { 21 | expect_is(a, "point") 22 | expect_is(a$type, "character") 23 | expect_is(b, "point") 24 | expect_is(b$type, "character") 25 | expect_is(d, "point") 26 | expect_is(d$type, "character") 27 | 28 | expect_equal(lawn_point_on_surface(lawn_point(c(1, 2)))$geometry$coordinates[1], 1) 29 | }) 30 | 31 | test_that("lawn_point_on_surface fails correctly", { 32 | # missing arguments 33 | expect_error(lawn_point_on_surface(), "argument \"x\" is missing, with no default") 34 | #expect_error(lawn_point_on_surface("{}"), "Unknown Geometry Type") 35 | }) 36 | -------------------------------------------------------------------------------- /tests/testthat/test-polygon.R: -------------------------------------------------------------------------------- 1 | context("lawn_polygon") 2 | 3 | test_that("lawn_polygon works", { 4 | rings <- list(list( 5 | c(-2.275543, 53.464547), 6 | c(-2.275543, 53.489271), 7 | c(-2.215118, 53.489271), 8 | c(-2.215118, 53.464547), 9 | c(-2.275543, 53.464547) 10 | )) 11 | a <- lawn_polygon(rings) 12 | b <- lawn_polygon(rings, properties = list(name = 'poly1', population = 400)) 13 | 14 | expect_is(a, "polygon") 15 | expect_is(unclass(a), "list") 16 | expect_equal(a$type, "Feature") 17 | expect_is(a$geometry, "list") 18 | expect_is(a$geometry$coordinates, "array") 19 | expect_equal(length(a$geometry$coordinates), 10) 20 | expect_equal(length(a$properties), 0) 21 | 22 | expect_is(b, "polygon") 23 | expect_is(unclass(b), "list") 24 | expect_equal(b$type, "Feature") 25 | expect_is(b$geometry, "list") 26 | expect_is(b$properties, "list") 27 | expect_equal(length(b$properties), 2) 28 | 29 | # passing NULL gives empty polygon geometry 30 | expect_is(lawn_polygon(NULL), "polygon") 31 | }) 32 | 33 | test_that("lawn_polygon fails correctly", { 34 | expect_error(lawn_polygon(), 35 | "argument \"coordinates\" is missing, with no default") 36 | expect_error(lawn_polygon("stuff")) 37 | }) 38 | -------------------------------------------------------------------------------- /tests/testthat/test-random.R: -------------------------------------------------------------------------------- 1 | context("lawn_random") 2 | 3 | ## set of points 4 | pt_a <- lawn_random(n = 2) 5 | pt_b <- lawn_random(n = 10) 6 | pt_d <- lawn_random(n = 1000) 7 | 8 | ## set of polygons 9 | ply_a <- lawn_random('polygons', 2) 10 | ply_b <- lawn_random('polygons', 10) 11 | ply_d <- lawn_random('polygons', 1000) 12 | 13 | # with options 14 | # lawn_random(bbox = c(-70, 40, -60, 60)) 15 | # lawn_random(num_vertices = 5) 16 | 17 | test_that("lawn_random returns correct classes", { 18 | expect_is(pt_a, "featurecollection") 19 | expect_is(pt_a$type, "character") 20 | expect_is(pt_a$features$geometry$type, "character") 21 | 22 | expect_is(pt_b, "featurecollection") 23 | expect_is(pt_b$type, "character") 24 | expect_is(pt_b$features$geometry$type, "character") 25 | 26 | expect_is(pt_d, "featurecollection") 27 | expect_is(pt_d$type, "character") 28 | expect_is(pt_d$features$geometry$type, "character") 29 | 30 | expect_is(ply_a, "featurecollection") 31 | expect_is(ply_b, "featurecollection") 32 | expect_is(ply_d, "featurecollection") 33 | }) 34 | 35 | test_that("cellWidth parameter works as expected", { 36 | expect_equal(NROW(pt_a$features), 2) 37 | expect_equal(NROW(pt_b$features), 10) 38 | expect_equal(NROW(pt_d$features), 1000) 39 | 40 | expect_equal(NROW(ply_a$features), 2) 41 | expect_equal(NROW(ply_b$features), 10) 42 | expect_equal(NROW(ply_d$features), 1000) 43 | }) 44 | 45 | test_that("lawn_random fails correctly", { 46 | # missing arguments 47 | expect_error(lawn_random("stuff")) 48 | }) 49 | -------------------------------------------------------------------------------- /tests/testthat/test-remove.R: -------------------------------------------------------------------------------- 1 | context("lawn_remove") 2 | 3 | dat <- lawn_data$remove_features 4 | a <- lawn_remove(dat, 'marker-color', '#00f') 5 | b <- lawn_remove(dat, 'marker-color', '#0f0') 6 | 7 | test_that("lawn_remove returns correct classes", { 8 | expect_is(a, "featurecollection") 9 | expect_is(a$type, "character") 10 | expect_is(a$features, "data.frame") 11 | expect_is(a$features$geometry$type, "character") 12 | expect_is(a$features$geometry$coordinates, "list") 13 | 14 | expect_is(b, "featurecollection") 15 | expect_is(a$features, "data.frame") 16 | }) 17 | 18 | test_that("property parameter works as expected", { 19 | expect_gt(length(a$features$geometry$coordinates), 20 | length(b$features$geometry$coordinates)) 21 | }) 22 | 23 | test_that("lawn_remove fails correctly", { 24 | # missing arguments 25 | expect_error(lawn_remove(), "argument \"features\" is missing, with no default") 26 | # not found property param gives back empty feature collection 27 | expect_equal(length(unclass(lawn_remove(dat, 'stuff', '#0f0'))$features), 0) 28 | # not found value param gives back empty feature collection 29 | expect_equal(length(unclass(lawn_remove(dat, 'marker-color', '#zzz'))$features), 0) 30 | # can't pass in a character string to cellWidth 31 | expect_error(lawn_remove(lawn_point(c(4, 5)), 'marker-color', '#0f0', lint = TRUE)) 32 | }) 33 | -------------------------------------------------------------------------------- /tests/testthat/test-sample.R: -------------------------------------------------------------------------------- 1 | context("lawn_sample") 2 | 3 | dat <- lawn_data$points_average 4 | a <- lawn_sample(dat, 1) 5 | b <- lawn_sample(dat, 2) 6 | d <- lawn_sample(dat, 3) 7 | 8 | test_that("lawn_sample returns correct classes", { 9 | expect_is(a, "featurecollection") 10 | expect_is(a$type, "character") 11 | expect_is(a$features, "data.frame") 12 | expect_is(a$features$geometry$type, "character") 13 | 14 | expect_is(b, "featurecollection") 15 | expect_is(b$features, "data.frame") 16 | expect_is(b$features$geometry$coordinates, "list") 17 | 18 | expect_is(d, "featurecollection") 19 | expect_is(d$features, "data.frame") 20 | }) 21 | 22 | test_that("n parameter works as expected", { 23 | expect_lt(NROW(a$features), NROW(b$features)) 24 | expect_lt(NROW(a$features), NROW(d$features)) 25 | expect_lt(NROW(b$features), NROW(d$features)) 26 | }) 27 | 28 | test_that("lawn_sample fails correctly", { 29 | # n of negative number gives back no data 30 | expect_equal(length(lawn_sample(dat, -1)$features), 0) 31 | # n of character string errors 32 | expect_error(lawn_sample(dat, "a")) 33 | }) 34 | -------------------------------------------------------------------------------- /tests/testthat/test-square.R: -------------------------------------------------------------------------------- 1 | context("lawn_square") 2 | 3 | bbox <- c(-20, -20, -15, 0) 4 | a <- lawn_square(bbox) 5 | 6 | test_that("lawn_square returns correct classes", { 7 | expect_is(a, "numeric") 8 | expect_equal(length(a), 4) 9 | }) 10 | 11 | test_that("lawn_square fails correctly", { 12 | # missing arguments 13 | expect_error(lawn_square(), "argument \"bbox\" is missing, with no default") 14 | # only length 3 gives error message 15 | expect_error(lawn_square(c(1, 2, 4))) 16 | # can't pass in a character string to cellWidth 17 | expect_error(lawn_square(c(1, 2, 4, "a")), "bbox must be") 18 | }) 19 | -------------------------------------------------------------------------------- /tests/testthat/test-sum.R: -------------------------------------------------------------------------------- 1 | context("lawn_sum") 2 | 3 | poly <- lawn_data$polygons_average 4 | pt <- lawn_data$points_average 5 | a <- lawn_sum(poly, pt, 'population', 'sum') 6 | 7 | test_that("lawn_sum returns correct classes", { 8 | expect_is(a, "featurecollection") 9 | expect_is(a$type, "character") 10 | expect_is(a$features, "data.frame") 11 | expect_is(a$features$geometry$type, "character") 12 | expect_is(a$features$geometry$coordinates[[1]], "array") 13 | expect_equal(a$features$geometry$type[1], "Polygon") 14 | }) 15 | 16 | test_that("in_field and out_field parameters works as expected", { 17 | expect_named( 18 | lawn_sum(poly, pt, 'population')$features$properties, c('values', "sum")) 19 | expect_named( 20 | lawn_sum( 21 | poly, pt, 'population', 'stuff')$features$properties, 22 | c('values', "stuff")) 23 | expect_true( 24 | is.na( 25 | lawn_sum(poly, pt, 'population2', 'sum')$features$properties$sum[1])) 26 | }) 27 | 28 | test_that("lawn_sum fails correctly", { 29 | # missing arguments 30 | expect_error(lawn_sum(), 31 | "argument \"in_field\" is missing, with no default") 32 | # can't pass in a character string to cellWidth 33 | expect_error(lawn_sum(poly, "{}", 'population', 'sum')) 34 | }) 35 | -------------------------------------------------------------------------------- /tests/testthat/test-tag.R: -------------------------------------------------------------------------------- 1 | context("lawn_tag") 2 | 3 | bbox <- c(0, 0, 10, 10) 4 | pts <- lawn_random(n = 30, bbox = bbox) 5 | polys <- lawn_triangle_grid(bbox, 50, 'miles') 6 | polys$features$properties$fill <- "#f92" 7 | polys$features$properties$stroke <- 0 8 | polys$features$properties$`fill-opacity` <- 1 9 | 10 | a <- lawn_tag(pts, polys, 'fill', 'marker-color') 11 | 12 | test_that("lawn_tag returns correct classes", { 13 | expect_is(a, "featurecollection") 14 | expect_is(a$type, "character") 15 | expect_is(a$features, "data.frame") 16 | expect_is(a$features$geometry$type, "character") 17 | expect_is(a$features$geometry$coordinates, "list") 18 | expect_equal(a$features$geometry$type[1], "Point") 19 | 20 | expect_is(a$features$properties, "data.frame") 21 | expect_named(a$features$properties, "marker-color") 22 | expect_equal(a$features$properties$`marker-color`[1], "#f92") 23 | }) 24 | 25 | test_that("polyId, out_field parameters works as expected", { 26 | expect_named(lawn_tag(pts, polys, 'fill', 'stuff')$features$properties, 27 | "stuff") 28 | }) 29 | 30 | test_that("lawn_tag fails correctly", { 31 | # missing arguments 32 | expect_error(lawn_tag(), "argument \"points\" is missing, with no default") 33 | # wrong geojson input 34 | expect_error(lawn_tag(pts, "{}", 'fill', 'marker-color')) 35 | }) 36 | -------------------------------------------------------------------------------- /tests/testthat/test-tin.R: -------------------------------------------------------------------------------- 1 | context("lawn_tin") 2 | 3 | pts <- lawn_random(bbox = c(-70, 40, -60, 60)) 4 | pts$features$properties <- 5 | data.frame(z = round(rnorm(NROW(pts$features$geometry), mean = 3, sd = 1))) 6 | 7 | ### FIXME - need to replicate eg at http://turfjs.org/static/docs/module-turf_tin.html 8 | # tin <- jsonlite::fromJSON(jsonlite::toJSON(unclass(lawn_tin(pts, "z"))), FALSE) 9 | # tin <- lapply(tin$features, function(x) { 10 | # modifyList(x, list(properties = list(style = list(fill = paste0("#", paste0(x$properties, collapse = "")) )))) 11 | # }) 12 | # jsonlite::toJSON(tin) %>% view 13 | 14 | 15 | test_that("lawn_tin", { 16 | skip_on_fedora() 17 | 18 | a <- lawn_tin(pts) 19 | b <- lawn_tin(pts, propertyName = "z") 20 | 21 | expect_is(a, "featurecollection") 22 | expect_is(a$type, "character") 23 | expect_is(a$features, "data.frame") 24 | expect_is(a$features$geometry$type, "character") 25 | expect_is(a$features$geometry$coordinates[[1]], "array") 26 | expect_equal(a$features$geometry$type[1], "Polygon") 27 | 28 | expect_is(b, "featurecollection") 29 | 30 | # propertyName parameter works as expected 31 | expect_equal(length(names(a$features$properties)), 0) 32 | expect_named(b$features$properties, c("a", "b", "c")) 33 | }) 34 | 35 | test_that("lawn_tin fails correctly", { 36 | skip_on_fedora() 37 | 38 | # missing arguments 39 | expect_error(lawn_tin(), 40 | "argument \"pt\" is missing, with no default") 41 | # bad geojson input 42 | expect_error(lawn_tin("{}")) 43 | }) 44 | -------------------------------------------------------------------------------- /tests/testthat/test-transform_rotate.R: -------------------------------------------------------------------------------- 1 | context("lawn_transform_rotate") 2 | 3 | x <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Polygon", 8 | "coordinates": [ 9 | [ 10 | [ 0, 29 ], [ 3.5, 29 ], [ 2.5, 32 ], [ 0, 29 ] 11 | ] 12 | ] 13 | } 14 | }' 15 | xl <- jsonlite::fromJSON(x) 16 | 17 | test_that("lawn_transform_rotate works", { 18 | a <- lawn_transform_rotate(x, angle = 100, pivot = c(15, 15)) 19 | 20 | expect_is(a, "feature") 21 | expect_is(unclass(a), "list") 22 | expect_is(a$type, "character") 23 | expect_is(a$geometry, "list") 24 | expect_is(a$geometry$type, "character") 25 | expect_is(a$geometry$coordinates, "array") 26 | # points are not identical to original 27 | expect_false(identical(xl$geometry$coordinates, a$geometry$coordinates)) 28 | }) 29 | 30 | test_that("lawn_transform_rotate fails correctly", { 31 | expect_error(lawn_transform_rotate(), 32 | "argument \"x\" is missing, with no default") 33 | expect_error(lawn_transform_rotate(5), 34 | "argument \"angle\" is missing, with no default") 35 | expect_error(lawn_transform_rotate(list(), 100)) 36 | 37 | expect_error(lawn_transform_rotate(x, "adf"), "angle must be") 38 | expect_error(lawn_transform_rotate(x, 34, c(1, "ad")), 39 | "class numeric") 40 | }) 41 | -------------------------------------------------------------------------------- /tests/testthat/test-union.R: -------------------------------------------------------------------------------- 1 | context("lawn_union") 2 | 3 | poly1 <- '{ 4 | "type": "Feature", 5 | "properties": { 6 | "fill": "#0f0" 7 | }, 8 | "geometry": { 9 | "type": "Polygon", 10 | "coordinates": [[ 11 | [-122.801742, 45.48565], 12 | [-122.801742, 45.60491], 13 | [-122.584762, 45.60491], 14 | [-122.584762, 45.48565], 15 | [-122.801742, 45.48565] 16 | ]] 17 | } 18 | }' 19 | 20 | poly2 <- '{ 21 | "type": "Feature", 22 | "properties": { 23 | "fill": "#00f" 24 | }, 25 | "geometry": { 26 | "type": "Polygon", 27 | "coordinates": [[ 28 | [-122.520217, 45.535693], 29 | [-122.64038, 45.553967], 30 | [-122.720031, 45.526554], 31 | [-122.669906, 45.507309], 32 | [-122.723464, 45.446643], 33 | [-122.532577, 45.408574], 34 | [-122.487258, 45.477466], 35 | [-122.520217, 45.535693] 36 | ]] 37 | } 38 | }' 39 | 40 | test_that("lawn_union works", { 41 | skip_on_fedora() 42 | 43 | a <- lawn_union(poly1, poly2) 44 | 45 | expect_is(a, "polygon") 46 | expect_is(a$type, "character") 47 | expect_is(a$geometry, "list") 48 | expect_is(a$geometry$coordinates, "array") 49 | }) 50 | 51 | test_that("lawn_union fails correctly", { 52 | skip_on_fedora() 53 | 54 | expect_error(lawn_union(), "missing, with no default") 55 | expect_error(lawn_union("afa", "asd")) 56 | expect_error(lawn_union(stuff = "adf"), "unused argument") 57 | }) 58 | -------------------------------------------------------------------------------- /tests/testthat/test-unkinkpolygon.R: -------------------------------------------------------------------------------- 1 | context("lawn_unkinkpolygon") 2 | 3 | x <- '{ 4 | "type": "Feature", 5 | "properties": {}, 6 | "geometry": { 7 | "type": "Polygon", 8 | "coordinates": [[[0, 0], [2, 0], [0, 2], [2, 2], [0, 0]]] 9 | } 10 | }' 11 | xl <- jsonlite::fromJSON(x) 12 | 13 | test_that("lawn_unkinkpolygon works", { 14 | aa <- lawn_unkinkpolygon(x) 15 | 16 | expect_is(aa, "featurecollection") 17 | expect_equal(aa$type, "FeatureCollection") 18 | expect_is(aa$features, "data.frame") 19 | expect_is(aa$features$properties, "data.frame") 20 | expect_equal(aa$features$geometry$type, c("Polygon", "Polygon")) 21 | 22 | ## aa has features slot for each polygon created, while original 23 | ## has one polygon 24 | expect_false("features" %in% names(xl)) 25 | expect_true("features" %in% names(aa)) 26 | }) 27 | 28 | test_that("lawn_unkinkpolygon fails correctly", { 29 | expect_error(lawn_unkinkpolygon(), 30 | "argument \"x\" is missing, with no default") 31 | }) 32 | -------------------------------------------------------------------------------- /tests/testthat/test-variance.R: -------------------------------------------------------------------------------- 1 | context("lawn_variance") 2 | 3 | poly <- lawn_data$polygons_average 4 | pt <- lawn_data$points_average 5 | 6 | a <- lawn_variance(poly, pt, 'population', 'variance') 7 | 8 | test_that("lawn_variance returns correct classes", { 9 | expect_is(a, "featurecollection") 10 | expect_is(a$type, "character") 11 | expect_is(a$features, "data.frame") 12 | expect_is(a$features$geometry$type, "character") 13 | expect_is(a$features$geometry$coordinates[[1]], "array") 14 | expect_equal(a$features$geometry$type[1], "Polygon") 15 | }) 16 | 17 | test_that("cellWidth parameter works as expected", { 18 | expect_equal( 19 | length(na.omit(lawn_variance(poly, pt, 'population2')$features$properties$variance)), 20 | 0 21 | ) 22 | expect_named(lawn_variance(poly, pt, 'population', 'bears')$features$properties, 23 | c('values', 'bears')) 24 | }) 25 | 26 | test_that("lawn_variance fails correctly", { 27 | # missing arguments 28 | expect_error(lawn_variance(), "argument \"in_field\" is missing, with no default") 29 | # wrong in_field param leads to no ouput for max 30 | expect_equal( 31 | length(na.omit(lawn_variance(poly, pt, 'population2')$features$properties$variance)), 32 | 0 33 | ) 34 | # can't pass in a character string to cellWidth 35 | expect_error(lawn_variance(poly, "{}", 'population', 'max')) 36 | # can't pass in a character string to cellWidth 37 | expect_error(lawn_variance(poly, "{}", 'population', 'max', lint = TRUE), 38 | 'Line 1 - "type" member required') 39 | }) 40 | -------------------------------------------------------------------------------- /tests/testthat/test-within.R: -------------------------------------------------------------------------------- 1 | context("lawn_within") 2 | 3 | a <- lawn_within(lawn_data$points_within, lawn_data$polygons_within) 4 | 5 | test_that("lawn_within returns correct classes", { 6 | expect_is(a, "featurecollection") 7 | expect_is(a$type, "character") 8 | expect_is(a$features, "data.frame") 9 | expect_is(a$features$geometry$type, "character") 10 | expect_is(a$features$geometry$coordinates, "list") 11 | expect_equal(a$features$geometry$type[1], "Point") 12 | }) 13 | 14 | test_that("lawn_within fails correctly", { 15 | # missing arguments 16 | expect_error(lawn_within(), "argument \"points\" is missing, with no default") 17 | # empty featurecollection if bbox is not correct 18 | expect_error(lawn_within(lawn_data$points_within, lawn_data$points_average)) 19 | }) 20 | -------------------------------------------------------------------------------- /vignettes/img/map1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/lawn/e196449f6855862760c2bf8e319b06fa393ebec2/vignettes/img/map1.png -------------------------------------------------------------------------------- /vignettes/img/map2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ropensci-archive/lawn/e196449f6855862760c2bf8e319b06fa393ebec2/vignettes/img/map2.png --------------------------------------------------------------------------------