Open and merge multiple shapefiles
source link: https://www.tuicool.com/articles/hit/jQZzYrJ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
(This article was first published on r.iresmi.net , and kindly contributed toR-bloggers)
or more precisely union many spatial tables in R in a tidy way.
- dplyr::bind_rows doesn’t work on sf objects ;
- base::rbind only work on two tables and so that’s not straightforward to use*.
So we’ll use purrr::map and tidyr::unnest.
First get some data, the communes of three french départements :
library(tidyverse) library(sf) library(fs) library(httr) library(leaflet) # https://fr.actualitix.com/blog/shapefiles-des-departements-de-france.html url <- c("https://fr.actualitix.com/blog/actgeoshap/01-Ain.zip", "https://fr.actualitix.com/blog/actgeoshap/73-savoie.zip", "https://fr.actualitix.com/blog/actgeoshap/74-haute-savoie.zip") dep <- str_extract(url, "\\d{2}.*$") list(url, dep) %>% pwalk(~ GET(.x, write_disk(.y))) walk(dep, unzip, junkpaths = TRUE, exdir = "shp")
We can then create a 3 rows data frame containing a list-column in which we store the sf object. Then we just unnest it. This operation erases the sf-class, we have to add it back.
res <- dir_ls("shp", glob = "*.shp") %>% tibble(fname = .) %>% mutate(data = map(fname, read_sf)) %>% unnest(data) %>% st_as_sf() %>% st_set_crs(2154) write_sf(res, "shp/3dep.shp") res %>% st_transform(4326) %>% leaflet() %>% addPolygons() %>% addTiles()
Bonus : we have the source filename stored in the resulting shapefile.
* We could have used
dir_ls("shp", glob = "*.shp") %>% map(read_sf) %>% do.call(rbind, .)
but the column structure doesn’t match here…
Recommend
-
24
The Zig stdlib often uses open single dispatch eg: // in stdlib pub fn serialize(self: *Serializer, value: var) !void { const T = comptime @TypeOf(value); if (compti...
-
8
DeFi and Bitcoin can finally come together, thanks to RSK.Bitcoin is considered the crown jewel of cryptocurrency, so it is essential for DeFi’s wonders to be fully felt via it.The RSK based ‘Money on Chain’ protocol is...
-
19
Spark – Cannot perform Merge as multiple source rows matched… In SQL when you are syncing a table (target) from an another table (source) you need to make sure there are no duplicates or repeated datasets in either of the So...
-
3
How to Open Multiple Websites With a Shortcut in Windows 11 By Jack Slater Published 14 hours ago While you can make a w...
-
11
Vim open multiple files Jul 20, 2013 vim -p file1 file2 ... vim -o file1 file2 ... vim -O file1 file2 ... 其中 -p 是在 tab 打开,
-
5
Francis T. O'Donovan Senior Data Science Manager at Hospital IQ – Planet discoverer, researcher, dev...
-
4
Know someone who can answer? Share a link to this Before answeringYou should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more i...
-
0
How to open File Explorer with multiple tabs in Windows 11...
-
0
How can I create a git feature branch that can merge into multiple other branches?
-
0
Support is great. Feedback is even better."Hey there! We hope ONEPDF has met your expectations. We'd love to hear your feedback! Please consider leaving a review to help us improve and serve you better."The makers of Me...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK