Skip to content

Commit

Permalink
Closed #15
Browse files Browse the repository at this point in the history
  • Loading branch information
uribo committed Jun 7, 2018
1 parent e8067b4 commit 28fa34c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
38 changes: 30 additions & 8 deletions R/export_mesh.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Export district's mesh polygon
#'
#' @param jis_code jis code for prefecture and city identifical number
#' @importFrom dplyr mutate select everything
#' @importFrom dplyr filter mutate select everything
#' @importFrom jpmesh fine_separate mesh_to_coords
#' @importFrom magrittr use_series
#' @importFrom purrr map set_names pmap
Expand All @@ -26,33 +26,55 @@ mesh_district <- function(jis_code = NULL) {
df_tmp$id <- 1:nrow(df_tmp)

df_pref10km_mesh <- jpmesh::sf_jpmesh[df_tmp %>%
dplyr::filter(!is.na(res_contains)) %>%
tidyr::unnest() %>%
magrittr::use_series(id) %>%
unique(), ] %>%
.$meshcode %>%
purrr::map(jpmesh::fine_separate) %>%
rlang::flatten_chr() %>%
unique()

sf_prefmesh <- df_pref10km_mesh %>%
unique() %>%
tibble::as_tibble() %>%
purrr::set_names("meshcode") %>%
dplyr::mutate(out = purrr::pmap(., ~ jpmesh::mesh_to_coords(...))) %>%
tidyr::unnest() %>%
dplyr::select(meshcode, dplyr::everything()) %>%
dplyr::mutate(geometry = purrr::pmap(., ~ jpmesh:::mesh_to_poly(...))) %>%
sf::st_sf(crs = 4326)
sf::st_sf(crs = 4326, stringsAsFactors = FALSE)

df_tmp <- tibble::tibble(
res_contains = suppressMessages(sf::st_intersects(sf_prefmesh,
res_contains = suppressMessages(sf::st_intersects(df_pref10km_mesh,
sf_pref) %>% as.numeric()))
df_tmp$id <- 1:nrow(df_tmp)

sf_prefmesh[df_tmp %>%
df_pref1km_mesh <-
df_pref10km_mesh[df_tmp %>%
dplyr::filter(!is.na(res_contains)) %>%
tidyr::unnest() %>%
magrittr::use_series(id) %>%
unique(), ] %>%
.$meshcode %>%
purrr::map(jpmesh::fine_separate) %>%
rlang::flatten_chr() %>%
unique() %>%
tibble::as_tibble() %>%
sf::st_sf(crs = 4326)
purrr::set_names("meshcode") %>%
dplyr::mutate(out = purrr::pmap(., ~ jpmesh::mesh_to_coords(...))) %>%
tidyr::unnest() %>%
dplyr::select(meshcode, dplyr::everything()) %>%
dplyr::mutate(geometry = purrr::pmap(., ~ jpmesh:::mesh_to_poly(...))) %>%
sf::st_sf(crs = 4326, stringsAsFactors = FALSE)

df_tmp <- tibble::tibble(
res_contains = suppressMessages(sf::st_intersects(df_pref1km_mesh,
sf_pref) %>% as.numeric()))
df_tmp$id <- 1:nrow(df_tmp)

df_pref1km_mesh[df_tmp %>%
dplyr::filter(!is.na(res_contains)) %>%
tidyr::unnest() %>%
magrittr::use_series(id) %>%
unique(), ]


}
7 changes: 6 additions & 1 deletion tests/testthat/test-export.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
context("test-export.R")

d <- mesh_district(jis_code = 33101)
d2 <- mesh_district(jis_code = 14401)

test_that("mesh_district", {
expect_is(
Expand All @@ -9,7 +10,11 @@ test_that("mesh_district", {
)
expect_equal(
dim(d),
c(11264L, 6L)
c(511L, 6L)
)
expect_equal(
dim(d2),
c(54L, 6L)
)
expect_named(
d,
Expand Down

0 comments on commit 28fa34c

Please sign in to comment.