Skip to content

Commit

Permalink
add WKT conversion
Browse files Browse the repository at this point in the history
* adds `st_astext` and `st_asewkb`
* enables `lwgeom_to_wkt` through `CPL_sfc_to_wkt`
  • Loading branch information
etiennebr committed Jul 18, 2018
1 parent 77f26cf commit 61b7f0b
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 0 deletions.
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
wkt.R
RoxygenNote: 6.0.1
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ S3method(st_transform_proj,sf)
S3method(st_transform_proj,sfc)
S3method(st_transform_proj,sfg)
export(lwgeom_extSoftVersion)
export(st_asewkt)
export(st_astext)
export(st_force_polygon_cw)
export(st_geod_area)
export(st_geod_azimuth)
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_sfc_to_wkt <- function(sfc, precision) {
.Call('_lwgeom_CPL_sfc_to_wkt', PACKAGE = 'lwgeom', sfc, precision)
}

CPL_proj_version <- function(b = FALSE) {
.Call('_lwgeom_CPL_proj_version', PACKAGE = 'lwgeom', b)
}
Expand Down
31 changes: 31 additions & 0 deletions R/wkt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#' Return Well-known Text representation of simple feature geometry
#'
#' Return Well-known Text representation of simple feature geometry or coordinate reference system
#' @param x object of class `sfg`, `sfc` or `crs``
#' @param precision number of digits to print
#' @param EWKT Use Postgis Enhanced WKT (includes srid)
#' @name st_astext
#' @details The returned WKT representation of simple feature geometry conforms to the
#' [simple features access](http://www.opengeospatial.org/standards/sfa) specification and extensions (if `EWKT = TRUE`),
#' [known as EWKT](http://postgis.net/docs/using_postgis_dbmanagement.html#EWKB_EWKT), supported by
#' PostGIS and other simple features implementations for addition of SRID to a WKT string.
#' @md
#' @examples
#' library(sf)
#' pt <- st_sfc(st_point(c(1.0002,2.3030303)), crs = 4326)
#' st_astext(pt, 3, TRUE)
#' @export
st_astext <- function(x, precision = 12, EWKT = FALSE) {
if (!EWKT) st_crs(x) <- NA_crs_
CPL_sfc_to_wkt(x, precision = precision)
}

#' @name st_astext
#' @inheritParams st_astext
#' @details `st_asewkt()` returns the Well-Known Text (WKT) representation of
#' the geometry with SRID meta data.
#' @md
#' @export
st_asewkt <- function(x, precision) {
st_astext(x, precision, EWKT = TRUE)
}
1 change: 1 addition & 0 deletions lwgeom.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ AutoAppendNewline: Yes
BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
36 changes: 36 additions & 0 deletions man/st_astext.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 @@ -246,6 +246,18 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// CPL_sfc_to_wkt
Rcpp::CharacterVector CPL_sfc_to_wkt(const Rcpp::List sfc, const int precision);
RcppExport SEXP _lwgeom_CPL_sfc_to_wkt(SEXP sfcSEXP, SEXP precisionSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const Rcpp::List >::type sfc(sfcSEXP);
Rcpp::traits::input_parameter< const int >::type precision(precisionSEXP);
rcpp_result_gen = Rcpp::wrap(CPL_sfc_to_wkt(sfc, precision));
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 +305,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_sfc_to_wkt", (DL_FUNC) &_lwgeom_CPL_sfc_to_wkt, 2},
{"_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
12 changes: 12 additions & 0 deletions src/lwgeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,15 @@ Rcpp::List CPL_force_polygon_cw(Rcpp::List sfc) {
}
return sfc_from_lwgeom(lwgeom_cw);
}

// [[Rcpp::export]]
Rcpp::CharacterVector CPL_sfc_to_wkt(const Rcpp::List sfc, const int precision) {
std::vector<LWGEOM *> lwgeom_cw = lwgeom_from_sfc(sfc);
size_t wkt_size = 0;
Rcpp::CharacterVector out;
for (size_t i = 0; i < lwgeom_cw.size(); i++) {
out.push_back(lwgeom_to_wkt(lwgeom_cw[i], WKT_EXTENDED, precision, &wkt_size));
}
return out;
}

21 changes: 21 additions & 0 deletions tests/testthat/test-as_text.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
context("test-as_text.R")
library(sf)

test_that("Prints Points", {
pt <- st_sfc(st_point(c(1.0002,2.3030303)), crs = 4326)
expect_equal(st_astext(pt, 3), "SRID=4326;POINT(1 2.3)")
expect_equal(st_astext(pt, 3, FALSE), "POINT(1 2.3)")
expect_equal(st_astext(pt, 4, FALSE), "POINT(1 2.303)")
expect_equal(st_astext(pt, 10, FALSE), "POINT(1.0002 2.3030303)")
})

test_that("Prints Polygons and Lines", {
pol <- st_sfc(st_polygon(list(
rbind(c(0,0),c(0.5,0),c(0.5,0.5),c(0.5,0),c(1,0),c(1,1),c(0,1),c(0,0))
)))
txt <- "POLYGON((0 0,0.5 0,0.5 0.5,0.5 0,1 0,1 1,0 1,0 0))"
expect_equal(st_astext(pol), txt)
ln <- st_cast(pol, "LINESTRING")
txt <- "LINESTRING(0 0,0.5 0,0.5 0.5,0.5 0,1 0,1 1,0 1,0 0)"
expect_equal(st_astext(ln), txt)
})

0 comments on commit 61b7f0b

Please sign in to comment.