Skip to content

Commit

Permalink
Fixed #18
Browse files Browse the repository at this point in the history
  • Loading branch information
uribo committed Jun 21, 2018
1 parent 3919f16 commit 9ca1273
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Depends:
Imports:
dplyr (>= 0.7.4),
leaflet (>= 1.1.0),
lwgeom (>= 0.1-4),
magrittr (>= 1.5),
miniUI (>= 0.1.1),
purrr (>= 0.2.4),
Expand Down
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import(miniUI)
import(rlang)
import(sf)
import(shiny)
importFrom(dplyr,arrange)
importFrom(dplyr,everything)
importFrom(dplyr,filter)
importFrom(dplyr,if_else)
Expand All @@ -23,6 +24,7 @@ importFrom(dplyr,matches)
importFrom(dplyr,mutate)
importFrom(dplyr,right_join)
importFrom(dplyr,select)
importFrom(dplyr,slice)
importFrom(jpmesh,coords_to_mesh)
importFrom(jpmesh,fine_separate)
importFrom(jpmesh,mesh_to_coords)
Expand All @@ -31,12 +33,12 @@ importFrom(magrittr,extract)
importFrom(magrittr,use_series)
importFrom(purrr,map)
importFrom(purrr,pmap)
importFrom(purrr,pmap_dbl)
importFrom(purrr,reduce)
importFrom(purrr,set_names)
importFrom(sf,st_buffer)
importFrom(sf,st_contains)
importFrom(sf,st_intersects)
importFrom(sf,st_is)
importFrom(sf,st_point)
importFrom(sf,st_read)
importFrom(sf,st_sf)
Expand Down
25 changes: 21 additions & 4 deletions R/find_city.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#' Detect prefecture by coordinates
#'
#' @inheritParams find_city
#' @importFrom dplyr mutate select right_join
#' @importFrom purrr set_names
#' @importFrom sf st_is st_point
#' @importFrom dplyr arrange mutate select slice right_join
#' @importFrom purrr pmap_dbl set_names
#' @import sf
#' @note The `find_pref` function was added in version 0.3.0
#' @examples
#' \dontrun{
Expand All @@ -15,7 +15,7 @@
#' }
#' @export
find_pref <- function(longitude, latitude, geometry = NULL, ...) {
pref_code <- prefecture <- city_code <- NULL
. <- pref_code <- prefecture <- city_code <- dist <- NULL

if (!is.null(geometry)) {
if (sf::st_is(geometry, "POINT")) {
Expand All @@ -28,6 +28,23 @@ find_pref <- function(longitude, latitude, geometry = NULL, ...) {

res <- find_city(longitude, latitude, ...)
if (!is.null(res)) {
if (nrow(res) > 1) {
res <-
res %>%
dplyr::mutate(dist = purrr::pmap_dbl(
., ~
sf::st_distance(sf::st_centroid(..4) %>%
sf::st_sfc(crs = 4326),
sf::st_point(c(longitude, latitude)) %>%
sf::st_sfc(crs = 4326), by_element = TRUE)))

res <-
res %>%
dplyr::arrange(dist) %>%
dplyr::slice(1L) %>%
dplyr::select(-dist)
}

df_tmp <- res %>%
dplyr::mutate(pref_code = substr(city_code, 1, 2)) %>%
dplyr::select(pref_code, prefecture)
Expand Down
6 changes: 6 additions & 0 deletions tests/testthat/test-find_city.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ test_that("prefecture", {
expect_equal(test$pref_code, "24")
test <- find_prefs(geometry = sf::st_point(c(136.6833, 35.05)))
expect_equal(dim(test), c(6, 4))

skip_if_not_installed("lwgeom")
test <-
find_pref(135.8167, 35.3)
expect_equal(dim(test), c(1, 3))

})

test_that("Failed", {
Expand Down

0 comments on commit 9ca1273

Please sign in to comment.