Skip to content

Commit

Permalink
fixes #2291
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Dec 21, 2023
1 parent a8ea6c9 commit 1cc62fb
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 62 deletions.
4 changes: 2 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ export(st_geometrycollection)
export(st_graticule)
export(st_inscribed_circle)
export(st_interpolate_aw)
export(st_interpolate_line)
export(st_intersection)
export(st_intersects)
export(st_is)
Expand All @@ -462,7 +461,9 @@ export(st_jitter)
export(st_join)
export(st_layers)
export(st_length)
export(st_line_interpolate)
export(st_line_merge)
export(st_line_project)
export(st_line_sample)
export(st_linestring)
export(st_m_range)
Expand All @@ -483,7 +484,6 @@ export(st_point_on_surface)
export(st_polygon)
export(st_polygonize)
export(st_precision)
export(st_project_point)
export(st_read)
export(st_read_db)
export(st_relate)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# version 1.0-16

* add `st_project_point()` to find how far a point is when projected on a line, and `st_interpolate_line()` to obtain a point along a line; #2291
* add `st_line_project()` to find how far a point is when projected on a line, and `st_line_interpolate()` to obtain a point at a certain distance along a line; #2291

# version 1.0-15

Expand Down
8 changes: 4 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ CPL_nary_intersection <- function(sfc) {
.Call(`_sf_CPL_nary_intersection`, sfc)
}

