4

I have a pair of longitude and latitude coordinates, and I want to plot a 500 meter buffer around it using leaflet or mapview. st_buffer, however, does not correctly buffer longitude/latitude data.

Initial code:

library(rgeos)
library(rgdal)
library(sf)

df <- data.frame(lon = -121.9552, lat = 37.35411)
coordinates(df) <- c("lon", "lat")
proj4string(df) <- CRS("+init=epsg:4326") 
df_sf <- st_as_sf(df)
#....
# Something goes here, but I don't know what?
#...
st_buffer(df_sf, 500)

Warning message:
In st_buffer.sfc(st_geometry(x), dist, nQuadSegs) :
st_buffer does not correctly buffer longitude/latitude data

What's the missing piece?

2

2 Answers 2

5

Your point should be in a Projected Coordinate System. If it is in a geographic coordinate system, the values will be in decimal degrees.

I am not sure what you use over there, but I have picked NAD83 California Albers, which uses metres as its unit.

library(rgeos)
library(rgdal)
library(sf)

df <- data.frame(lon = -121.9552, lat = 37.35411)
coordinates(df) <- c("lon", "lat")
proj4string(df) <- CRS("+init=epsg:4326") 
df_sf <- st_as_sf(df) %>% st_transform(3488) #transform to NAD83(NSRS2007) / California Albers
df_sf_buff <- st_buffer(df_sf, 500)
plot(df_sf_buff)
1
  • 3
    If you want to avoid all the faff of building a point using sp::coordinates, you can do it purely in sf with: df = st_sfc(st_point(cbind(-121, 37)),crs=4326) - then you don't need rgdal or rgeos.
    – Spacedman
    Commented Nov 19, 2018 at 8:41
3

In order to make st_buffer to output correct results, make sure:

  • the CRS unit is the same as the one used in the dist argument from st_buffer.

AND,

  • the CRS is in a projected coordinate system (i.e., with planar coordinates), as stated in wl1234's answer. This is because the geometrical operations from sf (and rgeos) package come from the GEOS library, which assume coordinates to be planar. This is why you get the message st_buffer does not correctly buffer longitude/latitude data (even if you were using degrees in the dist argument).
1
  • 2
    Good additional information. Commented Nov 19, 2018 at 4:00

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