Skip to content

Commit

Permalink
Merge pull request #22 from Robinlovelace/startpoint
Browse files Browse the repository at this point in the history
Add st_startpoint
  • Loading branch information
edzer authored Aug 21, 2018
2 parents 7fa2e36 + c1a31bd commit caa3ebe
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ appveyor.yml
codecov.yml
^docs$
tic.R
^.*\.Rproj$
^\.Rproj\.user$
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ Collate:
perimeter.R
clockwise.R
geod.R
startpoint.R
RoxygenNote: 6.0.1
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export(st_perimeter)
export(st_perimeter_2d)
export(st_snap_to_grid)
export(st_split)
export(st_startpoint)
export(st_subdivide)
export(st_transform_proj)
import(sf)
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ CPL_force_polygon_cw <- function(sfc) {
.Call('_lwgeom_CPL_force_polygon_cw', PACKAGE = 'lwgeom', sfc)
}

CPL_startpoint <- function(sfc) {
.Call('_lwgeom_CPL_startpoint', PACKAGE = 'lwgeom', sfc)
}

CPL_proj_version <- function(b = FALSE) {
.Call('_lwgeom_CPL_proj_version', PACKAGE = 'lwgeom', b)
}
Expand Down
17 changes: 17 additions & 0 deletions R/startpoint.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#' Return the start and end points from lines
#'
#' @param x line of class \code{sf}, \code{sfc} or \code{sfg}
#' @export
#' @details see \url{https://postgis.net/docs/ST_StartPoint.html} and \url{https://postgis.net/docs/ST_EndPoint.html}.
#' @return \code{sf} object representing start and end points
#' @examples
#' library(sf)
#' m = matrix(c(0, 1, 2, 0, 1, 4), ncol = 2)
#' l = st_sfc(st_linestring(m))
#' lwgeom::st_startpoint(l)
#' l2 = st_sfc(st_linestring(m), st_linestring(m[3:1, ]))
#' lwgeom::st_startpoint(l2)
st_startpoint = function(x) {
m <- CPL_startpoint(st_geometry(x))
st_sfc(lapply(seq_len(nrow(m)), function(i) st_point(m[i,])), crs = st_crs(x))
}
28 changes: 28 additions & 0 deletions man/st_startpoint.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// CPL_startpoint
Rcpp::NumericMatrix CPL_startpoint(Rcpp::List sfc);
RcppExport SEXP _lwgeom_CPL_startpoint(SEXP sfcSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::List >::type sfc(sfcSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_startpoint(sfc));
return rcpp_result_gen;
END_RCPP
}
// CPL_proj_version
std::string CPL_proj_version(bool b);
RcppExport SEXP _lwgeom_CPL_proj_version(SEXP bSEXP) {
Expand Down Expand Up @@ -293,6 +304,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_lwgeom_CPL_perimeter", (DL_FUNC) &_lwgeom_CPL_perimeter, 2},
{"_lwgeom_CPL_is_polygon_cw", (DL_FUNC) &_lwgeom_CPL_is_polygon_cw, 1},
{"_lwgeom_CPL_force_polygon_cw", (DL_FUNC) &_lwgeom_CPL_force_polygon_cw, 1},
{"_lwgeom_CPL_startpoint", (DL_FUNC) &_lwgeom_CPL_startpoint, 1},
{"_lwgeom_CPL_proj_version", (DL_FUNC) &_lwgeom_CPL_proj_version, 1},
{"_lwgeom_CPL_linesubstring", (DL_FUNC) &_lwgeom_CPL_linesubstring, 4},
{NULL, NULL, 0}
Expand Down
18 changes: 18 additions & 0 deletions src/lwgeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,21 @@ Rcpp::List CPL_force_polygon_cw(Rcpp::List sfc) {
}
return sfc_from_lwgeom(lwgeom_cw);
}

// [[Rcpp::export]]
Rcpp::NumericMatrix CPL_startpoint(Rcpp::List sfc) {

std::vector<LWGEOM *> lwgeom_cw = lwgeom_from_sfc(sfc);
Rcpp::NumericMatrix m(lwgeom_cw.size(), 2);

POINT4D p;
for (size_t i = 0; i < lwgeom_cw.size(); i++) {
lwgeom_startpoint(lwgeom_cw[i], &p);
m(i, 0) = p.x;
m(i, 1) = p.y;
}

return m;
// next step: get it into sf form
// return sfc_from_lwgeom(lwgeom_cw);
}

0 comments on commit caa3ebe

Please sign in to comment.