Created
July 31, 2017 05:00
-
-
Save swingley/8f332a6b329372a3b75ec1d6681bead1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const client = require('mongodb').MongoClient | |
const turf = require('@turf/turf') | |
const Table = require('easy-table') | |
const ncdcConnection = 'mongodb://localhost:27017/ncdc' | |
const ncdcCollection = 'normals' | |
let geojsonThreadex = 'json/threadex.geojson' | |
let stationsThreadex = JSON.parse(fs.readFileSync(geojsonThreadex)) | |
let stationCount = stationsThreadex.features.length | |
let queried = 0 | |
let allDistances = [] | |
let distanceTable = new Table() | |
let getNormals = (station, cb) => { | |
// console.log(station) | |
client.connect(ncdcConnection, (err, db) => { | |
// console.log(`station... ${station.properties.name}`) | |
let collection = db.collection(ncdcCollection) | |
let [lon, lat] = station.geometry.coordinates | |
collection.geoNear(lon, lat, { num: 1, spherical: true}, (err, docs) => { | |
db.close() | |
if ( docs ) { | |
let { properties } = docs.results[0].obj | |
// console.log(properties) | |
let distance = parseInt(turf.distance(station, docs.results[0].obj, 'miles') * 100) / 100 | |
allDistances.push(distance) | |
distanceTable.cell('ThreadEx', station.properties.name) | |
distanceTable.cell('NCDC', docs.results[0].obj.properties.name) | |
distanceTable.cell('Distance', distance) | |
distanceTable.newRow() | |
} else { | |
console.log(`${queried}, \tno results for ${station}`) | |
} | |
queried += 1 | |
if ( queried < stationCount ) { | |
processData() | |
} else { | |
let maxDistance = Math.max.apply(null, allDistances) | |
console.log(distanceTable.toString()) | |
console.log(`\nMax distance: ${maxDistance}\nFinished.`) | |
} | |
}) | |
}) | |
} | |
let processData = () => { | |
let next = stationsThreadex.features[queried] | |
getNormals(next, processData); | |
} | |
processData() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment