-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
201 lines (168 loc) · 5.15 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
const fs = require("fs")
const { series, src } = require('gulp')
const clean = require('gulp-clean')
const exec = require('child_process').execSync
const axios = require('axios');
const cheerio = require('cheerio');
const download = require('download');
const admZip = require('adm-zip');
const _ = require('lodash')
const iconv = require('iconv-lite')
const SRC = 'src/'
const DIST = 'dist/'
const shpPath = {
ctprvn: {
source: 'src/CTPRVN/TL_SCCO_CTPRVN.shp',
convert: 'src/CTPRVN/TL_SCCO_CTPRVN_CONVERT.shp',
json: 'dist/ctprvn.json'
},
sig: {
source: 'src/SIG/TL_SCCO_SIG.shp',
convert: 'src/SIG/TL_SCCO_SIG_CONVERT.shp',
json: 'dist/sig.json'
},
emd: {
source: 'src/EMD/TL_SCCO_EMD.shp',
convert: 'src/EMD/TL_SCCO_EMD_CONVERT.shp',
json: 'dist/emd.json'
}
}
const cleanAll = () => src(['download/*.zip', 'dist/**/*.json', 'src/**/*.*']).pipe(clean())
const decompress = (filePath) => {
const zip = new admZip(filePath)
const zipEntries = zip.getEntries();
const dirPath = SRC + filePath.split('_')[0].split('/')[1]
// Create SRC Directory
if (!fs.existsSync(SRC)) {
fs.mkdirSync(SRC);
}
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
// Unzip Files
zipEntries.forEach(zipEntry => {
fs.writeFileSync(`${dirPath}/${zipEntry.entryName}`, zipEntry.getData());
});
}
const downloadMapZip = async (done) => {
const getMapSite = async () => {
try {
return await axios.get(
`http://www.gisdeveloper.co.kr/?p=2332`
);
} catch (error) {
console.error(error);
}
};
return getMapSite()
.then(html => {
let filePathList = []
const $ = cheerio.load(html.data);
$('div.entry-content').children('table').each(function (i, elem) {
filePathList[i] = $(this).first().find('a').first().attr('href')
})
Promise.all(filePathList.map(x => download(x, 'download'))).then(() => {
console.log('Files downloaded!');
}).then(() => {
const fileNameList = []
fs.readdir('download/', (err, files) => {
files.forEach(file => {
fileNameList.push('download/' + file)
});
fileNameList.map(path => decompress(path))
});
});
})
}
const mapshaperTask = (done) => {
// Create DIST Directory
if (!fs.existsSync(DIST)) {
fs.mkdirSync(DIST);
}
mapshaper('ctprvn')
mapshaper('sig')
mapshaper('emd')
done()
}
const ogr2ogrTask = (done) => {
ogr2ogr('ctprvn')
ogr2ogr('sig')
ogr2ogr('emd')
done()
}
// 시군구 & 동 geojson 생성
const split = (done) => {
splitGeojson('sig')
splitGeojson('emd')
done()
}
const mapshaper = (key) => {
const mapshaperCommand = `mapshaper -i ${shpPath[key].source} encoding=euc-kr -simplify weighted 0.5% -o format=shapefile ${shpPath[key].convert}`
exec(mapshaperCommand, (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('=>')
})
}
const ogr2ogr = (key) => {
const command = `ogr2ogr -f GeoJSON ${shpPath[key].json} ${shpPath[key].convert}`
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('=>')
})
}
const cleanSplit = () => src(['dist/sig/*.json', 'dist/emd/*.json']).pipe(clean())
function splitGeojson(type) {
console.log("\n *Split geoJSON START* \n")
// 시군구 데이터 시도별로 자르기
const fileName = shpPath[type].json
const contents = iconv.decode(fs.readFileSync(fileName), 'euc-kr')
const features = {}
const jsonContent = JSON.parse(contents)
const typePath = DIST + type
if (!fs.existsSync(typePath)) {
fs.mkdirSync(typePath);
}
for (let key in jsonContent.features) {
const feature = jsonContent.features[key]
let subKey
if (type == 'sig') {
subKey = feature.properties.SIG_CD.substr(0, 2)
} else if (type == 'emd') {
subKey = feature.properties.EMD_CD.substr(0, 5)
}
if (features.hasOwnProperty(subKey)) {
features[subKey].push(feature)
} else {
features[subKey] = []
features[subKey].push(feature)
}
}
for (let key in features) {
const jsonStr = {
"type": "FeatureCollection",
"features": features[key]
}
fs.writeFileSync(`${typePath}/${key}.json`, JSON.stringify(jsonStr), 'utf-8')
}
console.log("\n *Split geoJSON END* \n")
}
// Default task to convert
// Gulp 4.0부터는 Task함수를 사용하기보다 일반 기명함수로 Task를 만들고, CommonJS 모듈 형식으로 내보내기를 권장한다.
// gulp.task('default', ['convert'])
exports.convert = series(mapshaperTask, ogr2ogrTask, cleanSplit, split)
exports.default = series(cleanAll, downloadMapZip)