Created
July 14, 2017 00:08
-
-
Save uribo/e7c56f1b35a725e6fee821c9aa6d0770 to your computer and use it in GitHub Desktop.
国勢調査 小地域 Shapefileのダウンロード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(RSelenium) | |
library(sf) | |
library(leaflet) | |
select_pref <- function(target) { | |
jpndistrict::jpnprefs %>% | |
dplyr::select(code = jis_code, pref = prefecture) %>% | |
dplyr::filter(code == target | pref == target) | |
} | |
select_city <- function(ch_elm = childElems3, target = NULL) { | |
cities <- ch_elm %>% | |
purrr::map(function(x) { | |
x$getElementText() | |
} | |
# unlist(recursive = TRUE) | |
) %>% purrr::flatten() %>% purrr::flatten_chr() | |
df.city <- tibble::data_frame( | |
row = 1:length(cities), | |
code = cities %>% | |
purrr::map_chr(~ substr(x = ., start = 1, stop = 5)), | |
city = cities %>% | |
purrr::map_chr( | |
# スペース1個分挟む | |
# stringi::stri_length | |
~ substr(x = ., start = 7, stop = stringr::str_length(.)) | |
) | |
) | |
df.city %>% | |
dplyr::filter(code == target | city == target) | |
} | |
jp_chome_boundary <- function(pref, city, download_dir = "~/Downloads/") { | |
# pjsだとダウンロードフォルダが不明 | |
# remDr <- remoteDriver(browserName = "phantomjs") | |
# remDr$open(silent = TRUE) | |
rD <- rsDriver(verbose = FALSE) # Connecting to remote server | |
remDr <- rD[["client"]] | |
remDr$navigate("http://e-stat.go.jp/SG2/eStatGIS/page/download.html") | |
# 国勢調査を選択 | |
webElem <- remDr$findElement("css selector", "#censusclass_list") | |
childElems <- webElem$findChildElements("css selector", "#censusclass_list > option") | |
childElems[[2]]$clickElement() | |
# 平成27年国勢調査 (小地域) | |
Sys.sleep(3) | |
childElems <- remDr$findElement("css selector", "#census_list > option:nth-child(1)") | |
childElems$clickElement() | |
Sys.sleep(3) | |
x <- remDr$findElement("css selector", "#step2_T000849") | |
# x <- remDr$findElement("xpath", '//*[@id="step2_label_T000849"]') | |
x$clickElement() | |
Sys.sleep(3) | |
# # ダウンロードページへ遷移 | |
childElems <- remDr$findElement("css selector", "#page1 > div.main-action > p > input.button-important.long") | |
childElems$clickElement() | |
Sys.sleep(3) | |
childElems2 <- remDr$findElement("css selector", | |
paste0("#pref_list > option:nth-child(", select_pref(pref) %>% | |
magrittr::use_series(code) %>% as.numeric(), ")")) | |
childElems2$clickElement() | |
Sys.sleep(3) | |
childElems3 <- remDr$findElements("css selector", "#city_list > option") | |
childElems3[[select_city(childElems3, city) %>% magrittr::use_series(row)]]$clickElement() | |
# 「検索」 | |
x2 <- remDr$findElements("css selector", "p > input.button-normal.short") | |
x2[[1]]$clickElement() | |
Sys.sleep(7) | |
x3 <- remDr$findElements("css selector", "#step4_list2 > tbody > tr:nth-child(3) > td.tdw35p > a") | |
x3[[1]]$clickElement() | |
Sys.sleep(8) | |
# remDr$close() | |
remDr$close() | |
rD[["server"]]$stop() | |
# ファイル名をどうやって得るか。... 最新のダウンロード? | |
downloadfs <- list.files(download_dir, | |
pattern = ".zip$", | |
full.names = TRUE) | |
zippath <- tibble::data_frame( | |
file = downloadfs, | |
mtime = downloadfs %>% | |
purrr::invoke(file.mtime, path = .) | |
) %>% | |
# mtimeがさいしんのもの | |
dplyr::arrange(dplyr::desc(mtime)) %>% | |
dplyr::slice(1L) %>% | |
magrittr::use_series(file) | |
shpdir <- paste0(download_dir, gsub(".zip$", "", basename(zippath)), "/") | |
unzip(zippath, | |
exdir = shpdir) | |
p01 <- sf::read_sf(shpdir) %>% | |
sf::st_transform(4326) %>% | |
sf::st_transform("+init=epsg:4326") | |
res <- leaflet::leaflet() %>% leaflet::addTiles() %>% leaflet::addPolygons(data = p01, | |
label = ~MOJI) | |
return(res) | |
} | |
jp_chome_boundary(pref = 33, city = "倉敷市") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment