Skip to content

Commit 393e71c

Browse files
committed
adjust examples and tests for stream reading
1 parent 7a36142 commit 393e71c

File tree

8 files changed

+36
-30
lines changed

8 files changed

+36
-30
lines changed

R/cast_sfg.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ClosePol <- function(mtrx) {
4343
#' @examples
4444
#' # example(st_read)
4545
#' nc = st_read(system.file("shape/nc.shp", package="sf"))
46-
#' mpl <- nc$geometry[[4]]
46+
#' mpl <- st_geometry(nc)[[4]]
4747
#' #st_cast(x) ## error 'argument "to" is missing, with no default'
4848
#' cast_all <- function(xg) {
4949
#' lapply(c("MULTIPOLYGON", "MULTILINESTRING", "MULTIPOINT", "POLYGON", "LINESTRING", "POINT"),
@@ -81,7 +81,7 @@ st_cast.MULTIPOLYGON <- function(x, to, ...) {
8181
#' @name st_cast
8282
#' @export
8383
#' @examples
84-
#' mls <- st_cast(nc$geometry[[4]], "MULTILINESTRING")
84+
#' mls <- st_cast(st_geometry(nc)[[4]], "MULTILINESTRING")
8585
#' st_sfc(cast_all(mls))
8686
st_cast.MULTILINESTRING <- function(x, to, ...) {
8787
switch(to,
@@ -108,7 +108,7 @@ st_cast.MULTILINESTRING <- function(x, to, ...) {
108108
#' @name st_cast
109109
#' @export
110110
#' @examples
111-
#' mpt <- st_cast(nc$geometry[[4]], "MULTIPOINT")
111+
#' mpt <- st_cast(st_geometry(nc)[[4]], "MULTIPOINT")
112112
#' st_sfc(cast_all(mpt))
113113
st_cast.MULTIPOINT <- function(x, to, ...) {
114114
switch(to,
@@ -135,7 +135,7 @@ st_cast.MULTIPOINT <- function(x, to, ...) {
135135
#' @name st_cast
136136
#' @export
137137
#' @examples
138-
#' pl <- st_cast(nc$geometry[[4]], "POLYGON")
138+
#' pl <- st_cast(st_geometry(nc)[[4]], "POLYGON")
139139
#' st_sfc(cast_all(pl))
140140
st_cast.POLYGON <- function(x, to, ...) {
141141
switch(to,
@@ -156,7 +156,7 @@ st_cast.POLYGON <- function(x, to, ...) {
156156
#' @name st_cast
157157
#' @export
158158
#' @examples
159-
#' ls <- st_cast(nc$geometry[[4]], "LINESTRING")
159+
#' ls <- st_cast(st_geometry(nc)[[4]], "LINESTRING")
160160
#' st_sfc(cast_all(ls))
161161
st_cast.LINESTRING <- function(x, to, ...) {
162162
switch(to,
@@ -173,7 +173,7 @@ st_cast.LINESTRING <- function(x, to, ...) {
173173
#' @name st_cast
174174
#' @export
175175
#' @examples
176-
#' pt <- st_cast(nc$geometry[[4]], "POINT")
176+
#' pt <- st_cast(st_geometry(nc)[[4]], "POINT")
177177
#' ## st_sfc(cast_all(pt)) ## Error: cannot create MULTIPOLYGON from POINT
178178
#' st_sfc(lapply(c("POINT", "MULTIPOINT"), function(x) st_cast(pt, x)))
179179
st_cast.POINT <- function(x, to, ...) {

R/read.R

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ process_cpl_read_ogr_stream = function(x, default_crs, num_features, fid_column_
238238
st_set_crs(x, crs)
239239
})
240240

241-
# Prefer "geometry" as the geometry column name
242-
if (any(is_geometry_column) && !("geometry" %in% names(df))) {
243-
names(df)[which(is_geometry_column)[1]] = "geometry"
244-
}
241+
# # Prefer "geometry" as the geometry column name
242+
# if (any(is_geometry_column) && !("geometry" %in% names(df))) {
243+
# names(df)[which(is_geometry_column)[1]] = "geometry"
244+
# }
245245

246246
# Rename OGC_FID to fid_column_name and move to end
247247
if (length(fid_column_name) == 1 && "OGC_FID" %in% names(df)) {
@@ -250,9 +250,11 @@ process_cpl_read_ogr_stream = function(x, default_crs, num_features, fid_column_
250250
}
251251

252252
# Move geometry to the end
253-
if ("geometry" %in% names(df)) {
254-
df <- df[c(setdiff(names(df), "geometry"), "geometry")]
255-
}
253+
# if ("geometry" %in% names(df)) {
254+
# df <- df[c(setdiff(names(df), "geometry"), "geometry")]
255+
# }
256+
gc1 = which(is_geometry_column)[1]
257+
df = df[c(setdiff(seq_along(df), gc1), gc1)]
256258

257259
process_cpl_read_ogr(df, ...)
258260
}

R/tidyverse.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ mutate.sf <- function(.data, ..., .dots) {
129129
#' @name tidyverse
130130
#' @examples
131131
#' if (require(dplyr, quietly = TRUE)) {
132-
#' nc %>% transmute(AREA = AREA/10, geometry = geometry) %>% class()
133132
#' nc %>% transmute(AREA = AREA/10) %>% class()
134133
#' }
135134
transmute.sf <- function(.data, ..., .dots) {
@@ -144,9 +143,7 @@ transmute.sf <- function(.data, ..., .dots) {
144143
#' @examples
145144
#' if (require(dplyr, quietly = TRUE)) {
146145
#' nc %>% select(SID74, SID79) %>% names()
147-
#' nc %>% select(SID74, SID79, geometry) %>% names()
148146
#' nc %>% select(SID74, SID79) %>% class()
149-
#' nc %>% select(SID74, SID79, geometry) %>% class()
150147
#' }
151148
#' @details \code{select} keeps the geometry regardless whether it is selected or not; to deselect it, first pipe through \code{as.data.frame} to let dplyr's own \code{select} drop it.
152149
select.sf <- function(.data, ...) {
@@ -391,7 +388,7 @@ distinct.sf <- function(.data, ..., .keep_all = FALSE) {
391388
#' @param na.rm see original function docs
392389
#' @param factor_key see original function docs
393390
#' @examples
394-
#' if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) {
391+
#' if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE) && "geometry" %in% names(nc)) {
395392
#' nc %>% select(SID74, SID79) %>% gather("VAR", "SID", -geometry) %>% summary()
396393
#' }
397394
gather.sf <- function(data, key, value, ..., na.rm = FALSE, convert = FALSE, factor_key = FALSE) {
@@ -527,7 +524,7 @@ pivot_wider.sf = function(data,
527524
#' @param fill see original function docs
528525
#' @param drop see original function docs
529526
#' @examples
530-
#' if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE)) {
527+
#' if (require(tidyr, quietly = TRUE) && require(dplyr, quietly = TRUE) && "geometry" %in% names(nc)) {
531528
#' nc$row = 1:100 # needed for spread to work
532529
#' nc %>% select(SID74, SID79, geometry, row) %>%
533530
#' gather("VAR", "SID", -geometry, -row) %>%

R/transform.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ chk_mpol = function(x) {
2525
sanity_check = function(x) {
2626
d = st_dimension(x) # flags empty geoms as NA
2727
if (any(d == 2, na.rm = TRUE)) { # the polygon stuff
28+
x = st_cast(x[d == 2]) # convert GEOMETRY to POLYGON or MULTIPOLYGON, if possible
2829
if (inherits(x, "sfc_POLYGON"))
2930
st_sfc(lapply(x, chk_pol), crs = st_crs(x))
3031
else if (inherits(x, "sfc_MULTIPOLYGON"))

man/st_cast.Rd

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tidyverse.Rd

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/dplyr.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ if (require(dplyr, quietly = TRUE)) {
44
options(dplyr.summarise.inform=FALSE)
55
read_sf(system.file("shape/nc.shp", package="sf"), quiet = TRUE) %>%
66
st_transform(3857) -> nc
7+
if ((gc <- attr(nc, "sf_column")) != "geometry") {
8+
nc$geometry = st_geometry(nc)
9+
nc = as.data.frame(nc)
10+
nc[gc] = NULL
11+
nc = st_as_sf(nc)
12+
}
13+
nc = st_as_sf(nc, sf_column_name = "geometry")
714
nc %>% filter(AREA > .1) %>% plot()
815

916
# plot 10 smallest counties in grey:

tests/plot.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ plot(nc)
7171
plot(nc, axes = TRUE)
7272
plot(nc, col="lightgrey")
7373
plot(st_centroid(nc), add = TRUE, col = 1)
74-
nc %>%
74+
if ("geometry" %in% names(nc)) {
75+
nc %>%
7576
select(geometry) %>%
7677
plot()
78+
}
7779

7880
nc$f = cut(nc[[1]], 5)
7981
plot(nc["f"], key.pos = 1)

0 commit comments

Comments
 (0)