2

I want to sample points (latitude-longitude pairs) on borders of the US states, or points close to borders. With the following code, I am able to sample points in an example multipolygon. How can I do this on the borders instead?

library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
plot(nc$geometry[1])
plot(st_sample(nc$geometry[1], 1000))

1 Answer 1

3

Cast the polygon into a multi-linestring and sample from that:

> plot(st_sample(st_cast(nc$geometry[1],"MULTILINESTRING"), 1000))
3
  • Cool approach! I have not seen this before. Mar 4, 2020 at 0:02
  • Thanks! Does it oversamples from the line segments with longer lengths? In other words, is the expected number of points from a line segment is proportional to its length? Mar 4, 2020 at 14:28
  • It distributes points uniformly along the perimeter of the feature. So if you have a triangle with sides 3,4,5 then the number of points on each side would have expected ratio of 3:4:5.
    – Spacedman
    Mar 4, 2020 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.