我目前遇到一个问题,我正在尝试将矩形多边形投影到北美地图上。我的问题源于这样一个事实:我在纬度/经度中指定多边形,然后对其进行投影。该函数st_transform能够准确地变换多边形的点,但不能变换连接这些点的线。如何让线条随着整个形状的变化而变化?
此代码是一个可重现的示例:
library(sf)
library(spData)
library(dplyr)
library(ggplot2)
data(world)
proWorld <- st_transform(world, "EPSG:9311")
box <- st_bbox(c(xmin = -110,
ymin = 25,
ymax = 33,
xmax = -90), crs = "EPSG:4326") %>%
st_as_sfc() %>%
st_transform(crs = st_crs(proWorld))
ggplot() +
geom_sf(data = proWorld) +
geom_sf(data = box, fill = NA) +
ylim(c(-2182970, 1099711)) +
xlim(c(-1994737, 2770402))
Run Code Online (Sandbox Code Playgroud)
如果这是正确的,则生成的红色矩形的边缘将随纬度线弯曲
谢谢!
如果您希望矩形沿其边缘均匀变形,则需要从多个小片段构建边缘。您可以通过多种不同的方式来执行此操作,但其中一种方法使用stplanr::line_segment
library(sf)
library(spData)
library(dplyr)
library(ggplot2)
data(world)
proWorld <- st_transform(world, "EPSG:9311")
box <- st_bbox(c(xmin = -110,
ymin = 25,
ymax = 33,
xmax = -90), crs = "EPSG:4326") %>%
st_as_sfc() %>%
st_cast("LINESTRING") %>%
st_sf() %>%
stplanr::line_segment(n_segments = 100) %>%
st_union() %>%
st_cast("POLYGON") %>%
st_transform(crs = st_crs(proWorld))
ggplot() +
geom_sf(data = proWorld) +
geom_sf(data = box, fill = NA, color = "red", linewidth = 1) +
ylim(c(-2182970, 1099711)) +
xlim(c(-1994737, 2770402))
Run Code Online (Sandbox Code Playgroud)