分配观察坐标为多个多边形

问题描述 投票:0回答:1

I有一个数据框,并在LAT/LON坐标处进行了观察。我有三个多边形跨越了进行观测的广泛区域。我想在新列中创建一个分组变量,该变量包含进行观察的多边形的简短字母代码。这是我的示例数据:

> dput(testdata)
structure(list(lon = c(-79.30315, -79.29561, -79.29572, -79.29833, 
-79.29603, -79.29659, -79.29097, -79.29347, -79.29347, -79.294, 
-79.29644, -79.29653, -79.27027, -79.27656, -79.26938, -79.28683, 
-79.29256, -79.28848, -79.29256, -79.29097, -79.292277, -79.29311, 
-79.29259, -79.292277, -79.29654, -79.29123, -79.3037, -79.28743, 
-79.29641, -79.29661, -79.29295, -79.29101, -79.2951, -79.29652, 
-79.29035, -79.29183, -79.29661, -79.30317, -79.28734, -79.28734, 
-79.296509, -79.24673, -79.24699, -79.24689, -79.24693, -79.26633, 
-79.2585, -79.25765, -79.27642, -79.2754, -79.28565, -79.29443, 
-79.28264, -79.24703, -79.24706, -79.24706, -79.247, -79.24863, 
-79.30019, -79.3023, -79.30154, -79.24672, -79.24672, -79.24672, 
-79.2969, -79.25789, -79.26389, -79.26274, -79.26976, -79.29689, 
-79.26999, -79.29676, -79.29634, -79.29671, -79.29676, -79.29739, 
-79.30154, -79.28073, -79.28101, -79.28164, -79.28447, -79.28244, 
-79.28674, -79.28123, -79.28515, -79.29752, -79.29449, -79.30283, 
-79.26671, -79.29492, -79.24765, -79.23946, -79.24728, -79.24826, 
-79.2477), lat = c(25.69511, 25.69913, 25.69909, 25.69808, 25.69791, 
25.6987, 25.6976, 25.69887, 25.69887, 25.69812, 25.698905, 25.69876, 
25.69545, 25.69664, 25.69499, 25.69707, 25.69877, 25.69738, 25.69877, 
25.69786, 25.698363, 25.69876, 25.69884, 25.698363, 25.699164, 
25.69808, 25.69149, 25.69682, 25.69919, 25.69897, 25.6987, 25.69785, 
25.69937, 25.69917, 25.69759, 25.69846, 25.69897, 25.69231, 25.69592, 
25.69592, 25.699178, 25.73256, 25.73151, 25.73248, 25.7323, 25.73618, 
25.75886, 25.76018, 25.69645, 25.69439, 25.69597, 25.69989, 25.69769, 
25.73171, 25.73161, 25.73156, 25.23161, 25.73258, 25.69319, 25.6926, 
25.69706, 25.73245, 25.73245, 25.73245, 25.70004, 25.77368, 25.7471, 
25.72693, 25.72724, 25.6997, 25.72725, 25.69891, 25.69924, 25.69885, 
25.69891, 25.69776, 25.69712, 25.72679, 25.72655, 25.73001, 25.72484, 
25.72546, 25.72225, 25.72625, 25.72549, 25.69781, 25.69989, 25.69358, 
25.69701, 25.69955, 25.73175, 25.73411, 25.73237, 25.73462, 25.73189
)), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 40L, 41L, 42L, 43L, 
44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 
57L, 58L, 59L, 60L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 
74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 
87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 
100L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 
145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L), class = "data.frame")
这些是我的三个多边形:

#POL1 coords1 <- matrix(c(-79.0877, 25.7213, -79.32764, 25.69931, -79.32665, 25.20054, -79.04101, 25.21296), ncol = 2, byrow = T) coords1 <- rbind(coords1, coords1[1, ]) # Ensure the polygon is closed by repeating the first coordinate at the end ### Create polygon pol_1 <- sf::st_sfc(sf::st_polygon(list(coords1)), crs = 4326) # POL2 ## Define pol boundaries coords2 <- matrix(c(-79.25322, 25.76284, -79.21888, 25.76624, -79.21459, 25.72063, -79.25613, 25.72349), ncol = 2, byrow = T) coords2 <- rbind(coords2, coords2[1, ]) # Ensure the polygon is closed by repeating the first coordinate at the end ## Create polygon pol_2 <- sf::st_sfc(sf::st_polygon(list(coords2)), crs = 4326) # POL3 ## Define pol boundaries coords3 <- matrix(c(-79.25446, 25.7759, -79.26378, 25.70252, -79.31184,25.71772, -79.26071, 25.77646), ncol = 2, byrow = T) coords3 <- rbind(coords3, coords3[1, ]) # Ensure the polygon is closed by repeating the first coordinate at the end ## Create polygon pol_3 <- sf::st_sfc(sf::st_polygon(list(coords3)), crs = 4326)
I使用以下代码单独创建一个具有True/False的新列,如果数据点在三个多边形之一之内。然后,我使用for循环填充了包含nr的另一列,称为

loc

。多边形
### Check within which polygon each data point lies test_sf <- sf::st_as_sf(testdata, coords = c("lon", "lat"), crs = 4326) test_sf $in_one <- sf::st_within(test_sf , pol_1, sparse = FALSE) test_sf $in_two <- sf::st_within(test_sf , pol_2, sparse = FALSE) test_sf $in_three <- sf::st_within(test_sf , pol_3, sparse = FALSE) ### convert back to dataframe test_loc<- st_drop_geometry(test_sf) ### initiate new column test_loc$loc <- NA ### Loop over each row for(i in 1:nrow(test_loc)) { # Loop over the columns (check each column for TRUE) if(test_loc[i, 3] == TRUE & test_loc[i,4] == FALSE & test_loc[i,5] == FALSE) { test_loc[i, 6] <- "One"} else if(test_loc[i, 3] == FALSE & test_loc[i,4] == TRUE & test_loc[i,5] == FALSE) { test_loc[i, 6] <- "Two"} else if(test_loc[i, 3] == FALSE & test_loc[i,4] == FALSE & test_loc[i,5] == TRUE) { test_loc[i, 6] <- "Three"} else {NA} }

wo,这是一个很长的代码,因为我认为可以更有效地做到这一点,如果将所有三个多边形一步评估,并且在所有三个多边形中填写
loc
列,并且使用多边形的名称填充新列。但是,我正在努力完成这项工作。任何帮助都非常感谢。

描述的操作通常称为空间连接,为此,您通常要处理

sf
r spatial
1个回答
0
投票
我将从将这些单独的多边形收集到一个单个

sf

对象中(请注意,您在那里有一个小的重叠),添加一些识别属性,然后将其添加到点。
st_join


如果需要,请转换为常规框架:
sf

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.