-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
209 lines (170 loc) · 6.32 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
const { src } = require('gulp');
const fs = require('fs');
const gutil = require('gulp-util');
const clean = require('gulp-clean');
const exec = require('child_process').exec;
const _ = require('lodash');
const iconv = require('iconv-lite');
const shpPath = {
ctprvn : {
source : 'shapefiles/CTPRVN_201804/TL_SCCO_CTPRVN.shp',
convert : 'shapefiles/CTPRVN_201804/TL_SCCO_CTPRVN_simplify.shp',
json : 'dist/ctprvn.json'
},
sig : {
source : 'shapefiles/SIG_201804/TL_SCCO_SIG.shp',
convert : 'shapefiles/SIG_201804/TL_SCCO_SIG_simplify.shp',
json : 'dist/sig.json'
},
emd : {
source : 'shapefiles/EMD_201804/TL_SCCO_EMD.shp',
convert : 'shapefiles/EMD_201804/TL_SCCO_EMD_simplify.shp',
json : 'dist/emd.json'
}
}
function convertTask(cb) {
// clean shp
src(['dist/*.json', 'src/**/*_simplify.*']).pipe(clean());
// simplify shp & convert shp to geojson
for (var key in shpPath) {
console.log('==========');
mapshaper(key);
}
cb();
}
exports.default = convertTask;
function mapshaper(key) {
var command = 'mapshaper -i '
+ shpPath[key].source
+ ' encoding=euc-kr -simplify weighted 1% -proj latlong -o format=shapefile '
+ shpPath[key].convert;
console.log(command);
exec(command, function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(stdout);
console.log(stderr);
console.log('=> convert size')
console.log('%s : %d bytes', shpPath[key].source, fs.statSync(shpPath[key].source).size);
console.log('%s : %d bytes', shpPath[key].convert, fs.statSync(shpPath[key].convert).size);
console.log('=>');
ogr2ogr(key);
});
}
function ogr2ogr(key) {
var command = 'ogr2ogr -f GeoJSON -lco COORDINATE_PRECISION=3 "'
+ shpPath[key].json
+'" "' + shpPath[key].convert + '"';
console.log(command);
exec(command, function (error, stdout, stderr) {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(stdout);
console.log(stderr);
console.log('=> convert json size')
console.log('%s : %d bytes', shpPath[key].json, fs.statSync(shpPath[key].json).size);
console.log('=>')
renameProperties(key);
});
}
function renameProperties(key) {
console.log("\n *Rename GeoJSON Properties START* \n");
console.log(key);
var fileName = shpPath[key].json;
var contents = fs.readFileSync(fileName);
var features ={};
contents = iconv.decode(contents, 'euc-kr');
var jsonContent = JSON.parse(contents);
jsonContent.features.forEach((feature) => {
switch(key) {
case 'ctprvn':
feature.properties.code = feature.properties['CTPRVN_CD'];
feature.properties.name = feature.properties['CTP_ENG_NM'];
feature.properties.label = feature.properties['CTP_KOR_NM'];
delete feature.properties['CTPRVN_CD'];
delete feature.properties['CTP_ENG_NM'];
delete feature.properties['CTP_KOR_NM'];
break;
case 'sig':
feature.properties.code = feature.properties['SIG_CD'];
feature.properties.name = feature.properties['SIG_ENG_NM'];
feature.properties.label = feature.properties['SIG_KOR_NM'];
delete feature.properties['SIG_CD'];
delete feature.properties['SIG_ENG_NM'];
delete feature.properties['SIG_KOR_NM'];
break;
case 'emd':
feature.properties.code = feature.properties['EMD_CD'];
feature.properties.name = feature.properties['EMD_ENG_NM'];
feature.properties.label = feature.properties['EMD_KOR_NM'];
delete feature.properties['EMD_CD'];
delete feature.properties['EMD_ENG_NM'];
delete feature.properties['EMD_KOR_NM'];
break;
}
});
fs.writeFileSync(shpPath[key].json, JSON.stringify(jsonContent));
console.log("\n *End rename task* \n");
splitGeojson(key);
}
function splitGeojson(type) {
console.log("\n *Split geoJSON START* \n");
console.log(type);
// clean split
if (!fs.existsSync(`dist/${type}`)){
fs.mkdirSync(`dist/${type}`);
}
src(`dist/${type}/*.json`).pipe(clean());
var fileName = shpPath[type].json;
//var exception = [ "47940" ];
var exception = [];
// 시군구 데이터 sido 별로 자르기
var contents = fs.readFileSync(fileName);
var features ={};
contents = iconv.decode(contents, 'utf-8');
var jsonContent = JSON.parse(contents);
for (var key in jsonContent.features) {
var feature = jsonContent.features[key];
var subKey, cd, name;
if (type == 'sig') {
cd = feature.properties.code;
name = feature.properties.label;
subKey = feature.properties.code.substr(0, 2);
} else if (type == 'emd') {
cd = feature.properties.code;
name = feature.properties.label;
subKey = feature.properties.code.substr(0, 5);
}
console.log(`feature.properties.cd: ${cd}, feature.properties.name: ${name}`);
if (features.hasOwnProperty(subKey)) {
if (!_.has(exception, cd)) {
features[subKey].push(feature);
}
} else {
features[subKey] = [];
if (!_.has(exception, cd)) {
features[subKey].push(feature);
}
}
}
for (var key in features) {
var featuresCollection = _.template('{"type": "FeatureCollection", "features": [ \
<% _.forEach(iterator, function(val, index, list) { %> \
\n <%= JSON.stringify(val) %><% \
if (index < list.length - 1) { \
%>, <% \
} \
}); %> \
\n]}');
var jsonStr = featuresCollection({
'iterator': features[key]
});
// split json파일 생성
fs.writeFileSync("dist/" + type + "/" + key + ".json", jsonStr);
}
console.log("\n *EXIT* \n");
}