node-red
Node-REDDay 24

Node-REDでe-stat APIから日本の人口を取得し、Google Chartノードでグラフ化してみた

More than 3 years have passed since last update.


はじめに


行ったこと

この記事では、2つのことにチャレンジしています。


  1. Node-REDからe-stat APIをたたいて日本の人口を取得

  2. Node-REDのGoogle Chartノードを用いて人口ピラミッドを描画


開発環境

OS : Mac OS X Yosemite

Node-RED : v0.10.10

Node.js : v0.12.7


現在のNode-REDの姿

Node-RED 2015-12-24 21-47-35.png


作業手順


1. Node-REDからe-stat APIをたたいて日本の人口を取得

e-Stat APIのページから利用登録を行い、アプリケーションIDを取得します。以下、アプリケーションIDはXXXの値を入れております。

今回は2011年の人口推計を使います。2011年の人口推計の統計IDは000304698であることがAPI検索によってわかるので、


http://api.e-stat.go.jp/rest/2.0/app/json/getStatsData?appId=XXX&statsDataId=0003046398&cdTime=2011000000&cdCat01=000&cdCat02=001&cdCat03From=01001&cdCat03To=01021&metaGetFlg=N


で統計データをAPI仕様に沿って取得します。


2011年の人口推計

{

"GET_STATS_DATA": {
"RESULT": {
"STATUS": 0,
"ERROR_MSG": "\u6B63\u5E38\u306B\u7D42\u4E86\u3057\u307E\u3057\u305F\u3002",
"DATE": "2015-12-24T20:25:20.077+09:00"
},
"PARAMETER": {
"LANG": "J",
"STATS_DATA_ID": "0003046398",
"NARROWING_COND": {
"CODE_CAT01_SELECT": "000",
"CODE_CAT02_SELECT": "001",
"CODE_CAT03_FROM": "01001",
"CODE_CAT03_TO": "01021",
"CODE_TIME_SELECT": 2011000000
},
"DATA_FORMAT": "J",
"START_POSITION": 1,
"METAGET_FLG": "N"
},
"STATISTICAL_DATA": {
"RESULT_INF": {
"TOTAL_NUMBER": 21,
"FROM_NUMBER": 1,
"TO_NUMBER": 21
},
"TABLE_INF": {
"@id": "0003046398",
"STAT_NAME": {
"@code": "00200524",
"$": "\u4EBA\u53E3\u63A8\u8A08"
},
"GOV_ORG": {
"@code": "00200",
"$": "\u7DCF\u52D9\u7701"
},
"STATISTICS_NAME": "\u4EBA\u53E3\u63A8\u8A08 \u5E73\u621023\u5E7410\u67081\u65E5\u73FE\u5728\u4EBA\u53E3\u63A8\u8A08",
"TITLE": {
"@no": "003",
"$": "\u5E74\u9F62\uFF08\uFF15\u6B73\u968E\u7D1A\uFF09\uFF0C\u7537\u5973\u5225\u4EBA\u53E3\u53CA\u3073\u5272\u5408\uFF0D\u7DCF\u4EBA\u53E3"
},
"CYCLE": "-",
"SURVEY_DATE": 201110,
"OPEN_DATE": "2012-04-17",
"SMALL_AREA": 0,
"MAIN_CATEGORY": {
"@code": "02",
"$": "\u4EBA\u53E3\u30FB\u4E16\u5E2F"
},
"SUB_CATEGORY": {
"@code": "01",
"$": "\u4EBA\u53E3"
},
"OVERALL_TOTAL_NUMBER": 810,
"UPDATED_DATE": "2012-04-17"
},
"DATA_INF": {
"NOTE": [{
"@char": "***",
"$": "\u3000"
}, {
"@char": "-",
"$": "\u8A72\u5F53\u6570\u5024\u304C\u306A\u3044\u3082\u306E\u3000"
}],
"VALUE": [{
"@cat01": "000",
"@cat02": "001",
"@cat03": "01001",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "5303"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01002",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "5490"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01003",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "5912"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01004",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "6075"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01005",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "6370"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01006",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "7219"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01007",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "8093"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01008",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "9712"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01009",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "9315"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01010",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "7966"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01011",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "7639"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01012",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "8320"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01013",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "10632"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01014",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "7861"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01015",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "7184"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01016",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "6143"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01017",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "4494"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01018",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "2625"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01019",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "1082"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01020",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "317"
}, {
"@cat01": "000",
"@cat02": "001",
"@cat03": "01021",
"@area": "00000",
"@time": "2011000000",
"@unit": "\u5343\u4EBA",
"$": "47"
}]
}
}
}
}

というデータ構造であることがわかります。

人口の値は、


GET_STATS_DATA.STATISTICAL_DATA.DATA_INF.VALUE


という階層に入っているので、


VALUE取り出し

msg.payload=msg.payload.GET_STATS_DATA.STATISTICAL_DATA.DATA_INF.VALUE;

return msg;

でその部分を取り出します。


2. Node-REDのGoogle Chartノードを用いて人口ピラミッドを描画

Google Chartノードをインストールします。このリンクの下に書いてあるソースをimportしたところfunctionノードがあるので、

Node-RED 2015-12-24 22-10-03.png

中身を調べます。


example

msg.payload=[

{name:'ute',age:20},
{name:'schnute',age:21},
{name:'kasimir',age:25},
];
return msg;

この形で流し込めば、グラフが描けるはずなので、先ほどの人口のデータを整形します。


整形

msg.payload=

