Created
February 2, 2023 09:03
-
-
Save TheBeachLab/576d267cbdad671fce24622745d6d642 to your computer and use it in GitHub Desktop.
Geocode missing LAT LNG in fablabs.io JSON file
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
// This file is used to add missing coordinates to labs from fablabs.io | |
// by Fran Sanchez (The Beach Lab) February 2023 | |
// Usage: npm install opencage-api-client && node geocoding.cjs | |
// Settings | |
const key = 'YOUR_API_KEY' // opencage geocoder key | |
const labs = require('./labs.json') // labs.json file to fix | |
// require modules | |
const { geocode } = require('opencage-api-client'); | |
const { writeFile } = require('node:fs'); | |
// variables | |
var x = 0 // labs geocoded | |
var save = [] // geocoded array | |
function prepare() { | |
// first pass to add extra fields: needs_geocode / geocoded / geocode_address | |
for (var i = 0; i < labs.length; i++) { | |
// add extra properties | |
labs[i].geocoded = false; | |
labs[i].geocode_address = encodeURIComponent(labs[i].address_1 + ', ' + labs[i].address_2 + ', ' + labs[i].city + ', ' + labs[i].county + ', ' + labs[i].postal_code) | |
// the lab needs geocoding? | |
if (labs[i].latitude == null || labs[i].longitude == null) { | |
labs[i].needs_geocode = true; | |
} | |
else { | |
labs[i].needs_geocode = false; | |
} | |
} | |
// optional write results to a temp file | |
//writeFile('./labs_prepared.json', JSON.stringify(labs), function (err) { if (err) console.log(err);}) | |
} | |
prepare() | |
// for every missing value geocode lat and lng | |
async function geocoder() { | |
for (var i = 0; i < labs.length; i++) { | |
// the lab needs geocoding? | |
if (labs[i].needs_geocode) { | |
const address = labs[i].geocode_address | |
try { | |
const apiResult = await geocode({ q: address , key: key , countrycode: labs[i].country_code}); | |
// API returns a proposal | |
if (apiResult?.results?.length > 0) { | |
labs[i].latitude = apiResult.results[0].geometry.lat; | |
labs[i].longitude = apiResult.results[0].geometry.lng; | |
// mark the lab as geocoded as in handle with care | |
labs[i].geocoded = true; | |
x++ | |
// display some info | |
console.log(labs[i].name,'Lab ID:',labs[i].id, 'Country:',labs[i].country_code,'Latitude, Longitude:',labs[i].latitude, labs[i].longitude) | |
// save the lab into a new array and overwrite the file | |
save.push(labs[i]) | |
writeFile('./labs_geocoded.json', JSON.stringify(save), function (err) { if (err) console.log(err);}) | |
} | |
} | |
catch(error) { | |
console.log('A problem ocurred while geocoding:', error); | |
// if (error.status.code === 402) { | |
// console.log('hit free trial daily limit'); | |
// console.log('become a customer: https://opencagedata.com/pricing'); | |
// } | |
}; | |
} | |
// otherwise the lab does not need geocoding | |
else { | |
// just save this lab as it was | |
save.push(labs[i]) | |
writeFile('./labs_geocoded.json', JSON.stringify(save), function (err) { if (err) console.log(err);}) | |
} | |
} | |
console.log('File saved to labs_geocoded.json. Total new labs on the map:',x) | |
} | |
geocoder() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment