Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
library(RSelenium)
library(sf)
library(magrittr)
library(tidyverse)
download_smallshape <- function(pref, city, download_dir = "~/Downloads") {
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment