Skip to content

Commit

Permalink
initial commit, working towards WKT2 support; #1146
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Nov 29, 2019
1 parent 097229a commit 245e30f
Show file tree
Hide file tree
Showing 33 changed files with 592 additions and 257 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Authors@R:
role = "ctb",
comment = c(ORCID = "0000-0002-4035-0289")),
person(given = "Kirill",
family = "Müller",
family = "M<U+00FC>ller",
role = "ctb"),
person(given = "Thomas Lin",
family = "Pedersen",
Expand Down Expand Up @@ -99,7 +99,7 @@ LinkingTo:
VignetteBuilder:
knitr
Encoding: UTF-8
RoxygenNote: 6.1.1
RoxygenNote: 7.0.1
SystemRequirements: C++11, GDAL (>= 2.0.1), GEOS (>= 3.4.0),
PROJ (>= 4.8.0)
Collate:
Expand Down
12 changes: 8 additions & 4 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ CPL_gdal_version <- function(what = "RELEASE_NAME") {
.Call('_sf_CPL_gdal_version', PACKAGE = 'sf', what)
}

CPL_crs_parameters <- function(p4s) {
.Call('_sf_CPL_crs_parameters', PACKAGE = 'sf', p4s)
CPL_crs_parameters <- function(crs) {
.Call('_sf_CPL_crs_parameters', PACKAGE = 'sf', crs)
}

CPL_wkt2_from_epsg <- function(epsg) {
.Call('_sf_CPL_wkt2_from_epsg', PACKAGE = 'sf', epsg)
}

CPL_crs_equivalent <- function(crs1, crs2) {
Expand Down Expand Up @@ -53,8 +57,8 @@ CPL_curve_to_linestring <- function(sfc) {
.Call('_sf_CPL_curve_to_linestring', PACKAGE = 'sf', sfc)
}

CPL_transform <- function(sfc, proj4) {
.Call('_sf_CPL_transform', PACKAGE = 'sf', sfc, proj4)
CPL_transform <- function(sfc, crs) {
.Call('_sf_CPL_transform', PACKAGE = 'sf', sfc, crs)
}

CPL_wrap_dateline <- function(sfc, opt, quiet = TRUE) {
Expand Down
37 changes: 23 additions & 14 deletions R/crs.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Ops.crs <- function(e1, e2) {
TRUE
else if (is.na(e1) || is.na(e2)) # only one of them is NA_crs_
FALSE
else CPL_crs_equivalent(e1$proj4string, e2$proj4string) # use GDAL's srs1->IsSame(srs2)
else CPL_crs_equivalent(e1, e2) # use GDAL's srs1->IsSame(srs2)
}
}

Expand All @@ -43,10 +43,6 @@ Ops.crs <- function(e1, e2) {
#' @details The *crs functions create, get, set or replace the \code{crs} attribute of a simple feature geometry
#' list-column. This attribute is of class \code{crs}, and is a list consisting of \code{epsg} (integer EPSG
#' code) and \code{proj4string} (character).
#' Two objects of class \code{crs} are semantically identical when: (1) they are completely identical, or
#' (2) they have identical proj4string but one of them has a missing EPSG ID. As a consequence, equivalent
#' but different proj4strings, e.g. \code{ "+proj=longlat +datum=WGS84" } and \code{ "+datum=WGS84 +proj=longlat" },
#' are considered different.
#' The operators \code{==} and \code{!=} are overloaded for \code{crs} objects to establish semantical identity.
#' @return Object of class \code{crs}, which is a list with elements \code{epsg} (length-1 integer) and
#' \code{proj4string} (length-1 character).
Expand Down Expand Up @@ -145,8 +141,12 @@ valid_proj4string = function(p4s) {
# return crs object from crs, integer, or character string
make_crs = function(x, wkt = FALSE) {

if (inherits(x, "CRS"))
x = x@projargs
if (inherits(x, "CRS")) {
x = if (!is.null(comment(x)))
comment(x) # WKT2
else
x@projargs
}

if (wkt)
CPL_crs_from_wkt(x)
Expand Down Expand Up @@ -255,7 +255,7 @@ udunits_from_proj = list(

crs_parameters = function(x) {
stopifnot(!is.na(x))
ret = structure(CPL_crs_parameters(x$proj4string),
ret = structure(CPL_crs_parameters(x),
names = c("SemiMajor", "SemiMinor", "InvFlattening", "units_gdal",
"IsVertical", "WktPretty", "Wkt"))
units(ret$SemiMajor) = as_units("m")
Expand Down Expand Up @@ -288,7 +288,11 @@ st_as_text.crs = function(x, ..., pretty = FALSE) {
#' @details
#' \code{NA_crs_} is the \code{crs} object with missing values for \code{epsg} and \code{proj4string}.
#' @export
NA_crs_ = structure(list(epsg = NA_integer_, proj4string = NA_character_), class = "crs")
NA_crs_ = structure(
list(epsg = NA_integer_,
proj4string = NA_character_,
wkt2 = NA_character_),
class = "crs")

#' @name st_crs
#' @export
Expand Down Expand Up @@ -326,25 +330,30 @@ print.crs = function(x, ...) {
cat(" NA\n")
} else {
cat("\n")
# print EPSG code:
if (is.na(x$epsg))
cat(" No EPSG code\n")
else
cat(" EPSG:", x$epsg, "\n")
if (is.na(x$proj4string))
stop(" invalid crs: please report an issue") # nocov
else

# print proj4string:
if (! is.na(x$proj4string))
cat(" proj4string: \"", x$proj4string, "\"\n", sep = "")

# print wkt2:
if (! is.na(x$wkt2))
cat(" wkt2:\n", x$wkt2, "\n", sep = "")
}
}

#' @export
st_crs.Raster = function(x, ...) {
st_crs(x@crs@projargs) # nocov
st_crs(x@crs) # nocov
}

#' @export
st_crs.Spatial = function(x, ...) {
if (! requireNamespace("sp", quietly = TRUE))
stop("package sp required, please install it first")
st_crs(sp::proj4string(x)) # nocov
st_crs(x@proj4string) # nocov
}
3 changes: 2 additions & 1 deletion R/transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ st_transform.sfc = function(x, crs, ..., partial = TRUE, check = FALSE, use_gdal

crs = make_crs(crs)

# FIXME: check for wkt2 here too?
if (grepl("+proj=geocent", crs$proj4string) && length(x) && Dimension(x[[1]]) == "XY") # add z:
x = st_zm(x, drop = FALSE, what = "Z")

Expand All @@ -86,7 +87,7 @@ st_transform.sfc = function(x, crs, ..., partial = TRUE, check = FALSE, use_gdal
}

if (crs != st_crs(x)) { # transform:
ret = structure(CPL_transform(x, crs$proj4string),
ret = structure(CPL_transform(x, crs),
single_type = NULL, crs = crs)
ret = st_sfc(ret)
if (check)
Expand Down
11 changes: 9 additions & 2 deletions man/aggregate.sf.Rd

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

8 changes: 4 additions & 4 deletions man/coerce-methods.Rd

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

1 change: 0 additions & 1 deletion man/dbDataType.Rd

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

54 changes: 40 additions & 14 deletions man/gdal.Rd

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

12 changes: 9 additions & 3 deletions man/gdal_utils.Rd

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

11 changes: 9 additions & 2 deletions man/geos_measures.Rd

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

10 changes: 8 additions & 2 deletions man/geos_unary.Rd

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

Loading

0 comments on commit 245e30f

Please sign in to comment.