Search code examples
rgeolocationmappingr-sfgeo

R, sf package: Buffers and points are not showing on map


Thank you for taking the time to read.

I have no idea why this code is not working. To summarize (changing variable names), I have locations of restaurants in St. Louis (coordinates, lat and long), as well as locations of convenience stores (also in lat long). I want to see how often a convenience store is 1000 feet from a restaurant.

#Bring in data
# MO county data
county.sf <- get_acs(state = "MO",
     county = c("St. Louis County", "St. Louis City"),
                 geography = "tract",
                 variables = "B03002_001", 
                 output="wide", 
                 geometry = TRUE) %>%
sf::st_transform(crs = "ESRI:102003")
class(county.sf)

# Restaurant data
res <- read.csv("Myfile1.csv")
res.sf <- st_as_sf(res, coords = c("lat", "long"), crs = st_crs(county.sf))
res.sf.utm <- st_transform(res.sf, crs = "ESRI:102003")

# Store data
store <- import("Myfile2.csv")
store.sf <- st_as_sf(store, coords = c("Latitude", "Longitude"), crs = st_crs(county.sf))
store.sf.utm <- st_transform(store.sf, crs = "ESRI:102003")

# Creating buffers
# Going to use 1000 ft which = 304.8 meter buffers
elem.buff <-st_buffer(res.sf.utm, 304)     
class(elem.buff)

#Create Map
ex.map<- tm_shape(county.sf) + tm_polygons() + tm_shape(elem.buff) + tm_borders(col="red") + tm_shape(res.sf.utm) + tm_dots(col = "red") + tm_shape(store.sf.utm) + tm_dots() 

ex.map

This code all runs, and even shows a map of the two counties in St. Louis I am interested in. But despite the coordinates of the restaurants and stores being in those counties, nothing shows up. There is some kind of mismatch where the files are not matching. I have no idea how to fix this.

Sample data:

Myfile1.csv | RestaurantName | lat | long | | Joes Diner | 38.705222421313 | -90.3084172639293|

Myfile2.csv | Store Name | Latitude | Longitude | | SuperShop | 38.5420000000000 | -90.2700000000000 |

Any help in fixing this would be greatly appreciated.


Solution

  • The problem is with your coordinate reference system (crs). In these two lines, you convert the csv files to spatially referenced points, and tell them to use the crs of the county.sf shapefile:

    res.sf <- st_as_sf(res, coords = c("lat", "long"), crs = st_crs(county.sf))
    store.sf <- st_as_sf(store, coords = c("Latitude", "Longitude"), crs = st_crs(county.sf))
    

    But you've earlier transformed county.sf to use the ESRI:102003 crs, which is a projection, not lat-long coordinates. When you convert the csv to spatial points, you need to tell st_as_sf() which crs the csv uses. In this case, that is lat-long, which usually means the epsg:4326 crs. You also need to specify long as the first coordinate, as it is the "x" direction and lat is the "y". So, replace the above lines with these:

    # create the spatial points, matching the lat/long coding of the csv
    res.sf <- st_as_sf(res, coords = c("long", "lat"), crs="epsg:4326")
    store.sf <- st_as_sf(store, coords = c("Longitude", "Latitude"), crs="epsg:4326")