如何判断鸟类观测点位是否在新西兰多边形范围内并筛选数据
Got it, let's walk through how to solve this—identifying those offshore bird observation points using the NZ polygon from maps::map("nz") is totally doable, and we'll cover both mapping and prepping the data for your analysis.
Step 1: Load Required Packages & Prep the NZ Boundary
First, we'll grab the NZ map data and convert it into a spatial object that makes it easy to check if points fall inside the landmass. We'll use sf (simple features) because it's great for straightforward spatial operations in R:
library(maps) library(sf) library(dplyr) # Get NZ polygon data and convert to an sf object nz_land <- map("nz", fill = TRUE, plot = FALSE) %>% st_as_sf()
Step 2: Convert Your Observation Data to Spatial Points
Next, turn your Count.df into spatial points—we just need to tell R which columns are longitude/latitude, and set the coordinate reference system (CRS) to WGS84 (EPSG:4326), which matches the maps package's default:
# Convert Count.df to sf spatial points count_points <- st_as_sf(Count.df, coords = c("longitude", "latitude"), crs = 4326)
Step 3: Flag Offshore Points
Use a spatial intersection check to mark which points are outside the NZ land polygon. st_within() returns TRUE for points inside the landmass, so we flip it with ! to flag offshore locations:
# Check which points are inside NZ land is_onshore <- st_within(count_points, nz_land, sparse = FALSE)[, 1] # Add a flag column to your data count_points <- count_points %>% mutate(is_offshore = !is_onshore)
Step 4: Map Onshore vs Offshore Points
Now you can plot the points with different colors to highlight offshore locations. Here's a clean ggplot2 version:
library(ggplot2) ggplot() + # Plot NZ landmass geom_sf(data = nz_land, fill = "olivedrab", color = "darkgreen") + # Plot onshore points (blue) geom_sf(data = filter(count_points, !is_offshore), color = "steelblue", size = 1, alpha = 0.7) + # Plot offshore points (red, more prominent) geom_sf(data = filter(count_points, is_offshore), color = "firebrick", size = 1.5) + labs(title = "NZ Bird Observations: Onshore vs Offshore", subtitle = "Red points = offshore/marine locations") + theme_minimal()
If you prefer base R plotting instead:
# Base R map map("nz", fill = TRUE, col = "olivedrab", border = "darkgreen") # Add onshore points points(Count.df$longitude[is_onshore], Count.df$latitude[is_onshore], col = "steelblue", pch = 16, cex = 0.8) # Add offshore points points(Count.df$longitude[!is_onshore], Count.df$latitude[!is_onshore], col = "firebrick", pch = 16, cex = 1.2) legend("topright", legend = c("Onshore", "Offshore"), col = c("steelblue", "firebrick"), pch = 16)
Step 5: Extract Offshore Data for Further Analysis
Finally, pull out the offshore points into a separate dataset if you need it for downstream work:
# Extract offshore observations (remove spatial geometry if needed) offshore_data <- count_points %>% filter(is_offshore) %>% st_drop_geometry() # Use this line to get a regular data frame without spatial info
内容的提问来源于stack exchange,提问作者jvan257

