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. Commented 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? Commented 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. Commented Mar 4, 2020 at 17:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.