Skip to content

Commit

Permalink
closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed May 11, 2024
1 parent 460c383 commit 2f943f5
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export(st_geod_distance)
export(st_geod_length)
export(st_geod_segmentize)
export(st_geohash)
export(st_geom_from_geohash)
export(st_is_polygon_cw)
export(st_linesubstring)
export(st_minimum_bounding_circle)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# version 0.2-16

* add `st_geom_from_geohash()`; #37

# version 0.2-15

* fix default for argument `digits` in `st_astext()`
Expand Down
4 changes: 4 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ CPL_geohash <- function(sfc, prec) {
.Call('_lwgeom_CPL_geohash', PACKAGE = 'lwgeom', sfc, prec)
}

CPL_bbox_from_geohash <- function(h, prec) {
.Call('_lwgeom_CPL_bbox_from_geohash', PACKAGE = 'lwgeom', h, prec)
}

CPL_lwgeom_transform <- function(sfc, p4s) {
.Call('_lwgeom_CPL_lwgeom_transform', PACKAGE = 'lwgeom', sfc, p4s)
}
Expand Down
19 changes: 19 additions & 0 deletions R/geohash.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,22 @@
st_geohash = function(x, precision = 0) {
CPL_geohash(st_geometry(x), precision)
}

#' @export
#' @name st_geohash
#' @param hash character vector with geohashes
#' @param crs object of class `crs`
#' @examples
#' o = options(digits = 20)
#' st_geom_from_geohash(c('9qqj7nmxncgyy4d0dbxqz0', 'u1hzz631zyd63zwsd7zt'))
#' st_geom_from_geohash('9qqj7nmxncgyy4d0dbxqz0', 4)
#' st_geom_from_geohash('9qqj7nmxncgyy4d0dbxqz0', 10)
#' options(o)
st_geom_from_geohash = function(hash, precision = -1, crs = st_crs('OGC:CRS84')) {
stopifnot(is.character(hash), is.numeric(precision), length(precision) == 1)
m = CPL_bbox_from_geohash(hash, as.integer(precision))
bb = matrix(m, nrow = 4)
rownames(bb) = c("xmin", "ymin", "xmax", "ymax")
bb = apply(bb, 2, sf::st_bbox, simplify = FALSE)
sf::st_set_crs(do.call(c, lapply(bb, sf::st_as_sfc)), crs)
}
2 changes: 1 addition & 1 deletion configure
Original file line number Diff line number Diff line change
Expand Up @@ -3978,7 +3978,7 @@ OBJECTS="${OBJECTS} \$(OBJECTS_LIBLWGEOM)"
# Must keep the leading ${CPPFLAGS} or the previous CPPFLAGS don't get saved
PKG_CPPFLAGS="${CPPFLAGS} ${PKG_CPPFLAGS} -I./liblwgeom -DHAVE_LIBGEOM_INTERNAL_H"
PKG_CPPFLAGS="${CPPFLAGS} ${PKG_CPPFLAGS} -I./liblwgeom -DHAVE_LIBLWGEOM_INTERNAL_H"
#
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ AC_CHECK_LIB(geos_c, GEOS_init_r,
AC_SUBST([OBJECTS], ["${OBJECTS} \$(OBJECTS_LIBLWGEOM)"])

# Must keep the leading ${CPPFLAGS} or the previous CPPFLAGS don't get saved
AC_SUBST([PKG_CPPFLAGS], ["${CPPFLAGS} ${PKG_CPPFLAGS} -I./liblwgeom -DHAVE_LIBGEOM_INTERNAL_H"])
AC_SUBST([PKG_CPPFLAGS], ["${CPPFLAGS} ${PKG_CPPFLAGS} -I./liblwgeom -DHAVE_LIBLWGEOM_INTERNAL_H"])

#
# concluding substitution
Expand Down
12 changes: 12 additions & 0 deletions man/st_geohash.Rd

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

13 changes: 13 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// CPL_bbox_from_geohash
Rcpp::NumericVector CPL_bbox_from_geohash(Rcpp::CharacterVector h, int prec);
RcppExport SEXP _lwgeom_CPL_bbox_from_geohash(SEXP hSEXP, SEXP precSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::CharacterVector >::type h(hSEXP);
Rcpp::traits::input_parameter< int >::type prec(precSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_bbox_from_geohash(h, prec));
return rcpp_result_gen;
END_RCPP
}
// CPL_lwgeom_transform
Rcpp::List CPL_lwgeom_transform(Rcpp::List sfc, Rcpp::CharacterVector p4s);
RcppExport SEXP _lwgeom_CPL_lwgeom_transform(SEXP sfcSEXP, SEXP p4sSEXP) {
Expand Down Expand Up @@ -361,6 +373,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_lwgeom_CPL_split", (DL_FUNC) &_lwgeom_CPL_split, 2},
{"_lwgeom_CPL_wrap_x", (DL_FUNC) &_lwgeom_CPL_wrap_x, 3},
{"_lwgeom_CPL_geohash", (DL_FUNC) &_lwgeom_CPL_geohash, 2},
{"_lwgeom_CPL_bbox_from_geohash", (DL_FUNC) &_lwgeom_CPL_bbox_from_geohash, 2},
{"_lwgeom_CPL_lwgeom_transform", (DL_FUNC) &_lwgeom_CPL_lwgeom_transform, 2},
{"_lwgeom_CPL_minimum_bounding_circle", (DL_FUNC) &_lwgeom_CPL_minimum_bounding_circle, 1},
{"_lwgeom_CPL_subdivide", (DL_FUNC) &_lwgeom_CPL_subdivide, 2},
Expand Down
18 changes: 18 additions & 0 deletions src/lwgeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,24 @@ Rcpp::CharacterVector CPL_geohash(Rcpp::List sfc, int prec) {
return chr;
}

// [[Rcpp::export]]
Rcpp::NumericVector CPL_bbox_from_geohash(Rcpp::CharacterVector h, int prec) {

// void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
Rcpp::NumericVector ret(4 * h.size()); // return
double lat[2], lon[2];
char *hash;
for (size_t i = 0; i < h.size(); i++) {
hash = h[i];
decode_geohash_bbox(hash, lat, lon, prec);
ret[4 * i + 0] = lon[0];
ret[4 * i + 1] = lat[0];
ret[4 * i + 2] = lon[1];
ret[4 * i + 3] = lat[1];
}
return ret;
}

// [[Rcpp::export]]
Rcpp::List CPL_lwgeom_transform(Rcpp::List sfc, Rcpp::CharacterVector p4s) {
if (p4s.size() != 2)
Expand Down

0 comments on commit 2f943f5

Please sign in to comment.