gpt4 book ai didi

r - 使用另一个多边形裁剪/剪辑复杂的 SpatialPolygonsDataFrame 的最快方法

转载 作者:行者123 更新时间:2023-12-04 10:13:40 25 4
gpt4 key购买 nike

使用另一个可能复杂的 SpatialPolygon 在 R 中保留 @data 来裁剪(剪辑)复杂的 SpatialPolygonsDataFrame 的最快方法是什么?我知道两种方法(如下所示)。 raster 方法对于不太复杂的 SpatialPolygonsDataFrames 更快,并返回示例中的 SpatialPolygonsDataFrame。但是,大型 SpatialPolygonsDataFrames 会变慢。 rgeos 方法对于大型 SpatialPolygonsDataFrame 更快,但是 it sometimes fails with very complex geometries并且不返回 SpatialPolygonsDataFrames 作为默认值。

我最近没有关注 R 中的 GIS 开发,现在可能有更快的方法来做这件事。

示例多边形很小,以适应人们的计算机和带宽。考虑 50-1000 MB 的“真实”多边形。

设置:

library(rnaturalearth)
library(sp)
library(raster)
library(rgeos)

dt <- rnaturalearth::ne_countries()
clip_boundary <- sp::SpatialPolygons(list(sp::Polygons(
list(sp::Polygon(data.frame(lon = c(0, 180, 180, 0), lat = c(40, 40, 80, 80)))), ID = 1)))

rgeos 方式:

system.time({
clipped.dt <- rgeos::gIntersection(dt, clip_boundary, byid = TRUE)
ids <- sapply(slot(clipped.dt, "polygons"), function(x) slot(x, "ID"))
ids <- sapply(strsplit(ids, " "), "[", 1)

tmp.df <- data.frame(dt@data[ids,])
names(tmp.df) <- names(dt@data)

out <- sp::SpatialPolygonsDataFrame(clipped.dt, tmp.df, match.ID = FALSE)
})

# user system elapsed
# 0.069 0.002 0.074

class(out)
# [1] "SpatialPolygonsDataFrame"
# attr(,"package")
# [1] "sp"

栅格方式:

system.time({
out <- raster::crop(dt, clip_boundary)
})

# user system elapsed
# 0.042 0.001 0.043

class(out)
# [1] "SpatialPolygonsDataFrame"
# attr(,"package")
# [1] "sp"

引用图(与问题无关):

plot(out)

enter image description here

最佳答案

R 的许多地理空间工作现在可以使用 sf 包完成。

https://r-spatial.github.io/sf/index.html

看起来它可能比 raster 和 sp 快一点点。

library(rnaturalearth)
library(sp)
#library(raster)
#library(rgeos)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
library(ggplot2)

# Original data
dt <- rnaturalearth::ne_countries()
clip_boundary <- sp::SpatialPolygons(list(sp::Polygons(
list(sp::Polygon(data.frame(lon = c(0, 180, 180, 0), lat = c(40, 40, 80, 80)))), ID = 1)))

# Original data as sf objects
dt_sf <- st_as_sf(dt)
boundary_sf <- st_as_sf(clip_boundary)

# clip
clipped <- st_crop(dt_sf, boundary_sf)
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries

#plot
ggplot(clipped) + geom_sf()


system.time({
out <- st_crop(dt_sf, boundary_sf)
})
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
#> user system elapsed
#> 0.019 0.000 0.018

system.time({
out <- raster::crop(dt, clip_boundary)
})
#> Warning in raster::crop(dt, clip_boundary): non identical CRS
#> user system elapsed
#> 0.526 0.000 0.525

system.time({
clipped.dt <- rgeos::gIntersection(dt, clip_boundary, byid = TRUE)
ids <- sapply(slot(clipped.dt, "polygons"), function(x) slot(x, "ID"))
ids <- sapply(strsplit(ids, " "), "[", 1)

tmp.df <- data.frame(dt@data[ids,])
names(tmp.df) <- names(dt@data)

out <- sp::SpatialPolygonsDataFrame(clipped.dt, tmp.df, match.ID = FALSE)
})
#> Warning in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_lower_td,
#> unaryUnion_if_byid_false, : spgeom1 and spgeom2 have different proj4 strings
#> user system elapsed
#> 0.045 0.000 0.045

reprex package 创建于 2020-04-13 (v0.3.0)

关于r - 使用另一个多边形裁剪/剪辑复杂的 SpatialPolygonsDataFrame 的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61192284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com