Skip to content

Commit d489e2c

Browse files
authored
Merge pull request #459 from rstudio/joe/bugfix/empty-sf
Fix #452: Leaflet returns an error for empty "sf" "dataframe" objects
2 parents 32bda8e + 58aaf50 commit d489e2c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+69
-84
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@ Suggests:
6464
RJSONIO,
6565
purrr,
6666
testthat
67-
RoxygenNote: 5.0.1
67+
RoxygenNote: 6.0.1
6868
LazyData: true

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ S3method(pointData,data.frame)
1818
S3method(pointData,default)
1919
S3method(pointData,matrix)
2020
S3method(pointData,sf)
21+
S3method(pointData,sfc_GEOMETRY)
2122
S3method(pointData,sfc_POINT)
2223
S3method(polygonData,Line)
2324
S3method(polygonData,Lines)

R/normalize-sf.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ metaData.sf <- function(obj) {
99

1010
#' @export
1111
pointData.sf <- function(obj) {
12-
geometry <- obj[[attr(obj, "sf_column")]]
13-
pointData(geometry)
12+
pointData(sf::st_geometry(obj))
1413
}
1514

1615
#' @export
1716
pointData.sfc_POINT <- function(obj) {
17+
if (length(obj) == 0) {
18+
# If a sfc_GEOMETRY is empty, just return nothing.
19+
return(data.frame(lng = numeric(0), lat = numeric(0)))
20+
}
21+
1822
check_crs(obj)
1923

2024
structure(
@@ -44,6 +48,21 @@ pointData.POINT <- function(obj) {
4448
)
4549
}
4650

51+
#' @export
52+
pointData.sfc_GEOMETRY <- function(obj) {
53+
if (length(obj) == 0) {
54+
# If a sfc_GEOMETRY is empty, just return nothing.
55+
data.frame(lng = numeric(0), lat = numeric(0))
56+
} else if (all(vapply(obj, inherits, logical(1), "POINT"))) {
57+
# If it's all POINT objects, then treat it as sfc_POINT.
58+
pointData.sfc_POINT(obj)
59+
} else {
60+
# Otherwise, we don't know what to do. Let pointData.default throw an
61+
# error.
62+
NextMethod("pointData")
63+
}
64+
}
65+
4766
# polygonData -------------------------------------------------------------
4867

4968
#' @export

man/addAwesomeMarkers.Rd

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

man/addGraticule.Rd

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

man/addLayersControl.Rd

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

man/addLegend.Rd

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

man/addMeasure.Rd

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

man/addMiniMap.Rd

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

man/addProviderTiles.Rd

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

0 commit comments

Comments
 (0)