英文:
Convert polygon to dataframe
问题
我通过从大型数据框派生的两个多边形相减创建了一个多边形。更具体地说:
plot(Cyp_occ_4326_df$lon, Cyp_occ_4326_df$lat,
xlim=c(min(Cyp_csl_4326_df$lon),max(Cyp_csl_4326_df$lon)),
ylim=c(min(Cyp_csl_4326_df$lat),max(Cyp_csl_4326_df$lat)),
type='p', col='red', xlab='x', ylab='y')
points(Cyp_csl_4326_df$lon, Cyp_csl_4326_df$lat, col='blue')
其中Cyp_csl_4326_df是一个数据框(蓝色点),Cyp_occ_4326_df是另一个数据框(红色点),它们在上部与第一个数据框重叠。
我通过以下方式创建了一个多边形:
poly1 <- Polygon(as.matrix(Cyp_csl_4326_df))
poly2 <- Polygon(as.matrix(Cyp_occ_4326_df))
p1 <- SpatialPolygons(list(Polygons(list(poly1), "p1"))
p2 <- SpatialPolygons(list(Polygons(list(poly2), "p2"))
res <- gDifference(p1, p2)
然后我得到了以下图片:
plot(res, col="blue")
问题是,我不知道如何将派生的多边形转换为点以供进一步使用。我得到的只是多边形的范围:
> summary(res)
Object of class SpatialPolygons
Coordinates:
min max
x 32.27102 34.58771
y 34.56312 35.69493
Is projected: NA
proj4string : [NA]
我找不到提取点的方法。
英文:
I have created a polygon by substracting two polygons, derived from large dataframes. More specific:
plot(Cyp_occ_4326_df$lon, Cyp_occ_4326_df$lat,
xlim=c(min(Cyp_csl_4326_df$lon),max(Cyp_csl_4326_df$lon)),
ylim=c(min(Cyp_csl_4326_df$lat),max(Cyp_csl_4326_df$lat)),
type='p', col='red', xlab='x', ylab='y')
points(Cyp_csl_4326_df$lon, Cyp_csl_4326_df$lat, col='blue')
where Cyp_csl_4326_df is a dataframe (blue points) and Cyp_occ_4326_df
is another dataframe (red points), which overlays with the first dataframe at the upper part.
I have created a polygon by substracting the two dataframes as follow:
poly1 <- Polygon(as.matrix(Cyp_csl_4326_df))
poly2 <- Polygon(as.matrix(Cyp_occ_4326_df))
p1 <- SpatialPolygons(list(Polygons(list(poly1), "p1")))
p2 <- SpatialPolygons(list(Polygons(list(poly2), "p2")))
res <- gDifference(p1, p2)
And I got the following pic:
plot(res, col="blue")
The problem is that I do not know how to convert the derived polygon to points for further use. What I get, is simply the extend of the polygon:
> summary(res)
Object of class SpatialPolygons
Coordinates:
min max
x 32.27102 34.58771
y 34.56312 35.69493
Is projected: NA
proj4string : [NA]
I could not find a way to extract points.
答案1
得分: 1
以下是您提供的代码部分的中文翻译:
# 我的答案基于`sf`包提供的数据以进行复制:
# 读取sf包中的`nc.shp`数据文件
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# 将数据转换为与您的类别相同
library(sp)
res <- as_Spatial(nc$geometry)
summary(res)
# 类别为SpatialPolygons的对象
# 坐标范围:
# 最小 最大
# x -84.32385 -75.45698
# y 33.88199 36.58965
# 投影状态:FALSE
# proj4string : [+proj=longlat +datum=NAD27 +no_defs]
通过这个示例数据,以下代码似乎有效:
# 加载data.table用于rbindlist()函数
library(data.table)
# 从SpatialPolygons中提取点
res_points <- lapply(1:length(res@polygons), function(i) {
lapply(1:length(res@polygons[[i]]@Polygons), function(j){
data.frame(res@polygons[[i]]@Polygons[[j]]@coords)
}) %>% rbindlist()
}) %>% rbindlist()
head(res_points)
# 前6行提取的点坐标:
# X1 X2
# 1: -81.47276 36.23436
# 2: -81.54084 36.27251
# 3: -81.56198 36.27359
# 4: -81.63306 36.34069
# 5: -81.74107 36.39178
# 6: -81.69828 36.47178
英文:
I base my answer on data from the sf
package for replication:
library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
# Convert data to the same class as yours
library(sp)
res <- as_Spatial(nc$geometry)
summary(res)
Object of class SpatialPolygons
Coordinates:
min max
x -84.32385 -75.45698
y 33.88199 36.58965
Is projected: FALSE
proj4string : [+proj=longlat +datum=NAD27 +no_defs]
With this example data the following code seems to work:
# Load data.table for the rbindlist() function
library(data.table)
# Extract points from SpatialPolygons
res_points <- lapply(1:length(res@polygons), function(i) {
lapply(1:length(res@polygons[[i]]@Polygons), function(j){
data.frame(res@polygons[[i]]@Polygons[[j]]@coords)
}) %>% rbindlist()
}) %>% rbindlist()
head(res_points)
X1 X2
1: -81.47276 36.23436
2: -81.54084 36.27251
3: -81.56198 36.27359
4: -81.63306 36.34069
5: -81.74107 36.39178
6: -81.69828 36.47178
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论