我正在使用sf包(和相关包)在 R 中制作 GIS 地图以读取 shapefile 和ggplot2(和朋友)进行绘图。这工作正常,但我找不到(自动/以编程方式)为河流和道路等要素创建标签放置的方法。这些特征通常是具有不规则形状的线串。请参阅例如来自维基媒体的图片。
该ggrepel软件包适用于以自动方式标记点,但这对于不是离散的纬度/经度点的其他地理特征没有多大意义。
我可以想象通过在每个功能上单独放置单独的文本标签来做到这一点,但如果可能的话,我正在寻找更自动化的东西。我意识到这种自动化不是一个小问题,但之前已经解决了(ArcGIS 显然有一种方法可以使用名为 Maplex 的扩展程序来做到这一点,但我无法访问该软件,我想留在R 如果可能)。
有谁知道这样做的方法?
MWE在这里:
#MWE Linestring labeling
library(tidyverse)
library(sf)
library(ggrepel)
set.seed(120)
#pick a county from the built-in North Carolina dataset
BuncombeCounty <- st_read(system.file("shapes/", package="maptools"), "sids") %>%
filter(NAME == "Buncombe")
#pick 4 random points in that county
pts_sf <- data.frame(
x = seq(-82.3, -82.7, by=-0.1) %>%
sample(4),
y = seq(35.5, 35.7, by=0.05) %>%
sample(4),
placenames = c("A", "B", "C", "D")
) %>%
st_as_sf(coords = …Run Code Online (Sandbox Code Playgroud) 我想使用将点捕捉到道路段上最接近的点sf::st_snap。但是,该函数似乎返回了错误的结果,它将我的观点捕捉到了路段起点处的一点。有想法该怎么解决这个吗?
下面提供了可重现的示例,包括对使用sf::st_snapvs 时得到的结果的比较maptools::snapPointsToLines
sf::st_snap# Max distance
cut_dist = 200 # meters
# snap points to closest road
new_point <- sf::st_snap(point1, roads, tolerance = cut_dist)
# view points on the map
mapView(point1, color="red") + mapView( st_buffer(point1, dist = cut_dist)) + mapView(new_point) + mapView(roads)
# Distance between pont1 and new_point
st_distance( point1, new_point)
> 1591 meters # note this is above the set maximun distance
Run Code Online (Sandbox Code Playgroud)
maptools::snapPointsToLines(我期望的结果)# convert sf to sp
point_sp <- …Run Code Online (Sandbox Code Playgroud)