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