Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind st_astext from lwgeom #25

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
src/*.o
src/*.so
src/*.dll
18 changes: 17 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,21 @@ License: GPL-2
Copyright: file COPYRIGHTS
URL: https://github.com/r-spatial/lwgeom/
BugReports: https://github.com/r-spatial/lwgeom/issues/
Collate: init.R RcppExports.R geohash.R split.R subdivide.R valid.R transform.R bounding_circle.R bearing.R snap_to_grid.R twkb.R perimeter.R clockwise.R geod.R startpoint.R
Collate:
init.R
RcppExports.R
geohash.R
split.R
subdivide.R
valid.R
transform.R
bounding_circle.R
bearing.R
snap_to_grid.R
startpoint.R
twkb.R
perimeter.R
clockwise.R
geod.R
wkt.R
RoxygenNote: 6.1.0
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 @@ -85,6 +85,10 @@ CPL_startpoint <- function(sfc) {
.Call('_lwgeom_CPL_startpoint', 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)
}
20 changes: 20 additions & 0 deletions lwgeom.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
35 changes: 35 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 @@ -257,6 +257,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 @@ -305,6 +317,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_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_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
11 changes: 11 additions & 0 deletions src/lwgeom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,14 @@ Rcpp::NumericMatrix CPL_startpoint(Rcpp::List sfc) {
// next step: get it into sf form
// 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_asewkt(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)
})