R 对多边形进行分类并对其加权
嗨,我有这个数据集:R 对多边形进行分类并对其加权,r,raster,sf,R,Raster,Sf,嗨,我有这个数据集: > head(sp_csv_data) Longitude Latitude Radius Site_Type 1 -177.87567 -24.715167 10 MIG 2 -83.21360 14.401800 1 OBS 3 -82.59392 9.589192 1 NES 4 -82.41060 9.492750 1 NES;BRE 5 -81.17555
> head(sp_csv_data)
Longitude Latitude Radius Site_Type
1 -177.87567 -24.715167 10 MIG
2 -83.21360 14.401800 1 OBS
3 -82.59392 9.589192 1 NES
4 -82.41060 9.492750 1 NES;BRE
5 -81.17555 7.196750 5 OBS
6 -80.95770 8.852700 1 NES
经度和纬度是站点中心的坐标。场地是圆,半径列是场地的半径。最后一列是场地类型
我需要将这些.csv信息转换为光栅。我能够画出圆圈并将其光栅化。现在我需要根据站点类型对它们进行分类。到目前为止,该层的外观如下所示:
> print(sp_robinson)
Simple feature collection with 46519 features and 1 field
geometry type: POLYGON
dimension: XY
bbox: xmin: -16675160 ymin: -5378567 xmax: 16655730 ymax: 6089138
CRS: +proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
First 10 features:
layer geometry
1 69 POLYGON ((11889163 6089138,...
2 69 POLYGON ((11927764 6089138,...
3 69 POLYGON ((11966365 6089138,...
4 73 POLYGON ((12622585 6089138,...
5 73 POLYGON ((12661186 6089138,...
6 73 POLYGON ((12699787 6089138,...
7 73 POLYGON ((12738389 6089138,...
8 73 POLYGON ((12776990 6089138,...
9 73 POLYGON ((12815591 6089138,...
10 73 POLYGON ((12854192 6089138,...
你知道我该如何根据场地类型对圆圈进行分类吗
这是我到目前为止使用的代码:
lon <- c(-177.87567, -83.2136, -82.59392, -82.4106, -81.17555, -80.9577)
lat <- c(-24.715167, 14.4018, 9.589192, 9.49275, 7.19675, 8.8527)
radius <- c(10, 1, 1, 1, 5, 1)
site <- c("MIG", "OBS", "NES", "NES;BRE", "OBS", "NES")
sp_csv_data <- data.frame(lon, lat, radius, site)
####Defining Datum and Coordinates system (best to do first to avoid errors later)
rob_pacific <- "+proj=robin +lon_0=180 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
longlat <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
####Creating the nodes####
nodes <- sp_csv_data %>%
st_buffer(dist = sp_csv_data$Radius)
####Converting to raster####
# Creating a empty raster at 0.5° resolution (you can increase the resolution to get a better border precision)
rs <- raster(ncol = 360*2, nrow = 180*2)
rs[] <- 1:ncell(rs)
crs(rs) <- CRS(longlat)
##Convering to raster
sp_raster <- rasterize(nodes, rs)
# Resampling to make sure that it's in the same resolution as sampling area
sp_raster <- resample(sp_raster, rs, resample = "ngb")
#converting into an sf spatial polygon dataframe
sp_raster <- as(sp_raster, "SpatialPolygonsDataFrame")
species_sp <- spTransform(sp_raster, CRS(longlat))
# Define a long & slim polygon that overlaps the meridian line & set its CRS to match that of world
polygon <- st_polygon(x = list(rbind(c(-0.0001, 90),
c(0, 90),
c(0, -90),
c(-0.0001, -90),
c(-0.0001, 90)))) %>%
st_sfc() %>%
st_set_crs(longlat)
# Transform the species distribution polygon object to a Pacific-centred projection polygon object
sp_robinson <- species_sp %>%
st_as_sf() %>%
st_difference(polygon) %>%
st_transform(crs = rob_pacific)
# There is a line in the middle of Antarctica. This is because we have split the map after reprojection. We need to fix this:
bbox1 <- st_bbox(sp_robinson)
bbox1[c(1,3)] <- c(-1e-5,1e-5)
polygon1 <- st_as_sfc(bbox1)
crosses1 <- sp_robinson %>%
st_intersects(polygon1) %>%
sapply(length) %>%
as.logical %>%
which
# Adding buffer 0
sp_robinson[crosses1, ] %<>%
st_buffer(0)
lon我向您展示了如何使您的示例更具可复制性。请相应地编辑您的问题,谢谢您的帮助和建议!我真的很感激,我对这个很陌生。。。我希望这是更清楚,更可复制!我想说的是,在我的回答中,我展示了如何使代码具有可复制性,因为您可以复制和粘贴代码,然后运行它。因此,如果你从这开始,你也可以显示你在哪里卡住了。你正在做很多事情。我的建议是首先在lon/lat光栅上进行光栅化。如果你有,下一件事就是担心你想要使用的不寻常的crs。谢谢!我现在明白多了!正如你所看到的,我能够光栅化我的圆圈,现在我所需要的就是对它们进行分类!我现在明白了,我在重做很多无用的步骤,但我认为现在它更一般,更容易。再次感谢!