感谢您抽出时间来阅读。
我不知道为什么这段代码不能工作。总而言之(更改变量名),我有圣路易斯餐馆的位置(坐标、经度和经度),以及便利店的位置(也是lat )。我想看看便利店离餐厅有多远。
#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 | RestaurantName | lat | long || Joes Diner | 38.705222421313 | -90.3084172639293|
Myfile2.csv |商店名称|纬度|经度| SuperShop | 38.5420000000000 | -90.2700000000000
任何帮助修复这个问题的人都将不胜感激。
发布于 2021-09-29 16:50:28
问题出在您的坐标参考系(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。在这种情况下,这是lat-long,这通常意味着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")https://stackoverflow.com/questions/69380365
复制相似问题