2

I'm trying to aggregate polygons at different geographic levels. As a hypothetical and reproducible example, let's suppose that I want to aggregate US county data set at the state level (of course, I can directly retrieve state-level polygons if this was my "actual" problem).

I've tried to do it by applying st_union for each group, where the group is each state. The code seems to work fine, but when I try to use it for plotting or other data manipulation, R throws an error message as below.

Error in CPL_geos_is_empty(st_geometry(x)) : Not a matrix.

In general, if we have multiple levels of geography within a data (e.g., state, county, city, zip code, etc) and if we want to aggregate it at different levels, what's the best approach?

counties <- USAboundaries::us_counties() 

state_union <- counties %>% 
  group_by(state_abbr) %>%
  summarise(geometry = sf::st_union(geometry)) %>%
  ungroup() %>% st_as_sf()

plot(st_geometry(state_union))
5
  • I am experiencing this as well when using ` y %>%group_by(DN)%>% summarize(geometry = st_union(geometry))` but only for certain files. Casting to multipolygon or polygon does not resolve this issue for me.
    – GISHuman
    Mar 31 '20 at 15:07
  • 1
    Actually, try this instead: counties %>% group_by(state_abbr) %>%summarize()
    – GISHuman
    Mar 31 '20 at 15:20
  • Thanks for the comments! Yea, indeed it has been discussed in Github (github.com/r-spatial/sf/issues/131).
    – qnp1521
    Mar 31 '20 at 21:06
  • Did that line of code solve the issue for you?
    – GISHuman
    Apr 1 '20 at 22:22
  • Yea it worked for me!
    – qnp1521
    Apr 2 '20 at 18:32
3

Instead try using this instead:

counties <- USAboundaries::us_counties() 

state_union <- counties %>% 
  group_by(state_abbr) %>%
  summarise() %>%
  ungroup() %>% st_as_sf()

Taking out the union statement seems to resolve the issue for me. I think it may be a bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.