オープンデータ

データやソフトウェア類の利用においては、必ず、各自で利用条件、著作権などを確認のこと。

目次

画像分類データセット

顔データセット

物体検出、セグメンテーションのデータセット

奥行き(デプス)、シーンフローのデータセット

体験:郵便番号データ(日本郵便のオープンデータ)を SQLite 3 で扱う

日本郵便は、「郵便番号データダウンロード」のページで、郵便番号データを公開している。ここでは、次の2つのデータをダウンロードし、SQLite 3 のデータベースに取り込み、SQL による検索を体験する。

郵便番号データは毎月更新される。以下の手順では、ダウンロードした時点での最新データが使用される。

前準備

手順

  1. 郵便番号データのダウンロード

    日本郵便の「郵便番号データダウンロード」のページ https://www.post.japanpost.jp/zipcode/download.html を開く。

    • 「住所の郵便番号(1レコード1行、UTF-8形式)」のページで、「最新データのダウンロード」をクリックして、utf_ken_all.zip をダウンロードする。
    • 「事業所の個別郵便番号」のページで、「最新データのダウンロード」をクリックして、jigyosyo.zip をダウンロードする。

    端末でダウンロードを行うときは、次のコマンドを実行する。

    cd /tmp
    curl -L -O https://www.post.japanpost.jp/zipcode/dl/utf/zip/utf_ken_all.zip
    curl -L -O https://www.post.japanpost.jp/zipcode/dl/jigyosyo/zip/jigyosyo.zip
    
  2. ダウンロードしたファイルの展開(解凍)

    ダウンロードした2つのファイルは zip 形式である。展開(解凍)を行う。

    cd /tmp
    unzip -o utf_ken_all.zip
    unzip -o jigyosyo.zip
    

    展開の結果、utf_ken_all.csvJIGYOSYO.CSV ができることを確認する。

    ls -l utf_ken_all.csv JIGYOSYO.CSV
    
  3. 文字コードの変換

    JIGYOSYO.CSV の文字コードは Shift_JIS である。SQLite 3 では UTF-8 を使用するので、UTF-8 に変換する。utf_ken_all.csv は、はじめから UTF-8 なので、変換は不要である。

    cd /tmp
    nkf -w -Lu JIGYOSYO.CSV > jigyosyo_utf8.csv
    
  4. SQLite 3 のデータベースへの取り込み

    次のシェルスクリプトを実行する。テーブル KENALL(住所の郵便番号)とテーブル JIGYOSYO(事業所の個別郵便番号)を定義し、CSV ファイルを取り込む。データベースファイルは /tmp/mydb01 である(すでに同じ名前のテーブルがあるときのために、最初に drop table を行っている)。

    #!/bin/bash
    
    cat >/tmp/a.$$.sql <<-SQL
    drop table if exists KENALL;
    drop table if exists JIGYOSYO;
    SQL
    cat /tmp/a.$$.sql | sqlite3 /tmp/mydb01
    #
    cat >/tmp/a.$$.sql <<-SQL
    create table KENALL (
        lgcode        text not null,
        zipcode5      text not null,
        zipcode       text not null,
        yomi_pref     text not null,
        yomi_city     text not null,
        yomi_town     text not null,
        pref          text not null,
        city          text not null,
        town          text not null,
        flag1         integer,
        flag2         integer,
        flag3         integer,
        flag4         integer,
        flag5         integer,
        flag6         integer );
    create table JIGYOSYO (
        lgcode        text not null,
        yomi_name     text not null,
        name          text not null,
        pref          text not null,
        city          text not null,
        town          text not null,
        banchi        text not null,
        zipcode       text not null,
        zipcode_old   text not null,
        postoffice    text not null,
        kind          integer,
        multiflag     integer,
        fixcode       integer );
    .mode csv
    .import /tmp/utf_ken_all.csv KENALL
    .import /tmp/jigyosyo_utf8.csv JIGYOSYO
    SQL
    cat /tmp/a.$$.sql | sqlite3 /tmp/mydb01
    rm -f /tmp/a.$$.sql
    
  5. 取り込み結果の確認

    それぞれのテーブルの行数を表示してみる。

    sqlite3 /tmp/mydb01 "select count(*) from KENALL;"
    sqlite3 /tmp/mydb01 "select count(*) from JIGYOSYO;"
    

    住所の郵便番号は約12万件、事業所の個別郵便番号は約2万件であることを確認する(件数は毎月の更新で変わる)。

  6. SQL による検索の体験

    郵便番号から住所を検索してみる(郵便番号は7けたの文字列で格納されている)。

    sqlite3 /tmp/mydb01 "select pref, city, town from KENALL where zipcode = '1000001';"
    

    住所(町域名)の一部から郵便番号を検索してみる。

    sqlite3 /tmp/mydb01 "select zipcode, pref, city, town from KENALL where town like '%銀座%';"
    

    事業所名の一部から、事業所の個別郵便番号を検索してみる。

    sqlite3 /tmp/mydb01 "select zipcode, name, pref, city, town from JIGYOSYO where name like '%放送%';"
    

    集計も行ってみる。都道府県ごとの郵便番号の件数を、多い順に表示する。

    sqlite3 /tmp/mydb01 "select pref, count(*) from KENALL group by pref order by count(*) desc;"
    

ヒント

考察ポイント