LoginSignup
92
81

More than 5 years have passed since last update.

国土交通省のシェイプ形式データから全都道府県市町村郡区のGeoJsonとTopoJsonを作成。

Last updated at Posted at 2015-05-16

日本全国シェイプ形式データ入手

国土交通省のサイトから全国のデータをダウンロード。
http://nlftp.mlit.go.jp/ksj/gml/datalist/KsjTmplt-N03.html
シェイプ形式のデータを使います。

% brew install gdal
% which ogr2ogr
/usr/local/bin/ogr2ogr
% ogr2ogr -f GeoJSON places.json N03-14_140401.shp

日本全国GeoJson解体

places.jsonはサイズがかなり大きいので県、市、町、村、郡、区単位に分割。

ruby split_geojson.rb
split_geojson.rb
require 'json'
require 'fileutils'

class GeoJsonToCity

  def initialize()
   @datas = {}
   Dir.glob('places.json').each do |path|
      io = File.open(path)
      str = io.read(nil, '')
      json = JSON.parse(str)
      features = json['features']
      features.each do |feature|
        # 名称抽出
        city = self.checkCity(feature['properties'])
        # 都道府県
        self.setData(city[0], feature)
        # 市町村1
        self.setData(city[1], feature)
        # 市町村2
        if !city[2].nil?
          word = city[1] + city[2]
          self.setData(word, feature)
        end
      end
    end
  end

  def setData(key, feature)
    if !key.nil?
      if !@datas.has_key?(key) then
        @datas[key] = []
      end
      @datas[key].push(feature)
    end
  end

  def checkCity(prop)
    cityList = []
    pref  = prop['N03_001']
    city1 = prop['N03_002']
    city2 = prop['N03_003']
    city3 = prop['N03_004']
    cityList.push(pref)
    if !city1.nil? && pref != '北海道'
      cityList.push(city1)
    end
    if !city2.nil?
      cityList.push(city2)
    end
    if !city3.nil?
      cityList.push(city3)
    end
    return cityList
  end

  def make
    @datas.each do |city, collection|
      data = {"type": "FeatureCollection", "features": []}
      data["features"] = collection
      addr = self.checkCity(collection[0]['properties'])
      pref = addr[0]
      FileUtils.mkdir_p('geojson/' + pref)
      File.open('geojson/' + pref + '/' + city + '.json', 'w').write(JSON.generate(data))
    end
  end
end

city = GeoJsonToCity.new
city.make()

日本全国TopoJson作成

GeoJsonよりサイズの小さいTopoJsonを作成します。
TopoJsonはd3.jsとかで使えます。

% npm install -g topojson 
% topojson --version
1.6.19

富山県のGeoJsonをTopoJsonへ変換。

id属性に氷見市、朝日町、富山市…を付与。

% topojson --id-property N03_004 -p NAME=name -p name -o toyama.topojson geojson/富山県/富山県.json

全国分をシェルで一括作成しました。

geojson_to_topojson.sh
#!/bin/bash

find ./geojson -type f | while read file
do
    arr=( `echo $file | tr -s '/' ' '`)
    pref=${arr[2]}
    # ディレクトリ
    dir="topojson/"$pref
    if [ ! -d $dir ]; then
      mkdir -p $dir
    fi
    basename=$(basename $file)
    name=( `echo $basename | sed -e "s/\.json/.topojson/"`)
    #echo $name
    topojson --id-property N03_004 -p NAME=name -p name -o $dir"/"$name geojson/$pref/$basename
done

Githubに全国の都道府県、市町村、郡、区単位のGeoJson,TopoJsonを設置しました。
https://github.com/niiyz/JapanCityGeoJson

92
81
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
92
81