R、sf 包:缓冲区和点未显示在地图上

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

感谢您抽出宝贵时间阅读。

我不知道为什么这段代码不起作用。总而言之(更改变量名称),我有圣路易斯餐馆的位置(坐标,纬度和经度),以及便利店的位置(也有经纬度)。我想看看便利店距离餐馆 1000 英尺的频率。

#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

这段代码全部运行,甚至显示了我感兴趣的圣路易斯两个县的地图。但是尽管餐馆和商店的坐标在这些县,但什么也没有显示。文件不匹配时存在某种不匹配。我不知道如何解决这个问题。

示例数据:

Myfile1.csv |餐厅名称 |纬度|长 | |乔斯晚餐 | 38.705222421313 | -90.3084172639293|

Myfile2.csv |店名 |纬度 |经度 | |超市 | 38.5420000000000 | -90.2700000000000 |

如能帮助解决此问题,我们将不胜感激。

问题出在您的坐标参考系统 (crs) 上。在这两行中,您将 csv 文件转换为空间参考点,并告诉它们使用 county.sf shapefile 的 crs:

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))

但是您之前已经将 county.sf 转换为使用 ESRI:102003 crs,这是一个投影,而不是经纬度坐标。当您将 csv 转换为空间点时,您需要告诉 st_as_sf() csv 使用哪个 crs。在这种情况下,即经纬度,通常表示 epsg:4326 crs。您还需要指定 long 作为第一个坐标,因为它是“x”方向,lat 是“y”。因此,用这些替换上面的行:

# 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")