1

I want to plot a circle with a radius of 5 miles around a hospital in Massachusetts. I was using some previous answers found here, but it is not working. st_buffer shows an error.

library(sp)
library(rgdal)
library(sf)

# Getting data:
download.file('http://download.massgis.digital.mass.gov/shapefiles/state/zipcodes_nt.zip',
              f <- tempfile(), mode='wb')
unzip(f, exdir=tempdir())
p <- readOGR(file.path(tempdir(), 'ZIPCODES_NT_POLY.shp'))
# location of hospital.
lon <- -71.06
lat <- 42.36

# defining the hospital location
dub <- st_point(x = c(lon, lat), dim = "XYZ")
dub <- dub %>% st_sfc(crs = 4326)
m5 <- set_units(8.04672, km) %>% set_units(m)
dub_buffer <-  st_buffer(dub, m5)
plot(p)
plot(dub_buffer, add=TRUE)
0

I agree with @mweber. If you are happy with UTM 19N, EPSG:32619 will do.

library(sp)
library(rgdal)
library(sf)
library(units)

# Getting data:
download.file('http://download.massgis.digital.mass.gov/shapefiles/state/zipcodes_nt.zip',
              f <- tempfile(), mode='wb')
unzip(f, exdir=tempdir())
p <- readOGR(file.path(tempdir(), 'ZIPCODES_NT_POLY.shp'))
# location of hospital.
lon <- -71.06
lat <- 42.36

# defining the hospital location
dub <- st_point(x = c(lon, lat), dim = "XYZ")
dub <- dub %>% st_sfc(crs = 32619)
m5 <- units::set_units(8.04672, km)
dub_buffer <-  st_buffer(dub, m5)
plot(p)
plot(dub_buffer, add=TRUE)
2

Looks like you need to project dub, your point for hospital - you've set its crs as geographic coordinates. Your code works for me if you add this line prior to the set_units line:

dub <- st_transform(dub, crs=5070)

Any projected crs using meters will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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