Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

st_union issues #275

Closed
bhaskarvk opened this issue Mar 27, 2017 · 2 comments

Comments

@bhaskarvk
Copy link
Contributor

@bhaskarvk bhaskarvk commented Mar 27, 2017

I'm trying to migrate my tilegramsR package from sp to sf.
One of the operations I do is combine polygons by an attribute. I was using rmapshaper::ms_dissolve for it

Here's how it works in the old sp way of things

library(sp)

evs <- tilegramsR::Pitch_US_Population_2016_v1
class(evs)
plot(evs) # Each polygon is an electoral vote

# Merge electoralvote polygons to state polygons using rmapshaper
evs_states <- rmapshaper::ms_dissolve(evs, 'state')
plot(evs_states) # Each polygon is a state

Before Combining

Imgur

After rmapshaper::ms_dissolve

Imgur

I converted the evs to an sf object and then tried st_union after grouping by state, but I get some extra lines (see California, Florida states).

sf_evs <- st_as_sf(evs)
plot(sf_evs['state'])

sf_evs_states <- dplyr::group_by(sf_evs, state) %>%
  dplyr::summarise(geometry = st_union(geometry)) %>%
  dplyr::ungroup()

plot(sf_evs_states['state'])

Imgur

Full code is uploaded at Rpubs

@bhaskarvk bhaskarvk changed the title sf_combine issues st_union issues Mar 27, 2017
@edzer

This comment has been minimized.

Copy link
Member

@edzer edzer commented Mar 27, 2017

Nice job for setting the precision: use e.g.

st_precision(sf_evs) = 10000

(or use st_set_precision in a pipe) after defining st_evs`:
evs

@bhaskarvk

This comment has been minimized.

Copy link
Contributor Author

@bhaskarvk bhaskarvk commented Mar 27, 2017

That works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.