Skip to content

Commit

Permalink
Add sample codes for grid_components() to README
Browse files Browse the repository at this point in the history
  • Loading branch information
UchidaMizuki committed May 26, 2024
1 parent 73a367c commit 187f140
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 30 deletions.
19 changes: 19 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ neighborhood[[1]] |>
geom_sf_text(aes(label = as.character(grid_neighborhood)))
```

### メッシュの連結成分を取得

`grid_components()`関数は,メッシュの連結成分を算出し,クラスターIDを返します.

- `grid_neighborhood()`関数と同じく`n``type`を指定できます.

```{r}
set.seed(1234)
grid_city_2020 |>
filter(str_starts(city_code, "121")) |>
slice_sample(prop = 0.5) |>
mutate(cluster = grid_components(grid,
type = "von_neumann")) |>
grid_as_sf(crs = JGD2011) |>
ggplot(aes(fill = fct_shuffle(as_factor(cluster)))) +
geom_sf(show.legend = FALSE)
```

### メッシュ間の線分描画

`grid_line()`関数により,メッシュ間の線分上に存在するメッシュを抽出します.
Expand Down
72 changes: 49 additions & 23 deletions README.en.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
output: github_document
editor_options:
markdown:
wrap: sentence
---

<!-- README.md is generated from README.Rmd. Please edit that file -->
Expand All @@ -14,18 +17,20 @@ knitr::opts_chunk$set(
)
```

# jpgrid <a href="https://uchidamizuki.github.io/jpgrid/"><img src="man/figures/logo.png" align="right" height="139" /></a>
# jpgrid <a href="https://uchidamizuki.github.io/jpgrid/"><img src="man/figures/logo.png" align="right" height="139"/></a>

