Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
fix #84 update turf js
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Dec 17, 2020
1 parent 774fdbc commit 18eba2c
Show file tree
Hide file tree
Showing 49 changed files with 200 additions and 163 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export(lawn_nearest)
export(lawn_planepoint)
export(lawn_point)
export(lawn_point_grid)
export(lawn_point_on_feature)
export(lawn_point_on_line)
export(lawn_point_on_surface)
export(lawn_polygon)
export(lawn_propeach)
export(lawn_pt2line_distance)
Expand Down
2 changes: 1 addition & 1 deletion R/along.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ lawn_along <- function(line, distance, units, lint = FALSE) {
if (lint) {
is_type(line, type_top = "Feature", type_lower = "LineString")
}
ct$eval(sprintf("var alg = turf.along(%s, %s, '%s');", line, distance, units))
ct$eval(sprintf("var alg = turf.along(%s, %s, {units:'%s'});", line, distance, units))
structure(ct$get("alg"), class = "point")
}
4 changes: 2 additions & 2 deletions R/bezier.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ lawn_bezier <- function(line, resolution = 10000L, sharpness = 0.85,
line <- convert(line)
lawnlint(line, lint)
if (lint) is_type(line, type_top = "Feature", type_lower = "LineString")
ct$eval(sprintf("var bz = turf.bezier(%s, %s, %s);", line, resolution,
sharpness))
ct$eval(sprintf("var bz = turf.bezierSpline(%s, {resolution:%s, sharpness:%s});",
line, resolution, sharpness))
structure(ct$get("bz"), class = "linestring")
}
5 changes: 2 additions & 3 deletions R/buffer.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ lawn_buffer <- function(input, dist, units = "kilometers", lint = FALSE) {
if (lint) is_type(input, type_top = c("Feature", "FeatureCollection"))
units <- match.arg(units, c("meters", "feet", "kilometers",
"miles", "degrees"))
ct$eval(sprintf("var units = '%s';", units))
ct$eval(sprintf('var dist = %s;', dist))
ct$eval(sprintf("var buff = turf.buffer(%s, dist, units);", input))
ct$eval(sprintf("var buff = turf.buffer(%s, %s, {units:'%s'});",
input, dist, units))
output <- ct$get("buff")
if (is.null(output)) return(NULL)
if (output$type == "Feature") {
Expand Down
8 changes: 5 additions & 3 deletions R/concave.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@
lawn_concave <- function(points, maxEdge = 1, units = "miles", lint = FALSE) {
points <- convert(points)
lawnlint(points, lint)
if (lint) is_type(points, type_top = "FeatureCollection", type_lower = "Point")
ct$eval(sprintf("var cv = turf.concave(%s, %s, '%s');", points,
maxEdge, units))
if (lint) {
is_type(points, type_top = "FeatureCollection", type_lower = "Point")
}
ct$eval(sprintf("var cv = turf.concave(%s, {maxEdge:%s, units:'%s'});",
points, maxEdge, units))
structure(ct$get("cv"), class = "linestring")
}
2 changes: 1 addition & 1 deletion R/destination.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ lawn_destination <- function(start, distance, bearing, units, lint = FALSE) {
start <- convert(start)
lawnlint(start, lint)
if (lint) is_type(start, "Feature", "Point")
ct$eval(sprintf("var dest = turf.destination(%s, %s, %s, '%s');",
ct$eval(sprintf("var dest = turf.destination(%s, %s, %s, {units:'%s'});",
start, distance, bearing, units))
structure(ct$get("dest"), class = "point")
}
3 changes: 2 additions & 1 deletion R/distance.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ lawn_distance <- function(from, to, units = 'kilometers', lint = FALSE) {
}
ct$eval(sprintf('var point1 = %s;', from))
ct$eval(sprintf('var point2 = %s;', to))
ct$eval(sprintf("var avg = turf.distance(point1, point2, '%s');", units))
ct$eval(sprintf("var avg = turf.distance(point1, point2, {units:'%s'});",
units))
ct$get("avg")
}
4 changes: 2 additions & 2 deletions R/hex_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' lawn_hex_grid(c(-96,31,-84,40), 50, 'miles')
#' lawn_hex_grid(c(-96,31,-84,40), 30, 'miles')
lawn_hex_grid <- function(extent, cellWidth, units) {
ct$eval(sprintf("var hg = turf.hexGrid(%s, %s, '%s');", toj(extent),
cellWidth, units))
ct$eval(sprintf("var hg = turf.hexGrid(%s, %s, {units:'%s'});",
toj(extent), cellWidth, units))
as.fc(ct$get("hg"))
}
5 changes: 3 additions & 2 deletions R/line_distance.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#' Measure a linestring
#'
#' Takes a [data-LineString] and measures its length in
#' the specified units.
#' the specified units. Uses turf/length internally as
#' lineDistance was deprecated
#'
#' @export
#' @param line Line to measure, a [data-Feature]<([data-LineString])>,
Expand Down Expand Up @@ -35,6 +36,6 @@ lawn_line_distance <- function(line, units, lint = FALSE) {
lawnlint(line, lint)
assert(units, "character")
if (lint) is_type(line, type_top = c("Feature", "FeatureCollection"))
ct$eval(sprintf("var env = turf.lineDistance(%s, '%s');", line, units))
ct$eval(sprintf("var env = turf.length(%s, {units:'%s'});", line, units))
ct$get("env")
}
2 changes: 1 addition & 1 deletion R/onLoad.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ht <- NULL

.onLoad <- function(libname, pkgname){
ct <<- V8::v8();
ct$source(system.file("js/turf473.js", package = pkgname))
ct$source(system.file("js/turf516.js", package = pkgname))
ct$source(system.file("js/turf-meta.js", package = pkgname))
ct$source(system.file("js/turf-invariant.js", package = pkgname))
ct$source(system.file("js/cloner.js", package = pkgname))
Expand Down
24 changes: 15 additions & 9 deletions R/point_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@
#' @param cellSide (integer) the distance between points
#' @param units (character) Units to use for cellWidth, one of 'miles' or
#' 'kilometers' (default).
#' @param centered (logical) adjust points position to center the grid into
#' bbox. This parameter is going to be removed in the next major release,
#' having the output always centered into bbox. Default: `TRUE`
#' @param bboxIsMask if `TRUE`, and bbox is a Polygon or MultiPolygon, the
#' grid Point will be created only if inside the bbox Polygon(s).
#' Default: `FALSE`
#' @param mask (logical) if passed a Polygon or MultiPolygon, the grid
#' Points will be created only inside it
#' @family interpolation
#' @return [data-FeatureCollection] grid of points.
#' @note parameters `centered` and `bboxIsMask` removed
#' @examples
#' lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles')
#' lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles')
#' lawn_point_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles')
lawn_point_grid <- function(extent, cellSide, units = "kilometers",
centered = TRUE, bboxIsMask = FALSE) {
mask = NULL) {

ct$eval(sprintf("var pg = turf.pointGrid(%s, %s, '%s', %s, %s);", toj(extent),
cellSide, units, convert(centered), convert(bboxIsMask)))
assert(extent, c('numeric', 'integer'))
assert(cellSide, c('numeric', 'integer'))
assert(units, 'character')
if (!is.null(mask)) {
ct$eval(sprintf("var options = {units:'%s', mask:%s};", units,
convert(mask)))
} else {
ct$eval(sprintf("var options = {units:'%s'};", units))
}
ct$eval(sprintf("var pg = turf.pointGrid(%s, %s, options);",
toj(extent), cellSide))
as.fc(ct$get("pg"))
}
11 changes: 6 additions & 5 deletions R/point_on_surface.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,23 @@
#' @examples
#' # polygon
#' x <- lawn_random("polygon")
#' lawn_point_on_surface(x)
#' lawn_point_on_feature(x)
#' # point
#' x <- lawn_random("point")
#' lawn_point_on_surface(x)
#' lawn_point_on_feature(x)
#' # linestring
#' linestring <- '[
#' [-21.929054, 64.127985],
#' [-21.912918, 64.134726],
#' [-21.916007, 64.141016],
#' [-21.930084, 64.14446]
#' ]'
#' lawn_point_on_surface(lawn_linestring(linestring))
lawn_point_on_surface <- function(x, lint = FALSE) {
#' lawn_point_on_feature(lawn_linestring(linestring))
lawn_point_on_feature <- function(x, lint = FALSE) {
x <- convert(x)
lawnlint(x, lint)
is_type(x, type_top = c("Feature", "FeatureCollection"))
ct$eval(sprintf("var psf = turf.pointOnSurface(%s);", x))
ct$eval(sprintf("var psf = turf.pointOnFeature(%s);", x))
structure(ct$get("psf"), class = "point")
}
lawn_point_on_surface <- lawn_point_on_feature
34 changes: 28 additions & 6 deletions R/random.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#'
#' @export
#'
#' @param type Type of features desired: 'points' or 'polygons'.
#' @param type Type of features desired: 'position', 'points', 'polygons',
#' or 'lines'
#' @param n (integer) Number of features to generate.
#' @param bbox A bounding box inside of which geometries are placed. In the
#' case of Point features, they are guaranteed to be within this bounds,
Expand All @@ -19,26 +20,47 @@
#' @return A [data-FeatureCollection]
#'
#' @examples
#' ## a position
#' lawn_random("position")
#' ## set of points
#' lawn_random(n = 2)
#' lawn_random(n = 10)
#' ## set of polygons
#' lawn_random('polygons', 2)
#' lawn_random('polygons', 10)
#' ## set of lines
#' lawn_random('lines', 2)
#' lawn_random('lines', 10)
#' # with options
#' lawn_random(bbox = c(-70, 40, -60, 60))
#' lawn_random(num_vertices = 5)
lawn_random <- function(type = "points", n = 10, bbox = NULL,
num_vertices = NULL, max_radial_length = NULL) {
num_vertices = NULL, max_radial_length = NULL, max_length = NULL,
max_rotation = NULL) {

assert(type, 'character')
stopifnot(type %in% c("position", "points", "polygons", "lines"))
assert(n, c('numeric', 'integer'))
assert(bbox, c('numeric', 'integer'))
assert(num_vertices, c('numeric', 'integer'))
assert(max_radial_length, c('numeric', 'integer'))
jj <- jsonlite::toJSON(cmp(list(bbox = bbox, num_vertices = num_vertices,
max_radial_length = max_radial_length)),
auto_unbox = TRUE)
ct$eval(sprintf("var rnd = turf.random('%s', %s, %s);", type, n, jj))
if (type=="position") {
ct$eval(sprintf("var rnd = turf.randomPosition(%s);", bbox))
}
if (type=="points") {
opts <- jsonlite::toJSON(cmp(list(bbox = bbox)), auto_unbox = TRUE)
ct$eval(sprintf("var rnd = turf.randomPoint(%s, %s);", n, opts))
}
if (type=="polygons") {
opts <- jsonlite::toJSON(cmp(list(bbox = bbox, num_vertices = num_vertices,
max_radial_length = max_radial_length)), auto_unbox = TRUE)
ct$eval(sprintf("var rnd = turf.randomPolygon(%s, %s);", n, opts))
}
if (type=="lines") {
opts <- jsonlite::toJSON(cmp(list(bbox = bbox, num_vertices = num_vertices,
max_length = max_length, max_rotation = max_rotation)),
auto_unbox = TRUE)
ct$eval(sprintf("var rnd = turf.randomLineString(%s, %s);", n, opts))
}
as.fc(ct$get("rnd"))
}
8 changes: 5 additions & 3 deletions R/simplify.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#' @param tolerance (numeric) Simplification tolerance. Default value is 0.01.
#' @param high_quality (boolean) Whether or not to spend more time to create a
#' higher-quality simplification with a different algorithm. Default: `FALSE`
#' @param mutate (boolean) allows GeoJSON input to be mutated (significant
#' performance increase if `TRUE`). Default: `FALSE`
#' @return A simplified feature.
#' @template lint
#' @family transformations
Expand Down Expand Up @@ -53,12 +55,12 @@
#' lawn_simplify(feature, tolerance = 0.01) %>% view
#' }
lawn_simplify <- function(feature, tolerance = 0.01, high_quality = FALSE,
lint = FALSE) {
mutate = FALSE, lint = FALSE) {

lawnlint(feature, lint)
assert(high_quality, "logical")
assert(tolerance, c("numeric", "integer"))
ct$eval(sprintf("var simp = turf.simplify(%s, %s, %s);", convert(feature),
tolerance, tolower(high_quality)))
ct$eval(sprintf("var simp = turf.simplify(%s, {tolerance:%s, highQuality:%s, mutate:%s});",
convert(feature), tolerance, tolower(high_quality), tolower(mutate)))
structure(ct$get("simp"), class = tolower(ct$get("simp.geometry.type")))
}
27 changes: 18 additions & 9 deletions R/square_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@
#' @export
#'
#' @param extent (numeric) Extent in `[minX, minY, maxX, maxY]` order.
#' @param cellWidth (integer) Width of each cell.
#' @param units (character) Units to use for cellWidth, one of 'miles' or
#' @param cellSide (integer) dimension of each cell.
#' @param units (character) Units to use for cellSide, one of 'miles' or
#' 'kilometers'.
#' @family interpolation
#' @return [data-FeatureCollection] grid of polygons.
#' @examples
#' lawn_square_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles')
#' lawn_square_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles')
#' lawn_square_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles')
lawn_square_grid <- function(extent, cellWidth, units) {
#' lawn_square_grid(c(-95, 30 ,-85, 40), 30, 'miles')
#' lawn_square_grid(c(-95, 30 ,-85, 40), 10, 'miles')
#' lawn_square_grid(c(-95, 30 ,-85, 40), 3, 'miles')
lawn_square_grid <- function(extent, cellSide, units = "kilometers",
mask = NULL) {

assert(extent, c('numeric', 'integer'))
assert(cellWidth, c('numeric', 'integer'))
ct$eval(sprintf("var sg = turf.squareGrid(%s, %s, '%s');", toj(extent),
cellWidth, units))
assert(cellSide, c('numeric', 'integer'))
assert(units, 'character')
if (!is.null(mask)) {
ct$eval(sprintf("var options = {units:'%s', mask:%s};", units,
convert(mask)))
} else {
ct$eval(sprintf("var options = {units:'%s'};", units))
}
ct$eval(sprintf("var sg = turf.squareGrid(%s, %s, options);",
toj(extent), cellSide))
as.fc(ct$get("sg"))
}
4 changes: 2 additions & 2 deletions R/transform_rotate.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ lawn_transform_rotate <- function(x, angle, pivot = c(0, 0),
assert(angle, c('numeric', 'integer'))
assert(pivot, c('numeric', 'integer'))
assert(mutate, 'logical')
ct$eval(sprintf("var rot = turf.transformRotate(%s, %s, %s, %s);",
x, angle, toj(pivot), toj(mutate)))
ct$eval(sprintf("var rot = turf.transformRotate(%s, %s, {pivot:%s, mutate:%s});",
x, angle, toj(pivot), toj(mutate)))
as.f(ct$get("rot"))
}
4 changes: 2 additions & 2 deletions R/transform_scale.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ lawn_transform_scale <- function(x, factor, origin = "centroid",
lawnlint(x, lint)
assert(origin, 'character')
assert(mutate, 'logical')
ct$eval(sprintf("var sc = turf.transformScale(%s, %s, '%s', %s);",
x, factor, origin, toj(mutate)))
ct$eval(sprintf("var sc = turf.transformScale(%s, %s, {origin:'%s', mutate:%s});",
x, factor, origin, toj(mutate)))
as.f(ct$get("sc"))
}
8 changes: 4 additions & 4 deletions R/transform_translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
#' units = "kilometers", mutate = TRUE)
#' ))
#' }
lawn_transform_translate <- function(x, distance, direction, units = "kilometers",
zTranslation = 0, mutate = FALSE, lint = FALSE) {
lawn_transform_translate <- function(x, distance, direction,
units = "kilometers", zTranslation = 0, mutate = FALSE, lint = FALSE) {

x <- convert(x)
lawnlint(x, lint)
Expand All @@ -64,7 +64,7 @@ lawn_transform_translate <- function(x, distance, direction, units = "kilometers
assert(units, 'character')
assert(zTranslation, c('numeric', 'integer'))
assert(mutate, 'logical')
ct$eval(sprintf("var tt = turf.transformTranslate(%s, %s, %s, '%s', %s, %s);",
x, distance, direction, units, zTranslation, toj(mutate)))
ct$eval(sprintf("var tt = turf.transformTranslate(%s, %s, %s, {units:'%s', zTranslation:%s, mutate:%s});",
x, distance, direction, units, zTranslation, toj(mutate)))
as.f(ct$get("tt"))
}
22 changes: 15 additions & 7 deletions R/triangle_grid.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@
#' @export
#'
#' @param extent (numeric) Extent in `[minX, minY, maxX, maxY]` order.
#' @param cellWidth (integer) Width of each cell.
#' @param units (character) Units to use for cellWidth, one of 'miles' or
#' 'kilometers'.
#' @param cellSide (integer) dimension of each cell.
#' @param units (character) Units to use for cellSide, one of miles,
#' kilometers, degrees, radians
#' @family interpolation
#' @return [data-FeatureCollection] grid of [data-Polygon]'s
#' @examples
#' lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 30, 'miles')
#' lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 10, 'miles')
#' lawn_triangle_grid(c(-77.3876, 38.7198, -76.9482, 39.0277), 3, 'miles')
lawn_triangle_grid <- function(extent, cellWidth, units) {
lawn_triangle_grid <- function(extent, cellSide, units = "kilometers",
mask = NULL) {

assert(extent, c('numeric', 'integer'))
assert(cellWidth, c('numeric', 'integer'))
assert(cellSide, c('numeric', 'integer'))
assert(units, 'character')
ct$eval(sprintf("var tg = turf.triangleGrid(%s, %s, '%s');", toj(extent),
cellWidth, units))
if (!is.null(mask)) {
ct$eval(sprintf("var options = {units:'%s', mask:%s};", units,
convert(mask)))
} else {
ct$eval(sprintf("var options = {units:'%s'};", units))
}
ct$eval(sprintf("var tg = turf.triangleGrid(%s, %s, options);",
toj(extent), cellSide))
as.fc(ct$get("tg"))
}
2 changes: 1 addition & 1 deletion inst/js/LICENSE-turfjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2017 TurfJS
Copyright (c) 2019 Morgan Herlocker

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
Loading

0 comments on commit 18eba2c

Please sign in to comment.