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') |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 14 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 15 | var osmBoundarySources = require('./osmBoundarySources.json') |
| 16 | var zoneCfg = require('./timezones.json') |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame^] | 17 | var expectedZoneOverlaps = require('./expectedZoneOverlaps.json') |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 18 | |
| 19 | // allow building of only a specified zones |
| 20 | var filteredIndex = process.argv.indexOf('--filtered-zones') |
| 21 | if (filteredIndex > -1 && process.argv[filteredIndex + 1]) { |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame^] | 22 | const filteredZones = process.argv[filteredIndex + 1].split(',') |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 23 | var newZoneCfg = {} |
| 24 | filteredZones.forEach((zoneName) => { |
| 25 | newZoneCfg[zoneName] = zoneCfg[zoneName] |
| 26 | }) |
| 27 | zoneCfg = newZoneCfg |
| 28 | |
| 29 | // filter out unneccessary downloads |
| 30 | var newOsmBoundarySources = {} |
| 31 | Object.keys(zoneCfg).forEach((zoneName) => { |
| 32 | zoneCfg[zoneName].forEach((op) => { |
| 33 | if (op.source === 'overpass') { |
| 34 | newOsmBoundarySources[op.id] = osmBoundarySources[op.id] |
| 35 | } |
| 36 | }) |
| 37 | }) |
| 38 | |
| 39 | osmBoundarySources = newOsmBoundarySources |
| 40 | } |
| 41 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 42 | var geoJsonReader = new jsts.io.GeoJSONReader() |
| 43 | var geoJsonWriter = new jsts.io.GeoJSONWriter() |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 44 | var precisionModel = new jsts.geom.PrecisionModel(1000000) |
| 45 | var precisionReducer = new jsts.precision.GeometryPrecisionReducer(precisionModel) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 46 | var distZones = {} |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 47 | var minRequestGap = 4 |
| 48 | var curRequestGap = 4 |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 49 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 50 | var safeMkdir = function (dirname, callback) { |
| 51 | fs.mkdir(dirname, function (err) { |
| 52 | if (err && err.code === 'EEXIST') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 53 | callback() |
| 54 | } else { |
| 55 | callback(err) |
| 56 | } |
| 57 | }) |
| 58 | } |
| 59 | |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 60 | var debugGeo = function (op, a, b, reducePrecision) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 61 | var result |
| 62 | |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 63 | if (reducePrecision) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 64 | a = precisionReducer.reduce(a) |
| 65 | b = precisionReducer.reduce(b) |
| 66 | } |
| 67 | |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 68 | try { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 69 | switch (op) { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 70 | case 'union': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 71 | result = a.union(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 72 | break |
| 73 | case 'intersection': |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 74 | result = a.intersection(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 75 | break |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 76 | case 'intersects': |
| 77 | result = a.intersects(b) |
| 78 | break |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 79 | case 'diff': |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 80 | result = a.difference(b) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 81 | break |
| 82 | default: |
| 83 | var err = new Error('invalid op: ' + op) |
| 84 | throw err |
| 85 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 86 | } catch (e) { |
Evan Siroky | b173fd4 | 2017-03-08 15:16:27 -0800 | [diff] [blame] | 87 | if (e.name === 'TopologyException') { |
| 88 | console.log('Encountered TopologyException, retry with GeometryPrecisionReducer') |
| 89 | return debugGeo(op, a, b, true) |
| 90 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 91 | console.log('op err') |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 92 | console.log(e) |
| 93 | console.log(e.stack) |
| 94 | fs.writeFileSync('debug_' + op + '_a.json', JSON.stringify(geoJsonWriter.write(a))) |
| 95 | fs.writeFileSync('debug_' + op + '_b.json', JSON.stringify(geoJsonWriter.write(b))) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 96 | throw e |
| 97 | } |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 98 | |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 99 | return result |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 102 | var fetchIfNeeded = function (file, superCallback, downloadCallback, fetchFn) { |
| 103 | // check for file that got downloaded |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 104 | fs.stat(file, function (err) { |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 105 | if (!err) { |
| 106 | // file found, skip download steps |
| 107 | return superCallback() |
| 108 | } |
| 109 | // check for manual file that got fixed and needs validation |
| 110 | var fixedFile = file.replace('.json', '_fixed.json') |
| 111 | fs.stat(fixedFile, function (err) { |
| 112 | if (!err) { |
| 113 | // file found, return fixed file |
| 114 | return downloadCallback(null, require(fixedFile)) |
| 115 | } |
| 116 | // no manual fixed file found, download from overpass |
| 117 | fetchFn() |
| 118 | }) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 119 | }) |
| 120 | } |
| 121 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 122 | var geoJsonToGeom = function (geoJson) { |
Evan Siroky | 8326cf0 | 2017-03-02 08:27:55 -0800 | [diff] [blame] | 123 | try { |
| 124 | return geoJsonReader.read(JSON.stringify(geoJson)) |
| 125 | } catch (e) { |
| 126 | console.error('error converting geojson to geometry') |
| 127 | fs.writeFileSync('debug_geojson_read_error.json', JSON.stringify(geoJson)) |
| 128 | throw e |
| 129 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 132 | var geomToGeoJson = function (geom) { |
| 133 | return geoJsonWriter.write(geom) |
| 134 | } |
| 135 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 136 | var geomToGeoJsonString = function (geom) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 137 | return JSON.stringify(geoJsonWriter.write(geom)) |
| 138 | } |
| 139 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 140 | var downloadOsmBoundary = function (boundaryId, boundaryCallback) { |
| 141 | var cfg = osmBoundarySources[boundaryId] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 142 | var query = '[out:json][timeout:60];(' |
| 143 | if (cfg.way) { |
| 144 | query += 'way' |
| 145 | } else { |
| 146 | query += 'relation' |
| 147 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 148 | var boundaryFilename = './downloads/' + boundaryId + '.json' |
| 149 | var debug = 'getting data for ' + boundaryId |
| 150 | var queryKeys = Object.keys(cfg) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 151 | |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 152 | for (var i = queryKeys.length - 1; i >= 0; i--) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 153 | var k = queryKeys[i] |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 154 | if (k === 'way') continue |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 155 | var v = cfg[k] |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 156 | |
| 157 | query += '["' + k + '"="' + v + '"]' |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 158 | } |
| 159 | |
evansiroky | 283ebbc | 2018-07-16 15:13:07 -0700 | [diff] [blame] | 160 | query += ';);out body;>;out meta qt;' |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 161 | |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 162 | console.log(debug) |
| 163 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 164 | asynclib.auto({ |
| 165 | downloadFromOverpass: function (cb) { |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 166 | console.log('downloading from overpass') |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 167 | fetchIfNeeded(boundaryFilename, boundaryCallback, cb, function () { |
Evan Siroky | b57a5b9 | 2016-11-07 10:22:34 -0800 | [diff] [blame] | 168 | var overpassResponseHandler = function (err, data) { |
| 169 | if (err) { |
| 170 | console.log(err) |
| 171 | console.log('Increasing overpass request gap') |
| 172 | curRequestGap *= 2 |
| 173 | makeQuery() |
| 174 | } else { |
| 175 | console.log('Success, decreasing overpass request gap') |
| 176 | curRequestGap = Math.max(minRequestGap, curRequestGap / 2) |
| 177 | cb(null, data) |
| 178 | } |
| 179 | } |
| 180 | var makeQuery = function () { |
| 181 | console.log('waiting ' + curRequestGap + ' seconds') |
| 182 | setTimeout(function () { |
| 183 | overpass(query, overpassResponseHandler, { flatProperties: true }) |
| 184 | }, curRequestGap * 1000) |
| 185 | } |
| 186 | makeQuery() |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 187 | }) |
| 188 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 189 | validateOverpassResult: ['downloadFromOverpass', function (results, cb) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 190 | var data = results.downloadFromOverpass |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 191 | if (!data.features) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 192 | var err = new Error('Invalid geojson for boundary: ' + boundaryId) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 193 | return cb(err) |
| 194 | } |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 195 | if (data.features.length === 0) { |
| 196 | console.error('No data for the following query:') |
| 197 | console.error(query) |
| 198 | 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^] | 199 | return cb(new Error('No data found for from overpass query')) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 200 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 201 | cb() |
| 202 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 203 | saveSingleMultiPolygon: ['validateOverpassResult', function (results, cb) { |
| 204 | var data = results.downloadFromOverpass |
| 205 | var combined |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 206 | |
| 207 | // union all multi-polygons / polygons into one |
| 208 | for (var i = data.features.length - 1; i >= 0; i--) { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 209 | var curOsmGeom = data.features[i].geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 210 | if (curOsmGeom.type === 'Polygon' || curOsmGeom.type === 'MultiPolygon') { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 211 | console.log('combining border') |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 212 | let errors = geojsonhint.hint(curOsmGeom) |
| 213 | if (errors && errors.length > 0) { |
| 214 | const stringifiedGeojson = JSON.stringify(curOsmGeom, null, 2) |
| 215 | errors = geojsonhint.hint(stringifiedGeojson) |
| 216 | console.error('Invalid geojson received in Overpass Result') |
| 217 | console.error('Overpass query: ' + query) |
| 218 | const problemFilename = boundaryId + '_convert_to_geom_error.json' |
| 219 | fs.writeFileSync(problemFilename, stringifiedGeojson) |
| 220 | console.error('saved problem file to ' + problemFilename) |
| 221 | console.error('To read more about this error, please visit https://git.io/vxKQq') |
| 222 | return cb(errors) |
| 223 | } |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 224 | try { |
| 225 | var curGeom = geoJsonToGeom(curOsmGeom) |
| 226 | } catch (e) { |
| 227 | console.error('error converting overpass result to geojson') |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 228 | console.error(e) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 229 | |
Evan Siroky | 1bcd477 | 2017-10-14 23:47:21 -0700 | [diff] [blame] | 230 | fs.writeFileSync(boundaryId + '_convert_to_geom_error-all-features.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 231 | return cb(e) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 232 | } |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 233 | if (!combined) { |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 234 | combined = curGeom |
| 235 | } else { |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 236 | combined = debugGeo('union', curGeom, combined) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 240 | try { |
| 241 | fs.writeFile(boundaryFilename, geomToGeoJsonString(combined), cb) |
| 242 | } catch (e) { |
| 243 | console.error('error writing combined border to geojson') |
| 244 | fs.writeFileSync(boundaryId + '_combined_border_convert_to_geom_error.json', JSON.stringify(data)) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 245 | return cb(e) |
Evan Siroky | 081c8e4 | 2017-05-29 14:53:52 -0700 | [diff] [blame] | 246 | } |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 247 | }] |
| 248 | }, boundaryCallback) |
| 249 | } |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 250 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 251 | var getTzDistFilename = function (tzid) { |
| 252 | return './dist/' + tzid.replace(/\//g, '__') + '.json' |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get the geometry of the requested source data |
| 257 | * |
| 258 | * @return {Object} geom The geometry of the source |
| 259 | * @param {Object} source An object representing the data source |
| 260 | * must have `source` key and then either: |
| 261 | * - `id` if from a file |
| 262 | * - `id` if from a file |
| 263 | */ |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 264 | var getDataSource = function (source) { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 265 | var geoJson |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 266 | if (source.source === 'overpass') { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 267 | geoJson = require('./downloads/' + source.id + '.json') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 268 | } else if (source.source === 'manual-polygon') { |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 269 | geoJson = polygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 270 | } else if (source.source === 'manual-multipolygon') { |
Evan Siroky | 8e30a2e | 2016-08-06 19:55:35 -0700 | [diff] [blame] | 271 | geoJson = multiPolygon(source.data).geometry |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 272 | } else if (source.source === 'dist') { |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 273 | geoJson = require(getTzDistFilename(source.id)) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 274 | } else { |
| 275 | var err = new Error('unknown source: ' + source.source) |
| 276 | throw err |
| 277 | } |
Evan Siroky | 5669adc | 2016-07-07 17:25:31 -0700 | [diff] [blame] | 278 | return geoJsonToGeom(geoJson) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Post process created timezone boundary. |
| 283 | * - remove small holes and exclaves |
| 284 | * - reduce geometry precision |
| 285 | * |
| 286 | * @param {Geometry} geom The jsts geometry of the timezone |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 287 | * @param {boolean} returnAsObject if true, return as object, otherwise return stringified |
| 288 | * @return {Object|String} geojson as object or stringified |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 289 | */ |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 290 | var postProcessZone = function (geom, returnAsObject) { |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 291 | // reduce precision of geometry |
| 292 | const geojson = geomToGeoJson(precisionReducer.reduce(geom)) |
| 293 | |
| 294 | // iterate through all polygons |
| 295 | const filteredPolygons = [] |
| 296 | let allPolygons = geojson.coordinates |
| 297 | if (geojson.type === 'Polygon') { |
| 298 | allPolygons = [geojson.coordinates] |
| 299 | } |
| 300 | |
| 301 | allPolygons.forEach((curPolygon, idx) => { |
| 302 | // remove any polygon with very small area |
| 303 | const polygonFeature = polygon(curPolygon) |
| 304 | const polygonArea = area.geometry(polygonFeature.geometry) |
| 305 | |
| 306 | if (polygonArea < 1) return |
| 307 | |
| 308 | // find all holes |
| 309 | const filteredLinearRings = [] |
| 310 | |
| 311 | curPolygon.forEach((curLinearRing, lrIdx) => { |
| 312 | if (lrIdx === 0) { |
| 313 | // always keep first linearRing |
| 314 | filteredLinearRings.push(curLinearRing) |
| 315 | } else { |
| 316 | const polygonFromLinearRing = polygon([curLinearRing]) |
| 317 | const linearRingArea = area.geometry(polygonFromLinearRing.geometry) |
| 318 | |
| 319 | // only include holes with relevant area |
| 320 | if (linearRingArea > 1) { |
| 321 | filteredLinearRings.push(curLinearRing) |
| 322 | } |
| 323 | } |
| 324 | }) |
| 325 | |
| 326 | filteredPolygons.push(filteredLinearRings) |
| 327 | }) |
| 328 | |
| 329 | // recompile to geojson string |
| 330 | const newGeojson = { |
| 331 | type: geojson.type |
| 332 | } |
| 333 | |
| 334 | if (geojson.type === 'Polygon') { |
| 335 | newGeojson.coordinates = filteredPolygons[0] |
| 336 | } else { |
| 337 | newGeojson.coordinates = filteredPolygons |
| 338 | } |
| 339 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 340 | return returnAsObject ? newGeojson : JSON.stringify(newGeojson) |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 343 | var makeTimezoneBoundary = function (tzid, callback) { |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 344 | console.log('makeTimezoneBoundary for', tzid) |
| 345 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 346 | var ops = zoneCfg[tzid] |
| 347 | var geom |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 348 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 349 | asynclib.eachSeries(ops, function (task, cb) { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 350 | var taskData = getDataSource(task) |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 351 | console.log('-', task.op, task.id) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 352 | if (task.op === 'init') { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 353 | geom = taskData |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 354 | } else if (task.op === 'intersect') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 355 | geom = debugGeo('intersection', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 356 | } else if (task.op === 'difference') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 357 | geom = debugGeo('diff', geom, taskData) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 358 | } else if (task.op === 'difference-reverse-order') { |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 359 | geom = debugGeo('diff', taskData, geom) |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 360 | } else if (task.op === 'union') { |
evansiroky | 6f9d8f7 | 2016-06-21 16:27:54 -0700 | [diff] [blame] | 361 | geom = debugGeo('union', geom, taskData) |
Evan Siroky | 8ccaf0b | 2016-09-03 11:36:13 -0700 | [diff] [blame] | 362 | } else { |
| 363 | var err = new Error('unknown op: ' + task.op) |
| 364 | return cb(err) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 365 | } |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 366 | cb() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 367 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 368 | function (err) { |
| 369 | if (err) { return callback(err) } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 370 | fs.writeFile(getTzDistFilename(tzid), |
Evan Siroky | 477ece6 | 2017-08-01 07:08:51 -0700 | [diff] [blame] | 371 | postProcessZone(geom), |
evansiroky | becb56e | 2016-07-06 12:42:35 -0700 | [diff] [blame] | 372 | callback) |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 373 | }) |
| 374 | } |
| 375 | |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 376 | var loadDistZonesIntoMemory = function () { |
| 377 | console.log('load zones into memory') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 378 | var zones = Object.keys(zoneCfg) |
| 379 | var tzid |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 380 | |
| 381 | for (var i = 0; i < zones.length; i++) { |
| 382 | tzid = zones[i] |
| 383 | distZones[tzid] = getDataSource({ source: 'dist', id: tzid }) |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | var getDistZoneGeom = function (tzid) { |
| 388 | return distZones[tzid] |
| 389 | } |
| 390 | |
| 391 | var validateTimezoneBoundaries = function () { |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 392 | console.log('do validation... this may take a few minutes') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 393 | var allZonesOk = true |
| 394 | var zones = Object.keys(zoneCfg) |
| 395 | var compareTzid, tzid, zoneGeom |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 396 | |
| 397 | for (var i = 0; i < zones.length; i++) { |
| 398 | tzid = zones[i] |
| 399 | zoneGeom = getDistZoneGeom(tzid) |
| 400 | |
| 401 | for (var j = i + 1; j < zones.length; j++) { |
| 402 | compareTzid = zones[j] |
| 403 | |
| 404 | var compareZoneGeom = getDistZoneGeom(compareTzid) |
Evan Siroky | 070bbb9 | 2017-03-07 23:48:29 -0800 | [diff] [blame] | 405 | |
| 406 | var intersects = false |
| 407 | try { |
| 408 | intersects = debugGeo('intersects', zoneGeom, compareZoneGeom) |
| 409 | } catch (e) { |
| 410 | console.warn('warning, encountered intersection error with zone ' + tzid + ' and ' + compareTzid) |
| 411 | } |
| 412 | if (intersects) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 413 | var intersectedGeom = debugGeo('intersection', zoneGeom, compareZoneGeom) |
| 414 | var intersectedArea = intersectedGeom.getArea() |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 415 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 416 | if (intersectedArea > 0.0001) { |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame^] | 417 | // check if the intersected area(s) are one of the expected areas of overlap |
| 418 | const allowedOverlapBounds = expectedZoneOverlaps[`${tzid}-${compareTzid}`] || expectedZoneOverlaps[`${compareTzid}-${tzid}`] |
| 419 | const overlapsGeoJson = geoJsonWriter.write(intersectedGeom) |
| 420 | |
| 421 | // these zones are allowed to overlap in certain places, make sure the |
| 422 | // found overlap(s) all fit within the expected areas of overlap |
| 423 | if (allowedOverlapBounds) { |
| 424 | // if the overlaps are a multipolygon, make sure each individual |
| 425 | // polygon of overlap fits within at least one of the expected |
| 426 | // overlaps |
| 427 | let overlapsPolygons |
| 428 | switch (overlapsGeoJson.type) { |
| 429 | case 'Polygon': |
| 430 | overlapsPolygons = [overlapsGeoJson] |
| 431 | break |
| 432 | default: |
| 433 | break |
| 434 | } |
| 435 | |
| 436 | let allOverlapsOk = true |
| 437 | overlapsPolygons.forEach((polygon, idx) => { |
| 438 | const bounds = bbox(polygon) |
| 439 | if ( |
| 440 | !allowedOverlapBounds.some(allowedBounds => |
| 441 | allowedBounds[0] <= bounds[0] && // minX |
| 442 | allowedBounds[1] <= bounds[1] && // minY |
| 443 | allowedBounds[2] >= bounds[2] && // maxX |
| 444 | allowedBounds[3] >= bounds[3] // maxY |
| 445 | ) |
| 446 | ) { |
| 447 | console.error('Unexpected intersection with bounds: ', bounds) |
| 448 | allOverlapsOk = false |
| 449 | } |
| 450 | }) |
| 451 | |
| 452 | if (allOverlapsOk) continue |
| 453 | } |
| 454 | |
| 455 | // not an expected overlap, output an error |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 456 | console.error('Validation error: ' + tzid + ' intersects ' + compareTzid + ' area: ' + intersectedArea) |
| 457 | const debugFilename = tzid.replace('/', '-') + '-' + compareTzid.replace('/', '-') + '-overlap.json' |
| 458 | fs.writeFileSync( |
| 459 | debugFilename, |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame^] | 460 | JSON.stringify(overlapsGeoJson) |
evansiroky | 70b35fe | 2018-04-01 21:06:36 -0700 | [diff] [blame] | 461 | ) |
| 462 | console.error('wrote overlap area as file ' + debugFilename) |
| 463 | 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] | 464 | allZonesOk = false |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return allZonesOk ? null : 'Zone validation unsuccessful' |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 471 | } |
| 472 | |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 473 | let oceanZoneBoundaries |
| 474 | |
| 475 | var addOceans = function (callback) { |
| 476 | console.log('adding ocean boundaries') |
| 477 | const oceanZones = [ |
| 478 | { tzid: 'Etc/GMT-12', left: 172.5, right: 180 }, |
| 479 | { tzid: 'Etc/GMT-11', left: 157.5, right: 172.5 }, |
| 480 | { tzid: 'Etc/GMT-10', left: 142.5, right: 157.5 }, |
| 481 | { tzid: 'Etc/GMT-9', left: 127.5, right: 142.5 }, |
| 482 | { tzid: 'Etc/GMT-8', left: 112.5, right: 127.5 }, |
| 483 | { tzid: 'Etc/GMT-7', left: 97.5, right: 112.5 }, |
| 484 | { tzid: 'Etc/GMT-6', left: 82.5, right: 97.5 }, |
| 485 | { tzid: 'Etc/GMT-5', left: 67.5, right: 82.5 }, |
| 486 | { tzid: 'Etc/GMT-4', left: 52.5, right: 67.5 }, |
| 487 | { tzid: 'Etc/GMT-3', left: 37.5, right: 52.5 }, |
| 488 | { tzid: 'Etc/GMT-2', left: 22.5, right: 37.5 }, |
| 489 | { tzid: 'Etc/GMT-1', left: 7.5, right: 22.5 }, |
| 490 | { tzid: 'Etc/GMT', left: -7.5, right: 7.5 }, |
| 491 | { tzid: 'Etc/GMT+1', left: -22.5, right: -7.5 }, |
| 492 | { tzid: 'Etc/GMT+2', left: -37.5, right: -22.5 }, |
| 493 | { tzid: 'Etc/GMT+3', left: -52.5, right: -37.5 }, |
| 494 | { tzid: 'Etc/GMT+4', left: -67.5, right: -52.5 }, |
| 495 | { tzid: 'Etc/GMT+5', left: -82.5, right: -67.5 }, |
| 496 | { tzid: 'Etc/GMT+6', left: -97.5, right: -82.5 }, |
| 497 | { tzid: 'Etc/GMT+7', left: -112.5, right: -97.5 }, |
| 498 | { tzid: 'Etc/GMT+8', left: -127.5, right: -112.5 }, |
| 499 | { tzid: 'Etc/GMT+9', left: -142.5, right: -127.5 }, |
| 500 | { tzid: 'Etc/GMT+10', left: -157.5, right: -142.5 }, |
| 501 | { tzid: 'Etc/GMT+11', left: -172.5, right: -157.5 }, |
| 502 | { tzid: 'Etc/GMT+12', left: -180, right: -172.5 } |
| 503 | ] |
| 504 | |
| 505 | const zones = Object.keys(zoneCfg) |
| 506 | |
| 507 | oceanZoneBoundaries = oceanZones.map(zone => { |
| 508 | console.log(zone.tzid) |
| 509 | const geoJson = polygon([[ |
| 510 | [zone.left, 90], |
evansiroky | 0ea1d1e | 2018-10-30 22:30:51 -0700 | [diff] [blame^] | 511 | [zone.left, -90], |
| 512 | [zone.right, -90], |
| 513 | [zone.right, 90], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 514 | [zone.left, 90] |
| 515 | ]]).geometry |
| 516 | |
| 517 | let geom = geoJsonToGeom(geoJson) |
| 518 | |
| 519 | // diff against every zone |
| 520 | zones.forEach(distZone => { |
| 521 | geom = debugGeo('diff', geom, getDistZoneGeom(distZone)) |
| 522 | }) |
| 523 | |
| 524 | return { |
| 525 | geom: postProcessZone(geom, true), |
| 526 | tzid: zone.tzid |
| 527 | } |
| 528 | }) |
| 529 | |
| 530 | callback() |
| 531 | } |
| 532 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 533 | var combineAndWriteZones = function (callback) { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 534 | var stream = fs.createWriteStream('./dist/combined.json') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 535 | var streamWithOceans = fs.createWriteStream('./dist/combined-with-oceans.json') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 536 | var zones = Object.keys(zoneCfg) |
| 537 | |
| 538 | stream.write('{"type":"FeatureCollection","features":[') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 539 | streamWithOceans.write('{"type":"FeatureCollection","features":[') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 540 | |
| 541 | for (var i = 0; i < zones.length; i++) { |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 542 | if (i > 0) { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 543 | stream.write(',') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 544 | streamWithOceans.write(',') |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 545 | } |
| 546 | var feature = { |
| 547 | type: 'Feature', |
| 548 | properties: { tzid: zones[i] }, |
| 549 | geometry: geomToGeoJson(getDistZoneGeom(zones[i])) |
| 550 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 551 | const stringified = JSON.stringify(feature) |
| 552 | stream.write(stringified) |
| 553 | streamWithOceans.write(stringified) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 554 | } |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 555 | oceanZoneBoundaries.forEach(boundary => { |
| 556 | streamWithOceans.write(',') |
| 557 | var feature = { |
| 558 | type: 'Feature', |
| 559 | properties: { tzid: boundary.tzid }, |
| 560 | geometry: boundary.geom |
| 561 | } |
| 562 | streamWithOceans.write(JSON.stringify(feature)) |
| 563 | }) |
| 564 | asynclib.parallel([ |
| 565 | cb => { |
| 566 | stream.end(']}', cb) |
| 567 | }, |
| 568 | cb => { |
| 569 | streamWithOceans.end(']}', cb) |
| 570 | } |
| 571 | ], callback) |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 574 | asynclib.auto({ |
| 575 | makeDownloadsDir: function (cb) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 576 | console.log('creating downloads dir') |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 577 | safeMkdir('./downloads', cb) |
| 578 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 579 | makeDistDir: function (cb) { |
evansiroky | 4be1c7a | 2016-06-16 18:23:34 -0700 | [diff] [blame] | 580 | console.log('createing dist dir') |
| 581 | safeMkdir('./dist', cb) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 582 | }, |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 583 | getOsmBoundaries: ['makeDownloadsDir', function (results, cb) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 584 | console.log('downloading osm boundaries') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 585 | asynclib.eachSeries(Object.keys(osmBoundarySources), downloadOsmBoundary, cb) |
evansiroky | 63d35e1 | 2016-06-16 10:08:15 -0700 | [diff] [blame] | 586 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 587 | createZones: ['makeDistDir', 'getOsmBoundaries', function (results, cb) { |
evansiroky | 35f6434 | 2016-06-16 22:17:04 -0700 | [diff] [blame] | 588 | console.log('createZones') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 589 | asynclib.each(Object.keys(zoneCfg), makeTimezoneBoundary, cb) |
evansiroky | 50216c6 | 2016-06-16 17:41:47 -0700 | [diff] [blame] | 590 | }], |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 591 | validateZones: ['createZones', function (results, cb) { |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 592 | console.log('validating zones') |
| 593 | loadDistZonesIntoMemory() |
Evan Siroky | 081648a | 2017-07-04 09:53:36 -0700 | [diff] [blame] | 594 | if (process.argv.indexOf('no-validation') > -1) { |
| 595 | console.warn('WARNING: Skipping validation!') |
| 596 | cb() |
| 597 | } else { |
| 598 | cb(validateTimezoneBoundaries()) |
| 599 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 600 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 601 | addOceans: ['validateZones', function (results, cb) { |
| 602 | addOceans(cb) |
| 603 | }], |
| 604 | mergeZones: ['addOceans', function (results, cb) { |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 605 | console.log('merge zones') |
| 606 | combineAndWriteZones(cb) |
| 607 | }], |
| 608 | zipGeoJson: ['mergeZones', function (results, cb) { |
| 609 | console.log('zip geojson') |
| 610 | exec('zip dist/timezones.geojson.zip dist/combined.json', cb) |
| 611 | }], |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 612 | zipGeoJsonWithOceans: ['mergeZones', function (results, cb) { |
| 613 | console.log('zip geojson with oceans') |
| 614 | exec('zip dist/timezones-with-oceans.geojson.zip dist/combined-with-oceans.json', cb) |
| 615 | }], |
Evan Siroky | 8b47abe | 2016-10-02 12:28:52 -0700 | [diff] [blame] | 616 | makeShapefile: ['mergeZones', function (results, cb) { |
| 617 | console.log('convert from geojson to shapefile') |
evansiroky | 2632584 | 2018-04-03 14:10:42 -0700 | [diff] [blame] | 618 | rimraf.sync('dist/combined-shapefile.*') |
| 619 | exec( |
| 620 | 'ogr2ogr -nlt MULTIPOLYGON dist/combined-shapefile.shp dist/combined.json OGRGeoJSON', |
| 621 | function (err, stdout, stderr) { |
| 622 | if (err) { return cb(err) } |
| 623 | exec('zip dist/timezones.shapefile.zip dist/combined-shapefile.*', cb) |
| 624 | } |
| 625 | ) |
| 626 | }], |
| 627 | makeShapefileWithOceans: ['mergeZones', function (results, cb) { |
| 628 | console.log('convert from geojson with oceans to shapefile') |
| 629 | rimraf.sync('dist/combined-shapefile-with-oceans.*') |
| 630 | exec( |
| 631 | 'ogr2ogr -nlt MULTIPOLYGON dist/combined-shapefile-with-oceans.shp dist/combined-with-oceans.json OGRGeoJSON', |
| 632 | function (err, stdout, stderr) { |
| 633 | if (err) { return cb(err) } |
| 634 | exec('zip dist/timezones-with-oceans.shapefile.zip dist/combined-shapefile-with-oceans.*', cb) |
| 635 | } |
| 636 | ) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 637 | }] |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 638 | }, function (err, results) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 639 | console.log('done') |
Evan Siroky | 7891a6e | 2016-11-05 11:50:50 -0700 | [diff] [blame] | 640 | if (err) { |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 641 | console.log('error!', err) |
evansiroky | d401c89 | 2016-06-16 00:05:14 -0700 | [diff] [blame] | 642 | } |
Evan Siroky | 4fc596c | 2016-09-25 19:52:30 -0700 | [diff] [blame] | 643 | }) |