<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/jpgrid)](https://CRAN.R-project.org/package=jpgrid)
[![R-CMD-check](https://github.com/UchidaMizuki/jpgrid/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/UchidaMizuki/jpgrid/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/UchidaMizuki/jpgrid/branch/main/graph/badge.svg)](https://app.codecov.io/gh/UchidaMizuki/jpgrid?branch=main)

[![CRAN status](https://www.r-pkg.org/badges/version/jpgrid)](https://CRAN.R-project.org/package=jpgrid) [![R-CMD-check](https://github.com/UchidaMizuki/jpgrid/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/UchidaMizuki/jpgrid/actions/workflows/R-CMD-check.yaml) [![Codecov test coverage](https://codecov.io/gh/UchidaMizuki/jpgrid/branch/main/graph/badge.svg)](https://app.codecov.io/gh/UchidaMizuki/jpgrid?branch=main)

<!-- badges: end -->

jpgrid is an R package for using the JIS (Japan Industrial Standard) X 0410 '[Grid Square Code](https://www.jisc.go.jp/app/jis/general/GnrJISNumberNameSearchList?show&jisStdNo=X0410)'.
grid square codes are square-like regional divisions set up for all regions of Japan based on longitude and latitude. For more information, please check [the Statistics Bureau of Japan page](https://www.stat.go.jp/data/mesh/pdf/gaiyo1.pdf).
grid square codes are square-like regional divisions set up for all regions of Japan based on longitude and latitude.
For more information, please check [the Statistics Bureau of Japan page](https://www.stat.go.jp/data/mesh/pdf/gaiyo1.pdf).

A summary of the grid square codes is shown below. In jpgrid, each code is distinguished by the length of a piece of grid, such as `grid_80km`.
A summary of the grid square codes is shown below.
In jpgrid, each code is distinguished by the length of a piece of grid, such as `grid_80km`.

```{r,echo=FALSE}
df <- tibble::tibble(`Name` = c("1st grid", "2nd grid", "3rd grid", "1/2 grid", "1/4 grid", "1/8 grid", "1/10 subdivision of the 3rd grid"),
Expand All @@ -37,9 +42,9 @@ knitr::kable(df)
jpgrid has been developed to enable faster processing than the R package [jpmesh](https://github.com/uribo/jpmesh).
The main differences between jpgrid and jpmesh are as follows

1. Explicitly given a grid size (such as `grid_80km`).
2. Non-land (ocean) grids are supported.
3. It can extract nth-order neighboring grids and perform complex operations such as extracting grids and calculating the distance of (line) paths between grids.
1. Explicitly given a grid size (such as `grid_80km`).
2. Non-land (ocean) grids are supported.
3. It can extract nth-order neighboring grids and perform complex operations such as extracting grids and calculating the distance of (line) paths between grids.

## Installation

Expand All @@ -55,6 +60,7 @@ You can also install the development version from GitHub.
# install.packages("devtools")
devtools::install_github("UchidaMizuki/jpgrid")
```

## Usage

```{r,message=FALSE}
Expand Down Expand Up @@ -101,9 +107,9 @@ grid_city_2020 |>

Use `parse_grid()` to generate grid square codes from strings or numbers.

- Specify the grid size as `grid_size = "80km"`.
- If `grid_size = NULL`, the grid size is automatically determined.
- The default (`strict = TRUE`) requires the grid square codes to have a given number of digits.
- Specify the grid size as `grid_size = "80km"`.
- If `grid_size = NULL`, the grid size is automatically determined.
- The default (`strict = TRUE`) requires the grid square codes to have a given number of digits.

```{r}
x <- c("53394526313", "5339358633", "533945764", "53394611", "523503", "5339", NA)
Expand All @@ -125,8 +131,8 @@ parse_grid(x,
Use `grid_convert()` to coarsen the grid size of grid square codes.
The `grid_subdivide()` function can be used to subdivide grid square codes.

- `grid_subdivide()` outputs a list of grid square codes whose elements are contained in the original grids.
- The conversion between 500m grid and 100m grid is supported.
- `grid_subdivide()` outputs a list of grid square codes whose elements are contained in the original grids.
- The conversion between 500m grid and 100m grid is supported.

```{r}
grid_500m <- parse_grid("533945764", "500m")
Expand Down Expand Up @@ -169,8 +175,8 @@ tibble(grid = parse_grid(c("5339452660", "5235034590"), "100m")) |>

The `grid_neighborhood()` function calculates the neighboring grids.

- nth order neighboring grids can be calculated by specifying `n`.
- You can specify a Neumann neighborhood with `type = "von_neumann"` and a Moore neighborhood with `type = "moore"`.
- nth order neighboring grids can be calculated by specifying `n`.
- You can specify a Neumann neighborhood with `type = "von_neumann"` and a Moore neighborhood with `type = "moore"`.

```{r}
neighborhood <- parse_grid("644142", "10km") |>
Expand Down Expand Up @@ -199,6 +205,25 @@ neighborhood[[1]] |>
geom_sf_text(aes(label = as.character(grid_neighborhood)))
```

### Get the connected components of grid square codes

`grid_components()` calculates the connected components of grid square codes and returns the cluster ID.

- You can specify `n` and `type` as in the `grid_neighborhood()`.

```{r}
set.seed(1234)
grid_city_2020 |>
filter(str_starts(city_code, "121")) |>
slice_sample(prop = 0.5) |>
mutate(cluster = grid_components(grid,
type = "von_neumann")) |>
grid_as_sf(crs = JGD2011) |>
ggplot(aes(fill = fct_shuffle(as_factor(cluster)))) +
geom_sf(show.legend = FALSE)
```

### Draw line segments between grids

The `grid_line()` function extracts grids that lie on the line segments between grids.
Expand All @@ -218,8 +243,8 @@ tibble::tibble(grid = line[[1]]) |>

It can handle the case of passing through multiple grids by giving a `list` of grids.

- Close the line segment with `close = TRUE`.
- `skip_na = TRUE` to skip `NA`.
- Close the line segment with `close = TRUE`.
- `skip_na = TRUE` to skip `NA`.

```{r}
grid_1 <- parse_grid(c("6441", "5339", NA, "5250"), "80km")
Expand All @@ -240,7 +265,7 @@ tibble::tibble(grid = line[[1]]) |>

The `grid_distance()` function calculates the distance between grids (great circle distance).

- As with `grid_line()`, the path distance can be calculated by `list` of grids.
- As with `grid_line()`, the path distance can be calculated by `list` of grids.

```{r}
grid_from <- parse_grid(c("6441", "5339"), "80km")
Expand All @@ -252,8 +277,9 @@ print(distance)
```

### Others
- `grid_move()` function can be used to calculate the grid square code in the east-west and north-south directions.
- For grid outside the range of the 80 km grid, where the digits are negative or exceed three digits, the relevant code is displayed as `<-1>` or `<123>` to clearly distinguish them from existing grids.

- `grid_move()` function can be used to calculate the grid square code in the east-west and north-south directions.
- For grid outside the range of the 80 km grid, where the digits are negative or exceed three digits, the relevant code is displayed as `<-1>` or `<123>` to clearly distinguish them from existing grids.

## Comparison of processing speed with jpmesh package

Expand Down Expand Up @@ -283,6 +309,6 @@ microbenchmark::microbenchmark(`jpgrid::grid_to_coords()` = grid_to_coords(grid_
autoplot()
```

<img src="man/figures/README-microbenchmark-coords-to-grid.png" width="100%" />
<img src="man/figures/README-microbenchmark-coords-to-grid.png" width="100%"/>

<img src="man/figures/README-microbenchmark-grid-to-coords.png" width="100%" />
<img src="man/figures/README-microbenchmark-grid-to-coords.png" width="100%"/>
33 changes: 28 additions & 5 deletions README.en.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# jpgrid <a href="https://uchidamizuki.github.io/jpgrid/"><img src="man/figures/logo.png" align="right" height="139" /></a>
# jpgrid <a href="https://uchidamizuki.github.io/jpgrid/"><img src="man/figures/logo.png" align="right" height="139"/></a>

<!-- badges: start -->

Expand All @@ -10,6 +10,7 @@ status](https://www.r-pkg.org/badges/version/jpgrid)](https://CRAN.R-project.org
[![R-CMD-check](https://github.com/UchidaMizuki/jpgrid/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/UchidaMizuki/jpgrid/actions/workflows/R-CMD-check.yaml)
[![Codecov test
coverage](https://codecov.io/gh/UchidaMizuki/jpgrid/branch/main/graph/badge.svg)](https://app.codecov.io/gh/UchidaMizuki/jpgrid?branch=main)

<!-- badges: end -->

jpgrid is an R package for using the JIS (Japan Industrial Standard) X
Expand Down Expand Up @@ -276,6 +277,28 @@ neighborhood[[1]] |>

<img src="man/figures/README-unnamed-chunk-11-1.png" width="100%" />

### Get the connected components of grid square codes

`grid_components()` calculates the connected components of grid square
codes and returns the cluster ID.

- You can specify `n` and `type` as in the `grid_neighborhood()`.

``` r
set.seed(1234)

grid_city_2020 |>
filter(str_starts(city_code, "121")) |>
slice_sample(prop = 0.5) |>
mutate(cluster = grid_components(grid,
type = "von_neumann")) |>
grid_as_sf(crs = JGD2011) |>
ggplot(aes(fill = fct_shuffle(as_factor(cluster)))) +
geom_sf(show.legend = FALSE)
```

<img src="man/figures/README-unnamed-chunk-12-1.png" width="100%" />

### Draw line segments between grids

The `grid_line()` function extracts grids that lie on the line segments
Expand All @@ -294,7 +317,7 @@ tibble::tibble(grid = line[[1]]) |>
geom_sf_text(aes(label = as.character(grid)))
```

<img src="man/figures/README-unnamed-chunk-12-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-13-1.png" width="100%" />

It can handle the case of passing through multiple grids by giving a
`list` of grids.
Expand All @@ -317,7 +340,7 @@ tibble::tibble(grid = line[[1]]) |>
geom_sf_text(aes(label = as.character(grid)))
```

<img src="man/figures/README-unnamed-chunk-13-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-14-1.png" width="100%" />

### Calculation of distance between grids

Expand Down Expand Up @@ -352,6 +375,6 @@ The conversion speed between grids and latitude/longitude in this
package is several tens to several hundred times faster than in the
jpmesh package.

<img src="man/figures/README-microbenchmark-coords-to-grid.png" width="100%" />
<img src="man/figures/README-microbenchmark-coords-to-grid.png" width="100%"/>

<img src="man/figures/README-microbenchmark-grid-to-coords.png" width="100%" />
<img src="man/figures/README-microbenchmark-grid-to-coords.png" width="100%"/>
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ neighborhood[[1]] |>

<img src="man/figures/README-unnamed-chunk-11-1.png" width="100%" />

### メッシュの連結成分を取得

`grid_components()`関数は,メッシュの連結成分を算出し,クラスターIDを返します.

- `grid_neighborhood()`関数と同じく`n``type`を指定できます.

``` r
set.seed(1234)

grid_city_2020 |>
filter(str_starts(city_code, "121")) |>
slice_sample(prop = 0.5) |>
mutate(cluster = grid_components(grid,
type = "von_neumann")) |>
grid_as_sf(crs = JGD2011) |>
ggplot(aes(fill = fct_shuffle(as_factor(cluster)))) +
geom_sf(show.legend = FALSE)
```

<img src="man/figures/README-unnamed-chunk-12-1.png" width="100%" />

### メッシュ間の線分描画

`grid_line()`関数により,メッシュ間の線分上に存在するメッシュを抽出します.
Expand All @@ -288,7 +309,7 @@ tibble::tibble(grid = line[[1]]) |>
geom_sf_text(aes(label = as.character(grid)))
```

<img src="man/figures/README-unnamed-chunk-12-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-13-1.png" width="100%" />

メッシュの`list`を与えることで複数メッシュを通る場合に対応可能です.

Expand All @@ -310,7 +331,7 @@ tibble::tibble(grid = line[[1]]) |>
geom_sf_text(aes(label = as.character(grid)))
```

<img src="man/figures/README-unnamed-chunk-13-1.png" width="100%" />
<img src="man/figures/README-unnamed-chunk-14-1.png" width="100%" />

### メッシュ間距離の算出

Expand Down
Binary file modified man/figures/README-unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/README-unnamed-chunk-13-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/README-unnamed-chunk-14-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 187f140

Please sign in to comment.