raster::rasterToContour;轮廓线不连续
raster::rasterToContour; contour lines are not continuous
我正在尝试使用 R 中的 raster
包从栅格 object 中提取等高线。
rasterToContour
似乎工作良好并且绘制得很好,但在调查时发现轮廓线被分解成不规则的段。来自 ?rasterToContour
的示例数据
library(raster)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
x <- rasterToContour(r)
class(x)
plot(r)
plot(x, add=TRUE)
我正在尝试提取栅格中示例站点的等高线。因此,我们选择一个随机站点,提取其高程,然后再次 运行 rasterToContour()
,指定等高线 level
.
的高程
# our sample site - a random cell chosen on the raster
xyFromCell(r, 5000) %>%
SpatialPoints(proj4string = crs(r)) %>%
{. ->> site_sp} %>%
st_as_sf %>%
{. ->> site_sf}
# find elevation of sample site, and extract contour lines
extract(r, site_sf) %>%
{. ->> site_elevation}
# extract contour lines
r %>%
rasterToContour(levels = site_elevation) %>%
{. ->> contours_sp} %>%
st_as_sf %>%
{. ->> contours_sf}
# plot the site and new contour lines (approx elevation 326)
plot(r)
plot(contours_sf, add = TRUE)
plot(site_sf, add = TRUE)
# plot the contour lines and sample site - using sf and ggplot
ggplot()+
geom_sf(data = contours_sf)+
geom_sf(data = site_sf, color = 'red')
然后我们使用 st_intersects
找到与站点相交的线(缓冲区宽度为 100 以确保它接触线)。但是,这 returns 所有的轮廓线。
contours_sf %>%
filter(
st_intersects(., site_sf %>% st_buffer(100), sparse = FALSE)[1,]
) %>%
ggplot()+
geom_sf()
我认为所有行都已返回,因为它们看起来是单个 MULTILINESTRING
sf
object.
contours_sf
# Simple feature collection with 1 feature and 1 field
# geometry type: MULTILINESTRING
# dimension: XY
# bbox: xmin: 178923.1 ymin: 329720 xmax: 181460 ymax: 333412.3
# CRS: +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs
# level geometry
# C_1 326.849822998047 MULTILINESTRING ((179619.3 ...
因此,我使用 ngeo::st_segments()
将 contours_sf
MULTILINESTRING
拆分为单独的行(我找不到任何 sf
方法来执行此操作,但我是开放的使用替代方法,特别是如果这是问题所在)。
没想到这个returns394个特征;通过查看该图,我预计大约有 15 条单独的线。
contours_sf %>%
nngeo::st_segments()
# Simple feature collection with 394 features and 1 field
# geometry type: LINESTRING
# dimension: XY
# bbox: xmin: 178923.1 ymin: 329720 xmax: 181460 ymax: 333412.3
# CRS: +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs
# First 10 features:
# level result
# 1 326.849822998047 LINESTRING (179619.3 329739...
# 2 326.849822998047 LINESTRING (179580 329720.4...
# 3 326.849822998047 LINESTRING (179540 329720, ...
# 4 326.849822998047 LINESTRING (179500 329735.8...
# 5 326.849822998047 LINESTRING (179495.3 329740...
# 6 326.849822998047 LINESTRING (179460 329764, ...
# 7 326.849822998047 LINESTRING (179442.6 329780...
# 8 326.849822998047 LINESTRING (179420 329810, ...
# 9 326.849822998047 LINESTRING (179410.2 329820...
# 10 326.849822998047 LINESTRING (179380 329847.3...
然后,当我们过滤以仅保留与站点相交的线(缓冲区宽度为 100)时,仅返回预期轮廓线的一小部分(线的红色部分,我假设反映了100 缓冲区宽度)。
contours_sf %>%
nngeo::st_segments() %>%
filter(
# this syntax used as recommended by this answer
st_intersects(., site_sf %>% st_buffer(100), sparse = FALSE)
) %>%
ggplot()+
geom_sf(colour = 'red', size = 3)+
geom_sf(data = contours_sf)+
geom_sf(data = site_sf, colour = 'cyan')+
geom_sf(data = site_sf %>% st_buffer(100), colour = 'cyan', fill = NA)
任何人对以下几点有想法:
- 解释为什么轮廓线是'broken'
- 提供一种有效的方法'joining'把碎片拼起来
-
nngeo::st_segments()
的替代方案,如果这实际上是 394 行的来源而不是 ~15
将 MULTILINESTRING 转换为 LINESTRING 似乎可以满足您的需要:
contours_sf %>% st_cast("LINESTRING") %>%
filter(st_intersects(., st_buffer(site_sf, 100), sparse=FALSE)[,1]) %>%
ggplot()+
geom_sf(data = contours_sf)+
geom_sf(data = site_sf, color = 'red') +
geom_sf(color = 'pink')
如果您从分解行开始,也许效果会更好
library(raster)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
x <- rasterToContour(r)
x <- disaggregate(x)
或 terra
library(terra)
r <- rast(f)
x <- as.contour(r)
x
# class : SpatVector
# geometry : lines
# dimensions : 8, 1 (geometries, attributes)
x <- disaggregate(x)
x
# class : SpatVector
# geometry : lines
# dimensions : 56, 1 (geometries, attributes)
你可以这样继续
y <- st_as_sf(x)
或者像这样
r <- rast(system.file("ex/meuse.tif", package="terra"))
site <- vect(xyFromCell(r, 5000), crs=crs(r))
elevation <- extract(r, site)
v <- disaggregate(as.contour(r, levels=elevation))
i <- which.min(distance(site, v))
vv <- v[i]
plot(r)
lines(v)
lines(vv, col="red", lwd=2)
points(site, col="blue", cex=2)
我正在尝试使用 R 中的 raster
包从栅格 object 中提取等高线。
rasterToContour
似乎工作良好并且绘制得很好,但在调查时发现轮廓线被分解成不规则的段。来自 ?rasterToContour
library(raster)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
x <- rasterToContour(r)
class(x)
plot(r)
plot(x, add=TRUE)
我正在尝试提取栅格中示例站点的等高线。因此,我们选择一个随机站点,提取其高程,然后再次 运行 rasterToContour()
,指定等高线 level
.
# our sample site - a random cell chosen on the raster
xyFromCell(r, 5000) %>%
SpatialPoints(proj4string = crs(r)) %>%
{. ->> site_sp} %>%
st_as_sf %>%
{. ->> site_sf}
# find elevation of sample site, and extract contour lines
extract(r, site_sf) %>%
{. ->> site_elevation}
# extract contour lines
r %>%
rasterToContour(levels = site_elevation) %>%
{. ->> contours_sp} %>%
st_as_sf %>%
{. ->> contours_sf}
# plot the site and new contour lines (approx elevation 326)
plot(r)
plot(contours_sf, add = TRUE)
plot(site_sf, add = TRUE)
# plot the contour lines and sample site - using sf and ggplot
ggplot()+
geom_sf(data = contours_sf)+
geom_sf(data = site_sf, color = 'red')
然后我们使用 st_intersects
找到与站点相交的线(缓冲区宽度为 100 以确保它接触线)。但是,这 returns 所有的轮廓线。
contours_sf %>%
filter(
st_intersects(., site_sf %>% st_buffer(100), sparse = FALSE)[1,]
) %>%
ggplot()+
geom_sf()
我认为所有行都已返回,因为它们看起来是单个 MULTILINESTRING
sf
object.
contours_sf
# Simple feature collection with 1 feature and 1 field
# geometry type: MULTILINESTRING
# dimension: XY
# bbox: xmin: 178923.1 ymin: 329720 xmax: 181460 ymax: 333412.3
# CRS: +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs
# level geometry
# C_1 326.849822998047 MULTILINESTRING ((179619.3 ...
因此,我使用 ngeo::st_segments()
将 contours_sf
MULTILINESTRING
拆分为单独的行(我找不到任何 sf
方法来执行此操作,但我是开放的使用替代方法,特别是如果这是问题所在)。
没想到这个returns394个特征;通过查看该图,我预计大约有 15 条单独的线。
contours_sf %>%
nngeo::st_segments()
# Simple feature collection with 394 features and 1 field
# geometry type: LINESTRING
# dimension: XY
# bbox: xmin: 178923.1 ymin: 329720 xmax: 181460 ymax: 333412.3
# CRS: +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs
# First 10 features:
# level result
# 1 326.849822998047 LINESTRING (179619.3 329739...
# 2 326.849822998047 LINESTRING (179580 329720.4...
# 3 326.849822998047 LINESTRING (179540 329720, ...
# 4 326.849822998047 LINESTRING (179500 329735.8...
# 5 326.849822998047 LINESTRING (179495.3 329740...
# 6 326.849822998047 LINESTRING (179460 329764, ...
# 7 326.849822998047 LINESTRING (179442.6 329780...
# 8 326.849822998047 LINESTRING (179420 329810, ...
# 9 326.849822998047 LINESTRING (179410.2 329820...
# 10 326.849822998047 LINESTRING (179380 329847.3...
然后,当我们过滤以仅保留与站点相交的线(缓冲区宽度为 100)时,仅返回预期轮廓线的一小部分(线的红色部分,我假设反映了100 缓冲区宽度)。
contours_sf %>%
nngeo::st_segments() %>%
filter(
# this syntax used as recommended by this answer
st_intersects(., site_sf %>% st_buffer(100), sparse = FALSE)
) %>%
ggplot()+
geom_sf(colour = 'red', size = 3)+
geom_sf(data = contours_sf)+
geom_sf(data = site_sf, colour = 'cyan')+
geom_sf(data = site_sf %>% st_buffer(100), colour = 'cyan', fill = NA)
任何人对以下几点有想法:
- 解释为什么轮廓线是'broken'
- 提供一种有效的方法'joining'把碎片拼起来
-
nngeo::st_segments()
的替代方案,如果这实际上是 394 行的来源而不是 ~15
将 MULTILINESTRING 转换为 LINESTRING 似乎可以满足您的需要:
contours_sf %>% st_cast("LINESTRING") %>%
filter(st_intersects(., st_buffer(site_sf, 100), sparse=FALSE)[,1]) %>%
ggplot()+
geom_sf(data = contours_sf)+
geom_sf(data = site_sf, color = 'red') +
geom_sf(color = 'pink')
如果您从分解行开始,也许效果会更好
library(raster)
f <- system.file("external/test.grd", package="raster")
r <- raster(f)
x <- rasterToContour(r)
x <- disaggregate(x)
或 terra
library(terra)
r <- rast(f)
x <- as.contour(r)
x
# class : SpatVector
# geometry : lines
# dimensions : 8, 1 (geometries, attributes)
x <- disaggregate(x)
x
# class : SpatVector
# geometry : lines
# dimensions : 56, 1 (geometries, attributes)
你可以这样继续
y <- st_as_sf(x)
或者像这样
r <- rast(system.file("ex/meuse.tif", package="terra"))
site <- vect(xyFromCell(r, 5000), crs=crs(r))
elevation <- extract(r, site)
v <- disaggregate(as.contour(r, levels=elevation))
i <- which.min(distance(site, v))
vv <- v[i]
plot(r)
lines(v)
lines(vv, col="red", lwd=2)
points(site, col="blue", cex=2)