使用 tmap 保存有关点到点移动路径的信息

Save information abouth the path moving from point to point with tmap

如果您能帮助我解决以下问题,我将不胜感激。我有一张美国各县的地图,每个县都有自己的特点,比如土壤电导率。然后我有源和信号接收器的坐标。我需要做的是测量从我的信号源到接收器的距离,并保存有关距离的信息以及我的信号经过每个县的信息。比如我的源在A县,接收者在B县,那么我需要保存它经过A县的距离和经过B县的距离,它经过的县数可能不止2个。我已经制作了地图并将源和接收器的坐标放在上面。

library(sf)
library(tmap)
county_map <- st_read("cty_1930_gis.shp")
qtm(county_map, fill = "cndctmn")
tm_shape(county_map) + tm_polygons(alpha = 0.2, legend.show = FALSE) + tm_shape(stations1) + tm_bubbles(col = "red", size=0.02) + tm_basemap(server = c('OpenStreetMap'))

县地图的电导率

我的消息来源和我的接收器在地图上

根据地图上的边界,我应该使用什么函数来计算我的信号在每个县移动的距离?

这是一个 sf 问题,而不是 tmap 问题。

这是我推荐的方法:

  1. 用 sf 从原点(源)到目的地(接收者)创建空间线。确保使用能够很好地近似距离的地图投影 (crs)(因此不要使用 mercator/wgs84)。
  2. 使用带有两个参数的 sf::st_intersection:您刚刚创建的线和空间多边形。
  3. 结果已经告诉你信号经过了哪些县。
  4. 使用st::st_distance测量每个相交部分的距离。

不确定您是否已经熟悉 sf。如果没有,则有很多 SO 主题涵盖了这些步骤之一的详细信息。

这是回答我在 Python 中的问题的代码,以防有人想使用它

import geopandas
from geopandas import GeoSeries
from shapely.geometry import LineString

data = geopandas.read_file('cty_1930_gis.shp')
data = data.to_crs(epsg=4326)
line = LineString([(lon_x, lat_x), (lon_y, lat_y)])
intersections = map_data.intersection(line)
intersections = intersections[~intersections.is_empty]
intersections = intersections.to_crs(epsg=3815)
lengths = intersections.length

然后在所有感兴趣的点上写一个向量函数循环。 (当然,选择正确的 epsg 代码很重要)