最終更新:2026-01-01

1 はじめに

  • 日本の市区町村地図行政境界データを使って、地図を作成し、Rで可視化する方法を説明する。

Rで利用しやすい行政境界データとしては、主に以下が挙げられる。

  1. 国土交通省国土地理院「地球地図日本」
  2. 国土交通省「国土数値情報 行政区域データ」
  3. 総務省「国勢調査」
  4. NipponMapパッケージ
  • このページでは、国土交通省「国土数値情報 行政区域データ」を用いて、市区町村境界データを取得し、Rで可視化する方法を説明する。

2 データの取得

  • 国土数値情報の「行政区域データ」から市区町村境界データをダウンロードする。
  • URL: https://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N03-2025.html
  • 最新のデータセットをダウンロードする。
  • 全国 世界測地系 2025年(令和7年) 603.03MB N03-20250101_GML.zip

  • ダウンロードしたファイルは以下の通り。
  • 以下の形式で提供されているが、今回は、シェープファイル形式を用いる。
  1. GML形式(JPGIS2014準拠)。
  2. GeoJSON形式
  3. シェープファイル形式。

  • N03-20250101_prefecture.shp: 都道府県境界データ
  • N03-20250101.shp: 市区町村境界データ

3 市区町村境界データ

3.1 読み込み

  • 座標参照系は、JGD2011(日本測地系2011、EPSG: 6668)である。
  • 座標形式は、経度・緯度である。平面直角座標\((X, Y)\)ではない。
  • フィーチャー数:124094個
  • 属性フィールド:6個
# 必要なパッケージの読み込み
library(sf)
# シェープファイルの読み込み
suuchi <- sf::st_read("N03-20250101_GML/N03-20250101.shp")
## Reading layer `N03-20250101' from data source 
##   `/Users/ayumu/Documents/GitHub/research/4_GIS/MapJapan/N03-20250101_GML/N03-20250101.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 124094 features and 6 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 122.9326 ymin: 20.42275 xmax: 153.9867 ymax: 45.55724
## Geodetic CRS:  JGD2011
# 座標系の確認
st_crs(suuchi)
## Coordinate Reference System:
##   User input: JGD2011 
##   wkt:
## GEOGCRS["JGD2011",
##     DATUM["Japanese Geodetic Datum 2011",
##         ELLIPSOID["GRS 1980",6378137,298.257222101,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["Horizontal component of 3D system."],
##         AREA["Japan - onshore and offshore."],
##         BBOX[17.09,122.38,46.05,157.65]],
##     ID["EPSG",6668]]

3.2 データ確認

  • 含まれる属性フィールドは以下の通りである。

  • 都道府県名、市区町村名などは日本語表記である。

  • N03_001: 都道府県名

  • N03_002: 北海道の振興局名

  • N03_003: 郡名

  • N03_004: 市区町村名

  • N03_005: 政令指定都市の行政区域名

  • N03_007: 全国地方公共団体コード

3.3 軽量化

  • N03-20250101.shpは、253MB程度の大きなデータである。このデータを、QGISで地図描画するのに時間がかからないが、Rで地図描画するのは非常に時間がかかる。
  • そのため、まず、精度を削減し、データを軽量化する。
  • 完成したデータは、N03-20250101_rev1.shp(2.6MB)として保存する。
library(sf)
suuchi2 <-  sf::st_simplify(suuchi, dTolerance = 500)  # 500m の精度で簡略化

# 保存
sf::st_write(suuchi2, "Data_output/N03-20250101_rev1.shp", delete_dsn = TRUE)
## Deleting source `Data_output/N03-20250101_rev1.shp' using driver `ESRI Shapefile'
## Writing layer `N03-20250101_rev1' to data source 
##   `Data_output/N03-20250101_rev1.shp' using driver `ESRI Shapefile'
## Writing 124094 features with 6 fields and geometry type Unknown (any).

3.4 市区町村ごとに結合

  • 市区町村コード(N03_007)ごとに地理データを結合し、フィーチャー数を削減する。
  • 完成したデータは、N03-20250101_rev2.shp(1.1MB)として保存する。
library(dplyr)
library(sf)

suuchi3 <- suuchi2 %>%
  group_by(N03_007) %>%
  summarise(
    geometry = sf::st_union(geometry),
    pref = first(N03_001),
    gun = first(N03_003),
    city = first(N03_004),
    ku = first(N03_005),
    code = first(N03_007)
    ) %>%
  ungroup()

# 保存
sf::st_write(suuchi3, "Data_output/N03-20250101_rev2.shp", delete_dsn = TRUE)
## Deleting source `Data_output/N03-20250101_rev2.shp' using driver `ESRI Shapefile'
## Writing layer `N03-20250101_rev2' to data source 
##   `Data_output/N03-20250101_rev2.shp' using driver `ESRI Shapefile'
## Writing 1905 features with 6 fields and geometry type Unknown (any).

3.5 可視化1

# 簡単な地図表示
library(sf)
# 地図表示(高速化)
plot(st_geometry(suuchi3))
TRUE

