Skip to content

Commit

Permalink
Merge pull request #34 from munterfinger/release/v0.2.0
Browse files Browse the repository at this point in the history
Release/v0.2.0
  • Loading branch information
munterfi authored Dec 19, 2020
2 parents 1ef6ee5 + 727fc7c commit 4f4ceec
Show file tree
Hide file tree
Showing 23 changed files with 498 additions and 243 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^docs$
^pkgdown$
^codecov\.yml$
^.vscode$
9 changes: 5 additions & 4 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
on:
push:
branches:
- refs/tags/*
release:
types: [created, edited]

name: pkgdown

Expand All @@ -24,7 +23,9 @@ jobs:
libgdal-dev \
libgeos-dev \
libproj-dev \
libcurl4-openssl-dev
libcurl4-openssl-dev \
libharfbuzz-dev \
libfribidi-dev
- name: Query dependencies
run: |
install.packages('remotes')
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ vignettes/*.pdf
*.DS_Store
inst/doc
docs

# vscode
.vscode
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: flexpolyline
Type: Package
Title: Flexible Polyline Encoding
Version: 0.1.1.9000
Version: 0.2.0
Authors@R:
c(person(given = "Merlin",
family = "Unterfinger",
Expand All @@ -20,8 +20,8 @@ Description: Binding to the C++ implementation of the flexible polyline
(3) using variable length for each coordinate delta; and
(4) using 64 URL-safe characters to display the result.
License: GPL-3
URL: https://munterfinger.github.io/flexpolyline, https://github.com/munterfinger/flexpolyline
BugReports: https://github.com/munterfinger/flexpolyline/issues
URL: https://munterfinger.github.io/flexpolyline/, https://github.com/munterfinger/flexpolyline/
BugReports: https://github.com/munterfinger/flexpolyline/issues/
LinkingTo:
Rcpp
Imports:
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export(decode)
export(decode_sf)
export(encode)
export(encode_sf)
export(get_third_dimension)
export(set_third_dimension)
importFrom(Rcpp,sourceCpp)
useDynLib(flexpolyline, .registration = TRUE)
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# flexpolyline 0.1.1.9000
# flexpolyline 0.2.0

* Fix clang range-loop-analysis warning on macOS in `flexpolyline.h` (Apple clang version 12.0.0).
* Support for geometry types `"POLYGON"` and `"POINT"` in `encode_sf()` and `decode_sf()`, closes #31.
* Added functions to get (`get_third_dimension()`) and set (`set_third_dimension()`) the third dimension type of a flexible polyline encoded string.
* Sign in to CodeFactor.io and add badge to continuously track code quality.
* Use exception classes when throwing an exception in C++.
* Improve coverage of tests.
Expand Down
75 changes: 68 additions & 7 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@

#' Decode a flexible polyline encoded string
#'
#' This function calls \code{hf::polyline_decode} and \code{hf::get_third_dimension}
#' of the C++ implementation of the flexible polyline encoding by HERE. Depending
#' on the dimensions of the encoded line, a two or three dimensional line is decoded.
#' This function calls \code{hf::polyline_decode} and
#' \code{hf::get_third_dimension} of the C++ implementation of the flexible
#' polyline encoding by HERE. Depending on the dimensions of the encoded line,
#' a two or three dimensional line is decoded.
#'
#' @param encoded character, encoded flexible polyline string.
#'
Expand All @@ -30,10 +31,14 @@ decode <- function(encoded) {
#' the flexible polyline encoding by HERE. Depending on the dimensions of the
#' input coordinates, a two or three dimensional line is encoded.
#'
#' @param line matrix, coordinates of the line in 2d or 3d (column order: LNG, LAT, DIM3).
#' @param precision integer, precision to use in encoding (between 0 and 15, \code{default=5}).
#' @param third_dim integer, type of the third dimension (0: ABSENT, 1: LEVEL, 2: ALTITUDE, 3: ELEVATION, 4, 6: CUSTOM1, 7: CUSTOM2, \code{default=3}).
#' @param third_dim_precision integer, precision to use in encoding for the third dimension (between 1 and 15, \code{default=5}).
#' @param line matrix, coordinates of the line in 2d or 3d (column order: LNG,
#' LAT, DIM3).
#' @param precision integer, precision to use in encoding (between 0 and 15,
#' \code{default=5}).
#' @param third_dim integer, type of the third dimension (0: ABSENT, 1: LEVEL,
#' 2: ALTITUDE, 3: ELEVATION, 4, 6: CUSTOM1, 7: CUSTOM2, \code{default=3}).
#' @param third_dim_precision integer, precision to use in encoding for the
#' third dimension (between 1 and 15, \code{default=5}).
#'
#' @return
#' The line as string in the flexible polyline encoding format.
Expand Down Expand Up @@ -64,3 +69,59 @@ encode <- function(line, precision = 5L, third_dim = 3L, third_dim_precision = 5
.Call(`_flexpolyline_encode`, line, precision, third_dim, third_dim_precision)
}

#' Get third dimension of a flexible polyline encoded string
#'
#' This function calls \code{hf::get_third_dimension} of the C++ implementation
#' of the flexible polyline encoding by HERE and return the type of the third
#' dimension.
#'
#' @param encoded character, encoded flexible polyline string.
#'
#' @return
#' A string describing the third dimension.
#'
#' @export
#'
#' @examples
#' # 2d line
#' get_third_dimension("BFoz5xJ67i1B1B7PzIhaxL7Y")
#'
#' # 3d line
#' get_third_dimension("BlBoz5xJ67i1BU1B7PUzIhaUxL7YU")
get_third_dimension <- function(encoded) {
.Call(`_flexpolyline_get_third_dimension`, encoded)
}

#' Set third dimension of a flexible polyline encoded string
#'
#' This function decodes the flexible polyline encoded line, changes the third
#' dimension and encodes the line again.
#'
#' @note
#' The precision is not read from the header of the encoded line. Therefore it
#' must be provided as a parameter for re-encoding.
#'
#' @param encoded character, encoded flexible polyline string.
#' @param third_dim_name character, name of the third dimension to set (ABSENT,
#' LEVEL, ALTITUDE, ELEVATION, CUSTOM1, CUSTOM2).
#' @param precision integer, precision to use in encoding (between 0 and 15,
#' \code{default=5}).
#' @param third_dim_precision integer, precision to use in encoding for the
#' third dimension (between 1 and 15, \code{default=5}).
#'
#' @return
#' The line with the new third dimension as string in the flexible polyline
#' encoding format.
#'
#' @export
#'
#' @examples
#' # 2d line (nothing happens...)
#' set_third_dimension("BFoz5xJ67i1B1B7PzIhaxL7Y", "ELEVATION")
#'
#' # 3d line
#' set_third_dimension("BlBoz5xJ67i1BU1B7PUzIhaUxL7YU", "ELEVATION")
set_third_dimension <- function(encoded, third_dim_name, precision = 5L, third_dim_precision = 5L) {
.Call(`_flexpolyline_set_third_dimension`, encoded, third_dim_name, precision, third_dim_precision)
}

22 changes: 12 additions & 10 deletions R/decode_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@
#' @export
#'
#' @examples
#' # 2d line
#' decode_sf("BFoz5xJ67i1B1B7PzIhaxL7Y")
#'
#' # 3d line
#' decode_sf("BlBoz5xJ67i1BU1B7PUzIhaUxL7YU")
#' decode_sf("B1Voz5xJ67i1Bgkh9B")
#' decode_sf("BFoz5xJ67i1B1B7PlU9yB")
#' decode_sf("BlXoz5xJ67i1Bgkh9B1B7Pgkh9BzIhagkh9BqK-pB_ni6D")
decode_sf <- function(encoded, crs = sf::NA_crs_) {
UseMethod("decode_sf", encoded)
}

#' @export
decode_sf.character <- function(encoded, crs = sf::NA_crs_) {

dim3 <- character(length(encoded))
ind3 <- 2
sfdi <- "XY"

geom <- sf::st_sfc(
lapply(1:length(encoded), function(x) {
m <- decode(encoded[[x]])
Expand All @@ -51,13 +47,19 @@ decode_sf.character <- function(encoded, crs = sf::NA_crs_) {
sfdi <<- "XYM"
}
}
sf::st_linestring(m, dim = sfdi)
if (nrow(m) <= 1) {
sf::st_point(m, dim = sfdi)
} else {
if (all(m[1, ] == m[nrow(m), ])) {
sf::st_polygon(list(m), dim = sfdi)
} else {
sf::st_linestring(m, dim = sfdi)
}
}
}),
crs = crs
)

dim3[is.na(dim3)] <- "ABSENT"

return(
sf::st_as_sf(
data.frame(
Expand Down
66 changes: 31 additions & 35 deletions R/encode_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' A wrapper function for \code{\link{encode}} that converts simple feature geometries
#' of the sf package to flexible polyline encoded strings.
#'
#' @param line simple feature, \code{sf}, \code{sfc} or \code{sfg} object with geometry type \code{"LINESTRING"}.
#' @param geom simple feature, \code{sf}, \code{sfc} or \code{sfg} object with geometry type \code{"POINT"}, \code{"LINESTRING"} or \code{"POLYGON"}.
#' @param precision integer, precision to use in encoding (between 0 and 15, \code{default=5}).
#' @param third_dim integer, type of the third dimension (0: ABSENT, 1: LEVEL, 2: ALTITUDE, 3: ELEVATION, 4, 6: CUSTOM1, 7: CUSTOM2, \code{default=NULL}).
#' @param third_dim_precision integer, precision to use in encoding for the third dimension (between 1 and 15, \code{default=precision}).
Expand All @@ -14,83 +14,79 @@
#' @export
#'
#' @examples
#' # 2D
#' # 3D point
#' point3d <- sf::st_point(
#' matrix(c(8.69821, 50.10228, 10), ncol = 3, byrow = TRUE), dim = "XYZ")
#' encode_sf(point3d)
#'
#' # 2D linestring
#' line2d <- sf::st_linestring(
#' matrix(
#' c(8.69821, 50.10228,
#' 8.69567, 50.10201,
#' 8.69150, 50.10063,
#' 8.68752, 50.09878),
#' ncol = 2, byrow = TRUE
#' )
#' )
#' matrix(c(8.69821, 50.10228,
#' 8.69567, 50.10201,
#' 8.68752, 50.09878), ncol = 2, byrow = TRUE))
#' encode_sf(line2d)
#'
#' # 3D
#' line3d <- sf::st_linestring(
#' matrix(
#' c(8.69821, 50.10228, 10,
#' 8.69567, 50.10201, 20,
#' 8.69150, 50.10063, 30,
#' 8.68752, 50.09878, 40),
#' ncol = 3, byrow = TRUE
#' )
#' )
#' encode_sf(line3d)
encode_sf <- function(line, precision = 5, third_dim = NULL,
#' # 3D polygon
#' poly3d <- sf::st_polygon(list(
#' matrix(c(8.69821, 50.10228, 10,
#' 8.69567, 50.10201, 20,
#' 8.69150, 50.10063, 30,
#' 8.69821, 50.10228, 10), ncol = 3, byrow = TRUE)), dim = "XYM")
#' encode_sf(poly3d)
encode_sf <- function(geom, precision = 5, third_dim = NULL,
third_dim_precision = precision) {
UseMethod("encode_sf", line)
UseMethod("encode_sf", geom)
}

#' @export
encode_sf.sfg <- function(line, precision = 5, third_dim = NULL,
encode_sf.sfg <- function(geom, precision = 5, third_dim = NULL,
third_dim_precision = precision) {
if(sf::st_geometry_type(line) != "LINESTRING"){
if(!sf::st_geometry_type(geom) %in% c("POINT", "LINESTRING", "POLYGON")){
stop(
sprintf(
"Invalid geometry type '%s' of input, only 'LINESTRING' is supported.",
sf::st_geometry_type(line)
"Invalid geometry type '%s' of input, only 'POINT', 'LINESTRING' and 'POLYGON' is supported.",
sf::st_geometry_type(geom)
)
)
}
if (class(line)[1] == "XY") {
if (class(geom)[1] == "XY") {
third_dim <- 0
encoded <- encode(
sf::st_coordinates(line)[, c(1:2)],
sf::st_coordinates(geom)[, c(1:2), drop = FALSE],
precision, third_dim, third_dim_precision
)
} else {
if (is.null(third_dim)) {
if (class(line)[1] == "XYZ") {
if (class(geom)[1] == "XYZ") {
third_dim <- 3
} else {
third_dim <- 6
}
}
encoded <- encode(
sf::st_coordinates(line)[, c(1:3)],
sf::st_coordinates(geom)[, c(1:3), drop = FALSE],
precision, third_dim, third_dim_precision
)
}
return(encoded)
}

#' @export
encode_sf.sfc <- function(line, precision = 5, third_dim = NULL,
encode_sf.sfc <- function(geom, precision = 5, third_dim = NULL,
third_dim_precision = precision) {
return(
sapply(line, function(x) {
sapply(geom, function(x) {
encode_sf.sfg(x, precision, third_dim, third_dim_precision)
})
)
}

#' @export
encode_sf.sf <- function(line, precision = 5, third_dim = NULL,
encode_sf.sf <- function(geom, precision = 5, third_dim = NULL,
third_dim_precision = precision) {
return(
encode_sf.sfc(
sf::st_geometry(line),
sf::st_geometry(geom),
precision, third_dim, third_dim_precision
)
)
Expand Down
9 changes: 4 additions & 5 deletions inst/include/hf/flexpolyline.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copy from :https://github.com/heremaps/flexible-polyline/blob/master/cpp/
* Modified :Line 93, 211
* Modified :Line 93, 210, 278
* Date :2020-06-09, Merlin Unterfinger <[email protected]>
*
* Copyright (C) 2019 HERE Europe B.V.
Expand Down Expand Up @@ -201,7 +201,6 @@ class Encoder {
std::string get_encoded() {
return m_result;
}

};

class Decoder {
Expand Down Expand Up @@ -276,9 +275,9 @@ template<typename Iter>
std::string polyline_encode(Iter iter, int precision, ThirdDim third_dim, int third_dim_precision) {
auto enc = encoder::Encoder(precision, third_dim, third_dim_precision);

for (const auto item : iter) {
enc.add(&item);
}
for (const auto& item : iter) { // Mod: Return by reference to avoid clang (Apple clang version 12.0.0) range-loop-analysis
enc.add(&item); // warning on MacOS: "loop variable 'item' of type 'const std::__1::pair<double, double>'
} // creates a copy from type 'const std::__1::pair<double, double>'"

return enc.get_encoded();
}
Expand Down
7 changes: 4 additions & 3 deletions man/decode.Rd

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

8 changes: 3 additions & 5 deletions man/decode_sf.Rd

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

Loading

0 comments on commit 4f4ceec

Please sign in to comment.