Created
April 22, 2015 14:43
-
-
Save japboy/15443d79d1a02bfc577c to your computer and use it in GitHub Desktop.
地域メッシュの GeoJSON 変換
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var fs = require('fs'); | |
var path = require('path'); | |
var _ = require('underscore'); | |
var concat = require('concat-stream'); | |
var csv = require('csv'); | |
var GeoJSON = require('geojson'); | |
var iconv = require('iconv'); | |
var latlng = function (_mesh) { | |
var mesh = _mesh.toString(); | |
var mesh1 = mesh.slice(0, 4); | |
var mesh2 = mesh.slice(4, 6); | |
var mesh3 = mesh.slice(6, 8); | |
var latDeg = parseInt(mesh1.slice(0, 2)) / 1.5; | |
var latMin = parseInt(mesh2.slice(0, 1)) * 5; | |
var latSec = parseInt(mesh3.slice(0, 1)) * 30; | |
var lngDeg = parseInt(mesh1.slice(2, 4)) + 100; | |
var lngMin = parseInt(mesh2.slice(1, 2)) * 7.5; | |
var lngSec = parseInt(mesh3.slice(1, 2)) * 45; | |
var lat = ((latDeg * 3600) + (latMin * 60) + latSec) / 3600; | |
var lng = ((lngDeg * 3600) + (lngMin * 60) + lngSec) / 3600; | |
return [ lat, lng ]; | |
}; | |
var f = fs.statSync(process.argv[2]).isFile() ? path.resolve(process.argv[2]) : undefined; | |
var filein = fs.createReadStream(f); | |
var fileconv = new iconv.Iconv('Shift_JIS', 'UTF-8//TRANSLIT//IGNORE'); | |
var fileout = fs.createWriteStream(f.replace(/\.csv$/, '.json')); | |
filein | |
.pipe(fileconv) | |
.pipe(csv.parse()) | |
.pipe(csv.transform(function (values) { | |
return { | |
code: parseInt(values[0]), | |
name: values[1], | |
coords: [ latlng(values[2]) ] | |
}; | |
})) | |
.pipe(concat(function (_data) { | |
var data = _.reduce(_data, function (memo, item) { | |
if (!_.isArray(memo)) return [ item ]; | |
var last = _.last(memo); | |
if (last.code === item.code) { | |
last.coords.push(item.coords[0]); | |
} else { | |
memo.push(item); | |
} | |
return memo; | |
}); | |
var geoData = GeoJSON.parse(data, { 'MultiPoint': 'coords' }); | |
fileout.end(JSON.stringify(geoData)); | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "mesh2latlng", | |
"author": "Yu I.", | |
"license": "UNLICENSE", | |
"scripts": { | |
"convert": "node ./mesh2geojson.js" | |
}, | |
"dependencies": { | |
"concat-stream": "^1.4.8", | |
"csv": "^0.4.1", | |
"geojson": "^0.2.0", | |
"iconv": "^2.1.6", | |
"underscore": "^1.8.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment