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

Combine polygons by attribute #382

Closed
Nowosad opened this issue Jun 13, 2017 · 6 comments
Closed

Combine polygons by attribute #382

Nowosad opened this issue Jun 13, 2017 · 6 comments

Comments

@Nowosad
Copy link
Contributor

Nowosad commented Jun 13, 2017

related to #275

I've combined polygons by attribute using group_by() and summarise(). It leaves some lines, so I've tried @edzer tip about using st_set_precision(). However, every value I've tested (100 - 1000000000) keeps some "artefacts". What's the best way to deal with that kind of problems?

# devtools::install_github("nowosad/spData")
library(spData)
library(sf)
library(tidyverse)

world_continents1 = world %>% 
  group_by(continent) %>% 
  summarise(pop = sum(pop, na.rm = TRUE), country_n = n())
plot(world_continents1[1]) 

world_continents2 = world %>% 
  st_set_precision(10000) %>% 
  group_by(continent) %>% 
  summarise(pop = sum(pop, na.rm = TRUE), country_n = n())
plot(world_continents2[1]) 

rplot

@edzer
Copy link
Member

edzer commented Jun 13, 2017

Which lines do you see? You're sure these are not holes?

@Nowosad
Copy link
Contributor Author

Nowosad commented Jun 13, 2017

Yes, I'm certain.

Data before group_by() and summarise() - no holes:

plot(world[0])

rplot01

Data after group_by() and summarise(), different value of precision - "artefacts" in a different places (e.g. North America):

world_continents3 = world %>% 
  st_set_precision(100) %>% 
  group_by(continent) %>% 
  summarise(pop = sum(pop, na.rm = TRUE), country_n = n())
plot(world_continents3[1]) 

rplot02

@edzer
Copy link
Member

edzer commented Jun 13, 2017

None of the geometries you get are actual lines, all have dimension 2 and positive area:

> world_continents2[1] %>% st_area() %>% units::set_units(km^2)
Units: km^2
[1] 29946212.22 12335958.52 31252444.82 23065279.95 24484309.31  8504478.20
[7]    11602.57 17762597.45
> world_continents2[1] %>% st_dimension()
[1] 2 2 2 2 2 2 2 2

what is of course possible is that there's no man's land between polygons. Africa seems to have 4 holes:

> world_continents2[1] %>% st_cast("MULTIPOLYGON") %>% st_geometry() %>% sapply(lengths)
[[1]]
[1] 1 5

[[2]]
[1] 1 1 1 1 1 1 1 1

[[3]]
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1

[[4]]
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1

[[5]]
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[39] 1 1 1 1 1 1 1 1 1

[[6]]
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

[[7]]
[1] 1

[[8]]
[1] 1 1 1

Maybe use mapview to explore them, and mapedit to remove them?

@tim-salabim
Copy link
Member

Thanks for the use-case :-)

@Nowosad
Copy link
Contributor Author

Nowosad commented Jun 13, 2017

@edzer Thanks for the investigation. I'm just still unsure why the number and location of these holes changes with different values of a "precision".

@tim-salabim I'll definitely try to use mapedit.

@edzer
Copy link
Member

edzer commented Jun 13, 2017

@Nowosad see also ?st_as_binary for more docs, also try precision values smaller than 0; create a toy example to understand how it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants