This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
forked from og-kyogikai/papamama
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gulpfile.js
214 lines (200 loc) · 7.26 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
var gulp = require('gulp');
var browserSync = require("browser-sync");
var shapefile = require('shapefile');
var fs = require('fs');
var inside = require('point-in-polygon');
var runSequence = require('run-sequence');
var csv = require('csv');
var iconv = require('iconv-lite');
var sutil = require('line-stream-util')
var GeoJSON = require('geojson');
var minimist = require('minimist');
var argv = minimist(process.argv.slice(2));
// ローカルサーバ起動
gulp.task("serve", () => {
browserSync({
server: {
baseDir: "."
}
});
// 監視対象ファイル一覧
gulp.watch(["css/**/*", "data/**/*", "image/**/*", "js/**/*", "index.html"], () => {
browserSync.reload();
});
});
// 全データ更新
gulp.task("updatedata", (cb) => {
runSequence(['data-wards', 'data-middleSchool', 'data-elementary', 'data-school'], 'data-station', 'data-nursery', cb);
});
// 行政区域のデータ更新
gulp.task("data-wards", (cb) => {
shapefile.read('data_org/N03-15_12_150101.shp', {encoding: 'shift_jis'}, (err, json) => {
if(err) {
console.log(err);
} else {
json.features = json.features.filter((feature) => {
var cityCode = feature.properties.N03_007;
return cityCode && cityCode.indexOf('121') === 0;
});
fs.writeFileSync( 'data/wards.geojson', JSON.stringify(json) );
}
cb();
});
});
// 中学校区域のデータ更新
gulp.task("data-middleSchool", (cb) => {
shapefile.read('data_org/A32-13_12.shp', {encoding: 'shift_jis'}, (err, json) => {
if(err) {
console.log(err);
} else {
json.features = json.features.filter((feature) => {
var cityCode = feature.properties.A32_001;
return cityCode && cityCode.indexOf('121') === 0;
});
fs.writeFileSync( 'data/MiddleSchool.geojson', JSON.stringify(json) );
}
cb();
});
});
// 小学校区域のデータ更新
gulp.task("data-elementary", (cb) => {
shapefile.read('data_org/A27-10_12-g_SchoolDistrict.shp', {encoding: 'shift_jis'}, (err, json) => {
if(err) {
console.log(err);
} else {
json.features = json.features.filter((feature) => {
var cityCode = feature.properties.A27_005;
return cityCode && cityCode.indexOf('121') === 0;
});
fs.writeFileSync( 'data/Elementary.geojson', JSON.stringify(json) );
}
cb();
});
});
// 学校のデータ作成
gulp.task("data-school", (cb) => {
shapefile.read('data_org/p29-13_12.shp', {encoding: 'shift_jis'}, (err, json) => {
if(err) {
console.log(err);
} else {
var features = json.features.filter((feature) => {
var cityCode = feature.properties.P29_001;
feature.properties.label = feature.properties.P29_005.replace(/学校$/, '');
return cityCode && cityCode.indexOf('121') === 0;
});
// 小学校
json.features = features.filter((feature) => {
return feature.properties.P29_004 === '16001';
});
fs.writeFileSync( 'data/Elementary_loc.geojson', JSON.stringify(json) );
// 中学校
json.features = features.filter((feature) => {
return feature.properties.P29_004 === '16002' || feature.properties.P29_004 === '16003';
});
fs.writeFileSync( 'data/MiddleSchool_loc.geojson', JSON.stringify(json) );
}
cb();
});
});
// 駅のデータ更新
gulp.task("data-station", (cb) => {
shapefile.read('data_org/N02-14_Station.shp', {encoding: 'shift_jis'}, (err, json) => {
if(err) {
console.log(err);
} else {
var wardsJson = JSON.parse(fs.readFileSync('data/wards.geojson', 'utf8'));
json.features = json.features.filter((feature) => {
var ward = wardsJson.features.find((wardFeature) => {
return inside(feature.geometry.coordinates[0], wardFeature.geometry.coordinates[0]);
});
return !!(ward);
});
json.features = json.features.map((feature) => {
var data = {type: "Feature", properties: {}, geometry: {type: "Point", coordinates:[]}};
data.properties.line = feature.properties.N02_003;
data.properties.station_name = feature.properties.N02_005;
data.properties.shubetsu = feature.properties.N02_004;
data.properties.lon = (feature.geometry.coordinates[0][0] + feature.geometry.coordinates[1][0]) / 2;
data.properties.lat = (feature.geometry.coordinates[0][1] + feature.geometry.coordinates[1][1]) / 2;
data.geometry.coordinates[0] = data.properties.lon;
data.geometry.coordinates[1] = data.properties.lat;
return data;
});
fs.writeFileSync( 'data/station.geojson', JSON.stringify(json) );
}
cb();
});
});
// 保育園等のデータ更新(国土数値情報 ダウンロードサービスベース)
gulp.task("data-nursery-bk", (cb) => {
shapefile.read('data_org/P14_12.shp', {encoding: 'shift_jis'}, (err, json) => {
if(err) {
console.log(err);
} else {
json.features = json.features.filter((feature) => {
var code = feature.properties.P14_006;
var cityName = feature.properties.P14_002;
return cityName.indexOf('千葉市') === 0
//&& (code === '801' || code === '802' || code === '803' || code === '804' || code === '805');
&& (code === '804');
});
json.features.forEach((feature) => {
feature.properties.Name = feature.properties.P14_007;
feature.properties.Label = feature.properties.P14_007;
feature.properties.Address = feature.properties.P14_002;
feature.properties.Address2 = feature.properties.P14_003;
var code = feature.properties.P14_006;
switch (code) {
case '801':
feature.properties.Type = '認可保育施設'
break;
case '802':
feature.properties.Type = '認可保育施設'
break;
case '803':
feature.properties.Type = '認可外保育施設'
break;
case '804':
feature.properties.Type = '幼稚園'
break;
case '805':
feature.properties.Type = '認可保育施設'
break;
}
});
fs.writeFileSync( 'data/nurseryFacilities_temp.geojson', JSON.stringify(json) );
}
cb();
});
});
// 保育園等のデータ更新(千葉市保育所データCSV)
gulp.task("data-nursery", (cb) => {
var fileName = 'data_org/nurseryData' + (argv.year ? '_' + argv.year : '') + '.csv';
console.log('use file:' + fileName);
fs.createReadStream(fileName)
.pipe(sutil.head(1)) // get head lines
.pipe(sutil.split())
.setEncoding('utf8')
.pipe(csv.parse())
.on('data', function(headers){
var dataList = [];
fs.createReadStream(fileName)
.pipe(iconv.decodeStream('utf-8'))
.pipe(csv.parse())
.pipe(csv.transform(function(record){
var json = {};
record.forEach(function(data, index) {
json[headers[index]] = data ? data : null;
});
return json;
}))
.on('data', function(data) {
dataList.push(data)
}).on('end', function(){
var output = 'data/nurseryFacilities' + (argv.year ? '_' + argv.year : '') + '.geojson';
console.log('output file:' + output);
fs.writeFileSync(output, JSON.stringify(GeoJSON.parse(dataList.slice(1), {Point: ['Y', 'X']})) );
cb();
});
});
});