Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 1 | var exec = require('child_process').exec |
| 2 | var fs = require('fs') |
evansiroky | f3bcf05 | 2020-10-17 23:10:15 -0700 | [diff] [blame] | 3 | var path = require('path') |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 4 | |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 5 | var area = require('@mapbox/geojson-area') |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 6 | var geojsonhint = require('@mapbox/geojsonhint') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 7 | var bbox = require('@turf/bbox').default |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 8 | var helpers = require('@turf/helpers') |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 9 | var multiPolygon = helpers.multiPolygon |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 10 | var polygon = helpers.polygon |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 11 | var asynclib = require('async') |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 12 | var https = require('follow-redirects').https |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 13 | var jsts = require('jsts') |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 14 | var rimraf = require('rimraf') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 15 | var overpass = require('query-overpass') |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 16 | var yargs = require('yargs') |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 17 | |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 18 | const FeatureWriterStream = require('./util/featureWriterStream') |
| 19 | const ProgressStats = require('./util/progressStats') |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 20 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 21 | var osmBoundarySources = require('./osmBoundarySources.json') |
| 22 | var zoneCfg = require('./timezones.json') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 23 | var expectedZoneOverlaps = require('./expectedZoneOverlaps.json') |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 24 | |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 25 | const argv = yargs |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 26 | .option('downloads_dir', { |
| 27 | description: 'Set the download location', |
| 28 | default: './downloads', |
| 29 | type: 'string' |
| 30 | }) |
| 31 | .option('dist_dir', { |
| 32 | description: 'Set the dist location', |
| 33 | default: './dist', |
| 34 | type: 'string' |
| 35 | }) |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 36 | .option('excluded_zones', { |
| 37 | description: 'Exclude specified zones', |
| 38 | type: 'array' |
| 39 | }) |
| 40 | .option('included_zones', { |
| 41 | description: 'Include specified zones', |
| 42 | type: 'array' |
| 43 | }) |
| 44 | .option('skip_analyze_diffs', { |
| 45 | description: 'Skip analysis of diffs between versions', |
| 46 | type: 'boolean' |
| 47 | }) |
| 48 | .option('skip_shapefile', { |
| 49 | description: 'Skip shapefile creation', |
| 50 | type: 'boolean' |
| 51 | }) |
| 52 | .option('skip_validation', { |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 53 | description: 'Skip validation', |
| 54 | type: 'boolean' |
| 55 | }) |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 56 | .option('skip_zip', { |
| 57 | description: 'Skip zip creation', |
| 58 | type: 'boolean' |
| 59 | }) |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 60 | .help() |
| 61 | .strict() |
| 62 | .alias('help', 'h') |
| 63 | .argv |
| 64 | |
Neil Fuller | 0180319 | 2020-08-14 11:19:42 +0100 | [diff] [blame] | 65 | // Resolve the arguments with paths so relative paths become absolute. |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 66 | const downloadsDir = path.resolve(argv.downloads_dir) |
| 67 | const distDir = path.resolve(argv.dist_dir) |
Neil Fuller | 0180319 | 2020-08-14 11:19:42 +0100 | [diff] [blame] | 68 | |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 69 | // allow building of only a specified zones |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 70 | let includedZones = [] |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 71 | let excludedZones = [] |
| 72 | if (argv.included_zones || argv.excluded_zones) { |
| 73 | if (argv.included_zones) { |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 74 | const newZoneCfg = {} |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 75 | includedZones = argv.included_zones |
| 76 | includedZones.forEach((zoneName) => { |
| 77 | newZoneCfg[zoneName] = zoneCfg[zoneName] |
| 78 | }) |
| 79 | zoneCfg = newZoneCfg |
| 80 | } |
| 81 | if (argv.excluded_zones) { |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 82 | const newZoneCfg = {} |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 83 | excludedZones = argv.excluded_zones |
| 84 | Object.keys(zoneCfg).forEach((zoneName) => { |
| 85 | if (!excludedZones.includes(zoneName)) { |
| 86 | newZoneCfg[zoneName] = zoneCfg[zoneName] |
| 87 | } |
| 88 | }) |
| 89 | zoneCfg = newZoneCfg |
| 90 | } |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 91 | |
| 92 | // filter out unneccessary downloads |
| 93 | var newOsmBoundarySources = {} |
| 94 | Object.keys(zoneCfg).forEach((zoneName) => { |
| 95 | zoneCfg[zoneName].forEach((op) => { |
| 96 | if (op.source === 'overpass') { |
| 97 | newOsmBoundarySources[op.id] = osmBoundarySources[op.id] |
| 98 | } |
| 99 | }) |
| 100 | }) |
| 101 | |
| 102 | osmBoundarySources = newOsmBoundarySources |
| 103 | } |
| 104 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 105 | var geoJsonReader = new jsts.io.GeoJSONReader() |
| 106 | var geoJsonWriter = new jsts.io.GeoJSONWriter() |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 107 | var precisionModel = new jsts.geom.PrecisionModel(1000000) |
| 108 | var precisionReducer = new jsts.precision.GeometryPrecisionReducer(precisionModel) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 109 | var distZones = {} |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 110 | var lastReleaseJSONfile |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 111 | var minRequestGap = 4 |
| 112 | var curRequestGap = 4 |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 113 | const bufferDistance = 0.01 |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 114 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 115 | var safeMkdir = function (dirname, callback) { |
| 116 | fs.mkdir(dirname, function (err) { |
| 117 | if (err && err.code === 'EEXIST') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 118 | callback() |
| 119 | } else { |
| 120 | callback(err) |
| 121 | } |
| 122 | }) |
| 123 | } |
| 124 | |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 125 | var debugGeo = function ( |
| 126 | op, |
| 127 | a, |
| 128 | b, |
| 129 | reducePrecision, |
| 130 | bufferAfterPrecisionReduction |
| 131 | ) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 132 | var result |
| 133 | |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 134 | if (reducePrecision) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 135 | a = precisionReducer.reduce(a) |
| 136 | b = precisionReducer.reduce(b) |
| 137 | } |
| 138 | |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 139 | try { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 140 | switch (op) { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 141 | case 'union': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 142 | result = a.union(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 143 | break |
| 144 | case 'intersection': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 145 | result = a.intersection(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 146 | break |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 147 | case 'intersects': |
| 148 | result = a.intersects(b) |
| 149 | break |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 150 | case 'diff': |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 151 | result = a.difference(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 152 | break |
| 153 | default: |
| 154 | var err = new Error('invalid op: ' + op) |
| 155 | throw err |
| 156 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 157 | } catch (e) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 158 | if (e.name === 'TopologyException') { |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 159 | if (reducePrecision) { |
| 160 | if (bufferAfterPrecisionReduction) { |
| 161 | console.log('Encountered TopologyException, retry with buffer increase') |
| 162 | return debugGeo( |
| 163 | op, |
| 164 | a.buffer(bufferDistance), |
| 165 | b.buffer(bufferDistance), |
| 166 | true, |
| 167 | bufferAfterPrecisionReduction |
| 168 | ) |
| 169 | } else { |
| 170 | throw new Error('Encountered TopologyException after reducing precision') |
| 171 | } |
| 172 | } else { |
| 173 | console.log('Encountered TopologyException, retry with GeometryPrecisionReducer') |
| 174 | return debugGeo(op, a, b, true, bufferAfterPrecisionReduction) |
| 175 | } |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 176 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 177 | console.log('op err') |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 178 | console.log(e) |
| 179 | console.log(e.stack) |
| 180 | fs.writeFileSync('debug_' + op + '_a.json', JSON.stringify(geoJsonWriter.write(a))) |
| 181 | fs.writeFileSync('debug_' + op + '_b.json', JSON.stringify(geoJsonWriter.write(b))) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 182 | throw e |
| 183 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 184 | |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 185 | return result |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 188 | var fetchIfNeeded = function (file, superCallback, downloadCallback, fetchFn) { |
| 189 | // check for file that got downloaded |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 190 | fs.stat(file, function (err) { |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 191 | if (!err) { |
| 192 | // file found, skip download steps |
| 193 | return superCallback() |
| 194 | } |
| 195 | // check for manual file that got fixed and needs validation |
| 196 | var fixedFile = file.replace('.json', '_fixed.json') |
| 197 | fs.stat(fixedFile, function (err) { |
| 198 | if (!err) { |
| 199 | // file found, return fixed file |
| 200 | return downloadCallback(null, require(fixedFile)) |
| 201 | } |
| 202 | // no manual fixed file found, download from overpass |
| 203 | fetchFn() |
| 204 | }) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 205 | }) |
| 206 | } |
| 207 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 208 | var geoJsonToGeom = function (geoJson) { |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 209 | try { |
| 210 | return geoJsonReader.read(JSON.stringify(geoJson)) |
| 211 | } catch (e) { |
| 212 | console.error('error converting geojson to geometry') |
| 213 | fs.writeFileSync('debug_geojson_read_error.json', JSON.stringify(geoJson)) |
| 214 | throw e |
| 215 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 218 | var geomToGeoJson = function (geom) { |
| 219 | return geoJsonWriter.write(geom) |
| 220 | } |
| 221 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 222 | var geomToGeoJsonString = function (geom) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 223 | return JSON.stringify(geoJsonWriter.write(geom)) |
| 224 | } |
| 225 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 226 | const downloadProgress = new ProgressStats( |
| 227 | 'Downloading', |
| 228 | Object.keys(osmBoundarySources).length |
| 229 | ) |
| 230 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 231 | var downloadOsmBoundary = function (boundaryId, boundaryCallback) { |
| 232 | var cfg = osmBoundarySources[boundaryId] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 233 | var query = '[out:json][timeout:60];(' |
| 234 | if (cfg.way) { |
| 235 | query += 'way' |
| 236 | } else { |
| 237 | query += 'relation' |
| 238 | } |
Neil Fuller | dce1d04 | 2020-10-14 09:53:00 +0100 | [diff] [blame] | 239 | var boundaryFilename = downloadsDir + '/' + boundaryId + '.json' |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 240 | var debug = 'getting data for ' + boundaryId |
| 241 | var queryKeys = Object.keys(cfg) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 242 | |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 243 | for (var i = queryKeys.length - 1; i >= 0; i--) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 244 | var k = queryKeys[i] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 245 | if (k === 'way') continue |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 246 | var v = cfg[k] |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 247 | |
| 248 | query += '["' + k + '"="' + v + '"]' |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 249 | } |
| 250 | |
evansiroky | 283ebbc | 2018-07-16 15:13:07 -0700 | [diff] [blame] | 251 | query += ';);out body;>;out meta qt;' |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 252 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 253 | downloadProgress.beginTask(debug, true) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 254 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 255 | asynclib.auto({ |
| 256 | downloadFromOverpass: function (cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 257 | console.log('downloading from overpass') |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 258 | fetchIfNeeded(boundaryFilename, boundaryCallback, cb, function () { |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 259 | var overpassResponseHandler = function (err, data) { |
| 260 | if (err) { |
| 261 | console.log(err) |
| 262 | console.log('Increasing overpass request gap') |
| 263 | curRequestGap *= 2 |
| 264 | makeQuery() |
| 265 | } else { |
| 266 | console.log('Success, decreasing overpass request gap') |
| 267 | curRequestGap = Math.max(minRequestGap, curRequestGap / 2) |
| 268 | cb(null, data) |
| 269 | } |
| 270 | } |
| 271 | var makeQuery = function () { |
| 272 | console.log('waiting ' + curRequestGap + ' seconds') |
| 273 | setTimeout(function () { |
| 274 | overpass(query, overpassResponseHandler, { flatProperties: true }) |
| 275 | }, curRequestGap * 1000) |
| 276 | } |
| 277 | makeQuery() |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 278 | }) |
| 279 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 280 | validateOverpassResult: ['downloadFromOverpass', function (results, cb) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 281 | var data = results.downloadFromOverpass |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 282 | if (!data.features) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 283 | var err = new Error('Invalid geojson for boundary: ' + boundaryId) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 284 | return cb(err) |
| 285 | } |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 286 | if (data.features.length === 0) { |
| 287 | console.error('No data for the following query:') |
| 288 | console.error(query) |
| 289 | console.error('To read more about this error, please visit https://git.io/vxKQL') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 290 | return cb(new Error('No data found for from overpass query')) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 291 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 292 | cb() |
| 293 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 294 | saveSingleMultiPolygon: ['validateOverpassResult', function (results, cb) { |
| 295 | var data = results.downloadFromOverpass |
| 296 | var combined |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 297 | |
| 298 | // union all multi-polygons / polygons into one |
| 299 | for (var i = data.features.length - 1; i >= 0; i--) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 300 | var curOsmGeom = data.features[i].geometry |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 301 | const curOsmProps = data.features[i].properties |
| 302 | if ( |
| 303 | (curOsmGeom.type === 'Polygon' || curOsmGeom.type === 'MultiPolygon') && |
| 304 | curOsmProps.type === 'boundary' // need to make sure enclaves aren't unioned |
| 305 | ) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 306 | console.log('combining border') |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 307 | let errors = geojsonhint.hint(curOsmGeom) |
| 308 | if (errors && errors.length > 0) { |
| 309 | const stringifiedGeojson = JSON.stringify(curOsmGeom, null, 2) |
| 310 | errors = geojsonhint.hint(stringifiedGeojson) |
| 311 | console.error('Invalid geojson received in Overpass Result') |
| 312 | console.error('Overpass query: ' + query) |
| 313 | const problemFilename = boundaryId + '_convert_to_geom_error.json' |
| 314 | fs.writeFileSync(problemFilename, stringifiedGeojson) |
| 315 | console.error('saved problem file to ' + problemFilename) |
| 316 | console.error('To read more about this error, please visit https://git.io/vxKQq') |
| 317 | return cb(errors) |
| 318 | } |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 319 | try { |
| 320 | var curGeom = geoJsonToGeom(curOsmGeom) |
| 321 | } catch (e) { |
| 322 | console.error('error converting overpass result to geojson') |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 323 | console.error(e) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 324 | |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 325 | fs.writeFileSync(boundaryId + '_convert_to_geom_error-all-features.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 326 | return cb(e) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 327 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 328 | if (!combined) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 329 | combined = curGeom |
| 330 | } else { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 331 | combined = debugGeo('union', curGeom, combined) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 335 | try { |
| 336 | fs.writeFile(boundaryFilename, geomToGeoJsonString(combined), cb) |
| 337 | } catch (e) { |
| 338 | console.error('error writing combined border to geojson') |
| 339 | fs.writeFileSync(boundaryId + '_combined_border_convert_to_geom_error.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 340 | return cb(e) |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 341 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 342 | }] |
| 343 | }, boundaryCallback) |
| 344 | } |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 345 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 346 | var getTzDistFilename = function (tzid) { |
Neil Fuller | dce1d04 | 2020-10-14 09:53:00 +0100 | [diff] [blame] | 347 | return distDir + '/' + tzid.replace(/\//g, '__') + '.json' |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Get the geometry of the requested source data |
| 352 | * |
| 353 | * @return {Object} geom The geometry of the source |
| 354 | * @param {Object} source An object representing the data source |
| 355 | * must have `source` key and then either: |
| 356 | * - `id` if from a file |
| 357 | * - `id` if from a file |
| 358 | */ |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 359 | var getDataSource = function (source) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 360 | var geoJson |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 361 | if (source.source === 'overpass') { |
Neil Fuller | dce1d04 | 2020-10-14 09:53:00 +0100 | [diff] [blame] | 362 | geoJson = require(downloadsDir + '/' + source.id + '.json') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 363 | } else if (source.source === 'manual-polygon') { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 364 | geoJson = polygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 365 | } else if (source.source === 'manual-multipolygon') { |
Evan Siroky | 8e30a2e | 2016-08-06 19:55:35 -0700 | [diff] [blame] | 366 | geoJson = multiPolygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 367 | } else if (source.source === 'dist') { |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 368 | geoJson = require(getTzDistFilename(source.id)) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 369 | } else { |
| 370 | var err = new Error('unknown source: ' + source.source) |
| 371 | throw err |
| 372 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 373 | return geoJsonToGeom(geoJson) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 376 | /** |
| 377 | * Post process created timezone boundary. |
| 378 | * - remove small holes and exclaves |
| 379 | * - reduce geometry precision |
| 380 | * |
| 381 | * @param {Geometry} geom The jsts geometry of the timezone |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 382 | * @param {boolean} returnAsObject if true, return as object, otherwise return stringified |
| 383 | * @return {Object|String} geojson as object or stringified |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 384 | */ |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 385 | var postProcessZone = function (geom, returnAsObject) { |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 386 | // reduce precision of geometry |
| 387 | const geojson = geomToGeoJson(precisionReducer.reduce(geom)) |
| 388 | |
| 389 | // iterate through all polygons |
| 390 | const filteredPolygons = [] |
| 391 | let allPolygons = geojson.coordinates |
| 392 | if (geojson.type === 'Polygon') { |
| 393 | allPolygons = [geojson.coordinates] |
| 394 | } |
| 395 | |
| 396 | allPolygons.forEach((curPolygon, idx) => { |
| 397 | // remove any polygon with very small area |
| 398 | const polygonFeature = polygon(curPolygon) |
| 399 | const polygonArea = area.geometry(polygonFeature.geometry) |
| 400 | |
| 401 | if (polygonArea < 1) return |
| 402 | |
| 403 | // find all holes |
| 404 | const filteredLinearRings = [] |
| 405 | |
| 406 | curPolygon.forEach((curLinearRing, lrIdx) => { |
| 407 | if (lrIdx === 0) { |
| 408 | // always keep first linearRing |
| 409 | filteredLinearRings.push(curLinearRing) |
| 410 | } else { |
| 411 | const polygonFromLinearRing = polygon([curLinearRing]) |
| 412 | const linearRingArea = area.geometry(polygonFromLinearRing.geometry) |
| 413 | |
| 414 | // only include holes with relevant area |
| 415 | if (linearRingArea > 1) { |
| 416 | filteredLinearRings.push(curLinearRing) |
| 417 | } |
| 418 | } |
| 419 | }) |
| 420 | |
| 421 | filteredPolygons.push(filteredLinearRings) |
| 422 | }) |
| 423 | |
| 424 | // recompile to geojson string |
| 425 | const newGeojson = { |
| 426 | type: geojson.type |
| 427 | } |
| 428 | |
| 429 | if (geojson.type === 'Polygon') { |
| 430 | newGeojson.coordinates = filteredPolygons[0] |
| 431 | } else { |
| 432 | newGeojson.coordinates = filteredPolygons |
| 433 | } |
| 434 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 435 | return returnAsObject ? newGeojson : JSON.stringify(newGeojson) |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 436 | } |
| 437 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 438 | const buildingProgress = new ProgressStats( |
| 439 | 'Building', |
| 440 | Object.keys(zoneCfg).length |
| 441 | ) |
| 442 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 443 | var makeTimezoneBoundary = function (tzid, callback) { |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 444 | buildingProgress.beginTask(`makeTimezoneBoundary for ${tzid}`, true) |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 445 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 446 | var ops = zoneCfg[tzid] |
| 447 | var geom |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 448 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 449 | asynclib.eachSeries(ops, function (task, cb) { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 450 | var taskData = getDataSource(task) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 451 | console.log('-', task.op, task.id) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 452 | if (task.op === 'init') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 453 | geom = taskData |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 454 | } else if (task.op === 'intersect') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 455 | geom = debugGeo('intersection', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 456 | } else if (task.op === 'difference') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 457 | geom = debugGeo('diff', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 458 | } else if (task.op === 'difference-reverse-order') { |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 459 | geom = debugGeo('diff', taskData, geom) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 460 | } else if (task.op === 'union') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 461 | geom = debugGeo('union', geom, taskData) |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 462 | } else { |
| 463 | var err = new Error('unknown op: ' + task.op) |
| 464 | return cb(err) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 465 | } |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 466 | cb() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 467 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 468 | function (err) { |
| 469 | if (err) { return callback(err) } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 470 | fs.writeFile(getTzDistFilename(tzid), |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 471 | postProcessZone(geom), |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 472 | callback) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 473 | }) |
| 474 | } |
| 475 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 476 | var loadDistZonesIntoMemory = function () { |
| 477 | console.log('load zones into memory') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 478 | var zones = Object.keys(zoneCfg) |
| 479 | var tzid |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 480 | |
| 481 | for (var i = 0; i < zones.length; i++) { |
| 482 | tzid = zones[i] |
| 483 | distZones[tzid] = getDataSource({ source: 'dist', id: tzid }) |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | var getDistZoneGeom = function (tzid) { |
| 488 | return distZones[tzid] |
| 489 | } |
| 490 | |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 491 | var roundDownToTenth = function (n) { |
| 492 | return Math.floor(n * 10) / 10 |
| 493 | } |
| 494 | |
| 495 | var roundUpToTenth = function (n) { |
| 496 | return Math.ceil(n * 10) / 10 |
| 497 | } |
| 498 | |
| 499 | var formatBounds = function (bounds) { |
| 500 | let boundsStr = '[' |
| 501 | boundsStr += roundDownToTenth(bounds[0]) + ', ' |
| 502 | boundsStr += roundDownToTenth(bounds[1]) + ', ' |
| 503 | boundsStr += roundUpToTenth(bounds[2]) + ', ' |
| 504 | boundsStr += roundUpToTenth(bounds[3]) + ']' |
| 505 | return boundsStr |
| 506 | } |
| 507 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 508 | var validateTimezoneBoundaries = function () { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 509 | const numZones = Object.keys(zoneCfg).length |
| 510 | const validationProgress = new ProgressStats( |
| 511 | 'Validation', |
| 512 | numZones * (numZones + 1) / 2 |
| 513 | ) |
| 514 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 515 | console.log('do validation... this may take a few minutes') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 516 | var allZonesOk = true |
| 517 | var zones = Object.keys(zoneCfg) |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 518 | var lastPct = 0 |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 519 | var compareTzid, tzid, zoneGeom |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 520 | |
| 521 | for (var i = 0; i < zones.length; i++) { |
| 522 | tzid = zones[i] |
| 523 | zoneGeom = getDistZoneGeom(tzid) |
| 524 | |
| 525 | for (var j = i + 1; j < zones.length; j++) { |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 526 | const curPct = Math.floor(validationProgress.getPercentage()) |
| 527 | if (curPct % 10 === 0 && curPct !== lastPct) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 528 | validationProgress.printStats('Validating zones', true) |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 529 | lastPct = curPct |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 530 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 531 | compareTzid = zones[j] |
| 532 | |
| 533 | var compareZoneGeom = getDistZoneGeom(compareTzid) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 534 | |
| 535 | var intersects = false |
| 536 | try { |
| 537 | intersects = debugGeo('intersects', zoneGeom, compareZoneGeom) |
| 538 | } catch (e) { |
| 539 | console.warn('warning, encountered intersection error with zone ' + tzid + ' and ' + compareTzid) |
| 540 | } |
| 541 | if (intersects) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 542 | var intersectedGeom = debugGeo('intersection', zoneGeom, compareZoneGeom) |
| 543 | var intersectedArea = intersectedGeom.getArea() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 544 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 545 | if (intersectedArea > 0.0001) { |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 546 | // check if the intersected area(s) are one of the expected areas of overlap |
| 547 | const allowedOverlapBounds = expectedZoneOverlaps[`${tzid}-${compareTzid}`] || expectedZoneOverlaps[`${compareTzid}-${tzid}`] |
| 548 | const overlapsGeoJson = geoJsonWriter.write(intersectedGeom) |
| 549 | |
| 550 | // these zones are allowed to overlap in certain places, make sure the |
| 551 | // found overlap(s) all fit within the expected areas of overlap |
| 552 | if (allowedOverlapBounds) { |
| 553 | // if the overlaps are a multipolygon, make sure each individual |
| 554 | // polygon of overlap fits within at least one of the expected |
| 555 | // overlaps |
| 556 | let overlapsPolygons |
| 557 | switch (overlapsGeoJson.type) { |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 558 | case 'MultiPolygon': |
| 559 | overlapsPolygons = overlapsGeoJson.coordinates.map( |
| 560 | polygonCoords => ({ |
| 561 | coordinates: polygonCoords, |
| 562 | type: 'Polygon' |
| 563 | }) |
| 564 | ) |
| 565 | break |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 566 | case 'Polygon': |
| 567 | overlapsPolygons = [overlapsGeoJson] |
| 568 | break |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 569 | case 'GeometryCollection': |
| 570 | overlapsPolygons = [] |
| 571 | overlapsGeoJson.geometries.forEach(geom => { |
| 572 | if (geom.type === 'Polygon') { |
| 573 | overlapsPolygons.push(geom) |
| 574 | } else if (geom.type === 'MultiPolygon') { |
| 575 | geom.coordinates.forEach(polygonCoords => { |
| 576 | overlapsPolygons.push({ |
| 577 | coordinates: polygonCoords, |
| 578 | type: 'Polygon' |
| 579 | }) |
| 580 | }) |
| 581 | } |
| 582 | }) |
| 583 | break |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 584 | default: |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 585 | console.error('unexpected geojson overlap type') |
| 586 | console.log(overlapsGeoJson) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 587 | break |
| 588 | } |
| 589 | |
| 590 | let allOverlapsOk = true |
| 591 | overlapsPolygons.forEach((polygon, idx) => { |
| 592 | const bounds = bbox(polygon) |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 593 | const polygonArea = area.geometry(polygon) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 594 | if ( |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 595 | polygonArea > 10 && // ignore small polygons |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 596 | !allowedOverlapBounds.some(allowedBounds => |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 597 | allowedBounds.bounds[0] <= bounds[0] && // minX |
| 598 | allowedBounds.bounds[1] <= bounds[1] && // minY |
| 599 | allowedBounds.bounds[2] >= bounds[2] && // maxX |
| 600 | allowedBounds.bounds[3] >= bounds[3] // maxY |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 601 | ) |
| 602 | ) { |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 603 | console.error(`Unexpected intersection (${polygonArea} area) with bounds: ${formatBounds(bounds)}`) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 604 | allOverlapsOk = false |
| 605 | } |
| 606 | }) |
| 607 | |
| 608 | if (allOverlapsOk) continue |
| 609 | } |
| 610 | |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 611 | // at least one unexpected overlap found, output an error and write debug file |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 612 | console.error('Validation error: ' + tzid + ' intersects ' + compareTzid + ' area: ' + intersectedArea) |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 613 | const debugFilename = tzid.replace(/\//g, '-') + '-' + compareTzid.replace(/\//g, '-') + '-overlap.json' |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 614 | fs.writeFileSync( |
| 615 | debugFilename, |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 616 | JSON.stringify(overlapsGeoJson) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 617 | ) |
| 618 | console.error('wrote overlap area as file ' + debugFilename) |
| 619 | console.error('To read more about this error, please visit https://git.io/vx6nx') |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 620 | allZonesOk = false |
| 621 | } |
| 622 | } |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 623 | validationProgress.logNext() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | return allZonesOk ? null : 'Zone validation unsuccessful' |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 628 | } |
| 629 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 630 | let oceanZoneBoundaries |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 631 | let oceanZones = [ |
| 632 | { tzid: 'Etc/GMT-12', left: 172.5, right: 180 }, |
| 633 | { tzid: 'Etc/GMT-11', left: 157.5, right: 172.5 }, |
| 634 | { tzid: 'Etc/GMT-10', left: 142.5, right: 157.5 }, |
| 635 | { tzid: 'Etc/GMT-9', left: 127.5, right: 142.5 }, |
| 636 | { tzid: 'Etc/GMT-8', left: 112.5, right: 127.5 }, |
| 637 | { tzid: 'Etc/GMT-7', left: 97.5, right: 112.5 }, |
| 638 | { tzid: 'Etc/GMT-6', left: 82.5, right: 97.5 }, |
| 639 | { tzid: 'Etc/GMT-5', left: 67.5, right: 82.5 }, |
| 640 | { tzid: 'Etc/GMT-4', left: 52.5, right: 67.5 }, |
| 641 | { tzid: 'Etc/GMT-3', left: 37.5, right: 52.5 }, |
| 642 | { tzid: 'Etc/GMT-2', left: 22.5, right: 37.5 }, |
| 643 | { tzid: 'Etc/GMT-1', left: 7.5, right: 22.5 }, |
| 644 | { tzid: 'Etc/GMT', left: -7.5, right: 7.5 }, |
| 645 | { tzid: 'Etc/GMT+1', left: -22.5, right: -7.5 }, |
| 646 | { tzid: 'Etc/GMT+2', left: -37.5, right: -22.5 }, |
| 647 | { tzid: 'Etc/GMT+3', left: -52.5, right: -37.5 }, |
| 648 | { tzid: 'Etc/GMT+4', left: -67.5, right: -52.5 }, |
| 649 | { tzid: 'Etc/GMT+5', left: -82.5, right: -67.5 }, |
| 650 | { tzid: 'Etc/GMT+6', left: -97.5, right: -82.5 }, |
| 651 | { tzid: 'Etc/GMT+7', left: -112.5, right: -97.5 }, |
| 652 | { tzid: 'Etc/GMT+8', left: -127.5, right: -112.5 }, |
| 653 | { tzid: 'Etc/GMT+9', left: -142.5, right: -127.5 }, |
| 654 | { tzid: 'Etc/GMT+10', left: -157.5, right: -142.5 }, |
| 655 | { tzid: 'Etc/GMT+11', left: -172.5, right: -157.5 }, |
| 656 | { tzid: 'Etc/GMT+12', left: -180, right: -172.5 } |
| 657 | ] |
| 658 | |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 659 | if (includedZones.length > 0) { |
| 660 | oceanZones = oceanZones.filter(oceanZone => includedZones.indexOf(oceanZone) > -1) |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 661 | } |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 662 | if (excludedZones.length > 0) { |
| 663 | oceanZones = oceanZones.filter(oceanZone => excludedZones.indexOf(oceanZone) === -1) |
| 664 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 665 | |
| 666 | var addOceans = function (callback) { |
| 667 | console.log('adding ocean boundaries') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 668 | const zones = Object.keys(zoneCfg) |
| 669 | |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 670 | const oceanProgress = new ProgressStats( |
| 671 | 'Oceans', |
| 672 | oceanZones.length |
| 673 | ) |
| 674 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 675 | oceanZoneBoundaries = oceanZones.map(zone => { |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 676 | oceanProgress.beginTask(zone.tzid, true) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 677 | const geoJson = polygon([[ |
| 678 | [zone.left, 90], |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 679 | [zone.left, -90], |
| 680 | [zone.right, -90], |
| 681 | [zone.right, 90], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 682 | [zone.left, 90] |
| 683 | ]]).geometry |
| 684 | |
| 685 | let geom = geoJsonToGeom(geoJson) |
| 686 | |
| 687 | // diff against every zone |
| 688 | zones.forEach(distZone => { |
| 689 | geom = debugGeo('diff', geom, getDistZoneGeom(distZone)) |
| 690 | }) |
| 691 | |
| 692 | return { |
| 693 | geom: postProcessZone(geom, true), |
| 694 | tzid: zone.tzid |
| 695 | } |
| 696 | }) |
| 697 | |
| 698 | callback() |
| 699 | } |
| 700 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 701 | var combineAndWriteZones = function (callback) { |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 702 | const regularWriter = new FeatureWriterStream(distDir + '/combined.json') |
| 703 | const oceanWriter = new FeatureWriterStream(distDir + '/combined-with-oceans.json') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 704 | var zones = Object.keys(zoneCfg) |
| 705 | |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 706 | zones.forEach(zoneName => { |
| 707 | const feature = { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 708 | type: 'Feature', |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 709 | properties: { tzid: zoneName }, |
| 710 | geometry: geomToGeoJson(getDistZoneGeom(zoneName)) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 711 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 712 | const stringified = JSON.stringify(feature) |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 713 | regularWriter.add(stringified) |
| 714 | oceanWriter.add(stringified) |
| 715 | }) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 716 | oceanZoneBoundaries.forEach(boundary => { |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 717 | var feature = { |
| 718 | type: 'Feature', |
| 719 | properties: { tzid: boundary.tzid }, |
| 720 | geometry: boundary.geom |
| 721 | } |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 722 | oceanWriter.add(JSON.stringify(feature)) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 723 | }) |
| 724 | asynclib.parallel([ |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 725 | cb => regularWriter.end(cb), |
| 726 | cb => oceanWriter.end(cb) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 727 | ], callback) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 728 | } |
| 729 | |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 730 | var downloadLastRelease = function (cb) { |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 731 | // download latest release info |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 732 | https.get( |
| 733 | { |
| 734 | headers: { 'user-agent': 'timezone-boundary-builder' }, |
| 735 | host: 'api.github.com', |
| 736 | path: '/repos/evansiroky/timezone-boundary-builder/releases/latest' |
| 737 | }, |
| 738 | function (res) { |
| 739 | var data = '' |
| 740 | res.on('data', function (chunk) { |
| 741 | data += chunk |
| 742 | }) |
| 743 | res.on('end', function () { |
| 744 | data = JSON.parse(data) |
| 745 | // determine last release version name and download link |
| 746 | const lastReleaseName = data.name |
| 747 | lastReleaseJSONfile = `./dist/${lastReleaseName}.json` |
| 748 | let lastReleaseDownloadUrl |
| 749 | for (var i = 0; i < data.assets.length; i++) { |
| 750 | if (data.assets[i].browser_download_url.indexOf('timezones-with-oceans.geojson') > -1) { |
| 751 | lastReleaseDownloadUrl = data.assets[i].browser_download_url |
| 752 | } |
| 753 | } |
| 754 | if (!lastReleaseDownloadUrl) { |
| 755 | return cb(new Error('geojson not found')) |
| 756 | } |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 757 | |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 758 | // check for file that got downloaded |
| 759 | fs.stat(lastReleaseJSONfile, function (err) { |
| 760 | if (!err) { |
| 761 | // file found, skip download steps |
| 762 | return cb() |
| 763 | } |
| 764 | // file not found, download |
| 765 | console.log(`Downloading latest release to ${lastReleaseJSONfile}.zip`) |
| 766 | https.get({ |
| 767 | headers: { 'user-agent': 'timezone-boundary-builder' }, |
| 768 | host: 'github.com', |
| 769 | path: lastReleaseDownloadUrl.replace('https://github.com', '') |
| 770 | }, function (response) { |
| 771 | var file = fs.createWriteStream(`${lastReleaseJSONfile}.zip`) |
| 772 | response.pipe(file) |
| 773 | file.on('finish', function () { |
| 774 | file.close((err) => { |
| 775 | if (err) return cb(err) |
| 776 | // unzip file |
| 777 | console.log('unzipping latest release') |
| 778 | exec( |
| 779 | `unzip -o ${lastReleaseJSONfile} -d dist`, |
| 780 | err => { |
| 781 | if (err) { return cb(err) } |
| 782 | console.log('unzipped file') |
| 783 | console.log('moving unzipped file') |
| 784 | // might need to change this after changes to how files are |
| 785 | // zipped after 2020a |
| 786 | fs.copyFile( |
| 787 | path.join( |
| 788 | 'dist', |
| 789 | 'dist', |
| 790 | 'combined-with-oceans.json' |
| 791 | ), |
| 792 | lastReleaseJSONfile, |
| 793 | cb |
| 794 | ) |
| 795 | } |
| 796 | ) |
| 797 | }) |
| 798 | }) |
| 799 | }).on('error', cb) |
| 800 | }) |
| 801 | }) |
| 802 | } |
| 803 | ) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | var analyzeChangesFromLastRelease = function (cb) { |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 807 | // load last release data into memory |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 808 | console.log('loading previous release into memory') |
| 809 | const lastReleaseData = require(lastReleaseJSONfile) |
| 810 | |
| 811 | // load each feature's geojson into JSTS format and then organized by tzid |
| 812 | const lastReleaseZones = {} |
| 813 | lastReleaseData.features.forEach( |
| 814 | feature => { |
| 815 | lastReleaseZones[feature.properties.tzid] = feature |
| 816 | } |
| 817 | ) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 818 | |
| 819 | // generate set of keys from last release and current |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 820 | const zoneNames = new Set() |
| 821 | Object.keys(distZones).forEach(zoneName => zoneNames.add(zoneName)) |
| 822 | Object.keys(lastReleaseZones).forEach(zoneName => zoneNames.add(zoneName)) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 823 | |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 824 | // create diff for each zone |
| 825 | const analysisProgress = new ProgressStats( |
| 826 | 'Analyzing diffs', |
| 827 | zoneNames.size |
| 828 | ) |
| 829 | const additionsWriter = new FeatureWriterStream(distDir + '/additions.json') |
| 830 | const removalsWriter = new FeatureWriterStream(distDir + '/removals.json') |
| 831 | zoneNames.forEach(zoneName => { |
| 832 | analysisProgress.beginTask(zoneName, true) |
| 833 | if (distZones[zoneName] && lastReleaseZones[zoneName]) { |
| 834 | // some zones take forever to diff unless they are buffered, so buffer by |
| 835 | // just a small amount |
| 836 | const lastReleaseGeom = geoJsonToGeom( |
| 837 | lastReleaseZones[zoneName].geometry |
| 838 | ).buffer(bufferDistance) |
| 839 | const curDataGeom = getDistZoneGeom(zoneName).buffer(bufferDistance) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 840 | |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 841 | // don't diff equal geometries |
| 842 | if (curDataGeom.equals(lastReleaseGeom)) return |
| 843 | |
| 844 | // diff current - last = additions |
| 845 | const addition = debugGeo( |
| 846 | 'diff', |
| 847 | curDataGeom, |
| 848 | lastReleaseGeom, |
| 849 | false, |
| 850 | true |
| 851 | ) |
| 852 | if (addition.getArea() > 0.0001) { |
| 853 | additionsWriter.add(JSON.stringify({ |
| 854 | type: 'Feature', |
| 855 | properties: { tzid: zoneName }, |
| 856 | geometry: geomToGeoJson(addition) |
| 857 | })) |
| 858 | } |
| 859 | |
| 860 | // diff last - current = removals |
| 861 | const removal = debugGeo( |
| 862 | 'diff', |
| 863 | lastReleaseGeom, |
| 864 | curDataGeom, |
| 865 | false, |
| 866 | true |
| 867 | ) |
| 868 | if (removal.getArea() > 0.0001) { |
| 869 | removalsWriter.add(JSON.stringify({ |
| 870 | type: 'Feature', |
| 871 | properties: { tzid: zoneName }, |
| 872 | geometry: geomToGeoJson(removal) |
| 873 | })) |
| 874 | } |
| 875 | } else if (distZones[zoneName]) { |
| 876 | additionsWriter.add(JSON.stringify({ |
| 877 | type: 'Feature', |
| 878 | properties: { tzid: zoneName }, |
| 879 | geometry: geomToGeoJson(getDistZoneGeom(zoneName)) |
| 880 | })) |
| 881 | } else { |
| 882 | removalsWriter.add(JSON.stringify(lastReleaseZones[zoneName])) |
| 883 | } |
| 884 | }) |
| 885 | |
| 886 | // write files |
| 887 | asynclib.parallel([ |
| 888 | wcb => additionsWriter.end(wcb), |
| 889 | wcb => removalsWriter.end(wcb) |
| 890 | ], cb) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 891 | } |
| 892 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 893 | const autoScript = { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 894 | makeDownloadsDir: function (cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 895 | overallProgress.beginTask('Creating downloads dir') |
Neil Fuller | dce1d04 | 2020-10-14 09:53:00 +0100 | [diff] [blame] | 896 | safeMkdir(downloadsDir, cb) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 897 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 898 | makeDistDir: function (cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 899 | overallProgress.beginTask('Creating dist dir') |
Neil Fuller | dce1d04 | 2020-10-14 09:53:00 +0100 | [diff] [blame] | 900 | safeMkdir(distDir, cb) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 901 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 902 | getOsmBoundaries: ['makeDownloadsDir', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 903 | overallProgress.beginTask('Downloading osm boundaries') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 904 | asynclib.eachSeries(Object.keys(osmBoundarySources), downloadOsmBoundary, cb) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 905 | }], |
evansiroky | f3bcf05 | 2020-10-17 23:10:15 -0700 | [diff] [blame] | 906 | cleanDownloadFolder: ['makeDistDir', 'getOsmBoundaries', function (results, cb) { |
| 907 | overallProgress.beginTask('cleanDownloadFolder') |
| 908 | const downloadedFilenames = Object.keys(osmBoundarySources).map(name => `${name}.json`) |
evansiroky | b0442ca | 2020-10-18 17:49:45 -0700 | [diff] [blame] | 909 | fs.readdir(downloadsDir, (err, files) => { |
evansiroky | f3bcf05 | 2020-10-17 23:10:15 -0700 | [diff] [blame] | 910 | if (err) return cb(err) |
| 911 | asynclib.each( |
| 912 | files, |
| 913 | (file, fileCb) => { |
| 914 | if (downloadedFilenames.indexOf(file) === -1) { |
evansiroky | b0442ca | 2020-10-18 17:49:45 -0700 | [diff] [blame] | 915 | return fs.unlink(path.join(downloadsDir, file), fileCb) |
evansiroky | f3bcf05 | 2020-10-17 23:10:15 -0700 | [diff] [blame] | 916 | } |
| 917 | fileCb() |
| 918 | }, |
| 919 | cb |
| 920 | ) |
| 921 | }) |
| 922 | }], |
| 923 | zipInputData: ['cleanDownloadFolder', function (results, cb) { |
evansiroky | a48b392 | 2020-04-27 15:29:06 -0700 | [diff] [blame] | 924 | overallProgress.beginTask('Zipping up input data') |
evansiroky | 7452c9b | 2020-10-18 00:27:06 -0700 | [diff] [blame] | 925 | exec('zip -j ' + distDir + '/input-data.zip ' + downloadsDir + |
Neil Fuller | dce1d04 | 2020-10-14 09:53:00 +0100 | [diff] [blame] | 926 | '/* timezones.json osmBoundarySources.json expectedZoneOverlaps.json', cb) |
evansiroky | a48b392 | 2020-04-27 15:29:06 -0700 | [diff] [blame] | 927 | }], |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 928 | downloadLastRelease: ['makeDistDir', function (results, cb) { |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 929 | if (argv.skip_analyze_diffs) { |
| 930 | overallProgress.beginTask('WARNING: Skipping download of last release for analysis!') |
| 931 | cb() |
| 932 | } else { |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 933 | overallProgress.beginTask('Downloading last release for analysis') |
| 934 | downloadLastRelease(cb) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 935 | } |
| 936 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 937 | createZones: ['makeDistDir', 'getOsmBoundaries', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 938 | overallProgress.beginTask('Creating timezone boundaries') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 939 | asynclib.each(Object.keys(zoneCfg), makeTimezoneBoundary, cb) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 940 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 941 | validateZones: ['createZones', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 942 | overallProgress.beginTask('Validating timezone boundaries') |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 943 | loadDistZonesIntoMemory() |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 944 | if (argv.skip_validation) { |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 945 | console.warn('WARNING: Skipping validation!') |
| 946 | cb() |
| 947 | } else { |
| 948 | cb(validateTimezoneBoundaries()) |
| 949 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 950 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 951 | addOceans: ['validateZones', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 952 | overallProgress.beginTask('Adding oceans') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 953 | addOceans(cb) |
| 954 | }], |
| 955 | mergeZones: ['addOceans', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 956 | overallProgress.beginTask('Merging zones') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 957 | combineAndWriteZones(cb) |
| 958 | }], |
| 959 | zipGeoJson: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 960 | if (argv.skip_zip) { |
| 961 | overallProgress.beginTask('Skipping zip') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 962 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 963 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 964 | overallProgress.beginTask('Zipping geojson') |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 965 | const zipFile = distDir + '/timezones.geojson.zip' |
| 966 | const jsonFile = distDir + '/combined.json' |
evansiroky | 7452c9b | 2020-10-18 00:27:06 -0700 | [diff] [blame] | 967 | exec('zip -j ' + zipFile + ' ' + jsonFile, cb) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 968 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 969 | zipGeoJsonWithOceans: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 970 | if (argv.skip_zip) { |
| 971 | overallProgress.beginTask('Skipping with oceans zip') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 972 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 973 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 974 | overallProgress.beginTask('Zipping geojson with oceans') |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 975 | const zipFile = distDir + '/timezones-with-oceans.geojson.zip' |
| 976 | const jsonFile = distDir + '/combined-with-oceans.json' |
evansiroky | 7452c9b | 2020-10-18 00:27:06 -0700 | [diff] [blame] | 977 | exec('zip -j ' + zipFile + ' ' + jsonFile, cb) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 978 | }], |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 979 | makeShapefile: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 980 | if (argv.skip_shapefile) { |
| 981 | overallProgress.beginTask('Skipping shapefile creation') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 982 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 983 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 984 | overallProgress.beginTask('Converting from geojson to shapefile') |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 985 | const shapeFileGlob = distDir + '/combined-shapefile.*' |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 986 | rimraf.sync(shapeFileGlob) |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 987 | const shapeFile = distDir + '/combined-shapefile.shp' |
| 988 | const jsonFile = distDir + '/combined.json' |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 989 | exec( |
| 990 | 'ogr2ogr -f "ESRI Shapefile" ' + shapeFile + ' ' + jsonFile, |
| 991 | function (err, stdout, stderr) { |
| 992 | if (err) { return cb(err) } |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 993 | const shapeFileZip = distDir + '/timezones.shapefile.zip' |
evansiroky | 7452c9b | 2020-10-18 00:27:06 -0700 | [diff] [blame] | 994 | exec('zip -j ' + shapeFileZip + ' ' + shapeFileGlob, cb) |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 995 | } |
| 996 | ) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 997 | }], |
| 998 | makeShapefileWithOceans: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 999 | if (argv.skip_shapefile) { |
| 1000 | overallProgress.beginTask('Skipping with oceans shapefile creation') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 1001 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 1002 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 1003 | overallProgress.beginTask('Converting from geojson with oceans to shapefile') |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 1004 | const shapeFileGlob = distDir + '/combined-shapefile-with-oceans.*' |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 1005 | rimraf.sync(shapeFileGlob) |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 1006 | const shapeFile = distDir + '/combined-shapefile-with-oceans.shp' |
| 1007 | const jsonFile = distDir + '/combined-with-oceans.json' |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 1008 | exec( |
| 1009 | 'ogr2ogr -f "ESRI Shapefile" ' + shapeFile + ' ' + jsonFile, |
| 1010 | function (err, stdout, stderr) { |
| 1011 | if (err) { return cb(err) } |
evansiroky | b7d9d79 | 2020-10-17 16:50:14 -0700 | [diff] [blame] | 1012 | const shapeFileZip = distDir + '/timezones-with-oceans.shapefile.zip' |
evansiroky | 7452c9b | 2020-10-18 00:27:06 -0700 | [diff] [blame] | 1013 | exec('zip -j ' + shapeFileZip + ' ' + shapeFileGlob, cb) |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame] | 1014 | } |
| 1015 | ) |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 1016 | }], |
| 1017 | makeListOfTimeZoneNames: function (cb) { |
| 1018 | overallProgress.beginTask('Writing timezone names to file') |
| 1019 | let zoneNames = Object.keys(zoneCfg) |
| 1020 | oceanZones.forEach(oceanZone => { |
| 1021 | zoneNames.push(oceanZone.tzid) |
| 1022 | }) |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 1023 | if (includedZones.length > 0) { |
| 1024 | zoneNames = zoneNames.filter(zoneName => includedZones.indexOf(zoneName) > -1) |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 1025 | } |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 1026 | if (excludedZones.length > 0) { |
| 1027 | zoneNames = zoneNames.filter(zoneName => excludedZones.indexOf(zoneName) === -1) |
| 1028 | } |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 1029 | fs.writeFile( |
Neil Fuller | dce1d04 | 2020-10-14 09:53:00 +0100 | [diff] [blame] | 1030 | distDir + '/timezone-names.json', |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 1031 | JSON.stringify(zoneNames), |
| 1032 | cb |
| 1033 | ) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 1034 | }, |
| 1035 | analyzeChangesFromLastRelease: ['downloadLastRelease', 'mergeZones', function (results, cb) { |
evansiroky | 62278f0 | 2020-10-25 14:27:29 -0700 | [diff] [blame] | 1036 | if (argv.skip_analyze_diffs) { |
| 1037 | overallProgress.beginTask('WARNING: Skipping analysis of changes from last release!') |
| 1038 | cb() |
| 1039 | } else { |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 1040 | overallProgress.beginTask('Analyzing changes from last release') |
| 1041 | analyzeChangesFromLastRelease(cb) |
evansiroky | 96dfadc | 2020-04-28 01:31:38 -0700 | [diff] [blame] | 1042 | } |
| 1043 | }] |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | const overallProgress = new ProgressStats('Overall', Object.keys(autoScript).length) |
| 1047 | |
| 1048 | asynclib.auto(autoScript, function (err, results) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 1049 | console.log('done') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 1050 | if (err) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 1051 | console.log('error!', err) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 1052 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 1053 | }) |