Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create buffer around spatial data in R

I have a spatial dataset of shopping centers that I would like to create buffers around in R.

I think these packages will be useful:

require(maptools)
require(geosphere) 

I was able to do so for a set of coordinates, but not for spatial data. The code looks like this:

coordinates(locs) <- c("Longitude", "Latitude")  # set spatial  coordinates
fivekm <- cbind(coordinates(locs), X=rowSums(distm (coordinates(locs)[,1:2], fun = distHaversine) / 1000 <= 5)) # number of points within 5 km

But I don't know what function/package to use for a set of polygons. Can someone please advise on the function (or code) and I will go from there?

Thanks!

like image 954
JAG2024 Avatar asked Sep 01 '25 23:09

JAG2024


1 Answers

I think the only option at the moment is to project your longitude and latitude points to a flat map and then do everything there. As far as I know there are no packages for doing polygonal geometry on the sphere yet (I'm working on one, but there is no ETA).

Projection used to be done with spTransform from the sp package, but now it may be more convenient to use the more modern simple features package sf which has the function st_transform. The vignette https://cran.r-project.org/web/packages/sf/vignettes/sf1.html has a section called "Coordinate reference systems and transformations" to help you with this part. The buffering is described in the section "Geometrical operations".

like image 130
Ege Rubak Avatar answered Sep 03 '25 14:09

Ege Rubak