获取美国和加拿大各县的中心经纬度

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

我能够获得美国每个县和加拿大人口普查地理单位(CGU)的中心纬度和经度。然而,在这个过程中出现了一些虚假的县,特别是在五大湖地区。在该地区,五大湖(州/国际边界?)内有一些随机多边形产生这些虚假县。有没有办法删除这些水生多边形,以便我只获得陆地多边形的中心纬度和经度?

library(ggplot2)
library(dplyr)
library(geodata)
library(sf)

# Gets the polygons of each US county and Canadian CGU

can <- gadm(country = 'CAN', level = 2, path=tempdir()) %>%
  st_as_sf()

usa <- gadm(country = 'USA', level = 2, path=tempdir()) %>%
  st_as_sf()

# Gets the central latitude and longitude of each county and CGU

centroids_can <- can %>% 
  st_centroid(of_largest_polygon = TRUE)

centroids_usa <- usa %>% 
  st_centroid(of_largest_polygon = TRUE)

# Map of centroids

ggplot() +
  geom_sf(data = can,
          mapping = aes(geometry = geometry),
          color = "black") +
  geom_sf(data = usa,
          mapping = aes(geometry = geometry),
          color = "black") +
  geom_sf(data = centroids_can,
          mapping = aes(geometry = geometry),
          color = "black",
          fill = 'red',
          shape = 21,
          size = 2) +
  geom_sf(data = centroids_usa,
          mapping = aes(geometry = geometry),
          color = "black",
          fill = 'red',
          shape = 21,
          size = 2) +
  coord_sf(xlim = c(-93, -81.5), ylim = c(41.5, 50), expand = F) +
  theme_bw() +
  theme(plot.margin = grid::unit(c(2,2,0,2), "mm"),
        text = element_text(size = 16),
        axis.text.x = element_text(size = 14, color = "black"),
        axis.text.y = element_text(size = 14, color = "black"),
        panel.grid.major = element_blank())

明显不正确的县和 CGU 是黄色的,红色的看起来是正确的。我说很明显,因为五大湖的水生区域没有县或 CGU。

r gis geospatial spatial geo
© www.soinside.com 2019 - 2024. All rights reserved.