D3.js 㨠TopoJSON ã使ã£ã¦ããã©ã¦ã¶ä¸ã«å°å³ãæããã¨ãç°¡åã«ã§ããããã§ããã¦ã§ãä¸ã®è§£èª¬è¨äºãåèã«ããªããç§ã試ãã¦ã¿ã¾ãããä½æã«ããã£ã¦ã¯ã主ã«ä¸è¨ã¦ã§ããã¼ã¸ã®è§£èª¬ãåèã«ãã¾ããã
D3.js と TopoJSON で地図を作る
ã§ããããã¯ä»¥ä¸ã®ã¨ããã§ããå°å³ãã¼ã¿ã¯ Natural Earth ã§å
¬éããã¦ãããã®ãå©ç¨ãã¾ããã
D3.js と TopoJSON で日本地図を描く
ä½æ¥ã®æµãã¯æ¬¡ã®ããã«ãªãã¾ãã
- Shape å½¢å¼ã®å°å³ãã¼ã¿ãå ¥æãã
- GeoJSON å½¢å¼ã«å¤æãã
- TopoJSON å½¢å¼ã«å¤æãã
- D3.js 㨠TopoJSON ãã©ã°ã¤ã³ã使ã£ã¦ TopoJSON å½¢å¼ã®ãã¼ã¿ããã©ã¦ã¶ä¸ã«æç»ãã
ç§ã¯æ®æ®µ CentOS ãå©ç¨ãã¦ããã®ã§ãããCentOS 6.5 ã§ã¯ãä¸é£ã®ä½æ¥ã«å¿ è¦ãªã½ããã¦ã§ã¢ã¯ EPEL ã«æã£ã¦ãã¾ãããããã§ã¯ãEPEL ã®ããã±ã¼ã¸ã使ã£ã¦ CentOS ã® CUI ç°å¢ã®ã¿ã§ä½æ¥ããæé ã説æãã¾ãã
Shape å½¢å¼ã®å°å³ãã¼ã¿ãå ¥æãã
ã¾ããshape å½¢å¼ã®å°å³ãã¼ã¿ãå
¥æãã¾ããä»åã¯ãæ¥æ¬å
¨å³ãé½éåºçãã¨ã«å¡ãåãã¦ã¿ããã¨æãããã®ç¨éã«ä½¿ããããªå°å³ãæ¢ãã¾ãããNatural Earth ã®ä»¥ä¸ã®ãã¼ã¸ã«ãã "Admin 1 â States, Provinces" ã¨ãããã¼ã¿ãããã«è©²å½ããã®ã§ããããå©ç¨ãã¾ããã
1:10m Cultural Vectors | Natural Earth
ãã®ãã¼ã¿ããã¦ã³ãã¼ããã¦å±éãã¾ããã¦ã§ããµã¤ãã«ãè¨è¼ãããããã« zip ãã¡ã¤ã«ã®ç¶æ 㧠14MB ç¨åº¦ã®å¤§ãããããã¾ãããªãããã® zip ãã¡ã¤ã«ã¯ããã£ã¬ã¯ããªãä½ããã«ã¬ã³ããã£ã¬ã¯ããªã«ãã©ããã«å±éããã¾ããã
$ wget http://www.naturalearthdata.com/http//www.naturalearthdata.com/download/10m/cultural/ne_10m_admin_1_states_provinces.zip $ unzip ne_10m_admin_1_states_provinces.zip
GeoJSON å½¢å¼ã«å¤æãã
Shape å½¢å¼ã®ãã¡ã¤ã«ã GeoJSON å½¢å¼ã«å¤æããæ¹æ³ã¯è²ã
ããããã§ãããCentOS 6.5 ã® CUI ã§ã¯ ogr2ogr ã¨ããã³ãã³ãã使ããããã§ããCentOS 6.5 ã®å
¬å¼ãªãã¸ããªã§ã¯ããã±ã¼ã¸ãæä¾ããã¦ããªãã®ã§ãããEPEL ããã¤ã³ã¹ãã¼ã«ã§ãã¾ããyum ã« EPEL ãªãã¸ããªã追å ã㦠gdal ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¾ã*1ãyum ã¸ã®ãªãã¸ããªã®è¿½å æ¹æ³ã«ã¤ãã¦ã¯ä»¥ä¸ã®ã¦ã§ããã¼ã¸ã®æ
å ±ãåèã«ãã¾ããã
CentOSにEPELリポジトリを追加する - Qiita
$ wget http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm $ sudo rpm -ivh epel-release-6-8.noarch.rpm $ sudo yum install gdal --enablerepo=epel
以ä¸ã®ããã«ã㦠shape ãã¡ã¤ã«ãã GeoJSON å½¢å¼ã®ãã¡ã¤ã«ãçæã§ãã¾ããogr2ogr 㯠GeoJSON ã®ã»ãã«ãå種形å¼ã¸ã®å¤æããµãã¼ããã¦ããã-f ãªãã·ã§ã³ã§åºåå½¢å¼ãæå®ãã¾ãã
$ ogr2ogr -f GeoJSON ne_10m_admin_1_states_provinces.geojson ne_10m_admin_1_states_provinces.shp
ãã¦ãä»åãã¦ã³ãã¼ããã¦ãããã¼ã¿ã¯ãæ¥æ¬ã ãã§ã¯ãªãå ¨ä¸çã®æ å ±ãå«ããã®ã§ãããæ¥æ¬å°å³ãæãããã«ã¯ããã¼ã¿å ¨ä½ã®ä¸ããæ¥æ¬ã ããé¸æçã«æ½åºããå¿ è¦ãããã¾ããããã¯ãogr2ogr ã§ãã¡ã¤ã«å½¢å¼ãå¤æããéã« -where ãªãã·ã§ã³ãæå®ãããã¨ã§å®ç¾ã§ãã¾ãã
ã©ã®ãããªæ¡ä»¶ãæå®ããã°ãããããã¼ã¿ã®ä»æ§ã調ã¹ãã®ãæ¬æ¥ã®æ¹æ³ã ã¨ã¯æãã¾ããããããé¢åãªã®ã§é©å½ã«æ¢ãã¦ã¿ããã¨ã«ãã¾ãã
$ jq '.' ne_10m_admin_1_states_provinces.geojson | grep Japan "geonunit": "Japan", "admin": "Japan", "note": "JPN-99 (Japan minor island)", "geonunit": "Japan", "admin": "Japan", "woe_label": "Hiroshima Prefecture, JP, Japan", "geonunit": "Japan", ...
ãã®ããã«è¦ã¤ããã¾ããã®ã§ããã¨ãã° geonunit ã Japan ã§ãããã¨ãæ¡ä»¶ã«ããã°è¯ãããã§ã*2ããªããããã§å©ç¨ãã¦ãã jq ã¯ã³ãã³ãã©ã¤ã³ã® JSON ããã»ããµã§ããããããCentOS 6.5 ã§ã¯ EPEL ããã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
æ¹ãã¦ãæ¥æ¬ã®ã¿ã¨ããæ¡ä»¶ãã¤ã㦠shape ãã¡ã¤ã«ã GeoJSON å½¢å¼ã«å¤æãã¾ããæ¥æ¬ã ãã®ãã¼ã¿ã«ãªãã®ã§ãåºåãã¡ã¤ã«åã japan.geojson ã¨ãã¾ããã
$ ogr2ogr -f GeoJSON -where 'geonunit = "Japan"' japan.geojson ne_10m_admin_1_states_provinces.shp
ããã§ããã¼ã¿ã確èªãã¦ã¿ã¾ããjq ã使ã£ã¦èª¿ã¹ã¦ã¿ãã¨ãfeatures è¦ç´ ã«é½éåºçãã¨ã®ãã¼ã¿ (å¢çç·ã®åº§æ¨ãã¼ã¿ã¨ããããã£) ãæ ¼ç´ããã¦ãã¦ããã®ä¸ã® properties.name_local ã«é½éåºçåãå ¥ã£ã¦ããããããã¨ãåããã¾ãããããåºåããã¦ã¿ã¾ãã
$ jq '.features[].properties.name_local' japan.geojson | column "" "ç¦å²¡ç" "徳島ç" "å¯å±±ç" "æ»è³ç" "æ±äº¬é½" "æ°æ½ç" "åºå³¶ç" "çæ¬ç" "æç¥ç" "åæµ·é" "åæå±±ç" "山梨ç" "山形ç" "岡山ç" "å®®å´ç" "å²éç" "ç¦äºç" "åèç" "ç§ç°ç" "é·å´ç" "å³¶æ ¹ç" "æåªç" "ç³å·ç" "å µåº«ç" "è¨åç" "é森ç" "鹿å 島ç" "é³¥åç" "é¦å·ç" "ä¸éç" "京é½åº" "ç¥å¥å·ç" "ç¦å³¶ç" "æ²ç¸ç" "å±±å£ç" "é«ç¥ç" "é·éç" "å¥è¯ç" "å¼çç" "岩æç" "群馬ç" "ä½è³ç" "大åç" "" "大éªåº" "æ æ¨ç" "å®®åç"
å ¨é¨ã§ 48 è¦ç´ ããã¾ããã¾ããé½éåºçåã空ã«ãªã£ã¦ãããã®ã 2 è¦ç´ ããã¾ããã並ã³é ããé½éåºçã³ã¼ãé ã§ã¯ãªãã°ãã°ãã«è¦ãã¾ãã空ã«ãªã£ã¦ãã 2 è¦ç´ ã®ãã¡ãå é ã®ãã®ã¯
$ jq '.features[0].properties.note' japan.geojson | column "JPN-99 (Japan minor island)"
ã¨ã®ãã¨ã§ããããä¸æ¹ã¯ãã©ãããé岡çã® name_local ãã£ã¼ã«ããæ¬ è½ãã¦ããããã§ãã
TopoJSON å½¢å¼ã«å¤æãã
次ã«ããã®ãã¡ã¤ã«ã GeoJSON å½¢å¼ãã TopoJSON å½¢å¼ã«å¤æãã¾ãããã㯠topojson ã¨ããã³ãã³ãã使ãã°ããã®ã§ãããtopojson 㯠node.js ã®ããã±ã¼ã¸ç®¡çã·ã¹ãã ã§ãã npm ã使ã£ã¦ã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã®ã§ãã¾ã node.js 㨠npm ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ sudo yum install nodejs npm --enablerepo=epel
ç§ã®ç°å¢ã§ã¯ããããã®ããã±ã¼ã¸ã®ã¤ã³ã¹ãã¼ã«èªä½ã¯æ£å¸¸çµäºããã®ã§ãããã¤ã³ã¹ãã¼ã«ç´å¾ã« npm ã®ããã±ã¼ã¸æ§æã調ã¹ã¦ã¿ãã¨ããã以ä¸ã®ããã«ä¾åé¢ä¿ã®ã¨ã©ã¼ãçºçãã¦ãã¾ãã*3ã
$ npm -g list ... npm ERR! missing: inherits@*, required by undefined@undefined npm ERR! missing: inherits@*, required by block-stream@0.0.7 npm ERR! missing: inherits@*, required by fstream@0.1.24 npm ERR! missing: inherits@*, required by fstream-ignore@0.0.7 npm ERR! missing: inherits@*, required by fstream-npm@0.1.5 npm ERR! missing: inherits@*, required by glob@3.2.6 npm ERR! invalid: inherits@2.0.0 /usr/lib/node_modules/inherits@2 npm ERR! missing: inherits@*, required by npmconf@0.1.2 npm ERR! missing: inherits@*, required by tar@0.1.18 npm ERR! not ok code 0
ç§ã«ã¯ node.js ã®çµé¨ããªãç¶æ³ãããããããªãã®ã§ãããããã«å¯¾å¦ãããã¨ã㦠npm ã®ããã±ã¼ã¸æ´æ°ãè¡ã£ããããã¨ãä»åº¦ã¯ node-gyp ãã¨ã©ã¼ãèµ·ãããªã©ãã¦æ³¥æ²¼ã§ããããã®ä¸æ¹ã§ãtopojson èªä½ã¯ãã®ã¨ã©ã¼ããã®ã¾ã¾ã«ãã¦ãåé¡ãªãã¤ã³ã¹ãã¼ã«ã§ããããã§ããããã§ãä»åã¯ã¨ã©ã¼ã«å¯¾å¦ããæé ãé²ãããã¨ã«ãã¾ãã
$ sudo npm -g install topojson
ã¤ã³ã¹ãã¼ã«ããã topojson ã使ã£ã¦ãã¡ã¤ã«å½¢å¼ãå¤æãã¾ãã
$ topojson japan.geojson >japan.topojson
D3.js 㨠TopoJSON ãã©ã°ã¤ã³ã使ã£ã¦ TopoJSON å½¢å¼ã®ãã¼ã¿ããã©ã¦ã¶ä¸ã«æç»ãã
TopoJSON ãã¡ã¤ã«ãç¨æã§ãããããã¨ã¯ D3.js 㨠TopoJSON ãã©ã°ã¤ã³ã使ã£ã¦ããã©ã¦ã¶ä¸ã«æç»ã§ãã¾ãããã®é¨åã¯åé ã«ç¤ºããã¦ã§ããã¼ã¸ã®èª¬æã®ã¨ããã« HTML ãã¡ã¤ã«ã JavaScript ãã¡ã¤ã«ãä½æããã ããªã®ã§ã説æãçç¥ãã¾ããå ãã¼ã¸ã®è§£èª¬ããã§ããããã®ã¦ã§ããã¼ã¸ã®ã½ã¼ã¹ã³ã¼ããåèã«ãã¦ãã ããã
*1:gdal ããã±ã¼ã¸ã« ogr2ogr ã³ãã³ããå«ã¾ãã¦ãã¾ãã
*2:geounit ã§ã¯ãªã geonunit ã§ããç§ã¯ããã«æ°ä»ããæéãç¡é§ã«ãã¾ããã
*3:ç§ã®ä½æ¥ç°å¢ãæ±ãã¦ããå¯è½æ§ããããã¨æãã念ã®ããæ°è¦ã« CentOS 6.5 ã®ä»®æ³æ©æ¢°ãä½æã㦠OS ã»ããã¢ããç´å¾ã®ç¶æ ã«ã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ããããããã§ãåæ§ã§ããã