[英]Plot spatial polygon on worldmap and extract the coordinates within it (R)
給定一組全球分布的坐標,我想在世界地圖上 plot 一個多邊形(最好是一個矩形),然后提取這些圖中的所有坐標。 多邊形的位置將根據點密度手動放置,但它們的大小必須保持不變。
在這里,我提供了一個在歐洲隨機分布的點的最小可重現示例。 我還添加了一個定向圖像,希望有助於理解所需的 output。 首先,我想在 map 上添加多邊形,然后提取該區域內的所有點。 感謝您提前提供任何可能的幫助。
#Load libraries
library(rgdal) #v1.5-28
library(rgeos) #v.0.5-9
library(ggplot2) # 3.3.5
library(rworldmap) #plot worldmap v.1.3-6
library(dplyr) #v.1.0.7
#Create dataframe of coordinates that fall in Europe
coord <- data.frame(cbind(runif(1000,-15,45),runif(1000,30,75)))
colnames(coord) <- c("long","lat")
#Exlude ocean points following this post
#https://gis.stackexchange.com/questions/181586/remove-points-which-are-out-of-shapefile-or-raster-extent
URL <- "http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/110m/physical/ne_110m_ocean.zip"
fil <- basename(URL)
if (!file.exists(fil)) download.file(URL, fil)
fils <- unzip(fil)
oceans <- readOGR(grep("shp$", fils, value=TRUE), "ne_110m_ocean",
stringsAsFactors=FALSE, verbose=FALSE)
europe_coord <- data.frame(long = coord$long,
lat = coord$lat)
coordinates(europe_coord) <- ~long+lat
proj4string(europe_coord) <- CRS(proj4string(oceans))
ocean_points <- over(europe_coord, oceans)
#Add ocean points to dataset
coord$ocean <- ocean_points$featurecla
#Exlude ocean points
europe_land <- coord %>% filter(is.na(ocean))
#Load worldmap
world <- map_data("world")
#Plot europe spatial data
ggplot() + geom_map(data = world, map = world,
aes(long, lat, map_id = region), color = "white",
fill = "lightgray", size = 0.1) +
geom_point(data = europe_land,aes(long, lat),
alpha = 0.7, size = 0.05) + ylim(0,70) +
coord_sf(xlim = c(-15, 45), ylim = c(30, 75), expand = FALSE)
您可以將對象轉換為sf
,然后使用st_intersection
提取給定多邊形的點。 首先,我創建了一個邊界框(您可以在此處輸入所需多邊形范圍的坐標)。 然后,我將europe_land
轉換為sf
object。 然后,我使用st_intersection
只返回落在多邊形內的點。
library(sf)
bb <-
c(
"xmin" = 5.005,
"xmax" = 12.005,
"ymin" = 45.008,
"ymax" = 51.005
) %>%
sf::st_bbox() %>%
sf::st_as_sfc() %>%
sf::st_as_sf(crs = 4326) %>%
sf::st_transform(crs = 4326)
europe_land_sf <- europe_land %>%
st_as_sf(coords = c("long", "lat"), dim = "XY") %>%
st_set_crs(4326)
bb_extraction <- st_intersection(bb, europe_land_sf)
Output
head(bb_extraction)
Simple feature collection with 6 features and 1 field
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 7.136638 ymin: 45.69418 xmax: 11.78427 ymax: 49.51203
Geodetic CRS: WGS 84
ocean x
1 <NA> POINT (7.136638 46.90647)
2 <NA> POINT (9.035649 45.81661)
3 <NA> POINT (10.69865 45.91507)
4 <NA> POINT (11.78427 48.55559)
5 <NA> POINT (10.90349 45.69418)
6 <NA> POINT (9.417477 49.51203)
對於繪圖,一旦您創建了sf
object,然后您可以在現有代碼中使用geom_sf
將多邊形添加到世界 map。
ggplot() +
geom_map(
data = world,
map = world,
aes(long, lat, map_id = region),
color = "white",
fill = "lightgray",
size = 0.1
) +
geom_point(data = europe_land,
aes(long, lat),
alpha = 0.7,
size = 0.05) + ylim(0, 70) +
geom_sf(data = bb, colour = "black", fill = NA) +
coord_sf(xlim = c(-15, 45),
ylim = c(30, 75),
expand = FALSE)
Output
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.