blob: 52ed37bb1529e0bb8d3c6a83c80214a987c9265e [file] [log] [blame]
Evan Siroky7891a6e2016-11-05 11:50:50 -07001var exec = require('child_process').exec
2var fs = require('fs')
evansirokyf3bcf052020-10-17 23:10:15 -07003var path = require('path')
evansirokyd401c892016-06-16 00:05:14 -07004
Evan Siroky477ece62017-08-01 07:08:51 -07005var area = require('@mapbox/geojson-area')
evansiroky70b35fe2018-04-01 21:06:36 -07006var geojsonhint = require('@mapbox/geojsonhint')
evansiroky0ea1d1e2018-10-30 22:30:51 -07007var bbox = require('@turf/bbox').default
Evan Siroky8326cf02017-03-02 08:27:55 -08008var helpers = require('@turf/helpers')
Evan Siroky070bbb92017-03-07 23:48:29 -08009var multiPolygon = helpers.multiPolygon
Evan Siroky8326cf02017-03-02 08:27:55 -080010var polygon = helpers.polygon
Evan Siroky7891a6e2016-11-05 11:50:50 -070011var asynclib = require('async')
12var jsts = require('jsts')
Evan Sirokyb57a5b92016-11-07 10:22:34 -080013var rimraf = require('rimraf')
Evan Siroky7891a6e2016-11-05 11:50:50 -070014var overpass = require('query-overpass')
Neil Fullerc4ae49b2020-04-30 18:08:43 +010015var yargs = require('yargs')
evansirokyd401c892016-06-16 00:05:14 -070016
evansiroky5348a6f2019-01-05 15:39:28 -080017const ProgressStats = require('./progressStats')
18
Evan Siroky7891a6e2016-11-05 11:50:50 -070019var osmBoundarySources = require('./osmBoundarySources.json')
20var zoneCfg = require('./timezones.json')
evansiroky0ea1d1e2018-10-30 22:30:51 -070021var expectedZoneOverlaps = require('./expectedZoneOverlaps.json')
Evan Siroky081648a2017-07-04 09:53:36 -070022
Neil Fullerc4ae49b2020-04-30 18:08:43 +010023const argv = yargs
24 .option('included_zones', {
25 description: 'Include specified zones',
26 type: 'array'
27 })
Neil Fuller2b4b80a2020-04-30 18:20:56 +010028 .option('excluded_zones', {
29 description: 'Exclude specified zones',
30 type: 'array'
31 })
Neil Fullera4e73272020-04-30 18:25:44 +010032 .option('downloads_dir', {
33 description: 'Set the download location',
34 default: './downloads',
35 type: 'string'
36 })
37 .option('dist_dir', {
38 description: 'Set the dist location',
39 default: './dist',
40 type: 'string'
41 })
Neil Fullerc4ae49b2020-04-30 18:08:43 +010042 .option('no_validation', {
43 description: 'Skip validation',
44 type: 'boolean'
45 })
Neil Fullera83cc6d2020-04-30 18:37:08 +010046 .option('skip_zip', {
47 description: 'Skip zip creation',
48 type: 'boolean'
49 })
50 .option('skip_shapefile', {
51 description: 'Skip shapefile creation',
52 type: 'boolean'
53 })
Neil Fullerc4ae49b2020-04-30 18:08:43 +010054 .help()
55 .strict()
56 .alias('help', 'h')
57 .argv
58
Neil Fuller01803192020-08-14 11:19:42 +010059// Resolve the arguments with paths so relative paths become absolute.
evansirokyb7d9d792020-10-17 16:50:14 -070060const downloadsDir = path.resolve(argv.downloads_dir)
61const distDir = path.resolve(argv.dist_dir)
Neil Fuller01803192020-08-14 11:19:42 +010062
Evan Siroky081648a2017-07-04 09:53:36 -070063// allow building of only a specified zones
Neil Fullerc4ae49b2020-04-30 18:08:43 +010064let includedZones = []
Neil Fuller2b4b80a2020-04-30 18:20:56 +010065let excludedZones = []
66if (argv.included_zones || argv.excluded_zones) {
67 if (argv.included_zones) {
evansirokyb7d9d792020-10-17 16:50:14 -070068 const newZoneCfg = {}
Neil Fuller2b4b80a2020-04-30 18:20:56 +010069 includedZones = argv.included_zones
70 includedZones.forEach((zoneName) => {
71 newZoneCfg[zoneName] = zoneCfg[zoneName]
72 })
73 zoneCfg = newZoneCfg
74 }
75 if (argv.excluded_zones) {
evansirokyb7d9d792020-10-17 16:50:14 -070076 const newZoneCfg = {}
Neil Fuller2b4b80a2020-04-30 18:20:56 +010077 excludedZones = argv.excluded_zones
78 Object.keys(zoneCfg).forEach((zoneName) => {
79 if (!excludedZones.includes(zoneName)) {
80 newZoneCfg[zoneName] = zoneCfg[zoneName]
81 }
82 })
83 zoneCfg = newZoneCfg
84 }
Evan Siroky081648a2017-07-04 09:53:36 -070085
86 // filter out unneccessary downloads
87 var newOsmBoundarySources = {}
88 Object.keys(zoneCfg).forEach((zoneName) => {
89 zoneCfg[zoneName].forEach((op) => {
90 if (op.source === 'overpass') {
91 newOsmBoundarySources[op.id] = osmBoundarySources[op.id]
92 }
93 })
94 })
95
96 osmBoundarySources = newOsmBoundarySources
97}
98
Evan Siroky7891a6e2016-11-05 11:50:50 -070099var geoJsonReader = new jsts.io.GeoJSONReader()
100var geoJsonWriter = new jsts.io.GeoJSONWriter()
Evan Siroky477ece62017-08-01 07:08:51 -0700101var precisionModel = new jsts.geom.PrecisionModel(1000000)
102var precisionReducer = new jsts.precision.GeometryPrecisionReducer(precisionModel)
Evan Siroky7891a6e2016-11-05 11:50:50 -0700103var distZones = {}
evansiroky96dfadc2020-04-28 01:31:38 -0700104var lastReleaseJSONfile
Evan Sirokyb57a5b92016-11-07 10:22:34 -0800105var minRequestGap = 4
106var curRequestGap = 4
evansirokyd401c892016-06-16 00:05:14 -0700107
Evan Siroky7891a6e2016-11-05 11:50:50 -0700108var safeMkdir = function (dirname, callback) {
109 fs.mkdir(dirname, function (err) {
110 if (err && err.code === 'EEXIST') {
evansiroky4be1c7a2016-06-16 18:23:34 -0700111 callback()
112 } else {
113 callback(err)
114 }
115 })
116}
117
Evan Sirokyb173fd42017-03-08 15:16:27 -0800118var debugGeo = function (op, a, b, reducePrecision) {
evansirokybecb56e2016-07-06 12:42:35 -0700119 var result
120
Evan Sirokyb173fd42017-03-08 15:16:27 -0800121 if (reducePrecision) {
Evan Sirokyb173fd42017-03-08 15:16:27 -0800122 a = precisionReducer.reduce(a)
123 b = precisionReducer.reduce(b)
124 }
125
evansiroky6f9d8f72016-06-21 16:27:54 -0700126 try {
Evan Siroky7891a6e2016-11-05 11:50:50 -0700127 switch (op) {
evansiroky6f9d8f72016-06-21 16:27:54 -0700128 case 'union':
evansirokybecb56e2016-07-06 12:42:35 -0700129 result = a.union(b)
evansiroky6f9d8f72016-06-21 16:27:54 -0700130 break
131 case 'intersection':
evansirokybecb56e2016-07-06 12:42:35 -0700132 result = a.intersection(b)
evansiroky6f9d8f72016-06-21 16:27:54 -0700133 break
Evan Siroky070bbb92017-03-07 23:48:29 -0800134 case 'intersects':
135 result = a.intersects(b)
136 break
evansiroky6f9d8f72016-06-21 16:27:54 -0700137 case 'diff':
Evan Sirokyb173fd42017-03-08 15:16:27 -0800138 result = a.difference(b)
evansiroky6f9d8f72016-06-21 16:27:54 -0700139 break
140 default:
141 var err = new Error('invalid op: ' + op)
142 throw err
143 }
Evan Siroky7891a6e2016-11-05 11:50:50 -0700144 } catch (e) {
Evan Sirokyb173fd42017-03-08 15:16:27 -0800145 if (e.name === 'TopologyException') {
146 console.log('Encountered TopologyException, retry with GeometryPrecisionReducer')
147 return debugGeo(op, a, b, true)
148 }
evansiroky6f9d8f72016-06-21 16:27:54 -0700149 console.log('op err')
evansirokybecb56e2016-07-06 12:42:35 -0700150 console.log(e)
151 console.log(e.stack)
152 fs.writeFileSync('debug_' + op + '_a.json', JSON.stringify(geoJsonWriter.write(a)))
153 fs.writeFileSync('debug_' + op + '_b.json', JSON.stringify(geoJsonWriter.write(b)))
evansiroky6f9d8f72016-06-21 16:27:54 -0700154 throw e
155 }
evansiroky6f9d8f72016-06-21 16:27:54 -0700156
evansirokybecb56e2016-07-06 12:42:35 -0700157 return result
evansiroky4be1c7a2016-06-16 18:23:34 -0700158}
159
Evan Siroky070bbb92017-03-07 23:48:29 -0800160var fetchIfNeeded = function (file, superCallback, downloadCallback, fetchFn) {
161 // check for file that got downloaded
Evan Siroky7891a6e2016-11-05 11:50:50 -0700162 fs.stat(file, function (err) {
Evan Siroky070bbb92017-03-07 23:48:29 -0800163 if (!err) {
164 // file found, skip download steps
165 return superCallback()
166 }
167 // check for manual file that got fixed and needs validation
168 var fixedFile = file.replace('.json', '_fixed.json')
169 fs.stat(fixedFile, function (err) {
170 if (!err) {
171 // file found, return fixed file
172 return downloadCallback(null, require(fixedFile))
173 }
174 // no manual fixed file found, download from overpass
175 fetchFn()
176 })
evansiroky50216c62016-06-16 17:41:47 -0700177 })
178}
179
Evan Siroky7891a6e2016-11-05 11:50:50 -0700180var geoJsonToGeom = function (geoJson) {
Evan Siroky8326cf02017-03-02 08:27:55 -0800181 try {
182 return geoJsonReader.read(JSON.stringify(geoJson))
183 } catch (e) {
184 console.error('error converting geojson to geometry')
185 fs.writeFileSync('debug_geojson_read_error.json', JSON.stringify(geoJson))
186 throw e
187 }
Evan Siroky5669adc2016-07-07 17:25:31 -0700188}
189
Evan Siroky8b47abe2016-10-02 12:28:52 -0700190var geomToGeoJson = function (geom) {
191 return geoJsonWriter.write(geom)
192}
193
Evan Siroky7891a6e2016-11-05 11:50:50 -0700194var geomToGeoJsonString = function (geom) {
Evan Siroky5669adc2016-07-07 17:25:31 -0700195 return JSON.stringify(geoJsonWriter.write(geom))
196}
197
evansiroky5348a6f2019-01-05 15:39:28 -0800198const downloadProgress = new ProgressStats(
199 'Downloading',
200 Object.keys(osmBoundarySources).length
201)
202
Evan Siroky7891a6e2016-11-05 11:50:50 -0700203var downloadOsmBoundary = function (boundaryId, boundaryCallback) {
204 var cfg = osmBoundarySources[boundaryId]
Evan Siroky1bcd4772017-10-14 23:47:21 -0700205 var query = '[out:json][timeout:60];('
206 if (cfg.way) {
207 query += 'way'
208 } else {
209 query += 'relation'
210 }
Neil Fullerdce1d042020-10-14 09:53:00 +0100211 var boundaryFilename = downloadsDir + '/' + boundaryId + '.json'
Evan Siroky7891a6e2016-11-05 11:50:50 -0700212 var debug = 'getting data for ' + boundaryId
213 var queryKeys = Object.keys(cfg)
evansiroky63d35e12016-06-16 10:08:15 -0700214
Evan Siroky5669adc2016-07-07 17:25:31 -0700215 for (var i = queryKeys.length - 1; i >= 0; i--) {
Evan Siroky7891a6e2016-11-05 11:50:50 -0700216 var k = queryKeys[i]
Evan Siroky1bcd4772017-10-14 23:47:21 -0700217 if (k === 'way') continue
Evan Siroky7891a6e2016-11-05 11:50:50 -0700218 var v = cfg[k]
Evan Siroky5669adc2016-07-07 17:25:31 -0700219
220 query += '["' + k + '"="' + v + '"]'
evansiroky63d35e12016-06-16 10:08:15 -0700221 }
222
evansiroky283ebbc2018-07-16 15:13:07 -0700223 query += ';);out body;>;out meta qt;'
evansiroky4be1c7a2016-06-16 18:23:34 -0700224
evansiroky5348a6f2019-01-05 15:39:28 -0800225 downloadProgress.beginTask(debug, true)
evansiroky63d35e12016-06-16 10:08:15 -0700226
Evan Siroky7891a6e2016-11-05 11:50:50 -0700227 asynclib.auto({
228 downloadFromOverpass: function (cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800229 console.log('downloading from overpass')
Evan Siroky070bbb92017-03-07 23:48:29 -0800230 fetchIfNeeded(boundaryFilename, boundaryCallback, cb, function () {
Evan Sirokyb57a5b92016-11-07 10:22:34 -0800231 var overpassResponseHandler = function (err, data) {
232 if (err) {
233 console.log(err)
234 console.log('Increasing overpass request gap')
235 curRequestGap *= 2
236 makeQuery()
237 } else {
238 console.log('Success, decreasing overpass request gap')
239 curRequestGap = Math.max(minRequestGap, curRequestGap / 2)
240 cb(null, data)
241 }
242 }
243 var makeQuery = function () {
244 console.log('waiting ' + curRequestGap + ' seconds')
245 setTimeout(function () {
246 overpass(query, overpassResponseHandler, { flatProperties: true })
247 }, curRequestGap * 1000)
248 }
249 makeQuery()
evansiroky63d35e12016-06-16 10:08:15 -0700250 })
251 },
Evan Siroky7891a6e2016-11-05 11:50:50 -0700252 validateOverpassResult: ['downloadFromOverpass', function (results, cb) {
evansiroky63d35e12016-06-16 10:08:15 -0700253 var data = results.downloadFromOverpass
evansiroky70b35fe2018-04-01 21:06:36 -0700254 if (!data.features) {
Evan Siroky7891a6e2016-11-05 11:50:50 -0700255 var err = new Error('Invalid geojson for boundary: ' + boundaryId)
evansiroky63d35e12016-06-16 10:08:15 -0700256 return cb(err)
257 }
evansiroky70b35fe2018-04-01 21:06:36 -0700258 if (data.features.length === 0) {
259 console.error('No data for the following query:')
260 console.error(query)
261 console.error('To read more about this error, please visit https://git.io/vxKQL')
evansiroky0ea1d1e2018-10-30 22:30:51 -0700262 return cb(new Error('No data found for from overpass query'))
evansiroky70b35fe2018-04-01 21:06:36 -0700263 }
evansiroky63d35e12016-06-16 10:08:15 -0700264 cb()
265 }],
Evan Siroky7891a6e2016-11-05 11:50:50 -0700266 saveSingleMultiPolygon: ['validateOverpassResult', function (results, cb) {
267 var data = results.downloadFromOverpass
268 var combined
evansiroky63d35e12016-06-16 10:08:15 -0700269
270 // union all multi-polygons / polygons into one
271 for (var i = data.features.length - 1; i >= 0; i--) {
Evan Siroky5669adc2016-07-07 17:25:31 -0700272 var curOsmGeom = data.features[i].geometry
evansiroky92c15c42018-11-15 20:58:18 -0800273 const curOsmProps = data.features[i].properties
274 if (
275 (curOsmGeom.type === 'Polygon' || curOsmGeom.type === 'MultiPolygon') &&
276 curOsmProps.type === 'boundary' // need to make sure enclaves aren't unioned
277 ) {
evansiroky63d35e12016-06-16 10:08:15 -0700278 console.log('combining border')
evansiroky70b35fe2018-04-01 21:06:36 -0700279 let errors = geojsonhint.hint(curOsmGeom)
280 if (errors && errors.length > 0) {
281 const stringifiedGeojson = JSON.stringify(curOsmGeom, null, 2)
282 errors = geojsonhint.hint(stringifiedGeojson)
283 console.error('Invalid geojson received in Overpass Result')
284 console.error('Overpass query: ' + query)
285 const problemFilename = boundaryId + '_convert_to_geom_error.json'
286 fs.writeFileSync(problemFilename, stringifiedGeojson)
287 console.error('saved problem file to ' + problemFilename)
288 console.error('To read more about this error, please visit https://git.io/vxKQq')
289 return cb(errors)
290 }
Evan Siroky070bbb92017-03-07 23:48:29 -0800291 try {
292 var curGeom = geoJsonToGeom(curOsmGeom)
293 } catch (e) {
294 console.error('error converting overpass result to geojson')
Evan Siroky477ece62017-08-01 07:08:51 -0700295 console.error(e)
evansiroky70b35fe2018-04-01 21:06:36 -0700296
Evan Siroky1bcd4772017-10-14 23:47:21 -0700297 fs.writeFileSync(boundaryId + '_convert_to_geom_error-all-features.json', JSON.stringify(data))
evansiroky70b35fe2018-04-01 21:06:36 -0700298 return cb(e)
Evan Siroky070bbb92017-03-07 23:48:29 -0800299 }
Evan Siroky7891a6e2016-11-05 11:50:50 -0700300 if (!combined) {
evansiroky63d35e12016-06-16 10:08:15 -0700301 combined = curGeom
302 } else {
Evan Siroky5669adc2016-07-07 17:25:31 -0700303 combined = debugGeo('union', curGeom, combined)
evansiroky63d35e12016-06-16 10:08:15 -0700304 }
305 }
306 }
Evan Siroky081c8e42017-05-29 14:53:52 -0700307 try {
308 fs.writeFile(boundaryFilename, geomToGeoJsonString(combined), cb)
309 } catch (e) {
310 console.error('error writing combined border to geojson')
311 fs.writeFileSync(boundaryId + '_combined_border_convert_to_geom_error.json', JSON.stringify(data))
evansiroky70b35fe2018-04-01 21:06:36 -0700312 return cb(e)
Evan Siroky081c8e42017-05-29 14:53:52 -0700313 }
evansiroky63d35e12016-06-16 10:08:15 -0700314 }]
315 }, boundaryCallback)
316}
evansirokyd401c892016-06-16 00:05:14 -0700317
Evan Siroky4fc596c2016-09-25 19:52:30 -0700318var getTzDistFilename = function (tzid) {
Neil Fullerdce1d042020-10-14 09:53:00 +0100319 return distDir + '/' + tzid.replace(/\//g, '__') + '.json'
Evan Siroky4fc596c2016-09-25 19:52:30 -0700320}
321
322/**
323 * Get the geometry of the requested source data
324 *
325 * @return {Object} geom The geometry of the source
326 * @param {Object} source An object representing the data source
327 * must have `source` key and then either:
328 * - `id` if from a file
329 * - `id` if from a file
330 */
Evan Siroky7891a6e2016-11-05 11:50:50 -0700331var getDataSource = function (source) {
evansirokybecb56e2016-07-06 12:42:35 -0700332 var geoJson
Evan Siroky7891a6e2016-11-05 11:50:50 -0700333 if (source.source === 'overpass') {
Neil Fullerdce1d042020-10-14 09:53:00 +0100334 geoJson = require(downloadsDir + '/' + source.id + '.json')
Evan Siroky7891a6e2016-11-05 11:50:50 -0700335 } else if (source.source === 'manual-polygon') {
evansirokybecb56e2016-07-06 12:42:35 -0700336 geoJson = polygon(source.data).geometry
Evan Siroky7891a6e2016-11-05 11:50:50 -0700337 } else if (source.source === 'manual-multipolygon') {
Evan Siroky8e30a2e2016-08-06 19:55:35 -0700338 geoJson = multiPolygon(source.data).geometry
Evan Siroky7891a6e2016-11-05 11:50:50 -0700339 } else if (source.source === 'dist') {
Evan Siroky4fc596c2016-09-25 19:52:30 -0700340 geoJson = require(getTzDistFilename(source.id))
evansiroky4be1c7a2016-06-16 18:23:34 -0700341 } else {
342 var err = new Error('unknown source: ' + source.source)
343 throw err
344 }
Evan Siroky5669adc2016-07-07 17:25:31 -0700345 return geoJsonToGeom(geoJson)
evansiroky4be1c7a2016-06-16 18:23:34 -0700346}
347
Evan Siroky477ece62017-08-01 07:08:51 -0700348/**
349 * Post process created timezone boundary.
350 * - remove small holes and exclaves
351 * - reduce geometry precision
352 *
353 * @param {Geometry} geom The jsts geometry of the timezone
evansiroky26325842018-04-03 14:10:42 -0700354 * @param {boolean} returnAsObject if true, return as object, otherwise return stringified
355 * @return {Object|String} geojson as object or stringified
Evan Siroky477ece62017-08-01 07:08:51 -0700356 */
evansiroky26325842018-04-03 14:10:42 -0700357var postProcessZone = function (geom, returnAsObject) {
Evan Siroky477ece62017-08-01 07:08:51 -0700358 // reduce precision of geometry
359 const geojson = geomToGeoJson(precisionReducer.reduce(geom))
360
361 // iterate through all polygons
362 const filteredPolygons = []
363 let allPolygons = geojson.coordinates
364 if (geojson.type === 'Polygon') {
365 allPolygons = [geojson.coordinates]
366 }
367
368 allPolygons.forEach((curPolygon, idx) => {
369 // remove any polygon with very small area
370 const polygonFeature = polygon(curPolygon)
371 const polygonArea = area.geometry(polygonFeature.geometry)
372
373 if (polygonArea < 1) return
374
375 // find all holes
376 const filteredLinearRings = []
377
378 curPolygon.forEach((curLinearRing, lrIdx) => {
379 if (lrIdx === 0) {
380 // always keep first linearRing
381 filteredLinearRings.push(curLinearRing)
382 } else {
383 const polygonFromLinearRing = polygon([curLinearRing])
384 const linearRingArea = area.geometry(polygonFromLinearRing.geometry)
385
386 // only include holes with relevant area
387 if (linearRingArea > 1) {
388 filteredLinearRings.push(curLinearRing)
389 }
390 }
391 })
392
393 filteredPolygons.push(filteredLinearRings)
394 })
395
396 // recompile to geojson string
397 const newGeojson = {
398 type: geojson.type
399 }
400
401 if (geojson.type === 'Polygon') {
402 newGeojson.coordinates = filteredPolygons[0]
403 } else {
404 newGeojson.coordinates = filteredPolygons
405 }
406
evansiroky26325842018-04-03 14:10:42 -0700407 return returnAsObject ? newGeojson : JSON.stringify(newGeojson)
Evan Siroky477ece62017-08-01 07:08:51 -0700408}
409
evansiroky5348a6f2019-01-05 15:39:28 -0800410const buildingProgress = new ProgressStats(
411 'Building',
412 Object.keys(zoneCfg).length
413)
414
Evan Siroky7891a6e2016-11-05 11:50:50 -0700415var makeTimezoneBoundary = function (tzid, callback) {
evansiroky3046a3d2019-01-05 21:19:14 -0800416 buildingProgress.beginTask(`makeTimezoneBoundary for ${tzid}`, true)
evansiroky35f64342016-06-16 22:17:04 -0700417
Evan Siroky7891a6e2016-11-05 11:50:50 -0700418 var ops = zoneCfg[tzid]
419 var geom
evansiroky4be1c7a2016-06-16 18:23:34 -0700420
Evan Siroky7891a6e2016-11-05 11:50:50 -0700421 asynclib.eachSeries(ops, function (task, cb) {
evansiroky4be1c7a2016-06-16 18:23:34 -0700422 var taskData = getDataSource(task)
evansiroky6f9d8f72016-06-21 16:27:54 -0700423 console.log('-', task.op, task.id)
Evan Siroky7891a6e2016-11-05 11:50:50 -0700424 if (task.op === 'init') {
evansiroky4be1c7a2016-06-16 18:23:34 -0700425 geom = taskData
Evan Siroky7891a6e2016-11-05 11:50:50 -0700426 } else if (task.op === 'intersect') {
evansiroky6f9d8f72016-06-21 16:27:54 -0700427 geom = debugGeo('intersection', geom, taskData)
Evan Siroky7891a6e2016-11-05 11:50:50 -0700428 } else if (task.op === 'difference') {
evansiroky6f9d8f72016-06-21 16:27:54 -0700429 geom = debugGeo('diff', geom, taskData)
Evan Siroky7891a6e2016-11-05 11:50:50 -0700430 } else if (task.op === 'difference-reverse-order') {
Evan Siroky8ccaf0b2016-09-03 11:36:13 -0700431 geom = debugGeo('diff', taskData, geom)
Evan Siroky7891a6e2016-11-05 11:50:50 -0700432 } else if (task.op === 'union') {
evansiroky6f9d8f72016-06-21 16:27:54 -0700433 geom = debugGeo('union', geom, taskData)
Evan Siroky8ccaf0b2016-09-03 11:36:13 -0700434 } else {
435 var err = new Error('unknown op: ' + task.op)
436 return cb(err)
evansiroky4be1c7a2016-06-16 18:23:34 -0700437 }
evansiroky35f64342016-06-16 22:17:04 -0700438 cb()
Evan Siroky4fc596c2016-09-25 19:52:30 -0700439 },
Evan Siroky7891a6e2016-11-05 11:50:50 -0700440 function (err) {
441 if (err) { return callback(err) }
Evan Siroky4fc596c2016-09-25 19:52:30 -0700442 fs.writeFile(getTzDistFilename(tzid),
Evan Siroky477ece62017-08-01 07:08:51 -0700443 postProcessZone(geom),
evansirokybecb56e2016-07-06 12:42:35 -0700444 callback)
evansiroky4be1c7a2016-06-16 18:23:34 -0700445 })
446}
447
Evan Siroky4fc596c2016-09-25 19:52:30 -0700448var loadDistZonesIntoMemory = function () {
449 console.log('load zones into memory')
Evan Siroky7891a6e2016-11-05 11:50:50 -0700450 var zones = Object.keys(zoneCfg)
451 var tzid
Evan Siroky4fc596c2016-09-25 19:52:30 -0700452
453 for (var i = 0; i < zones.length; i++) {
454 tzid = zones[i]
455 distZones[tzid] = getDataSource({ source: 'dist', id: tzid })
456 }
457}
458
459var getDistZoneGeom = function (tzid) {
460 return distZones[tzid]
461}
462
evansiroky92c15c42018-11-15 20:58:18 -0800463var roundDownToTenth = function (n) {
464 return Math.floor(n * 10) / 10
465}
466
467var roundUpToTenth = function (n) {
468 return Math.ceil(n * 10) / 10
469}
470
471var formatBounds = function (bounds) {
472 let boundsStr = '['
473 boundsStr += roundDownToTenth(bounds[0]) + ', '
474 boundsStr += roundDownToTenth(bounds[1]) + ', '
475 boundsStr += roundUpToTenth(bounds[2]) + ', '
476 boundsStr += roundUpToTenth(bounds[3]) + ']'
477 return boundsStr
478}
479
Evan Siroky4fc596c2016-09-25 19:52:30 -0700480var validateTimezoneBoundaries = function () {
evansiroky5348a6f2019-01-05 15:39:28 -0800481 const numZones = Object.keys(zoneCfg).length
482 const validationProgress = new ProgressStats(
483 'Validation',
484 numZones * (numZones + 1) / 2
485 )
486
evansiroky26325842018-04-03 14:10:42 -0700487 console.log('do validation... this may take a few minutes')
Evan Siroky7891a6e2016-11-05 11:50:50 -0700488 var allZonesOk = true
489 var zones = Object.keys(zoneCfg)
evansiroky3046a3d2019-01-05 21:19:14 -0800490 var lastPct = 0
Evan Siroky7891a6e2016-11-05 11:50:50 -0700491 var compareTzid, tzid, zoneGeom
Evan Siroky4fc596c2016-09-25 19:52:30 -0700492
493 for (var i = 0; i < zones.length; i++) {
494 tzid = zones[i]
495 zoneGeom = getDistZoneGeom(tzid)
496
497 for (var j = i + 1; j < zones.length; j++) {
evansiroky3046a3d2019-01-05 21:19:14 -0800498 const curPct = Math.floor(validationProgress.getPercentage())
499 if (curPct % 10 === 0 && curPct !== lastPct) {
evansiroky5348a6f2019-01-05 15:39:28 -0800500 validationProgress.printStats('Validating zones', true)
evansiroky3046a3d2019-01-05 21:19:14 -0800501 lastPct = curPct
evansiroky5348a6f2019-01-05 15:39:28 -0800502 }
Evan Siroky4fc596c2016-09-25 19:52:30 -0700503 compareTzid = zones[j]
504
505 var compareZoneGeom = getDistZoneGeom(compareTzid)
Evan Siroky070bbb92017-03-07 23:48:29 -0800506
507 var intersects = false
508 try {
509 intersects = debugGeo('intersects', zoneGeom, compareZoneGeom)
510 } catch (e) {
511 console.warn('warning, encountered intersection error with zone ' + tzid + ' and ' + compareTzid)
512 }
513 if (intersects) {
Evan Siroky7891a6e2016-11-05 11:50:50 -0700514 var intersectedGeom = debugGeo('intersection', zoneGeom, compareZoneGeom)
515 var intersectedArea = intersectedGeom.getArea()
Evan Siroky4fc596c2016-09-25 19:52:30 -0700516
Evan Siroky7891a6e2016-11-05 11:50:50 -0700517 if (intersectedArea > 0.0001) {
evansiroky0ea1d1e2018-10-30 22:30:51 -0700518 // check if the intersected area(s) are one of the expected areas of overlap
519 const allowedOverlapBounds = expectedZoneOverlaps[`${tzid}-${compareTzid}`] || expectedZoneOverlaps[`${compareTzid}-${tzid}`]
520 const overlapsGeoJson = geoJsonWriter.write(intersectedGeom)
521
522 // these zones are allowed to overlap in certain places, make sure the
523 // found overlap(s) all fit within the expected areas of overlap
524 if (allowedOverlapBounds) {
525 // if the overlaps are a multipolygon, make sure each individual
526 // polygon of overlap fits within at least one of the expected
527 // overlaps
528 let overlapsPolygons
529 switch (overlapsGeoJson.type) {
evansiroky92c15c42018-11-15 20:58:18 -0800530 case 'MultiPolygon':
531 overlapsPolygons = overlapsGeoJson.coordinates.map(
532 polygonCoords => ({
533 coordinates: polygonCoords,
534 type: 'Polygon'
535 })
536 )
537 break
evansiroky0ea1d1e2018-10-30 22:30:51 -0700538 case 'Polygon':
539 overlapsPolygons = [overlapsGeoJson]
540 break
evansiroky92c15c42018-11-15 20:58:18 -0800541 case 'GeometryCollection':
542 overlapsPolygons = []
543 overlapsGeoJson.geometries.forEach(geom => {
544 if (geom.type === 'Polygon') {
545 overlapsPolygons.push(geom)
546 } else if (geom.type === 'MultiPolygon') {
547 geom.coordinates.forEach(polygonCoords => {
548 overlapsPolygons.push({
549 coordinates: polygonCoords,
550 type: 'Polygon'
551 })
552 })
553 }
554 })
555 break
evansiroky0ea1d1e2018-10-30 22:30:51 -0700556 default:
evansiroky92c15c42018-11-15 20:58:18 -0800557 console.error('unexpected geojson overlap type')
558 console.log(overlapsGeoJson)
evansiroky0ea1d1e2018-10-30 22:30:51 -0700559 break
560 }
561
562 let allOverlapsOk = true
563 overlapsPolygons.forEach((polygon, idx) => {
564 const bounds = bbox(polygon)
evansiroky92c15c42018-11-15 20:58:18 -0800565 const polygonArea = area.geometry(polygon)
evansiroky0ea1d1e2018-10-30 22:30:51 -0700566 if (
evansiroky92c15c42018-11-15 20:58:18 -0800567 polygonArea > 10 && // ignore small polygons
evansiroky0ea1d1e2018-10-30 22:30:51 -0700568 !allowedOverlapBounds.some(allowedBounds =>
evansiroky92c15c42018-11-15 20:58:18 -0800569 allowedBounds.bounds[0] <= bounds[0] && // minX
570 allowedBounds.bounds[1] <= bounds[1] && // minY
571 allowedBounds.bounds[2] >= bounds[2] && // maxX
572 allowedBounds.bounds[3] >= bounds[3] // maxY
evansiroky0ea1d1e2018-10-30 22:30:51 -0700573 )
574 ) {
evansiroky92c15c42018-11-15 20:58:18 -0800575 console.error(`Unexpected intersection (${polygonArea} area) with bounds: ${formatBounds(bounds)}`)
evansiroky0ea1d1e2018-10-30 22:30:51 -0700576 allOverlapsOk = false
577 }
578 })
579
580 if (allOverlapsOk) continue
581 }
582
evansiroky92c15c42018-11-15 20:58:18 -0800583 // at least one unexpected overlap found, output an error and write debug file
evansiroky70b35fe2018-04-01 21:06:36 -0700584 console.error('Validation error: ' + tzid + ' intersects ' + compareTzid + ' area: ' + intersectedArea)
evansiroky92c15c42018-11-15 20:58:18 -0800585 const debugFilename = tzid.replace(/\//g, '-') + '-' + compareTzid.replace(/\//g, '-') + '-overlap.json'
evansiroky70b35fe2018-04-01 21:06:36 -0700586 fs.writeFileSync(
587 debugFilename,
evansiroky0ea1d1e2018-10-30 22:30:51 -0700588 JSON.stringify(overlapsGeoJson)
evansiroky70b35fe2018-04-01 21:06:36 -0700589 )
590 console.error('wrote overlap area as file ' + debugFilename)
591 console.error('To read more about this error, please visit https://git.io/vx6nx')
Evan Siroky4fc596c2016-09-25 19:52:30 -0700592 allZonesOk = false
593 }
594 }
evansiroky5348a6f2019-01-05 15:39:28 -0800595 validationProgress.logNext()
Evan Siroky4fc596c2016-09-25 19:52:30 -0700596 }
597 }
598
599 return allZonesOk ? null : 'Zone validation unsuccessful'
Evan Siroky4fc596c2016-09-25 19:52:30 -0700600}
601
evansiroky26325842018-04-03 14:10:42 -0700602let oceanZoneBoundaries
evansiroky9fd50512019-07-07 12:06:28 -0700603let oceanZones = [
604 { tzid: 'Etc/GMT-12', left: 172.5, right: 180 },
605 { tzid: 'Etc/GMT-11', left: 157.5, right: 172.5 },
606 { tzid: 'Etc/GMT-10', left: 142.5, right: 157.5 },
607 { tzid: 'Etc/GMT-9', left: 127.5, right: 142.5 },
608 { tzid: 'Etc/GMT-8', left: 112.5, right: 127.5 },
609 { tzid: 'Etc/GMT-7', left: 97.5, right: 112.5 },
610 { tzid: 'Etc/GMT-6', left: 82.5, right: 97.5 },
611 { tzid: 'Etc/GMT-5', left: 67.5, right: 82.5 },
612 { tzid: 'Etc/GMT-4', left: 52.5, right: 67.5 },
613 { tzid: 'Etc/GMT-3', left: 37.5, right: 52.5 },
614 { tzid: 'Etc/GMT-2', left: 22.5, right: 37.5 },
615 { tzid: 'Etc/GMT-1', left: 7.5, right: 22.5 },
616 { tzid: 'Etc/GMT', left: -7.5, right: 7.5 },
617 { tzid: 'Etc/GMT+1', left: -22.5, right: -7.5 },
618 { tzid: 'Etc/GMT+2', left: -37.5, right: -22.5 },
619 { tzid: 'Etc/GMT+3', left: -52.5, right: -37.5 },
620 { tzid: 'Etc/GMT+4', left: -67.5, right: -52.5 },
621 { tzid: 'Etc/GMT+5', left: -82.5, right: -67.5 },
622 { tzid: 'Etc/GMT+6', left: -97.5, right: -82.5 },
623 { tzid: 'Etc/GMT+7', left: -112.5, right: -97.5 },
624 { tzid: 'Etc/GMT+8', left: -127.5, right: -112.5 },
625 { tzid: 'Etc/GMT+9', left: -142.5, right: -127.5 },
626 { tzid: 'Etc/GMT+10', left: -157.5, right: -142.5 },
627 { tzid: 'Etc/GMT+11', left: -172.5, right: -157.5 },
628 { tzid: 'Etc/GMT+12', left: -180, right: -172.5 }
629]
630
Neil Fullerc4ae49b2020-04-30 18:08:43 +0100631if (includedZones.length > 0) {
632 oceanZones = oceanZones.filter(oceanZone => includedZones.indexOf(oceanZone) > -1)
evansiroky9fd50512019-07-07 12:06:28 -0700633}
Neil Fuller2b4b80a2020-04-30 18:20:56 +0100634if (excludedZones.length > 0) {
635 oceanZones = oceanZones.filter(oceanZone => excludedZones.indexOf(oceanZone) === -1)
636}
evansiroky26325842018-04-03 14:10:42 -0700637
638var addOceans = function (callback) {
639 console.log('adding ocean boundaries')
evansiroky26325842018-04-03 14:10:42 -0700640 const zones = Object.keys(zoneCfg)
641
evansiroky3046a3d2019-01-05 21:19:14 -0800642 const oceanProgress = new ProgressStats(
643 'Oceans',
644 oceanZones.length
645 )
646
evansiroky26325842018-04-03 14:10:42 -0700647 oceanZoneBoundaries = oceanZones.map(zone => {
evansiroky3046a3d2019-01-05 21:19:14 -0800648 oceanProgress.beginTask(zone.tzid, true)
evansiroky26325842018-04-03 14:10:42 -0700649 const geoJson = polygon([[
650 [zone.left, 90],
evansiroky0ea1d1e2018-10-30 22:30:51 -0700651 [zone.left, -90],
652 [zone.right, -90],
653 [zone.right, 90],
evansiroky26325842018-04-03 14:10:42 -0700654 [zone.left, 90]
655 ]]).geometry
656
657 let geom = geoJsonToGeom(geoJson)
658
659 // diff against every zone
660 zones.forEach(distZone => {
661 geom = debugGeo('diff', geom, getDistZoneGeom(distZone))
662 })
663
664 return {
665 geom: postProcessZone(geom, true),
666 tzid: zone.tzid
667 }
668 })
669
670 callback()
671}
672
Evan Siroky7891a6e2016-11-05 11:50:50 -0700673var combineAndWriteZones = function (callback) {
Neil Fullerdce1d042020-10-14 09:53:00 +0100674 var stream = fs.createWriteStream(distDir + '/combined.json')
675 var streamWithOceans = fs.createWriteStream(distDir + '/combined-with-oceans.json')
Evan Siroky8b47abe2016-10-02 12:28:52 -0700676 var zones = Object.keys(zoneCfg)
677
678 stream.write('{"type":"FeatureCollection","features":[')
evansiroky26325842018-04-03 14:10:42 -0700679 streamWithOceans.write('{"type":"FeatureCollection","features":[')
Evan Siroky8b47abe2016-10-02 12:28:52 -0700680
681 for (var i = 0; i < zones.length; i++) {
Evan Siroky7891a6e2016-11-05 11:50:50 -0700682 if (i > 0) {
Evan Siroky8b47abe2016-10-02 12:28:52 -0700683 stream.write(',')
evansiroky26325842018-04-03 14:10:42 -0700684 streamWithOceans.write(',')
Evan Siroky8b47abe2016-10-02 12:28:52 -0700685 }
686 var feature = {
687 type: 'Feature',
688 properties: { tzid: zones[i] },
689 geometry: geomToGeoJson(getDistZoneGeom(zones[i]))
690 }
evansiroky26325842018-04-03 14:10:42 -0700691 const stringified = JSON.stringify(feature)
692 stream.write(stringified)
693 streamWithOceans.write(stringified)
Evan Siroky8b47abe2016-10-02 12:28:52 -0700694 }
evansiroky26325842018-04-03 14:10:42 -0700695 oceanZoneBoundaries.forEach(boundary => {
696 streamWithOceans.write(',')
697 var feature = {
698 type: 'Feature',
699 properties: { tzid: boundary.tzid },
700 geometry: boundary.geom
701 }
702 streamWithOceans.write(JSON.stringify(feature))
703 })
704 asynclib.parallel([
705 cb => {
706 stream.end(']}', cb)
707 },
708 cb => {
709 streamWithOceans.end(']}', cb)
710 }
711 ], callback)
Evan Siroky8b47abe2016-10-02 12:28:52 -0700712}
713
evansiroky96dfadc2020-04-28 01:31:38 -0700714var cleanDownloadsDir = function (cb) {
715 // TODO:
716
717 // list all files in downloads dir
718 // for each file
719 // if file does not exist in osmBoundarySources.json file, then remove
720 cb()
721}
722
723var downloadLastRelease = function (cb) {
724 // TODO:
725
726 // download latest release info
727 // determine last release version name
728 lastReleaseJSONfile = `./dist/${lastReleaseName}.json`
729
730 // check if file already downloaded, if so immediately callback
731 fetchIfNeeded(lastReleaseJSONfile, cb, cb, function () {
732 // find download link for geojson with oceans
733 // download the latest release data into the dist directory
734 // unzip geojson
735 cb()
736 })
737}
738
739var analyzeChangesFromLastRelease = function (cb) {
740 // TODO
741
742 // load last release data into memory
743
744 // generate set of keys from last release and current
745
746 // for each zone
747 // diff current - last = additions
748 // diff last - current = removals
749
750 // write file of additions
751 // write file of removals
752 cb()
753}
754
evansiroky5348a6f2019-01-05 15:39:28 -0800755const autoScript = {
Evan Siroky7891a6e2016-11-05 11:50:50 -0700756 makeDownloadsDir: function (cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800757 overallProgress.beginTask('Creating downloads dir')
Neil Fullerdce1d042020-10-14 09:53:00 +0100758 safeMkdir(downloadsDir, cb)
evansiroky4be1c7a2016-06-16 18:23:34 -0700759 },
Evan Siroky7891a6e2016-11-05 11:50:50 -0700760 makeDistDir: function (cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800761 overallProgress.beginTask('Creating dist dir')
Neil Fullerdce1d042020-10-14 09:53:00 +0100762 safeMkdir(distDir, cb)
evansirokyd401c892016-06-16 00:05:14 -0700763 },
Evan Siroky7891a6e2016-11-05 11:50:50 -0700764 getOsmBoundaries: ['makeDownloadsDir', function (results, cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800765 overallProgress.beginTask('Downloading osm boundaries')
Evan Siroky7891a6e2016-11-05 11:50:50 -0700766 asynclib.eachSeries(Object.keys(osmBoundarySources), downloadOsmBoundary, cb)
evansiroky63d35e12016-06-16 10:08:15 -0700767 }],
evansirokyf3bcf052020-10-17 23:10:15 -0700768 cleanDownloadFolder: ['makeDistDir', 'getOsmBoundaries', function (results, cb) {
769 overallProgress.beginTask('cleanDownloadFolder')
770 const downloadedFilenames = Object.keys(osmBoundarySources).map(name => `${name}.json`)
evansirokyb0442ca2020-10-18 17:49:45 -0700771 fs.readdir(downloadsDir, (err, files) => {
evansirokyf3bcf052020-10-17 23:10:15 -0700772 if (err) return cb(err)
773 asynclib.each(
774 files,
775 (file, fileCb) => {
776 if (downloadedFilenames.indexOf(file) === -1) {
evansirokyb0442ca2020-10-18 17:49:45 -0700777 return fs.unlink(path.join(downloadsDir, file), fileCb)
evansirokyf3bcf052020-10-17 23:10:15 -0700778 }
779 fileCb()
780 },
781 cb
782 )
783 })
784 }],
785 zipInputData: ['cleanDownloadFolder', function (results, cb) {
evansirokya48b3922020-04-27 15:29:06 -0700786 overallProgress.beginTask('Zipping up input data')
evansiroky7452c9b2020-10-18 00:27:06 -0700787 exec('zip -j ' + distDir + '/input-data.zip ' + downloadsDir +
Neil Fullerdce1d042020-10-14 09:53:00 +0100788 '/* timezones.json osmBoundarySources.json expectedZoneOverlaps.json', cb)
evansirokya48b3922020-04-27 15:29:06 -0700789 }],
evansiroky96dfadc2020-04-28 01:31:38 -0700790 downloadLastRelease: ['makeDistDir', function (results, cb) {
791 if (process.argv.indexOf('analyze-changes') > -1) {
792 overallProgress.beginTask('Downloading last release for analysis')
793 downloadLastRelease(cb)
794 } else {
795 overallProgress.beginTask('WARNING: Skipping download of last release for analysis!')
796 }
797 }],
Evan Siroky7891a6e2016-11-05 11:50:50 -0700798 createZones: ['makeDistDir', 'getOsmBoundaries', function (results, cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800799 overallProgress.beginTask('Creating timezone boundaries')
Evan Siroky7891a6e2016-11-05 11:50:50 -0700800 asynclib.each(Object.keys(zoneCfg), makeTimezoneBoundary, cb)
evansiroky50216c62016-06-16 17:41:47 -0700801 }],
Evan Siroky7891a6e2016-11-05 11:50:50 -0700802 validateZones: ['createZones', function (results, cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800803 overallProgress.beginTask('Validating timezone boundaries')
Evan Siroky4fc596c2016-09-25 19:52:30 -0700804 loadDistZonesIntoMemory()
Neil Fullerc4ae49b2020-04-30 18:08:43 +0100805 if (argv.no_validation) {
Evan Siroky081648a2017-07-04 09:53:36 -0700806 console.warn('WARNING: Skipping validation!')
807 cb()
808 } else {
809 cb(validateTimezoneBoundaries())
810 }
Evan Siroky4fc596c2016-09-25 19:52:30 -0700811 }],
evansiroky26325842018-04-03 14:10:42 -0700812 addOceans: ['validateZones', function (results, cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800813 overallProgress.beginTask('Adding oceans')
evansiroky26325842018-04-03 14:10:42 -0700814 addOceans(cb)
815 }],
816 mergeZones: ['addOceans', function (results, cb) {
evansiroky5348a6f2019-01-05 15:39:28 -0800817 overallProgress.beginTask('Merging zones')
Evan Siroky8b47abe2016-10-02 12:28:52 -0700818 combineAndWriteZones(cb)
819 }],
820 zipGeoJson: ['mergeZones', function (results, cb) {
Neil Fullera83cc6d2020-04-30 18:37:08 +0100821 if (argv.skip_zip) {
822 overallProgress.beginTask('Skipping zip')
Neil Fuller61287092020-07-29 14:46:16 +0100823 return cb()
Neil Fullera83cc6d2020-04-30 18:37:08 +0100824 }
Neil Fuller61287092020-07-29 14:46:16 +0100825 overallProgress.beginTask('Zipping geojson')
evansirokyb7d9d792020-10-17 16:50:14 -0700826 const zipFile = distDir + '/timezones.geojson.zip'
827 const jsonFile = distDir + '/combined.json'
evansiroky7452c9b2020-10-18 00:27:06 -0700828 exec('zip -j ' + zipFile + ' ' + jsonFile, cb)
Evan Siroky8b47abe2016-10-02 12:28:52 -0700829 }],
evansiroky26325842018-04-03 14:10:42 -0700830 zipGeoJsonWithOceans: ['mergeZones', function (results, cb) {
Neil Fullera83cc6d2020-04-30 18:37:08 +0100831 if (argv.skip_zip) {
832 overallProgress.beginTask('Skipping with oceans zip')
Neil Fuller61287092020-07-29 14:46:16 +0100833 return cb()
Neil Fullera83cc6d2020-04-30 18:37:08 +0100834 }
Neil Fuller61287092020-07-29 14:46:16 +0100835 overallProgress.beginTask('Zipping geojson with oceans')
evansirokyb7d9d792020-10-17 16:50:14 -0700836 const zipFile = distDir + '/timezones-with-oceans.geojson.zip'
837 const jsonFile = distDir + '/combined-with-oceans.json'
evansiroky7452c9b2020-10-18 00:27:06 -0700838 exec('zip -j ' + zipFile + ' ' + jsonFile, cb)
evansiroky26325842018-04-03 14:10:42 -0700839 }],
Evan Siroky8b47abe2016-10-02 12:28:52 -0700840 makeShapefile: ['mergeZones', function (results, cb) {
Neil Fullera83cc6d2020-04-30 18:37:08 +0100841 if (argv.skip_shapefile) {
842 overallProgress.beginTask('Skipping shapefile creation')
Neil Fuller61287092020-07-29 14:46:16 +0100843 return cb()
Neil Fullera83cc6d2020-04-30 18:37:08 +0100844 }
Neil Fuller61287092020-07-29 14:46:16 +0100845 overallProgress.beginTask('Converting from geojson to shapefile')
evansirokyb7d9d792020-10-17 16:50:14 -0700846 const shapeFileGlob = distDir + '/combined-shapefile.*'
Neil Fuller61287092020-07-29 14:46:16 +0100847 rimraf.sync(shapeFileGlob)
evansirokyb7d9d792020-10-17 16:50:14 -0700848 const shapeFile = distDir + '/combined-shapefile.shp'
849 const jsonFile = distDir + '/combined.json'
Neil Fuller61287092020-07-29 14:46:16 +0100850 exec(
851 'ogr2ogr -f "ESRI Shapefile" ' + shapeFile + ' ' + jsonFile,
852 function (err, stdout, stderr) {
853 if (err) { return cb(err) }
evansirokyb7d9d792020-10-17 16:50:14 -0700854 const shapeFileZip = distDir + '/timezones.shapefile.zip'
evansiroky7452c9b2020-10-18 00:27:06 -0700855 exec('zip -j ' + shapeFileZip + ' ' + shapeFileGlob, cb)
Neil Fuller61287092020-07-29 14:46:16 +0100856 }
857 )
evansiroky26325842018-04-03 14:10:42 -0700858 }],
859 makeShapefileWithOceans: ['mergeZones', function (results, cb) {
Neil Fullera83cc6d2020-04-30 18:37:08 +0100860 if (argv.skip_shapefile) {
861 overallProgress.beginTask('Skipping with oceans shapefile creation')
Neil Fuller61287092020-07-29 14:46:16 +0100862 return cb()
Neil Fullera83cc6d2020-04-30 18:37:08 +0100863 }
Neil Fuller61287092020-07-29 14:46:16 +0100864 overallProgress.beginTask('Converting from geojson with oceans to shapefile')
evansirokyb7d9d792020-10-17 16:50:14 -0700865 const shapeFileGlob = distDir + '/combined-shapefile-with-oceans.*'
Neil Fuller61287092020-07-29 14:46:16 +0100866 rimraf.sync(shapeFileGlob)
evansirokyb7d9d792020-10-17 16:50:14 -0700867 const shapeFile = distDir + '/combined-shapefile-with-oceans.shp'
868 const jsonFile = distDir + '/combined-with-oceans.json'
Neil Fuller61287092020-07-29 14:46:16 +0100869 exec(
870 'ogr2ogr -f "ESRI Shapefile" ' + shapeFile + ' ' + jsonFile,
871 function (err, stdout, stderr) {
872 if (err) { return cb(err) }
evansirokyb7d9d792020-10-17 16:50:14 -0700873 const shapeFileZip = distDir + '/timezones-with-oceans.shapefile.zip'
evansiroky7452c9b2020-10-18 00:27:06 -0700874 exec('zip -j ' + shapeFileZip + ' ' + shapeFileGlob, cb)
Neil Fuller61287092020-07-29 14:46:16 +0100875 }
876 )
evansiroky9fd50512019-07-07 12:06:28 -0700877 }],
878 makeListOfTimeZoneNames: function (cb) {
879 overallProgress.beginTask('Writing timezone names to file')
880 let zoneNames = Object.keys(zoneCfg)
881 oceanZones.forEach(oceanZone => {
882 zoneNames.push(oceanZone.tzid)
883 })
Neil Fullerc4ae49b2020-04-30 18:08:43 +0100884 if (includedZones.length > 0) {
885 zoneNames = zoneNames.filter(zoneName => includedZones.indexOf(zoneName) > -1)
evansiroky9fd50512019-07-07 12:06:28 -0700886 }
Neil Fuller2b4b80a2020-04-30 18:20:56 +0100887 if (excludedZones.length > 0) {
888 zoneNames = zoneNames.filter(zoneName => excludedZones.indexOf(zoneName) === -1)
889 }
evansiroky9fd50512019-07-07 12:06:28 -0700890 fs.writeFile(
Neil Fullerdce1d042020-10-14 09:53:00 +0100891 distDir + '/timezone-names.json',
evansiroky9fd50512019-07-07 12:06:28 -0700892 JSON.stringify(zoneNames),
893 cb
894 )
evansiroky96dfadc2020-04-28 01:31:38 -0700895 },
896 analyzeChangesFromLastRelease: ['downloadLastRelease', 'mergeZones', function (results, cb) {
897 if (process.argv.indexOf('analyze-changes') > -1) {
898 overallProgress.beginTask('Analyzing changes from last release')
899 analyzeChangesFromLastRelease(cb)
900 } else {
901 overallProgress.beginTask('WARNING: Skipping analysis of changes from last release!')
902 }
903 }]
evansiroky5348a6f2019-01-05 15:39:28 -0800904}
905
906const overallProgress = new ProgressStats('Overall', Object.keys(autoScript).length)
907
908asynclib.auto(autoScript, function (err, results) {
evansirokyd401c892016-06-16 00:05:14 -0700909 console.log('done')
Evan Siroky7891a6e2016-11-05 11:50:50 -0700910 if (err) {
evansirokyd401c892016-06-16 00:05:14 -0700911 console.log('error!', err)
evansirokyd401c892016-06-16 00:05:14 -0700912 }
Evan Siroky4fc596c2016-09-25 19:52:30 -0700913})