【问题标题】:R, sf package: Buffers and points are not showing on mapR,sf 包:缓冲区和点未显示在地图上
【发布时间】:2021-11-21 14:17:49
【问题描述】:

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

我不知道为什么这段代码不起作用。总而言之(更改变量名称),我有圣路易斯餐厅的位置(坐标,经纬度),以及便利店的位置(也在经纬度)。我想知道便利店距离餐厅 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 |

任何解决此问题的帮助将不胜感激。

【问题讨论】:

  • 对于 myfile1.csv,您没有为您的坐标使用正确的字段名称,即 st_as_sf(...,coord = c("long","lat")...)。 crs= 的点也应该是地理的(例如crs=4326),然后转换以匹配您的多边形。
  • 我不确定我是否 100% 理解。这里的经度在纬度之前吗?我是否只是将所有 crs 值更改为 4326?抱歉,这里的映射非常新。
  • 没问题。我在第一条评论中的第一点是,示例数据中的坐标字段是“long”和“lat”,而不是代码中的“longitude”和“latitude”。 @Wesley 的回答解决了第二点。祝你好运!

标签: r geolocation mapping sf geo


【解决方案1】:

问题在于您的坐标参考系 (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")

【讨论】:

  • 不错的答案!我只想向未来的读者指出,纬度/经度并不一定意味着使用 WGS84 (epsg 4326) 地理坐标系,尽管它通常是一个很好的猜测。更多信息在这里:nceas.ucsb.edu/sites/default/files/2020-04/…
  • 哇哦,这成功了!非常感谢您的帮助!
猜你喜欢
  • 2018-04-30
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多