从多边形中分离出一条线并测量其长度
Isolating a line from a polygon and measuring its length
我正在尝试测量海岸线的长度。这将用作分析中沿海岸线位置的度量。例如,假设我有一个地区所有 public 个海滩的位置数据,我想通过测量它们与海岸参考点的距离来描述它们在 space 中的分布情况。
我按照 this extremely helpful tutorial 使用不同长度的尺子计算海岸线的长度。但是,只有当你想测量整个多边形的长度时才准确,即你感兴趣的地理对象是一个岛屿。
获取海岸线的 shapefile(请注意,在 ne_countries
调用中,我故意使用粗比例,以使海岸线更平滑,并且仅保留返回的第一个形状 - "scalerank"名字不重要):
library(raster)
library(sf)
library(rnaturalearth)
basemap <- rnaturalearth::ne_countries(scale = 110, country = "united states of america", returnclass = "sf")[1]
bbox <- extent(-82, -65, 27, 35)
cropmap <- st_crop(basemap, bbox)
plot(cropmap)
这个 returns 形状显示南大西洋海岸一直延伸到佛罗里达州。但是,如果我测量这个形状的长度,它将包括多边形的所有边——而不仅仅是海岸线。如何隔离海岸线(请参阅下面的地图,了解多边形的哪一部分实际上是沿海地区)并仅在 R 中测量其长度?
ggplot() +
geom_sf(data=basemap) +
geom_sf(data=cropmap, color="blue", fill="blue")
如果在裁剪之前将多边形转换为直线,就很容易了
示例数据
library(raster)
library(rnaturalearth)
m <- rnaturalearth::ne_countries(scale = 110, country = "united states of america", returnclass = "sp")
ext <- extent(-82, -65, 27, 35)
转换和裁剪
m <- as(m, "SpatialLines")
croplines <- crop(m, ext)
因为 CRS 是 longitude/latitude,所以使用 geosphere 而不是 rgeos 来计算长度
library(geosphere)
lengthLine(croplines)
#[1] 1162849
(即1162.8公里)
在某些情况下,您可能需要与多边形而不是范围相交(请参阅 raster::drawPoly
和 raster::intersect
)或者可能使用 crop
、disaggregate
,并在视觉上select
得到你想要的台词。
这是一个类似于罗伯特的版本,留在 sf
。我将原始底图从多边形转换为线,然后像您一样 "cut out" bbox
。然后你可以用 st_length
.
测量
library(sf)
library(rnaturalearth)
basemap <- rnaturalearth::ne_countries(scale = 110, country = "united states of america", returnclass = "sf")[1]
从多边形转换为线以避免多边形中不需要的部分
basemap_lines <- basemap %>% st_cast("MULTILINESTRING")
plot(basemap_lines)
Basemap as multilinestring
然后创建千篇一律的多边形,将 crs
设置为 lat/lon
xmin <- -82
xmax <- -65
ymin <- 27
ymax <- 35
bbox <- st_polygon(list(rbind(c(xmin,ymin), c(xmin,ymax), c(xmax,ymax), c(xmax,ymin),c(xmin,ymin)))) %>% st_sfc()
st_crs(bbox) <- 4326
然后用 st_intersection
裁剪
crop_lines <- st_intersection(basemap_lines, bbox)
plot(crop_lines)
st_length(crop_lines)
Cropped line
对于你的 bbox
尺寸,我得到 1162849 米(~723 英里),这与另一个答案一致。
我正在尝试测量海岸线的长度。这将用作分析中沿海岸线位置的度量。例如,假设我有一个地区所有 public 个海滩的位置数据,我想通过测量它们与海岸参考点的距离来描述它们在 space 中的分布情况。
我按照 this extremely helpful tutorial 使用不同长度的尺子计算海岸线的长度。但是,只有当你想测量整个多边形的长度时才准确,即你感兴趣的地理对象是一个岛屿。
获取海岸线的 shapefile(请注意,在 ne_countries
调用中,我故意使用粗比例,以使海岸线更平滑,并且仅保留返回的第一个形状 - "scalerank"名字不重要):
library(raster)
library(sf)
library(rnaturalearth)
basemap <- rnaturalearth::ne_countries(scale = 110, country = "united states of america", returnclass = "sf")[1]
bbox <- extent(-82, -65, 27, 35)
cropmap <- st_crop(basemap, bbox)
plot(cropmap)
这个 returns 形状显示南大西洋海岸一直延伸到佛罗里达州。但是,如果我测量这个形状的长度,它将包括多边形的所有边——而不仅仅是海岸线。如何隔离海岸线(请参阅下面的地图,了解多边形的哪一部分实际上是沿海地区)并仅在 R 中测量其长度?
ggplot() +
geom_sf(data=basemap) +
geom_sf(data=cropmap, color="blue", fill="blue")
如果在裁剪之前将多边形转换为直线,就很容易了
示例数据
library(raster)
library(rnaturalearth)
m <- rnaturalearth::ne_countries(scale = 110, country = "united states of america", returnclass = "sp")
ext <- extent(-82, -65, 27, 35)
转换和裁剪
m <- as(m, "SpatialLines")
croplines <- crop(m, ext)
因为 CRS 是 longitude/latitude,所以使用 geosphere 而不是 rgeos 来计算长度
library(geosphere)
lengthLine(croplines)
#[1] 1162849
(即1162.8公里)
在某些情况下,您可能需要与多边形而不是范围相交(请参阅 raster::drawPoly
和 raster::intersect
)或者可能使用 crop
、disaggregate
,并在视觉上select
得到你想要的台词。
这是一个类似于罗伯特的版本,留在 sf
。我将原始底图从多边形转换为线,然后像您一样 "cut out" bbox
。然后你可以用 st_length
.
library(sf)
library(rnaturalearth)
basemap <- rnaturalearth::ne_countries(scale = 110, country = "united states of america", returnclass = "sf")[1]
从多边形转换为线以避免多边形中不需要的部分
basemap_lines <- basemap %>% st_cast("MULTILINESTRING")
plot(basemap_lines)
Basemap as multilinestring
然后创建千篇一律的多边形,将 crs
设置为 lat/lon
xmin <- -82
xmax <- -65
ymin <- 27
ymax <- 35
bbox <- st_polygon(list(rbind(c(xmin,ymin), c(xmin,ymax), c(xmax,ymax), c(xmax,ymin),c(xmin,ymin)))) %>% st_sfc()
st_crs(bbox) <- 4326
然后用 st_intersection
crop_lines <- st_intersection(basemap_lines, bbox)
plot(crop_lines)
st_length(crop_lines)
Cropped line
对于你的 bbox
尺寸,我得到 1162849 米(~723 英里),这与另一个答案一致。