Skip to content

Commit

Permalink
fix(update): correct dl url and resolve longitude 0 zones
Browse files Browse the repository at this point in the history
- updates to use the correct data download url so that data without the oceans is used
- implements a small hack so that antarctic zones along longitude 0 will not be intersected into LineStrings during the geo-index.

fixes #90
  • Loading branch information
evansiroky committed Apr 8, 2019
1 parent 217ef2e commit b59af67
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
Binary file modified data.zip
Binary file not shown.
68 changes: 64 additions & 4 deletions lib/geo-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,50 @@ module.exports = function (tzGeojson, dataDir, targetIndexPercent, callback) {
lookup: {}
}

const timezoneGeometries = tzGeojson.features.map(feature =>
geoJsonReader.read(JSON.stringify(feature.geometry))
)
/**
* iterate through geometry coordinates and change any coordinates along
* longitude 0 to longitude 0.00001
*/
function hackLongitude0Polygon (polygon) {
polygon.forEach(linearRing => {
linearRing.forEach(ringCoords => {
if (ringCoords[0] === 0 && ringCoords[1] < -55) {
ringCoords[0] = 0.00001
}
})
})
}

const timezoneGeometries = tzGeojson.features.map(feature => {
// Perform a quick hack to make sure two Antarctic zones can be indexed
// properly. Each of these zones shares a boundary at longitude 0. During
// the quadtree analysis, the zones were being intersected right aloing
// their boundaries which resulted in LineStrings being returned. This hack
// changes their boundares along longitude 0 to longitude 0.00001 to avoid
// LineStrings being intersected.
if (
feature.properties.tzid === 'Africa/Johannesburg' ||
feature.properties.tzid === 'Antarctica/Troll'
) {
if (feature.geometry.type === 'MultiPolygon') {
feature.geometry.coordinates.forEach(hackLongitude0Polygon)
} else {
hackLongitude0Polygon(feature.geometry.coordinates)
}
}

// load zone into memory as jsts geometry
return geoJsonReader.read(JSON.stringify(feature.geometry))
})

var debugWriteIdx = 1

var writeDebugData = function (filename, geom) {
fs.writeFileSync(
'debug_' + debugWriteIdx + '_' + filename + '.json',
JSON.stringify(geoJsonWriter.write(geom))
)
}

var getIntersectingGeojson = function (tzIdx, curBoundsGeometry) {
// console.log('intersecting', tzGeojson.features[tzIdx].properties)
Expand All @@ -36,6 +77,19 @@ module.exports = function (tzGeojson, dataDir, targetIndexPercent, callback) {
) {
return undefined
} else {
const tzName = data.timezones[tzIdx]
// If the geojson type is not a Polygon or a MultiPolygon, something weird
// is happening and the build should be failed as this will cause issues
// during the find method.
// See https://github.com/evansiroky/node-geo-tz/issues/90.
if (!intersectedGeoJson.type.match(/olyg/)) {
console.log(tzName)
console.log(intersectedGeoJson.type)
writeDebugData('tz', timezoneGeometries[tzIdx])
writeDebugData('curBounds', curBoundsGeometry)
writeDebugData('intersection', intersectedGeometry)
debugWriteIdx++
}
return {
type: 'Feature',
properties: {},
Expand Down Expand Up @@ -355,7 +409,13 @@ module.exports = function (tzGeojson, dataDir, targetIndexPercent, callback) {

fileWritingQueue.drain = function (err) {
console.log('done indexing')
callback(err)
callback(
err || (
debugWriteIdx > 1
? 'At least one unexpected intersected geometry type encountered!'
: null
)
)
}

// write index data to file
Expand Down
5 changes: 3 additions & 2 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ var downloadLatest = function (callback) {
res.on('end', function () {
data = JSON.parse(data)
for (var i = 0; i < data.assets.length; i++) {
data.assets[i].browser_download_url.indexOf('timezones.geojson') > -1
return cb(null, data.assets[i].browser_download_url)
if (data.assets[i].browser_download_url.indexOf('timezones.geojson') > -1) {
return cb(null, data.assets[i].browser_download_url)
}
}
cb('geojson not found')
})
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/issues.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,15 @@
"lat": 22.138993,
"lon": 31.416836,
"description": "#89 - 2019a - test for zone Asia/Khartoum in Sudanese part of Lake Nasser"
}, {
"zid": "America/Adak",
"lat": 52.031192,
"lon": 178.913872,
"description": "#90 - some file-based data was showing up as non-polygon geojson"
}, {
"zids": ["Africa/Johannesburg", "Antarctica/McMurdo", "Antarctica/Troll"],
"lat": -86,
"lon": 0.00001,
"description": "#90 - timezones that share the same corner should all be found when the coordinate corner is queried"
}
]

0 comments on commit b59af67

Please sign in to comment.