Skip to content

Commit a9ceef1

Browse files
committed
fix sampling in geodetic coordinates
see #2331 #2515
1 parent 6debaaf commit a9ceef1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

R/sample.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,15 @@ st_poly_sample = function(x, size, ..., type = "random",
207207
}
208208
}
209209
R = s2::s2_earth_radius_meters()
210+
earth_surface = 4 * pi * R^2.
210211
toRad = pi / 180.
211212
h1 = sin(bb["ymax"] * toRad)
212213
h2 = sin(bb["ymin"] * toRad)
213-
a0 = sum(s2::s2_area(st_as_s2(x, oriented = oriented))) # total
214-
a1 = 2 * pi * R^2. * (h1 - h2) * (bb["xmax"] - bb["xmin"]) / 360. # actual
214+
a0 = 2 * pi * R^2. * (h1 - h2) * (bb["xmax"] - bb["xmin"]) / 360. # total
215+
a1 = sum(s2::s2_area(st_as_s2(x, oriented = oriented))) # actual
215216
if (!is.finite(a1))
216217
stop("One or more geometries have a non-finite area")
217-
global = (a1 / a0) > .9999
218+
global = (a0 / earth_surface) > .9999
218219
if (a0 / a1 > 1e4 && !force)
219220
stop(paste0("sampling box is ", format(a0/a1), " times larger than sampling region;\nuse force=TRUE if you really want this, or try setting oriented=TRUE\n(after reading the documentation)"), call. = FALSE)
220221
size = round(size * a0 / a1)
@@ -230,6 +231,7 @@ st_poly_sample = function(x, size, ..., type = "random",
230231
round(r)
231232
}
232233
}
234+
size = max(1, size)
233235

234236
pts = if (type == "hexagonal") {
235237
dx = sqrt(a0 / size / (sqrt(3)/2))

0 commit comments

Comments
 (0)