如何计算时序访问多边形的相邻重叠百分比?(R技术求助)
Got it, let's work through this to get you the sequential overlap percentages you need. The key is to iterate through your ordered polygon list, compare each polygon to the one right before it, handle cases where there's no overlap, and package the results neatly.
Step 1: Convert Your Polygon List to SpatialPolygons Objects
First, we need to turn each individual Polygon in your list into a SpatialPolygons object—since the intersect() function from the sp package works with these. Here's a quick way to do this in bulk:
library(sp) # Assuming your polygon list is named Polygon.list sp_polygons_list <- lapply(seq_along(Polygon.list), function(i) { SpatialPolygons(list(Polygons(list(Polygon.list[[i]]), ID = as.character(i)))) })
Step 2: Calculate Sequential Overlaps & Percentages
Next, we'll loop through the list starting from the second polygon, compare it to the previous one, compute the intersection area, and calculate the overlap percentage relative to both the current and previous polygon. We'll use tryCatch() to gracefully handle cases where there's no overlap (which would otherwise throw an error).
# Initialize a dataframe to store results overlap_results <- data.frame( polygon_index = integer(), prev_polygon_index = integer(), overlap_area = numeric(), overlap_pct_of_prev = numeric(), overlap_pct_of_current = numeric(), stringsAsFactors = FALSE ) # Iterate through each polygon starting from the 2nd one for (i in 2:length(sp_polygons_list)) { current_poly <- sp_polygons_list[[i]] prev_poly <- sp_polygons_list[[i-1]] # Calculate intersection, handle no overlap with tryCatch intersection <- tryCatch({ intersect(current_poly, prev_poly) }, error = function(e) { NULL # Return NULL if no overlap }) # Calculate areas and percentages if (!is.null(intersection)) { overlap_area <- area(intersection) prev_area <- area(prev_poly) current_area <- area(current_poly) pct_prev <- round((overlap_area / prev_area) * 100, 2) pct_current <- round((overlap_area / current_area) * 100, 2) } else { overlap_area <- NA_real_ pct_prev <- 0 # Or NA if you prefer to mark no overlap explicitly pct_current <- 0 } # Add to results dataframe overlap_results <- rbind(overlap_results, data.frame( polygon_index = i, prev_polygon_index = i-1, overlap_area = overlap_area, overlap_pct_of_prev = pct_prev, overlap_pct_of_current = pct_current )) }
Step 3: Check the Results
If you run this with your sample data, you'll get:
- For polygon 2 vs 1: 100% overlap in both directions (since they're identical)
- For polygon 3 vs 2: 0% overlap (or NA if you adjusted the
elseblock)
Key Notes:
- Handling No Overlap: The
tryCatch()catches the error that occurs whenintersect()can't find any overlapping area, and we set the overlap values to 0 (or NA if you want to distinguish between "no overlap" and "calculation error"). - Scalability: This loop works for any number of polygons in your list—no need to hardcode combinations like the
combn()approach you referenced. - Result Format: The dataframe gives you clear, structured output with indices, raw overlap area, and percentages relative to both the current and previous polygon.
If you prefer using lapply() instead of a for loop, here's an equivalent version:
# Using lapply instead of for loop overlap_list <- lapply(2:length(sp_polygons_list), function(i) { current_poly <- sp_polygons_list[[i]] prev_poly <- sp_polygons_list[[i-1]] intersection <- tryCatch(intersect(current_poly, prev_poly), error = function(e) NULL) if (!is.null(intersection)) { overlap_area <- area(intersection) data.frame( polygon_index = i, prev_polygon_index = i-1, overlap_area = overlap_area, overlap_pct_of_prev = round((overlap_area / area(prev_poly))*100,2), overlap_pct_of_current = round((overlap_area / area(current_poly))*100,2) ) } else { data.frame( polygon_index = i, prev_polygon_index = i-1, overlap_area = NA_real_, overlap_pct_of_prev = 0, overlap_pct_of_current = 0 ) } }) # Combine list into dataframe overlap_results <- do.call(rbind, overlap_list)
Either approach will get you the sequential overlap metrics you need, even with thousands of polygons.
内容的提问来源于stack exchange,提问作者Ana