TRUE

3.6 可視化2

# 地図の作成

library(ggplot2)
ggplot(data = suuchi3) + # 地図データの指定
  geom_sf(fill = "lightgreen", color = "black") + # 塗り
  theme_minimal() + # ミニマルテーマの適用
  labs(title = "National Land Numerical Information",
       subtitle = "Administrative Boundaries of Japan Cities",
       caption = "Source: Ministry of Land, Infrastructure, Transport and Tourism")  +
       theme(axis.text = element_blank(), # 目盛りを非表示
        axis.title = element_blank()) # 軸タイトルを非表示
TRUE

TRUE

# 保存
ggsave("fig/Suuchi_Japan_City.png", width = 8, height = 6, dpi = 300)

4 都道府県境界データ

4.1 読み込み

  • 都道府県境界データを読み込む。
  • 座標参照系は、JGD2011(日本測地系2011、EPSG: 6668)である。
  • 座標形式は、経度・緯度である。平面直角座標\((X, Y)\)ではない。
  • フィーチャー数:121990個
  • 属性フィールド:7個
# 必要なパッケージの読み込み
library(sf)
# シェープファイルの読み込み
suuchi_pref <- sf::st_read("N03-20250101_GML/N03-20250101_prefecture.shp")
## Reading layer `N03-20250101_prefecture' from data source 
##   `/Users/ayumu/Documents/GitHub/research/4_GIS/MapJapan/N03-20250101_GML/N03-20250101_prefecture.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 121990 features and 6 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 122.9326 ymin: 20.42275 xmax: 153.9867 ymax: 45.55724
## Geodetic CRS:  JGD2011
# 座標系の確認
st_crs(suuchi_pref)
## Coordinate Reference System:
##   User input: JGD2011 
##   wkt:
## GEOGCRS["JGD2011",
##     DATUM["Japanese Geodetic Datum 2011",
##         ELLIPSOID["GRS 1980",6378137,298.257222101,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["geodetic latitude (Lat)",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["geodetic longitude (Lon)",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     USAGE[
##         SCOPE["Horizontal component of 3D system."],
##         AREA["Japan - onshore and offshore."],
##         BBOX[17.09,122.38,46.05,157.65]],
##     ID["EPSG",6668]]

4.2 データ確認

  • 含まれる属性フィールドは以下の通りである。

  • 都道府県名は日本語表記である。

  • N03_001: 都道府県名

  • N03_002: NA

  • N03_003: NA

  • N03_004: NA

  • N03_005: NA

  • N03_007: 全国地方公共団体コード

4.3 軽量化

  • N03-20250101_prefecture.shpは178MB程度の大きなデータであり、そのまま可視化するのは時間がかかる。
  • そのため、精度を削減し、データ量を軽量化する。
  • rmapshaper::ms_simplify(keep = 0.01, keep_shapes = TRUE)を用いると、1%の頂点を保持してデータを簡略化でき、地物間に隙間ができにくいが、処理中にRが停止したので、st_simplifyを用いる。
  • ジオメトリの簡略化については、Geocomputation with Rの解説が詳しい。
  • 完成したデータは、N03-20250101_prefecture_rev1.shp(1.8MB)として保存する。
library(sf)
# 500m の精度で簡略化
suuchi_pref2 <-  sf::st_simplify(suuchi_pref, dTolerance = 500)  
# 保存
sf::st_write(suuchi_pref2, "Data_output/N03-20250101_prefecture_rev1.shp", delete_dsn = TRUE)
## Deleting source `Data_output/N03-20250101_prefecture_rev1.shp' using driver `ESRI Shapefile'
## Writing layer `N03-20250101_prefecture_rev1' to data source 
##   `Data_output/N03-20250101_prefecture_rev1.shp' using driver `ESRI Shapefile'
## Writing 121990 features with 6 fields and geometry type Unknown (any).

4.4 都道府県ごとに結合

  • 都道府県名(N03_001)ごとに地理データを結合し、フィーチャー数を削減する。
  • 完成したデータは、N03-20250101_prefecture_rev2.shp(342KB)として保存する。
library(dplyr)
library(sf)

suuchi_pref3 <- suuchi_pref2 %>%
  group_by(N03_001) %>%
  summarise(
    geometry = sf::st_union(geometry),
    pref = first(N03_001),
    ) %>%
  ungroup()

# 保存
sf::st_write(suuchi_pref3, "Data_output/N03-20250101_prefecture_rev2.shp", delete_dsn = TRUE)
## Deleting source `Data_output/N03-20250101_prefecture_rev2.shp' using driver `ESRI Shapefile'
## Writing layer `N03-20250101_prefecture_rev2' to data source 
##   `Data_output/N03-20250101_prefecture_rev2.shp' using driver `ESRI Shapefile'
## Writing 47 features with 2 fields and geometry type Unknown (any).

4.5 可視化

# 簡単な地図表示
library(sf)
# 地図表示(高速化)
plot(st_geometry(suuchi_pref3))
TRUE

TRUE

参考文献