植生図をタイルにする(第6,7回版)

準備

以下をインストールしておく

OSGeo4W :shpファイルのインポートに利用
cygwinシェルスクリプトの実行に利用
spatialite:sqliteの作成、操作に利用

データをダウンロード

http://www.vegetation.jp/index.html

veg_download.shを参照

解凍

解凍すると、a.直接ファイルが作られるもの、b.フォルダの中にファイルが作られるもの、c.なぜだか他のファイルが紛れ込んでいるもの があるので取り扱い注意。

unzip.exeをダウンロードしておく。
http://gnuwin32.sourceforge.net/packages/unzip.htm

unzip.shで解凍&移動

for file in `find -type f -name "*.zip"`
do
  ./unzip -o -q $file
done
for file in `find shp* -maxdepth 1 -type f -name "*"`
do
  mv $file vegdir
done
mv p*.* vegdir

spatilaiteにインポート

veg.bat vegdir\*.shp

SET SHAPE_ENCODING=CP932
for %%A in (%1) do ogr2ogr -gt 65536 -append -nlt polygon -s_srs epsg:4612 -t_srs epsg:3857 -f SQLite veg67.sqlite %%A -dsco SPATIALITE=YES  

それぞれファイル名と同じテーブル名に格納される。

vegテーブルにマージ

vegテーブルにマージするために以下のスクリプトsqlを作成

$ mergesql.sh > merge.sql

echo "CREATE TABLE veg (OGC_FID INTEGER PRIMARY KEY, GEOMETRY POLYGON, mesh2_c VARCHAR, hanrei_c VARCHAR, surv_year VARCHAR, org_no VARCHAR, zukaku_no VARCHAR, shoku_c VARCHAR, shoku_n VARCHAR, dai_c VARCHAR, dai_n VARCHAR, chu_c VARCHAR, chu_n VARCHAR, sai_c VARCHAR, sai_n VARCHAR, hanrei_n VARCHAR);"

for file in `find vegdir -maxdepth 1 -type f -name "*.shp"| sed -e "s/.*\/\(.*\)\..*/\1/g"`
do
  echo "INSERT INTO veg(GEOMETRY, mesh2_c, hanrei_c, surv_year, org_no, zukaku_no, shoku_c, shoku_n, dai_c, dai_n, chu_c, chu_n , sai_c , sai_n, hanrei_n)"
  echo "SELECT GEOMETRY, mesh2_c, hanrei_c, surv_year, org_no, zukaku_no, shoku_c, shoku_n, dai_c, dai_n, chu_c, chu_n , sai_c , sai_n, hanrei_n"
  echo "FROM $file;"
done

spatialiteを起動して、merge.sqlを読み込み実行

$ spatialite veg67.sqlite
> .read merge.sql shift_jis

確認
> .charset shift_jis
> select * from veg limit 5;

ジオメトリをリカバー
select RecoverGeometryColumn('veg','GEOMETRY',3857,'POLYGON','XY');
Spatial Index を作成
select CreateSpatialIndex('veg','GEOMETRY');
個別のテーブルを削除

$ dropsql.sh > drop.sql
$ spatialite veg67.sqlite
> .read drop.sql shift_jis

for file in `find vegdir -maxdepth 1 -type f -name "*.shp"| sed -e "s/.*\/\(.*\)\..*/\1/g"`
do
  echo "select DisableSpatialIndex('$file','GEOMETRY');"
  echo "drop table $file;"
  echo "drop table idx_${file}_GEOMETRY;"
done
echo "VACUUM;"

tilemillで色つけ

tilemillを使って色つけします。
style.mss

