Skip to content

Commit 7315d73

Browse files
authored
Better sf support (#3164)
1 parent c2d489c commit 7315d73

File tree

3 files changed

+41
-40
lines changed

3 files changed

+41
-40
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Suggests:
5252
rgeos,
5353
rmarkdown,
5454
rpart,
55-
sf (>= 0.3-4),
55+
sf (>= 0.7-3),
5656
svglite (>= 1.2.0.9001),
5757
testthat (>= 0.11.0),
5858
vdiffr (>= 0.3.0)

R/coord-sf.R

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,7 @@ sf_rescale01 <- function(x, x_range, y_range) {
376376
return(x)
377377
}
378378

379-
# Shift + affine transformation to rescale to [0, 1] x [0, 1]
380-
# Contributed by @edzer
381-
(x - c(x_range[1], y_range[1])) *
382-
diag(1 / c(diff(x_range), diff(y_range)))
379+
sf::st_normalize(x, c(x_range[1], y_range[1], x_range[2], y_range[2]))
383380
}
384381
sf_rescale01_x <- function(x, range) {
385382
(x - range[1]) / diff(range)

R/geom-sf.R

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,7 @@ GeomSf <- ggproto("GeomSf", Geom,
105105

106106
# Need to refactor this to generate one grob per geometry type
107107
coord <- coord$transform(data, panel_params)
108-
grobs <- lapply(1:nrow(data), function(i) {
109-
sf_grob(
110-
coord[i, , drop = FALSE],
111-
lineend = lineend,
112-
linejoin = linejoin,
113-
linemitre = linemitre
114-
)
115-
})
116-
do.call("gList", grobs)
108+
sf_grob(coord, lineend = lineend, linejoin = linejoin, linemitre = linemitre)
117109
},
118110

119111
draw_key = function(data, params, size) {
@@ -138,33 +130,37 @@ default_aesthetics <- function(type) {
138130
}
139131
}
140132

141-
sf_grob <- function(row, lineend = "butt", linejoin = "round", linemitre = 10) {
133+
sf_grob <- function(x, lineend = "butt", linejoin = "round", linemitre = 10) {
142134
# Need to extract geometry out of corresponding list column
143-
geometry <- row$geometry[[1]]
144-
145-
if (inherits(geometry, c("POINT", "MULTIPOINT"))) {
146-
row <- modify_list(default_aesthetics("point"), row)
147-
gp <- gpar(
148-
col = alpha(row$colour, row$alpha),
149-
fill = alpha(row$fill, row$alpha),
150-
# Stroke is added around the outside of the point
151-
fontsize = row$size * .pt + row$stroke * .stroke / 2,
152-
lwd = row$stroke * .stroke / 2
153-
)
154-
sf::st_as_grob(geometry, gp = gp, pch = row$shape)
155-
} else {
156-
row <- modify_list(default_aesthetics("poly"), row)
157-
gp <- gpar(
158-
col = row$colour,
159-
fill = alpha(row$fill, row$alpha),
160-
lwd = row$size * .pt,
161-
lty = row$linetype,
162-
lineend = lineend,
163-
linejoin = linejoin,
164-
linemitre = linemitre
165-
)
166-
sf::st_as_grob(geometry, gp = gp)
167-
}
135+
geometry <- x$geometry
136+
type <- sf_types[sf::st_geometry_type(geometry)]
137+
is_point <- type %in% "point"
138+
type_ind <- match(type, c("point", "line", "other"))
139+
defaults <- list(
140+
GeomPoint$default_aes,
141+
GeomLine$default_aes,
142+
modify_list(GeomPolygon$default_aes, list(fill = "grey90", colour = "grey35"))
143+
)
144+
default_names <- unique(unlist(lapply(defaults, names)))
145+
defaults <- lapply(setNames(default_names, default_names), function(n) {
146+
unlist(lapply(defaults, function(def) def[[n]] %||% NA))
147+
})
148+
alpha <- x$alpha %||% defaults$alpha[type_ind]
149+
col <- x$colour %||% defaults$colour[type_ind]
150+
col[is_point] <- alpha(col[is_point], alpha[is_point])
151+
fill <- x$fill %||% defaults$fill[type_ind]
152+
fill <- alpha(fill, alpha)
153+
size <- x$size %||% defaults$size[type_ind]
154+
stroke <- (x$stroke %||% defaults$stroke[1]) * .stroke / 2
155+
fontsize <- size * .pt + stroke
156+
lwd <- ifelse(is_point, stroke, size * .pt)
157+
pch <- x$shape %||% defaults$shape[type_ind]
158+
lty <- x$linetype %||% defaults$linetype[type_ind]
159+
gp <- gpar(
160+
col = col, fill = fill, fontsize = fontsize, lwd = lwd, lty = lty,
161+
lineend = lineend, linejoin = linejoin, linemitre = linemitre
162+
)
163+
sf::st_as_grob(geometry, pch = pch, gp = gp)
168164
}
169165

170166
#' @export
@@ -282,3 +278,11 @@ geom_sf_text <- function(mapping = aes(), data = NULL,
282278
layer_class = LayerSf
283279
)
284280
}
281+
282+
sf_types <- c(GEOMETRY = "other", POINT = "point", LINESTRING = "line",
283+
POLYGON = "other", MULTIPOINT = "point", MULTILINESTRING = "line",
284+
MULTIPOLYGON = "other", GEOMETRYCOLLECTION = "other",
285+
CIRCULARSTRING = "line", COMPOUNDCURVE = "other", CURVEPOLYGON = "other",
286+
MULTICURVE = "other", MULTISURFACE = "other", CURVE = "other",
287+
SURFACE = "other", POLYHEDRALSURFACE = "other", TIN = "other",
288+
TRIANGLE = "other")

0 commit comments

Comments
 (0)