Simple feature collection with 7 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -180 ymin: -18.28799 xmax: 180 ymax: 83.23324
Geodetic CRS: WGS 84
# A tibble: 7 × 4
TYPE GEOUNIT ISO_A3 geometry
<chr> <chr> <chr> <MULTIPOLYGON [°]>
1 Sovereign country Fiji FJI (((180 -16.06713, 180 -16.5…
2 Sovereign country Tanzania TZA (((33.90371 -0.95, 34.07262…
3 Indeterminate Western Sahara ESH (((-8.66559 27.65643, -8.66…
4 Sovereign country Canada CAN (((-122.84 49, -122.9742 49…
5 Country United States of America USA (((-122.84 49, -120 49, -11…
6 Sovereign country Kazakhstan KAZ (((87.35997 49.21498, 86.59…
7 Sovereign country Uzbekistan UZB (((55.96819 41.30864, 55.92…
We can split MULTIPOLYGONs into their component POLYGONs with st_cast(). Russia consists of 14 different shapes:
(russia_polygons <- russia |>st_geometry() |>st_cast("POLYGON"))
Geometry set for 14 features
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -180 ymin: 41.15142 xmax: 180 ymax: 81.2504
Geodetic CRS: WGS 84
First 5 geometries:
plot(russia_polygons[2])
plot(russia_polygons[14])
Identifying the Crimea
Plotting each POLYGON is brittle (counts/order change by map scale). Instead, place a POINT in Crimea (e.g., 45°N, 34°E) and select the Russian POLYGON that contains it:
st_intersects() tells us which POLYGON contains the POINT:
# Extract the Russia MULTIPOLYGON and convert it to polygonsrussia_polygons <- world |>filter(admin =="Russia") |>st_geometry() |>st_cast("POLYGON")# Extract the Russia polygon that has Crimea in itcrimea_polygon <- russia_polygons |>keep(\(x) st_intersects(x, crimea_point, sparse =FALSE))# This is the same as russia_polygons[14]plot(crimea_polygon)
Extracting Crimea polygon from Russia
Now we can remove Crimea from Russia
# Remove Crimea from Russianew_russia <- russia_polygons |>discard(\(x) any(st_equals(x, crimea_polygon, sparse =FALSE))) |>st_combine() |>st_cast("MULTIPOLYGON")ggplot() +geom_sf(data = world, fill = clr_land) +geom_sf(data = new_russia, fill = rus_red) +geom_sf(data = ukraine, fill = ukr_blue, color = ukr_yellow, linewidth =2) +coord_sf(xlim =c(ukraine_bbox["xmin"], ukraine_bbox["xmax"]), ylim =c(ukraine_bbox["ymin"], ukraine_bbox["ymax"]) ) +theme_void() +theme(panel.background =element_rect(fill = clr_ocean))
Extracting Crimea polygon from Russia
Adding Crimea to Ukraine
# Extract the Ukraine MULTIPOLYGON and convert it to polygonsukraine_polygons <- world |>filter(admin =="Ukraine") |>st_geometry() |>st_cast("POLYGON")# Add Crimea to Ukrainetrue_ukraine <-st_union(c(ukraine_polygons, crimea_polygon)) |>st_cast("MULTIPOLYGON")ggplot() +geom_sf(data = world, fill = clr_land) +geom_sf(data = new_russia, fill = rus_red) +geom_sf(data = true_ukraine, fill = ukr_blue, color = ukr_yellow, linewidth =2) +coord_sf(xlim =c(ukraine_bbox["xmin"], ukraine_bbox["xmax"]), ylim =c(ukraine_bbox["ymin"], ukraine_bbox["ymax"]) ) +theme_void() +theme(panel.background =element_rect(fill = clr_ocean))