CPL_project_point <- function(lines, points, normalized) {
.Call(`_sf_CPL_project_point`, lines, points, normalized)
CPL_line_project <- function(lines, points, normalized) {
.Call(`_sf_CPL_line_project`, lines, points, normalized)
}

CPL_interpolate_line <- function(lines, dists, normalized) {
.Call(`_sf_CPL_interpolate_line`, lines, dists, normalized)
CPL_line_interpolate <- function(lines, dists, normalized) {
.Call(`_sf_CPL_line_interpolate`, lines, dists, normalized)
}

CPL_hex_to_raw <- function(cx) {
Expand Down
13 changes: 7 additions & 6 deletions R/geom-measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,15 @@ recycle_common = function (dots) {
#' @param line object of class `sfc` with `LINESTRING` geometry
#' @param point object of class `sfc` with `POINT` geometry
#' @param normalized logical; if `TRUE`, use or return distance normalised to 0-1
#' @name st_project_point
#' @name st_line_project_point
#' @returns `st_line_project` returns the distance(s) of point(s) along line(s), when projected on the line(s)
#' @export
#' @details
#' arguments `line`, `point` and `dist` are recycled as needed
#' arguments `line`, `point` and `dist` are recycled to common length when needed
#' @examples
#' st_project_point(st_as_sfc("LINESTRING (0 0, 10 10)"), st_as_sfc(c("POINT (0 0)", "POINT (5 5)")))
#' st_project_point(st_as_sfc("LINESTRING (0 0, 10 10)"), st_as_sfc("POINT (5 5)"), TRUE)
st_project_point = function(line, point, normalized = FALSE) {
#' st_line_project(st_as_sfc("LINESTRING (0 0, 10 10)"), st_as_sfc(c("POINT (0 0)", "POINT (5 5)")))
#' st_line_project(st_as_sfc("LINESTRING (0 0, 10 10)"), st_as_sfc("POINT (5 5)"), TRUE)
st_line_project = function(line, point, normalized = FALSE) {
stopifnot(inherits(line, "sfc"), inherits(point, "sfc"),
all(st_dimension(line) == 1), all(st_dimension(point) == 0),
is.logical(normalized), length(normalized) == 1,
Expand All @@ -257,5 +258,5 @@ st_project_point = function(line, point, normalized = FALSE) {
if (isTRUE(st_is_longlat(line)))
message_longlat("st_project_point")
recycled = recycle_common(list(line, point))
CPL_project_point(recycled[[1]], recycled[[2]], normalized)
CPL_line_project(recycled[[1]], recycled[[2]], normalized)
}
11 changes: 6 additions & 5 deletions R/geom-transformers.R
Original file line number Diff line number Diff line change
Expand Up @@ -1123,19 +1123,20 @@ st_line_sample = function(x, n, density, type = "regular", sample = NULL) {
} #nocov end

#' @param dist numeric, vector with distance value(s)
#' @name st_project_point
#' @name st_line_project_point
#' @returns `st_line_interpolate` returns the point(s) at dist(s), when measured along (interpolated on) the line(s)
#' @export
#' @examples
#' st_interpolate_line(st_as_sfc("LINESTRING (0 0, 1 1)"), 1)
#' st_interpolate_line(st_as_sfc("LINESTRING (0 0, 1 1)"), 1, TRUE)
st_interpolate_line = function(line, dist, normalized = FALSE) {
#' st_line_interpolate(st_as_sfc("LINESTRING (0 0, 1 1)"), 1)
#' st_line_interpolate(st_as_sfc("LINESTRING (0 0, 1 1)"), 1, TRUE)
st_line_interpolate = function(line, dist, normalized = FALSE) {
stopifnot(inherits(line, "sfc"), all(st_dimension(line) == 1),
is.logical(normalized), length(normalized) == 1,
is.numeric(dist))
if (isTRUE(st_is_longlat(line)))
message_longlat("st_project_point")
line = st_cast(line, "LINESTRING")
recycled = recycle_common(list(line, dist))
st_sfc(CPL_interpolate_line(recycled[[1]], recycled[[2]], normalized),
st_sfc(CPL_line_interpolate(recycled[[1]], recycled[[2]], normalized),
crs = st_crs(line))
}
32 changes: 0 additions & 32 deletions man/st_project_point.Rd

This file was deleted.

20 changes: 10 additions & 10 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,29 +880,29 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// CPL_project_point
Rcpp::NumericVector CPL_project_point(Rcpp::List lines, Rcpp::List points, bool normalized);
RcppExport SEXP _sf_CPL_project_point(SEXP linesSEXP, SEXP pointsSEXP, SEXP normalizedSEXP) {
// CPL_line_project
Rcpp::NumericVector CPL_line_project(Rcpp::List lines, Rcpp::List points, bool normalized);
RcppExport SEXP _sf_CPL_line_project(SEXP linesSEXP, SEXP pointsSEXP, SEXP normalizedSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::List >::type lines(linesSEXP);
Rcpp::traits::input_parameter< Rcpp::List >::type points(pointsSEXP);
Rcpp::traits::input_parameter< bool >::type normalized(normalizedSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_project_point(lines, points, normalized));
rcpp_result_gen = Rcpp::wrap(CPL_line_project(lines, points, normalized));
return rcpp_result_gen;
END_RCPP
}
// CPL_interpolate_line
Rcpp::List CPL_interpolate_line(Rcpp::List lines, Rcpp::NumericVector dists, bool normalized);
RcppExport SEXP _sf_CPL_interpolate_line(SEXP linesSEXP, SEXP distsSEXP, SEXP normalizedSEXP) {
// CPL_line_interpolate
Rcpp::List CPL_line_interpolate(Rcpp::List lines, Rcpp::NumericVector dists, bool normalized);
RcppExport SEXP _sf_CPL_line_interpolate(SEXP linesSEXP, SEXP distsSEXP, SEXP normalizedSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::List >::type lines(linesSEXP);
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type dists(distsSEXP);
Rcpp::traits::input_parameter< bool >::type normalized(normalizedSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_interpolate_line(lines, dists, normalized));
rcpp_result_gen = Rcpp::wrap(CPL_line_interpolate(lines, dists, normalized));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -1526,8 +1526,8 @@ static const R_CallMethodDef CallEntries[] = {
{"_sf_CPL_transpose_sparse_incidence", (DL_FUNC) &_sf_CPL_transpose_sparse_incidence, 2},
{"_sf_CPL_nary_difference", (DL_FUNC) &_sf_CPL_nary_difference, 1},
{"_sf_CPL_nary_intersection", (DL_FUNC) &_sf_CPL_nary_intersection, 1},
{"_sf_CPL_project_point", (DL_FUNC) &_sf_CPL_project_point, 3},
{"_sf_CPL_interpolate_line", (DL_FUNC) &_sf_CPL_interpolate_line, 3},
{"_sf_CPL_line_project", (DL_FUNC) &_sf_CPL_line_project, 3},
{"_sf_CPL_line_interpolate", (DL_FUNC) &_sf_CPL_line_interpolate, 3},
{"_sf_CPL_hex_to_raw", (DL_FUNC) &_sf_CPL_hex_to_raw, 1},
{"_sf_CPL_raw_to_hex", (DL_FUNC) &_sf_CPL_raw_to_hex, 1},
{"_sf_CPL_read_mdim", (DL_FUNC) &_sf_CPL_read_mdim, 8},
Expand Down
4 changes: 2 additions & 2 deletions src/geos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ Rcpp::List CPL_nary_intersection(Rcpp::List sfc) {
}

// [[Rcpp::export]]
Rcpp::NumericVector CPL_project_point(Rcpp::List lines, Rcpp::List points, bool normalized) {
Rcpp::NumericVector CPL_line_project(Rcpp::List lines, Rcpp::List points, bool normalized) {
GEOSContextHandle_t hGEOSCtxt = CPL_geos_init();
int dim = 2;
std::vector<GeomPtr> l = geometries_from_sfc(hGEOSCtxt, lines, &dim);
Expand All @@ -1397,7 +1397,7 @@ Rcpp::NumericVector CPL_project_point(Rcpp::List lines, Rcpp::List points, bool
}

// [[Rcpp::export]]
Rcpp::List CPL_interpolate_line(Rcpp::List lines, Rcpp::NumericVector dists, bool normalized) {
Rcpp::List CPL_line_interpolate(Rcpp::List lines, Rcpp::NumericVector dists, bool normalized) {
GEOSContextHandle_t hGEOSCtxt = CPL_geos_init();
int dim = 2;
std::vector<GeomPtr> l = geometries_from_sfc(hGEOSCtxt, lines, &dim);
Expand Down

0 comments on commit 1cc62fb

Please sign in to comment.