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 | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 3 | |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 4 | var area = require('@mapbox/geojson-area') |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 5 | var geojsonhint = require('@mapbox/geojsonhint') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 6 | var bbox = require('@turf/bbox').default |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 7 | var helpers = require('@turf/helpers') |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 8 | var multiPolygon = helpers.multiPolygon |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 9 | var polygon = helpers.polygon |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 10 | var asynclib = require('async') |
| 11 | var jsts = require('jsts') |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 12 | var rimraf = require('rimraf') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 13 | var overpass = require('query-overpass') |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 14 | var yargs = require('yargs') |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 15 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 16 | const ProgressStats = require('./progressStats') |
| 17 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 18 | var osmBoundarySources = require('./osmBoundarySources.json') |
| 19 | var zoneCfg = require('./timezones.json') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 20 | var expectedZoneOverlaps = require('./expectedZoneOverlaps.json') |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 21 | |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 22 | const argv = yargs |
| 23 | .option('included_zones', { |
| 24 | description: 'Include specified zones', |
| 25 | type: 'array' |
| 26 | }) |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 27 | .option('excluded_zones', { |
| 28 | description: 'Exclude specified zones', |
| 29 | type: 'array' |
| 30 | }) |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 31 | .option('downloads_dir', { |
| 32 | description: 'Set the download location', |
| 33 | default: './downloads', |
| 34 | type: 'string' |
| 35 | }) |
| 36 | .option('dist_dir', { |
| 37 | description: 'Set the dist location', |
| 38 | default: './dist', |
| 39 | type: 'string' |
| 40 | }) |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 41 | .option('no_validation', { |
| 42 | description: 'Skip validation', |
| 43 | type: 'boolean' |
| 44 | }) |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 45 | .option('skip_zip', { |
| 46 | description: 'Skip zip creation', |
| 47 | type: 'boolean' |
| 48 | }) |
| 49 | .option('skip_shapefile', { |
| 50 | description: 'Skip shapefile creation', |
| 51 | type: 'boolean' |
| 52 | }) |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 53 | .help() |
| 54 | .strict() |
| 55 | .alias('help', 'h') |
| 56 | .argv |
| 57 | |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 58 | // allow building of only a specified zones |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 59 | let includedZones = [] |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 60 | let excludedZones = [] |
| 61 | if (argv.included_zones || argv.excluded_zones) { |
| 62 | if (argv.included_zones) { |
| 63 | let newZoneCfg = {} |
| 64 | includedZones = argv.included_zones |
| 65 | includedZones.forEach((zoneName) => { |
| 66 | newZoneCfg[zoneName] = zoneCfg[zoneName] |
| 67 | }) |
| 68 | zoneCfg = newZoneCfg |
| 69 | } |
| 70 | if (argv.excluded_zones) { |
| 71 | let newZoneCfg = {} |
| 72 | excludedZones = argv.excluded_zones |
| 73 | Object.keys(zoneCfg).forEach((zoneName) => { |
| 74 | if (!excludedZones.includes(zoneName)) { |
| 75 | newZoneCfg[zoneName] = zoneCfg[zoneName] |
| 76 | } |
| 77 | }) |
| 78 | zoneCfg = newZoneCfg |
| 79 | } |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 80 | |
| 81 | // filter out unneccessary downloads |
| 82 | var newOsmBoundarySources = {} |
| 83 | Object.keys(zoneCfg).forEach((zoneName) => { |
| 84 | zoneCfg[zoneName].forEach((op) => { |
| 85 | if (op.source === 'overpass') { |
| 86 | newOsmBoundarySources[op.id] = osmBoundarySources[op.id] |
| 87 | } |
| 88 | }) |
| 89 | }) |
| 90 | |
| 91 | osmBoundarySources = newOsmBoundarySources |
| 92 | } |
| 93 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 94 | var geoJsonReader = new jsts.io.GeoJSONReader() |
| 95 | var geoJsonWriter = new jsts.io.GeoJSONWriter() |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 96 | var precisionModel = new jsts.geom.PrecisionModel(1000000) |
| 97 | var precisionReducer = new jsts.precision.GeometryPrecisionReducer(precisionModel) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 98 | var distZones = {} |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 99 | var minRequestGap = 4 |
| 100 | var curRequestGap = 4 |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 101 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 102 | var safeMkdir = function (dirname, callback) { |
| 103 | fs.mkdir(dirname, function (err) { |
| 104 | if (err && err.code === 'EEXIST') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 105 | callback() |
| 106 | } else { |
| 107 | callback(err) |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 112 | var debugGeo = function (op, a, b, reducePrecision) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 113 | var result |
| 114 | |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 115 | if (reducePrecision) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 116 | a = precisionReducer.reduce(a) |
| 117 | b = precisionReducer.reduce(b) |
| 118 | } |
| 119 | |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 120 | try { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 121 | switch (op) { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 122 | case 'union': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 123 | result = a.union(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 124 | break |
| 125 | case 'intersection': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 126 | result = a.intersection(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 127 | break |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 128 | case 'intersects': |
| 129 | result = a.intersects(b) |
| 130 | break |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 131 | case 'diff': |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 132 | result = a.difference(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 133 | break |
| 134 | default: |
| 135 | var err = new Error('invalid op: ' + op) |
| 136 | throw err |
| 137 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 138 | } catch (e) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 139 | if (e.name === 'TopologyException') { |
| 140 | console.log('Encountered TopologyException, retry with GeometryPrecisionReducer') |
| 141 | return debugGeo(op, a, b, true) |
| 142 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 143 | console.log('op err') |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 144 | console.log(e) |
| 145 | console.log(e.stack) |
| 146 | fs.writeFileSync('debug_' + op + '_a.json', JSON.stringify(geoJsonWriter.write(a))) |
| 147 | fs.writeFileSync('debug_' + op + '_b.json', JSON.stringify(geoJsonWriter.write(b))) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 148 | throw e |
| 149 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 150 | |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 151 | return result |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 154 | var fetchIfNeeded = function (file, superCallback, downloadCallback, fetchFn) { |
| 155 | // check for file that got downloaded |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 156 | fs.stat(file, function (err) { |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 157 | if (!err) { |
| 158 | // file found, skip download steps |
| 159 | return superCallback() |
| 160 | } |
| 161 | // check for manual file that got fixed and needs validation |
| 162 | var fixedFile = file.replace('.json', '_fixed.json') |
| 163 | fs.stat(fixedFile, function (err) { |
| 164 | if (!err) { |
| 165 | // file found, return fixed file |
| 166 | return downloadCallback(null, require(fixedFile)) |
| 167 | } |
| 168 | // no manual fixed file found, download from overpass |
| 169 | fetchFn() |
| 170 | }) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 171 | }) |
| 172 | } |
| 173 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 174 | var geoJsonToGeom = function (geoJson) { |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 175 | try { |
| 176 | return geoJsonReader.read(JSON.stringify(geoJson)) |
| 177 | } catch (e) { |
| 178 | console.error('error converting geojson to geometry') |
| 179 | fs.writeFileSync('debug_geojson_read_error.json', JSON.stringify(geoJson)) |
| 180 | throw e |
| 181 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 184 | var geomToGeoJson = function (geom) { |
| 185 | return geoJsonWriter.write(geom) |
| 186 | } |
| 187 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 188 | var geomToGeoJsonString = function (geom) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 189 | return JSON.stringify(geoJsonWriter.write(geom)) |
| 190 | } |
| 191 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 192 | const downloadProgress = new ProgressStats( |
| 193 | 'Downloading', |
| 194 | Object.keys(osmBoundarySources).length |
| 195 | ) |
| 196 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 197 | var downloadOsmBoundary = function (boundaryId, boundaryCallback) { |
| 198 | var cfg = osmBoundarySources[boundaryId] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 199 | var query = '[out:json][timeout:60];(' |
| 200 | if (cfg.way) { |
| 201 | query += 'way' |
| 202 | } else { |
| 203 | query += 'relation' |
| 204 | } |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 205 | var boundaryFilename = argv.downloads_dir + '/' + boundaryId + '.json' |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 206 | var debug = 'getting data for ' + boundaryId |
| 207 | var queryKeys = Object.keys(cfg) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 208 | |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 209 | for (var i = queryKeys.length - 1; i >= 0; i--) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 210 | var k = queryKeys[i] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 211 | if (k === 'way') continue |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 212 | var v = cfg[k] |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 213 | |
| 214 | query += '["' + k + '"="' + v + '"]' |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 215 | } |
| 216 | |
evansiroky | 283ebbc | 2018-07-16 15:13:07 -0700 | [diff] [blame] | 217 | query += ';);out body;>;out meta qt;' |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 218 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 219 | downloadProgress.beginTask(debug, true) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 220 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 221 | asynclib.auto({ |
| 222 | downloadFromOverpass: function (cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 223 | console.log('downloading from overpass') |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 224 | fetchIfNeeded(boundaryFilename, boundaryCallback, cb, function () { |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 225 | var overpassResponseHandler = function (err, data) { |
| 226 | if (err) { |
| 227 | console.log(err) |
| 228 | console.log('Increasing overpass request gap') |
| 229 | curRequestGap *= 2 |
| 230 | makeQuery() |
| 231 | } else { |
| 232 | console.log('Success, decreasing overpass request gap') |
| 233 | curRequestGap = Math.max(minRequestGap, curRequestGap / 2) |
| 234 | cb(null, data) |
| 235 | } |
| 236 | } |
| 237 | var makeQuery = function () { |
| 238 | console.log('waiting ' + curRequestGap + ' seconds') |
| 239 | setTimeout(function () { |
| 240 | overpass(query, overpassResponseHandler, { flatProperties: true }) |
| 241 | }, curRequestGap * 1000) |
| 242 | } |
| 243 | makeQuery() |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 244 | }) |
| 245 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 246 | validateOverpassResult: ['downloadFromOverpass', function (results, cb) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 247 | var data = results.downloadFromOverpass |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 248 | if (!data.features) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 249 | var err = new Error('Invalid geojson for boundary: ' + boundaryId) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 250 | return cb(err) |
| 251 | } |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 252 | if (data.features.length === 0) { |
| 253 | console.error('No data for the following query:') |
| 254 | console.error(query) |
| 255 | 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] | 256 | return cb(new Error('No data found for from overpass query')) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 257 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 258 | cb() |
| 259 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 260 | saveSingleMultiPolygon: ['validateOverpassResult', function (results, cb) { |
| 261 | var data = results.downloadFromOverpass |
| 262 | var combined |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 263 | |
| 264 | // union all multi-polygons / polygons into one |
| 265 | for (var i = data.features.length - 1; i >= 0; i--) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 266 | var curOsmGeom = data.features[i].geometry |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 267 | const curOsmProps = data.features[i].properties |
| 268 | if ( |
| 269 | (curOsmGeom.type === 'Polygon' || curOsmGeom.type === 'MultiPolygon') && |
| 270 | curOsmProps.type === 'boundary' // need to make sure enclaves aren't unioned |
| 271 | ) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 272 | console.log('combining border') |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 273 | let errors = geojsonhint.hint(curOsmGeom) |
| 274 | if (errors && errors.length > 0) { |
| 275 | const stringifiedGeojson = JSON.stringify(curOsmGeom, null, 2) |
| 276 | errors = geojsonhint.hint(stringifiedGeojson) |
| 277 | console.error('Invalid geojson received in Overpass Result') |
| 278 | console.error('Overpass query: ' + query) |
| 279 | const problemFilename = boundaryId + '_convert_to_geom_error.json' |
| 280 | fs.writeFileSync(problemFilename, stringifiedGeojson) |
| 281 | console.error('saved problem file to ' + problemFilename) |
| 282 | console.error('To read more about this error, please visit https://git.io/vxKQq') |
| 283 | return cb(errors) |
| 284 | } |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 285 | try { |
| 286 | var curGeom = geoJsonToGeom(curOsmGeom) |
| 287 | } catch (e) { |
| 288 | console.error('error converting overpass result to geojson') |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 289 | console.error(e) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 290 | |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 291 | fs.writeFileSync(boundaryId + '_convert_to_geom_error-all-features.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 292 | return cb(e) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 293 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 294 | if (!combined) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 295 | combined = curGeom |
| 296 | } else { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 297 | combined = debugGeo('union', curGeom, combined) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 301 | try { |
| 302 | fs.writeFile(boundaryFilename, geomToGeoJsonString(combined), cb) |
| 303 | } catch (e) { |
| 304 | console.error('error writing combined border to geojson') |
| 305 | fs.writeFileSync(boundaryId + '_combined_border_convert_to_geom_error.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 306 | return cb(e) |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 307 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 308 | }] |
| 309 | }, boundaryCallback) |
| 310 | } |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 311 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 312 | var getTzDistFilename = function (tzid) { |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 313 | return argv.dist_dir + '/' + tzid.replace(/\//g, '__') + '.json' |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Get the geometry of the requested source data |
| 318 | * |
| 319 | * @return {Object} geom The geometry of the source |
| 320 | * @param {Object} source An object representing the data source |
| 321 | * must have `source` key and then either: |
| 322 | * - `id` if from a file |
| 323 | * - `id` if from a file |
| 324 | */ |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 325 | var getDataSource = function (source) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 326 | var geoJson |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 327 | if (source.source === 'overpass') { |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 328 | geoJson = require(argv.downloads_dir + '/' + source.id + '.json') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 329 | } else if (source.source === 'manual-polygon') { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 330 | geoJson = polygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 331 | } else if (source.source === 'manual-multipolygon') { |
Evan Siroky | 8e30a2e | 2016-08-06 19:55:35 -0700 | [diff] [blame] | 332 | geoJson = multiPolygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 333 | } else if (source.source === 'dist') { |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 334 | geoJson = require(getTzDistFilename(source.id)) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 335 | } else { |
| 336 | var err = new Error('unknown source: ' + source.source) |
| 337 | throw err |
| 338 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 339 | return geoJsonToGeom(geoJson) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 342 | /** |
| 343 | * Post process created timezone boundary. |
| 344 | * - remove small holes and exclaves |
| 345 | * - reduce geometry precision |
| 346 | * |
| 347 | * @param {Geometry} geom The jsts geometry of the timezone |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 348 | * @param {boolean} returnAsObject if true, return as object, otherwise return stringified |
| 349 | * @return {Object|String} geojson as object or stringified |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 350 | */ |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 351 | var postProcessZone = function (geom, returnAsObject) { |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 352 | // reduce precision of geometry |
| 353 | const geojson = geomToGeoJson(precisionReducer.reduce(geom)) |
| 354 | |
| 355 | // iterate through all polygons |
| 356 | const filteredPolygons = [] |
| 357 | let allPolygons = geojson.coordinates |
| 358 | if (geojson.type === 'Polygon') { |
| 359 | allPolygons = [geojson.coordinates] |
| 360 | } |
| 361 | |
| 362 | allPolygons.forEach((curPolygon, idx) => { |
| 363 | // remove any polygon with very small area |
| 364 | const polygonFeature = polygon(curPolygon) |
| 365 | const polygonArea = area.geometry(polygonFeature.geometry) |
| 366 | |
| 367 | if (polygonArea < 1) return |
| 368 | |
| 369 | // find all holes |
| 370 | const filteredLinearRings = [] |
| 371 | |
| 372 | curPolygon.forEach((curLinearRing, lrIdx) => { |
| 373 | if (lrIdx === 0) { |
| 374 | // always keep first linearRing |
| 375 | filteredLinearRings.push(curLinearRing) |
| 376 | } else { |
| 377 | const polygonFromLinearRing = polygon([curLinearRing]) |
| 378 | const linearRingArea = area.geometry(polygonFromLinearRing.geometry) |
| 379 | |
| 380 | // only include holes with relevant area |
| 381 | if (linearRingArea > 1) { |
| 382 | filteredLinearRings.push(curLinearRing) |
| 383 | } |
| 384 | } |
| 385 | }) |
| 386 | |
| 387 | filteredPolygons.push(filteredLinearRings) |
| 388 | }) |
| 389 | |
| 390 | // recompile to geojson string |
| 391 | const newGeojson = { |
| 392 | type: geojson.type |
| 393 | } |
| 394 | |
| 395 | if (geojson.type === 'Polygon') { |
| 396 | newGeojson.coordinates = filteredPolygons[0] |
| 397 | } else { |
| 398 | newGeojson.coordinates = filteredPolygons |
| 399 | } |
| 400 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 401 | return returnAsObject ? newGeojson : JSON.stringify(newGeojson) |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 402 | } |
| 403 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 404 | const buildingProgress = new ProgressStats( |
| 405 | 'Building', |
| 406 | Object.keys(zoneCfg).length |
| 407 | ) |
| 408 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 409 | var makeTimezoneBoundary = function (tzid, callback) { |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 410 | buildingProgress.beginTask(`makeTimezoneBoundary for ${tzid}`, true) |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 411 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 412 | var ops = zoneCfg[tzid] |
| 413 | var geom |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 414 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 415 | asynclib.eachSeries(ops, function (task, cb) { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 416 | var taskData = getDataSource(task) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 417 | console.log('-', task.op, task.id) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 418 | if (task.op === 'init') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 419 | geom = taskData |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 420 | } else if (task.op === 'intersect') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 421 | geom = debugGeo('intersection', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 422 | } else if (task.op === 'difference') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 423 | geom = debugGeo('diff', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 424 | } else if (task.op === 'difference-reverse-order') { |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 425 | geom = debugGeo('diff', taskData, geom) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 426 | } else if (task.op === 'union') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 427 | geom = debugGeo('union', geom, taskData) |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 428 | } else { |
| 429 | var err = new Error('unknown op: ' + task.op) |
| 430 | return cb(err) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 431 | } |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 432 | cb() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 433 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 434 | function (err) { |
| 435 | if (err) { return callback(err) } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 436 | fs.writeFile(getTzDistFilename(tzid), |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 437 | postProcessZone(geom), |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 438 | callback) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 439 | }) |
| 440 | } |
| 441 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 442 | var loadDistZonesIntoMemory = function () { |
| 443 | console.log('load zones into memory') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 444 | var zones = Object.keys(zoneCfg) |
| 445 | var tzid |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 446 | |
| 447 | for (var i = 0; i < zones.length; i++) { |
| 448 | tzid = zones[i] |
| 449 | distZones[tzid] = getDataSource({ source: 'dist', id: tzid }) |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | var getDistZoneGeom = function (tzid) { |
| 454 | return distZones[tzid] |
| 455 | } |
| 456 | |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 457 | var roundDownToTenth = function (n) { |
| 458 | return Math.floor(n * 10) / 10 |
| 459 | } |
| 460 | |
| 461 | var roundUpToTenth = function (n) { |
| 462 | return Math.ceil(n * 10) / 10 |
| 463 | } |
| 464 | |
| 465 | var formatBounds = function (bounds) { |
| 466 | let boundsStr = '[' |
| 467 | boundsStr += roundDownToTenth(bounds[0]) + ', ' |
| 468 | boundsStr += roundDownToTenth(bounds[1]) + ', ' |
| 469 | boundsStr += roundUpToTenth(bounds[2]) + ', ' |
| 470 | boundsStr += roundUpToTenth(bounds[3]) + ']' |
| 471 | return boundsStr |
| 472 | } |
| 473 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 474 | var validateTimezoneBoundaries = function () { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 475 | const numZones = Object.keys(zoneCfg).length |
| 476 | const validationProgress = new ProgressStats( |
| 477 | 'Validation', |
| 478 | numZones * (numZones + 1) / 2 |
| 479 | ) |
| 480 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 481 | console.log('do validation... this may take a few minutes') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 482 | var allZonesOk = true |
| 483 | var zones = Object.keys(zoneCfg) |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 484 | var lastPct = 0 |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 485 | var compareTzid, tzid, zoneGeom |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 486 | |
| 487 | for (var i = 0; i < zones.length; i++) { |
| 488 | tzid = zones[i] |
| 489 | zoneGeom = getDistZoneGeom(tzid) |
| 490 | |
| 491 | for (var j = i + 1; j < zones.length; j++) { |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 492 | const curPct = Math.floor(validationProgress.getPercentage()) |
| 493 | if (curPct % 10 === 0 && curPct !== lastPct) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 494 | validationProgress.printStats('Validating zones', true) |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 495 | lastPct = curPct |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 496 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 497 | compareTzid = zones[j] |
| 498 | |
| 499 | var compareZoneGeom = getDistZoneGeom(compareTzid) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 500 | |
| 501 | var intersects = false |
| 502 | try { |
| 503 | intersects = debugGeo('intersects', zoneGeom, compareZoneGeom) |
| 504 | } catch (e) { |
| 505 | console.warn('warning, encountered intersection error with zone ' + tzid + ' and ' + compareTzid) |
| 506 | } |
| 507 | if (intersects) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 508 | var intersectedGeom = debugGeo('intersection', zoneGeom, compareZoneGeom) |
| 509 | var intersectedArea = intersectedGeom.getArea() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 510 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 511 | if (intersectedArea > 0.0001) { |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 512 | // check if the intersected area(s) are one of the expected areas of overlap |
| 513 | const allowedOverlapBounds = expectedZoneOverlaps[`${tzid}-${compareTzid}`] || expectedZoneOverlaps[`${compareTzid}-${tzid}`] |
| 514 | const overlapsGeoJson = geoJsonWriter.write(intersectedGeom) |
| 515 | |
| 516 | // these zones are allowed to overlap in certain places, make sure the |
| 517 | // found overlap(s) all fit within the expected areas of overlap |
| 518 | if (allowedOverlapBounds) { |
| 519 | // if the overlaps are a multipolygon, make sure each individual |
| 520 | // polygon of overlap fits within at least one of the expected |
| 521 | // overlaps |
| 522 | let overlapsPolygons |
| 523 | switch (overlapsGeoJson.type) { |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 524 | case 'MultiPolygon': |
| 525 | overlapsPolygons = overlapsGeoJson.coordinates.map( |
| 526 | polygonCoords => ({ |
| 527 | coordinates: polygonCoords, |
| 528 | type: 'Polygon' |
| 529 | }) |
| 530 | ) |
| 531 | break |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 532 | case 'Polygon': |
| 533 | overlapsPolygons = [overlapsGeoJson] |
| 534 | break |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 535 | case 'GeometryCollection': |
| 536 | overlapsPolygons = [] |
| 537 | overlapsGeoJson.geometries.forEach(geom => { |
| 538 | if (geom.type === 'Polygon') { |
| 539 | overlapsPolygons.push(geom) |
| 540 | } else if (geom.type === 'MultiPolygon') { |
| 541 | geom.coordinates.forEach(polygonCoords => { |
| 542 | overlapsPolygons.push({ |
| 543 | coordinates: polygonCoords, |
| 544 | type: 'Polygon' |
| 545 | }) |
| 546 | }) |
| 547 | } |
| 548 | }) |
| 549 | break |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 550 | default: |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 551 | console.error('unexpected geojson overlap type') |
| 552 | console.log(overlapsGeoJson) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 553 | break |
| 554 | } |
| 555 | |
| 556 | let allOverlapsOk = true |
| 557 | overlapsPolygons.forEach((polygon, idx) => { |
| 558 | const bounds = bbox(polygon) |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 559 | const polygonArea = area.geometry(polygon) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 560 | if ( |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 561 | polygonArea > 10 && // ignore small polygons |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 562 | !allowedOverlapBounds.some(allowedBounds => |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 563 | allowedBounds.bounds[0] <= bounds[0] && // minX |
| 564 | allowedBounds.bounds[1] <= bounds[1] && // minY |
| 565 | allowedBounds.bounds[2] >= bounds[2] && // maxX |
| 566 | allowedBounds.bounds[3] >= bounds[3] // maxY |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 567 | ) |
| 568 | ) { |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 569 | console.error(`Unexpected intersection (${polygonArea} area) with bounds: ${formatBounds(bounds)}`) |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 570 | allOverlapsOk = false |
| 571 | } |
| 572 | }) |
| 573 | |
| 574 | if (allOverlapsOk) continue |
| 575 | } |
| 576 | |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 577 | // at least one unexpected overlap found, output an error and write debug file |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 578 | console.error('Validation error: ' + tzid + ' intersects ' + compareTzid + ' area: ' + intersectedArea) |
evansiroky | 92c15c4 | 2018-11-15 20:58:18 -0800 | [diff] [blame] | 579 | const debugFilename = tzid.replace(/\//g, '-') + '-' + compareTzid.replace(/\//g, '-') + '-overlap.json' |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 580 | fs.writeFileSync( |
| 581 | debugFilename, |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 582 | JSON.stringify(overlapsGeoJson) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 583 | ) |
| 584 | console.error('wrote overlap area as file ' + debugFilename) |
| 585 | 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] | 586 | allZonesOk = false |
| 587 | } |
| 588 | } |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 589 | validationProgress.logNext() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
| 593 | return allZonesOk ? null : 'Zone validation unsuccessful' |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 594 | } |
| 595 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 596 | let oceanZoneBoundaries |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 597 | let oceanZones = [ |
| 598 | { tzid: 'Etc/GMT-12', left: 172.5, right: 180 }, |
| 599 | { tzid: 'Etc/GMT-11', left: 157.5, right: 172.5 }, |
| 600 | { tzid: 'Etc/GMT-10', left: 142.5, right: 157.5 }, |
| 601 | { tzid: 'Etc/GMT-9', left: 127.5, right: 142.5 }, |
| 602 | { tzid: 'Etc/GMT-8', left: 112.5, right: 127.5 }, |
| 603 | { tzid: 'Etc/GMT-7', left: 97.5, right: 112.5 }, |
| 604 | { tzid: 'Etc/GMT-6', left: 82.5, right: 97.5 }, |
| 605 | { tzid: 'Etc/GMT-5', left: 67.5, right: 82.5 }, |
| 606 | { tzid: 'Etc/GMT-4', left: 52.5, right: 67.5 }, |
| 607 | { tzid: 'Etc/GMT-3', left: 37.5, right: 52.5 }, |
| 608 | { tzid: 'Etc/GMT-2', left: 22.5, right: 37.5 }, |
| 609 | { tzid: 'Etc/GMT-1', left: 7.5, right: 22.5 }, |
| 610 | { tzid: 'Etc/GMT', left: -7.5, right: 7.5 }, |
| 611 | { tzid: 'Etc/GMT+1', left: -22.5, right: -7.5 }, |
| 612 | { tzid: 'Etc/GMT+2', left: -37.5, right: -22.5 }, |
| 613 | { tzid: 'Etc/GMT+3', left: -52.5, right: -37.5 }, |
| 614 | { tzid: 'Etc/GMT+4', left: -67.5, right: -52.5 }, |
| 615 | { tzid: 'Etc/GMT+5', left: -82.5, right: -67.5 }, |
| 616 | { tzid: 'Etc/GMT+6', left: -97.5, right: -82.5 }, |
| 617 | { tzid: 'Etc/GMT+7', left: -112.5, right: -97.5 }, |
| 618 | { tzid: 'Etc/GMT+8', left: -127.5, right: -112.5 }, |
| 619 | { tzid: 'Etc/GMT+9', left: -142.5, right: -127.5 }, |
| 620 | { tzid: 'Etc/GMT+10', left: -157.5, right: -142.5 }, |
| 621 | { tzid: 'Etc/GMT+11', left: -172.5, right: -157.5 }, |
| 622 | { tzid: 'Etc/GMT+12', left: -180, right: -172.5 } |
| 623 | ] |
| 624 | |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 625 | if (includedZones.length > 0) { |
| 626 | oceanZones = oceanZones.filter(oceanZone => includedZones.indexOf(oceanZone) > -1) |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 627 | } |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 628 | if (excludedZones.length > 0) { |
| 629 | oceanZones = oceanZones.filter(oceanZone => excludedZones.indexOf(oceanZone) === -1) |
| 630 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 631 | |
| 632 | var addOceans = function (callback) { |
| 633 | console.log('adding ocean boundaries') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 634 | const zones = Object.keys(zoneCfg) |
| 635 | |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 636 | const oceanProgress = new ProgressStats( |
| 637 | 'Oceans', |
| 638 | oceanZones.length |
| 639 | ) |
| 640 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 641 | oceanZoneBoundaries = oceanZones.map(zone => { |
evansiroky | 3046a3d | 2019-01-05 21:19:14 -0800 | [diff] [blame] | 642 | oceanProgress.beginTask(zone.tzid, true) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 643 | const geoJson = polygon([[ |
| 644 | [zone.left, 90], |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame] | 645 | [zone.left, -90], |
| 646 | [zone.right, -90], |
| 647 | [zone.right, 90], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 648 | [zone.left, 90] |
| 649 | ]]).geometry |
| 650 | |
| 651 | let geom = geoJsonToGeom(geoJson) |
| 652 | |
| 653 | // diff against every zone |
| 654 | zones.forEach(distZone => { |
| 655 | geom = debugGeo('diff', geom, getDistZoneGeom(distZone)) |
| 656 | }) |
| 657 | |
| 658 | return { |
| 659 | geom: postProcessZone(geom, true), |
| 660 | tzid: zone.tzid |
| 661 | } |
| 662 | }) |
| 663 | |
| 664 | callback() |
| 665 | } |
| 666 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 667 | var combineAndWriteZones = function (callback) { |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 668 | var stream = fs.createWriteStream(argv.dist_dir + '/combined.json') |
| 669 | var streamWithOceans = fs.createWriteStream(argv.dist_dir + '/combined-with-oceans.json') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 670 | var zones = Object.keys(zoneCfg) |
| 671 | |
| 672 | stream.write('{"type":"FeatureCollection","features":[') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 673 | streamWithOceans.write('{"type":"FeatureCollection","features":[') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 674 | |
| 675 | for (var i = 0; i < zones.length; i++) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 676 | if (i > 0) { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 677 | stream.write(',') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 678 | streamWithOceans.write(',') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 679 | } |
| 680 | var feature = { |
| 681 | type: 'Feature', |
| 682 | properties: { tzid: zones[i] }, |
| 683 | geometry: geomToGeoJson(getDistZoneGeom(zones[i])) |
| 684 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 685 | const stringified = JSON.stringify(feature) |
| 686 | stream.write(stringified) |
| 687 | streamWithOceans.write(stringified) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 688 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 689 | oceanZoneBoundaries.forEach(boundary => { |
| 690 | streamWithOceans.write(',') |
| 691 | var feature = { |
| 692 | type: 'Feature', |
| 693 | properties: { tzid: boundary.tzid }, |
| 694 | geometry: boundary.geom |
| 695 | } |
| 696 | streamWithOceans.write(JSON.stringify(feature)) |
| 697 | }) |
| 698 | asynclib.parallel([ |
| 699 | cb => { |
| 700 | stream.end(']}', cb) |
| 701 | }, |
| 702 | cb => { |
| 703 | streamWithOceans.end(']}', cb) |
| 704 | } |
| 705 | ], callback) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 706 | } |
| 707 | |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 708 | const autoScript = { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 709 | makeDownloadsDir: function (cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 710 | overallProgress.beginTask('Creating downloads dir') |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 711 | safeMkdir(argv.downloads_dir, cb) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 712 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 713 | makeDistDir: function (cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 714 | overallProgress.beginTask('Creating dist dir') |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 715 | safeMkdir(argv.dist_dir, cb) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 716 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 717 | getOsmBoundaries: ['makeDownloadsDir', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 718 | overallProgress.beginTask('Downloading osm boundaries') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 719 | asynclib.eachSeries(Object.keys(osmBoundarySources), downloadOsmBoundary, cb) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 720 | }], |
evansiroky | a48b392 | 2020-04-27 15:29:06 -0700 | [diff] [blame] | 721 | zipInputData: ['makeDistDir', 'getOsmBoundaries', function (results, cb) { |
| 722 | overallProgress.beginTask('Zipping up input data') |
| 723 | exec('zip dist/input-data.zip downloads/* timezones.json osmBoundarySources.json expectedZoneOverlaps.json', cb) |
| 724 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 725 | createZones: ['makeDistDir', 'getOsmBoundaries', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 726 | overallProgress.beginTask('Creating timezone boundaries') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 727 | asynclib.each(Object.keys(zoneCfg), makeTimezoneBoundary, cb) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 728 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 729 | validateZones: ['createZones', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 730 | overallProgress.beginTask('Validating timezone boundaries') |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 731 | loadDistZonesIntoMemory() |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 732 | if (argv.no_validation) { |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 733 | console.warn('WARNING: Skipping validation!') |
| 734 | cb() |
| 735 | } else { |
| 736 | cb(validateTimezoneBoundaries()) |
| 737 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 738 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 739 | addOceans: ['validateZones', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 740 | overallProgress.beginTask('Adding oceans') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 741 | addOceans(cb) |
| 742 | }], |
| 743 | mergeZones: ['addOceans', function (results, cb) { |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 744 | overallProgress.beginTask('Merging zones') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 745 | combineAndWriteZones(cb) |
| 746 | }], |
| 747 | zipGeoJson: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 748 | if (argv.skip_zip) { |
| 749 | overallProgress.beginTask('Skipping zip') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 750 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 751 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 752 | overallProgress.beginTask('Zipping geojson') |
| 753 | let zipFile = argv.dist_dir + '/timezones.geojson.zip' |
| 754 | let jsonFile = argv.dist_dir + '/combined.json' |
| 755 | exec('zip ' + zipFile + ' ' + jsonFile, cb) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 756 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 757 | zipGeoJsonWithOceans: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 758 | if (argv.skip_zip) { |
| 759 | overallProgress.beginTask('Skipping with oceans zip') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 760 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 761 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 762 | overallProgress.beginTask('Zipping geojson with oceans') |
| 763 | let zipFile = argv.dist_dir + '/timezones-with-oceans.geojson.zip' |
| 764 | let jsonFile = argv.dist_dir + '/combined-with-oceans.json' |
| 765 | exec('zip ' + zipFile + ' ' + jsonFile, cb) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 766 | }], |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 767 | makeShapefile: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 768 | if (argv.skip_shapefile) { |
| 769 | overallProgress.beginTask('Skipping shapefile creation') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 770 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 771 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 772 | overallProgress.beginTask('Converting from geojson to shapefile') |
| 773 | let shapeFileGlob = argv.dist_dir + '/combined-shapefile.*' |
| 774 | rimraf.sync(shapeFileGlob) |
| 775 | let shapeFile = argv.dist_dir + '/combined-shapefile.shp' |
| 776 | let jsonFile = argv.dist_dir + '/combined.json' |
| 777 | exec( |
| 778 | 'ogr2ogr -f "ESRI Shapefile" ' + shapeFile + ' ' + jsonFile, |
| 779 | function (err, stdout, stderr) { |
| 780 | if (err) { return cb(err) } |
| 781 | let shapeFileZip = argv.dist_dir + '/timezones.shapefile.zip' |
| 782 | exec('zip ' + shapeFileZip + ' ' + shapeFileGlob, cb) |
| 783 | } |
| 784 | ) |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 785 | }], |
| 786 | makeShapefileWithOceans: ['mergeZones', function (results, cb) { |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 787 | if (argv.skip_shapefile) { |
| 788 | overallProgress.beginTask('Skipping with oceans shapefile creation') |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 789 | return cb() |
Neil Fuller | a83cc6d | 2020-04-30 18:37:08 +0100 | [diff] [blame] | 790 | } |
Neil Fuller | 6128709 | 2020-07-29 14:46:16 +0100 | [diff] [blame^] | 791 | overallProgress.beginTask('Converting from geojson with oceans to shapefile') |
| 792 | let shapeFileGlob = argv.dist_dir + '/combined-shapefile-with-oceans.*' |
| 793 | rimraf.sync(shapeFileGlob) |
| 794 | let shapeFile = argv.dist_dir + '/combined-shapefile-with-oceans.shp' |
| 795 | let jsonFile = argv.dist_dir + '/combined-with-oceans.json' |
| 796 | exec( |
| 797 | 'ogr2ogr -f "ESRI Shapefile" ' + shapeFile + ' ' + jsonFile, |
| 798 | function (err, stdout, stderr) { |
| 799 | if (err) { return cb(err) } |
| 800 | let shapeFileZip = argv.dist_dir + '/timezones-with-oceans.shapefile.zip' |
| 801 | exec('zip ' + shapeFileZip + ' ' + shapeFileGlob, cb) |
| 802 | } |
| 803 | ) |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 804 | }], |
| 805 | makeListOfTimeZoneNames: function (cb) { |
| 806 | overallProgress.beginTask('Writing timezone names to file') |
| 807 | let zoneNames = Object.keys(zoneCfg) |
| 808 | oceanZones.forEach(oceanZone => { |
| 809 | zoneNames.push(oceanZone.tzid) |
| 810 | }) |
Neil Fuller | c4ae49b | 2020-04-30 18:08:43 +0100 | [diff] [blame] | 811 | if (includedZones.length > 0) { |
| 812 | zoneNames = zoneNames.filter(zoneName => includedZones.indexOf(zoneName) > -1) |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 813 | } |
Neil Fuller | 2b4b80a | 2020-04-30 18:20:56 +0100 | [diff] [blame] | 814 | if (excludedZones.length > 0) { |
| 815 | zoneNames = zoneNames.filter(zoneName => excludedZones.indexOf(zoneName) === -1) |
| 816 | } |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 817 | fs.writeFile( |
Neil Fuller | a4e7327 | 2020-04-30 18:25:44 +0100 | [diff] [blame] | 818 | argv.dist_dir + '/timezone-names.json', |
evansiroky | 9fd5051 | 2019-07-07 12:06:28 -0700 | [diff] [blame] | 819 | JSON.stringify(zoneNames), |
| 820 | cb |
| 821 | ) |
| 822 | } |
evansiroky | 5348a6f | 2019-01-05 15:39:28 -0800 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | const overallProgress = new ProgressStats('Overall', Object.keys(autoScript).length) |
| 826 | |
| 827 | asynclib.auto(autoScript, function (err, results) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 828 | console.log('done') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 829 | if (err) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 830 | console.log('error!', err) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 831 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 832 | }) |