Skip to content

Commit ca3fd4a

Browse files
committed
Refactoring landnuminfo urbanarea (not finished)
1 parent bb57f67 commit ca3fd4a

4 files changed

Lines changed: 21 additions & 27 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: geojp
22
Type: Package
33
Title: Download Spatial Data Sets of Japan
4-
Version: 0.0.6
4+
Version: 0.0.7
55
Authors@R: person("Yoshihiko", "Baba", role = c("aut", "cre"), email = "babayoshihiko@mac.com", comment = c(ORCID = "0000-0001-7034-9724"))
66
Description: Easy access to various spatial data sets of Japan as 'sf' objects in R. The package includes a wide range of geospatial data available at various geographic scales and for various years with harmonized attributes, projection and fixed topology.
77
License: MIT + file LICENSE

R/landnuminfo.R

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ read_landnuminfo <- function(maptype, code_pref, code_muni = NULL, year = 2020,
2929
year4digit <- check_year(year)
3030
strTempDir <- check_data_dir(data_dir)
3131
code_pref <- check_code_pref_as_char(code_pref)
32-
code_muni <- check_code_muni_as_char(code_muni, code_pref)
32+
code_muni <- check_code_muni_as_char(code_pref, code_muni)
3333

3434
if (mode(year) == "numeric"){
3535
year = year_2digit(year)
@@ -150,6 +150,7 @@ get_sfLNI <- function(maptype, strLNIFile1, strLNIFile2, strLNIFile3, strLNIUrl,
150150
message(paste("Downloaded the file and saved in", strTempDir))
151151
}
152152
unzip_ja(strLNIZip, exdir = strTempDir)
153+
message(paste("Unipped the file in", strTempDir))
153154
# Checks if the shp file exists
154155
strLNIFile <- get_sfLNI_file(strLNIFile1, strLNIFile2, strLNIFile3, strTempDir, multifiles)
155156
}
@@ -668,27 +669,28 @@ read_landnuminfo_officiallandprice <- function(code_pref, code_muni = NULL, year
668669
#'
669670
#' @param code_pref The 2-digit code of prefecture.
670671
#' @param code_muni Optional. The 3-digit code of municipality. If specified, subtract the data by the column A48_003.
671-
#' @param year Year of the data. Defaults to 2018.
672+
#' @param year Year of the data. The available years are 2018, 2011 and 2006. Defaults to 2018.
672673
#' @param data_dir The directory to store downloaded zip and extracted files. If not specified, the data will be stored in a temp directory and will be deleted after you quit the session.
673674
#'
674675
#'
675676
#' @return An `"sf" "data.frame"` object with extra attr "col" for tmap.
676677
#'
677678
#' @export
678-
read_landnuminfo_urbanarea <- function(code_pref, code_muni, year = 2018, data_dir = NULL){
679+
read_landnuminfo_urbanarea <- function(code_pref,
680+
code_muni,
681+
year = 2018,
682+
data_dir = NULL){
679683
year = check_year(year)
680-
if (year != 2011 && year != 2018) stop(paste("The data is not available for year", year))
681684
if (year == 2018 && code_pref == 23) {
682685
year = 2011
683686
warning("The data is not available for year 2018 for Aichi prefcture. Uses year 2011 instead.")
684687
}
685688

686-
if (year == 2006) {
687-
sfLNI = read_landnuminfo("A09", code_pref, code_muni, year, filetype = "shp", geometry = "POLYGON", data_dir = data_dir)
688-
} else if (year == 2011) {
689-
sfLNI = read_landnuminfo_urbanarea_2011(code_pref, data_dir = data_dir)
689+
if (year == 2011) {
690+
sfLNI <- read_landnuminfo_urbanarea_2011(code_pref, data_dir = data_dir)
690691
} else {
691-
sfLNI = read_landnuminfo("A09", code_pref, code_muni, year, filetype = "geojson", geometry = "POLYGON", data_dir = data_dir)
692+
#sfLNI = read_landnuminfo("A09", code_pref, code_muni, year, filetype = "shp", geometry = "POLYGON", data_dir = data_dir)
693+
sfLNI <- read_landnuminfo_by_csv("A09", code_pref, code_muni, year, data_dir = data_dir)
692694
}
693695

694696
sfLNI$layer = NA
@@ -698,29 +700,14 @@ read_landnuminfo_urbanarea <- function(code_pref, code_muni, year = 2018, data_d
698700
"\u7528\u9014\u672a\u8a2d\u5b9a",
699701
"\u90fd\u5e02\u5730\u57df"))
700702
# 2011
701-
if (year == 2011 || year == 2006) {
703+
if (year == 2011) {
702704
st_crs(sfLNI) <- "EPSG:4612"
703705
sfLNI[sfLNI$layer_no == 1, "layer"] <- "\u90fd\u5e02\u5730\u57df"
704706
sfLNI[sfLNI$layer_no == 2, "layer"] <- "\u5e02\u8857\u5316\u533a\u57df"
705707
sfLNI[sfLNI$layer_no == 3, "layer"] <- "\u5e02\u8857\u5316\u8abf\u6574\u533a\u57df"
706708
sfLNI[sfLNI$layer_no == 4, "layer"] <- "\u305d\u306e\u4ed6\u7528\u9014\u5730\u57df"
707-
} else {
708-
# 2018
709-
st_crs(sfLNI) <- "EPSG:6668"
710-
sfLNI[sfLNI$layer_no == 1, "layer"] <- "\u5e02\u8857\u5316\u533a\u57df"
711-
sfLNI[sfLNI$layer_no == 2, "layer"] <- "\u5e02\u8857\u5316\u8abf\u6574\u533a\u57df"
712-
sfLNI[sfLNI$layer_no == 3, "layer"] <- "\u305d\u306e\u4ed6\u7528\u9014\u5730\u57df"
713-
sfLNI[sfLNI$layer_no == 4, "layer"] <- "\u7528\u9014\u672a\u8a2d\u5b9a"
714709
}
715710

716-
sfLNI$layer_no = factor(sfLNI$layer_no,
717-
levels = c(1,2,3,4))
718-
attr(sfLNI, "mapname") = "\u90fd\u5e02\u5730\u57df\u30c7\u30fc\u30bf"
719-
attr(sfLNI, "sourceName") = "\u300c\u56fd\u571f\u6570\u5024\u60c5\u5831\uff08\u5730\u4fa1\u516c\u793a\u30c7\u30fc\u30bf\uff09\u300d\uff08\u56fd\u571f\u4ea4\u901a\u7701\uff09"
720-
attr(sfLNI, "sourceURL") = "https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-A09.html"
721-
attr(sfLNI, "col") = "layer"
722-
# attr(sfLNI, "palette") = c("#16A085","#D1F2EB","#1F618D","#229954","#BA4A00","#E74C3C","#808B96")
723-
724711
return(sfLNI)
725712
}
726713

R/sysdata.rda

221 Bytes
Binary file not shown.

R/utils.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#' @export
1616
check_year <- function(x) {
1717
if (missing(x)) stop("The argument is missing (check_year)")
18-
if (is.null(x)) stop("The argument is missing (check_year)")
18+
if (is.null(x)) stop("The argument is NULL (check_year)")
1919

2020
intYear = 0
2121
if (mode(x) == "numeric") {
@@ -349,6 +349,10 @@ unzip_ja <- function(zipfile, files = NULL, exdir) {
349349
#'
350350
#' @export
351351
year_2digit <- function(year) {
352+
if (missing(year)) stop("The argument is missing (year_2digit)")
353+
if (is.null(year)) stop("The argument is NULL (year_2digit)")
354+
year <- as.integer(year)
355+
352356
if (year > 2009) {
353357
year_2d = as.character(as.integer(year) - 2000)
354358
} else if (year > 1999) {
@@ -361,6 +365,9 @@ year_2digit <- function(year) {
361365
}
362366

363367
check_data_dir <- function(data_dir){
368+
if (missing(data_dir)) stop("The argument is missing (check_data_dir)")
369+
if (is.null(data_dir)) stop("The argument is NULL (check_data_dir)")
370+
364371
strTempDir = tempdir()
365372
if (!is.null(data_dir)) {
366373
if (dir.exists(data_dir)) {

0 commit comments

Comments
 (0)