0

I am trying to analyze spatial density of gas station points using R. I need to create a buffer (circle) around the gas stations and count the number of gas stations within the buffer. I'll then need to play around with buffer distances to see what's a reasonable buffer to see something interesting. These are the files I am working with: https://dl.dropboxusercontent.com/u/45095175/sbc_gas.shp; https://dl.dropboxusercontent.com/u/45095175/sbc_gas.shx; https://dl.dropboxusercontent.com/u/45095175/sbc_gas.dbf

# Install packages 
x <- c("ggmap", "rgdal", "rgeos", "maptools", "ks")
lapply(x, library, character.only = TRUE)
all <- readShapePoints("sbc_gas.shp") 
all.df <- as(all, "data.frame")
locs <- subset(all.df, select = c("OBJECTID", "Latitude", "Longitude"))
head(locs)  # a simple data frame with coordinates
coordinates(locs) <- c("Longitude", "Latitude")  # set spatial  coordinates
plot(locs)

Any help greatly appreciated!!

JAG2024
  • 3,987
  • 7
  • 29
  • 58

2 Answers2

0

We cannot use your data as provided because the .shp file alone is not enough. At minimum, you must also provide the .shx and the .dbf file to be able to load this data.

However, something that should work is to get the package geosphere. It contains a function called distGeo. You can use it to get the distance from each gas station to all other stations. From the distance matrix, you should be able to select all stations within a specified distance.

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Thanks @G5W! I added links to the other files. – JAG2024 Feb 28 '17 at 05:38
  • @JAG2024 When I tried to get the two additional files, I got a message that they could not be downloaded. Do you have to set some permissions to allow download? – G5W Feb 28 '17 at 13:05
  • Try it now? I've updated the links. You may still have problems with the .dbf file. Is there a way I can send it to you directly? – JAG2024 Feb 28 '17 at 15:59
0

I found an answer to my question: fivekm <- cbind(coordinates(locs), X=rowSums(distm (coordinates(locs)[,1:2], fun = distHaversine) / 1000 <= 5)) # number of points within 5 km

JAG2024
  • 3,987
  • 7
  • 29
  • 58