Map {
  background-color: #99ffff;
}
#veg{
  ::shape{
    [zoom <= 11]{
      line-width:0.1;
      polygon-opacity:1;
      [shoku_c='1']{polygon-fill:#fdf1ce;line-color:#fdf1ce;}
      [shoku_c='2']{polygon-fill:#997f60;line-color:#997f60;}
      [shoku_c='3']{polygon-fill:#a58f74;line-color:#a58f74;}
      [shoku_c='4']{polygon-fill:#178017;line-color:#178017;}
      [shoku_c='5']{polygon-fill:#5b9700;line-color:#5b9700;}
      [shoku_c='6']{polygon-fill:#003300;line-color:#003300;}
      [shoku_c='7']{polygon-fill:#004a00;line-color:#004a00;}
      [shoku_c='8']{polygon-fill:#ffff00;line-color:#ffff00;}
      [shoku_c='9']{polygon-fill:#8cd27d;line-color:#8cd27d;}
      [shoku_c='10']{polygon-fill:#868585;line-color:#868585;}
      [dai_n="植林地"]{polygon-fill:#697720;line-color:#697720;}
      [hanrei_n="開放水域"]{polygon-fill:#99ffff;line-color:#99ffff;}
 	}
    [zoom >= 12]{
      line-width:0.1;
      polygon-opacity:1;
      [shoku_c='1']{polygon-fill:#fdf1ce;line-color:#fdf1ce;}
      [shoku_c='2']{polygon-fill:#997f60;line-color:#997f60;}
      [shoku_c='3']{polygon-fill:#a58f74;line-color:#a58f74;}
      [shoku_c='4']{polygon-fill:#178017;line-color:#178017;}
      [shoku_c='5']{polygon-fill:#5b9700;line-color:#5b9700;}
      [shoku_c='6']{polygon-fill:#003300;line-color:#003300;}
      [shoku_c='7']{polygon-fill:#004a00;line-color:#004a00;}
      [shoku_c='8']{polygon-fill:#ffff00;line-color:#ffff00;}
      [shoku_c='10']{polygon-fill:#868585;line-color:#868585;}
      [dai_n="植林地"]{polygon-fill:#697720;line-color:#697720;}
      [dai_n="竹林"]{polygon-fill:#cccc20;line-color:#cccc20;}
      [dai_n="牧草地・ゴルフ場・芝地"]{polygon-fill:#69ff00;line-color:#69ff00;}
      [dai_n="耕作地"]{polygon-fill:#999662;line-color:#999662;}
      [hanrei_n="水田雑草群落"]{polygon-fill:#8cd27d;line-color:#8cd27d;}
      [hanrei_n="開放水域"]{polygon-fill:#99ffff;line-color:#99ffff;}
      [zoom >= 14]{line-width:0.5;line-color:#000000;}
    }
  }
  ::label[zoom >= 14]{
     text-face-name: "unifont Medium";
     text-fill:#000000;
     text-size:9;
     text-halo-fill:rgba(255,255,255,0.3);
     text-halo-radius:1;
     text-line-spacing:1;
     text-wrap-width:20;
     text-name: "[hanrei_n]";
	[zoom >= 15] {
      text-size:10;
    }
  }
}

project.mml

{
  "bounds": [
    122.9,
    20.4,
    154,
    45.6
  ],
  "center": [
    140.8314,
    40.7119,
    10
  ],
  "format": "png8:t=0:c=64:z=5",
  "interactivity": {
    "layer": "veg",
    "template_teaser": "{{{shoku_n}}}\n{{{dai_n}}}\n{{{hanrei_n}}}",
    "fields": [
      "shoku_c"
    ]
  },
  "minzoom": 1,
  "maxzoom": 15,
  "srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
  "Stylesheet": [
    "style.mss"
  ],
  "Layer": [
    {
      "geometry": "polygon",
      "extent": [
        122.873495939729,
        24.004206926072346,
        153.99615896406743,
        45.66879613975789
      ],
      "Datasource": {
        "type": "sqlite",
        "file": "veg67.sqlite",
        "table": "veg",
        "attachdb": "",
        "extent": "",
        "id": "veg",
        "project": "veg67",
        "srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over"
      },
      "id": "veg",
      "class": "",
      "srs-name": "900913",
      "srs": "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over",
      "advanced": {},
      "name": "veg"
    }
  ],
  "scale": 1,
  "metatile": 2,
  "name": "vegetation67",
  "description": "vegetation67",
  "attribution": "",
  "legend": ""
}

mbtileを出力

shmmaxの修正

これをしないとkillされる。ec2 microだとメモリが足りなくてkillされる。

sudo sysctl -w kernel.shmmax=268435456
sudo sysctl -p
タイル作成
cd /usr/share/tilemill
sudo node index.js export veg67 /usr/share/mapbox/export/veg67.mbtiles --format=mbtiles --bbox=122.9,20.4,154,45.6 --minzoom=1 --maxzoom=15 --metatile=2 --scale=1 --scheme=pyramid --log --files=/usr/share/mapbox/

フォルダに展開

S3に転送