[
{age:'0-4',population:Number(msg.payload[0].$)},
{age:'5-9',population:Number(msg.payload[1].$)},
{age:'10-14',population:Number(msg.payload[2].$)},
{age:'15-19',population:Number(msg.payload[3].$)},
{age:'20-24',population:Number(msg.payload[4].$)},
{age:'25-29',population:Number(msg.payload[5].$)},
{age:'30-34',population:Number(msg.payload[6].$)},
{age:'35-39',population:Number(msg.payload[7].$)},
{age:'40-44',population:Number(msg.payload[8].$)},
{age:'45-49',population:Number(msg.payload[9].$)},
{age:'50-54',population:Number(msg.payload[10].$)},
{age:'55-59',population:Number(msg.payload[11].$)},
{age:'60-64',population:Number(msg.payload[12].$)},
{age:'65-69',population:Number(msg.payload[13].$)},
{age:'70-74',population:Number(msg.payload[14].$)},
{age:'75-79',population:Number(msg.payload[15].$)},
{age:'80-84',population:Number(msg.payload[16].$)},
{age:'85-89',population:Number(msg.payload[17].$)},
{age:'90-94',population:Number(msg.payload[18].$)},
{age:'95-99',population:Number(msg.payload[19].$)},
{age:'100-',population:Number(msg.payload[20].$)},
];
return msg;


グラフが出ました!

スクリーンショット 2015-12-24 22.40.41.png 2015-12-25 12-13-46.png

今回は簡単に全国の人口を取得し描画しましたが、e-Stat APIにはもっと多くの情報が詰まっています。機械学習や分析を行う際の、外部データソースの一つとして活用されてみてはいかがでしょうか。


ソースコード

[{

"id": "b16e7524.4e9188",
"type": "http request",
"name": "",
"method": "GET",
"ret": "txt",
"url": "",
"x": 366.89581298828125,
"y": 157.09100341796875,
"z": "b5890722.4a76f8",
"wires": [
["63a98e14.9c567"]
]
}, {
"id": "7adf0296.8520fc",
"type": "change",
"name": "",
"rules": [{
"t": "set",
"p": "url",
"to": "http://api.e-stat.go.jp/rest/2.0/app/json/getStatsData?appId=XXX&statsDataId=0003046398&cdTime=2011000000&cdCat01=000&cdCat02=001&cdCat03From=01001&cdCat03To=01021&metaGetFlg=N"
}],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 359.96714782714844,
"y": 97.46469116210938,
"z": "b5890722.4a76f8",
"wires": [
["b16e7524.4e9188"]
]
}, {
"id": "6e8d8b44.917274",
"type": "function",
"name": "整形して流し込み",
"func": "msg.payload=\n[\n\t{age:'0-4',population:Number(msg.payload[0].$)},\n\t{age:'5-9',population:Number(msg.payload[1].$)},\n\t{age:'10-14',population:Number(msg.payload[2].$)},\n\t{age:'15-19',population:Number(msg.payload[3].$)},\n\t{age:'20-24',population:Number(msg.payload[4].$)},\n\t{age:'25-29',population:Number(msg.payload[5].$)},\n\t{age:'30-34',population:Number(msg.payload[6].$)},\n\t{age:'35-39',population:Number(msg.payload[7].$)},\n\t{age:'40-44',population:Number(msg.payload[8].$)},\n\t{age:'45-49',population:Number(msg.payload[9].$)},\n\t{age:'50-54',population:Number(msg.payload[10].$)},\n\t{age:'55-59',population:Number(msg.payload[11].$)},\n\t{age:'60-64',population:Number(msg.payload[12].$)},\n\t{age:'65-69',population:Number(msg.payload[13].$)},\n\t{age:'70-74',population:Number(msg.payload[14].$)},\n\t{age:'75-79',population:Number(msg.payload[15].$)},\n\t{age:'80-84',population:Number(msg.payload[16].$)},\n\t{age:'85-89',population:Number(msg.payload[17].$)},\n\t{age:'90-94',population:Number(msg.payload[18].$)},\n\t{age:'95-99',population:Number(msg.payload[19].$)},\n\t{age:'100-',population:Number(msg.payload[20].$)},\n];\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"x": 380.89573669433594,
"y": 305.8888854980469,
"z": "b5890722.4a76f8",
"wires": [
["c3c64bd8.3c39b8"]
]
}, {
"id": "63a98e14.9c567",
"type": "json",
"name": "",
"x": 537.8957977294922,
"y": 170.88888549804688,
"z": "b5890722.4a76f8",
"wires": [
["e52afd6.f1ad5"]
]
}, {
"id": "e52afd6.f1ad5",
"type": "function",
"name": "VALUE取り出し",
"func": "msg.payload=msg.payload.GET_STATS_DATA.STATISTICAL_DATA.DATA_INF.VALUE;\nreturn msg;",
"outputs": 1,
"noerr": 0,
"x": 376.8957977294922,
"y": 239.88888549804688,
"z": "b5890722.4a76f8",
"wires": [
["6e8d8b44.917274"]
]
}, {
"id": "b078f00c.4f871",
"type": "chart request",
"charttype": "BarChart",
"path": "/googlechart",
"refresh": "60",
"formatx": "",
"formaty": "",
"attribs": [{
"name": "age",
"type": "string"
}, {
"name": "population",
"type": "number"
}],
"x": 125.89582824707031,
"y": 225.11111450195312,
"z": "b5890722.4a76f8",
"wires": [
["7adf0296.8520fc"]
]
}, {
"id": "c3c64bd8.3c39b8",
"type": "chart response",
"x": 663.8957977294922,
"y": 306.1111145019531,
"z": "b5890722.4a76f8",
"wires": []
}]