1

I'm trying to merge groups of adjacent polygons, but I'm getting big multipolygons with non-adjacent areas. In the code block below plot(Matsuyama.sf) shows a large contiguous region and a few islands, but I can't extract those geometries. How do I get those areas and geometries.

library(sf)
library(tidyverse)
Matsuyama.sf <- st_read("https://geoshape.ex.nii.ac.jp/city/geojson/20210101/38/38201A1968.geojson")
Matsuyama.sf <- st_transform(Matsuyama.sf, crs=4326)
plot(Matsuyama.sf)
st_area(Matsuyama.sf)

I can split into hundreds of polygons, but the code options below just lump them back together into one

split.sf <- st_cast(Matsuyama.sf, "POLYGON") 
clumps_1.sf <- st_join(split.sf, split.sf, join = st_intersects)
clumps_2sf <- Matsuyama.sf %>% mutate(INTERSECT = st_intersects(.))

What am I missing?

Mark R
  • 775
  • 1
  • 8
  • 23
  • Can you define which geometries you want to extract & how you want to group the polygons? – mrhellmann Dec 15 '22 at 19:10
  • I’d like the polygons for individual islands (mostly top left) and for contiguous Matsuyama city. (large block bottom right) Those are the solid-colored, joined polygons generated by plot(Matsuyama.sf). So I can see what I want but can’t extract it! – Mark R Dec 15 '22 at 19:56
  • Does `st_cast(Matsuyama.sf, 'POLYGON')` not give you what you want? – mrhellmann Dec 15 '22 at 20:15
  • No, `st_cast(Matsuyama.sf, 'POLYGON')` creates an sfc_POLYGON of length 196. – Mark R Dec 15 '22 at 21:33
  • My question is how to use `st_union()` on that object to get about 20 objects. I think I might need a loop to "growth" the adjacent polygons – Mark R Dec 15 '22 at 21:36
  • 1
    It can be done, but you have to be able to define how to group the objects. – mrhellmann Dec 15 '22 at 21:58

0 Answers0