Skip to content

Commit 5f16b88

Browse files
committed
preliminary sf support for plot_mapbox() and plot_ly() based on GeomSf logic
1 parent 9750fa2 commit 5f16b88

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

R/plotly_build.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,16 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
115115
# perform the evaluation
116116
dat <- plotly_data(p, y)
117117

118+
119+
118120
# set special defaults for sf
119121
if (inherits(dat, "sf")) {
120122
# TODO:
121-
# (1) check/change the crs? https://github.com/rstudio/leaflet/blob/d489e2cd/R/normalize-sf.R#L94-L113
122-
# (2) One trace/layer can sometime map to multiple traces (e.g., an sf object with points and lines)
123-
# (3) st_cast() if a geometry collection?
123+
# (1) One trace/layer can sometime map to multiple traces (e.g., an sf object with points and lines)
124+
# (2) st_cast() if a geometry collection?
124125
x$`_bbox` <- sf::st_bbox(dat)
125-
dat <- fortify_sf(dat)
126-
x$x <- ~x
127-
x$y <- ~y
126+
dat <- to_basic.GeomSf(dat)
127+
x <- modify_list(geom2trace(dat, params = list()), x)
128128
}
129129

130130
trace <- structure(

R/sf.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ fortify_sf <- function(model, ...) {
22
# TODO:
33
# (1) avoid converting redundant features
44
# (2) warn/error if data already contains x/y
5+
sf_crs_check(model)
6+
57
geoms <- sf::st_geometry(sf::st_as_sf(model))
68
xy <- lapply(geoms, st_as_plotly)
79
ids <- rep(seq_len(nrow(model)), sapply(xy, nrow))
@@ -89,3 +91,24 @@ st_as_plotly.COMPOUNDCURVE = st_as_plotly.GEOMETRYCOLLECTION
8991
empty_xy <- function() {
9092
matrix(rep(NA, 2), ncol = 2)
9193
}
94+
95+
96+
# thanks Hadley Wickham https://github.com/rstudio/leaflet/blame/d489e2cd/R/normalize-sf.R#L94-L113
97+
sf_crs_check <- function(x) {
98+
crs <- sf::st_crs(x)
99+
100+
# Don't have enough information to check
101+
if (is.na(crs)) return()
102+
103+
if (identical(sf::st_is_longlat(x), FALSE)) {
104+
warning("sf layer is not long-lat data", call. = FALSE)
105+
}
106+
107+
if (!grepl("+datum=WGS84", crs$proj4string, fixed = TRUE)) {
108+
warning(
109+
"sf layer has inconsistent datum (", crs$proj4string, ").\n",
110+
"Need '+proj=longlat +datum=WGS84'",
111+
call. = FALSE
112+
)
113+
}
114+
}

R/utils.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ mapbox_fit_bounds <- function(p) {
213213
mapboxIDs <- grep("^mapbox", sapply(p$x$data, "[[", "subplot"), value = TRUE)
214214
for (id in mapboxIDs) {
215215
bboxes <- lapply(p$x$data, function(tr) if (identical(id, tr$subplot)) tr[["_bbox"]])
216+
if (sum(lengths(bboxes)) == 0) next
216217
# intentionally an array of numbers in [west, south, east, north] order
217218
# https://www.mapbox.com/mapbox-gl-js/api/#lnglatboundslike
218219
p$x$layout[[id]]$`_fitBounds` <- list(

0 commit comments

Comments
 (0)