Skip to content

Instantly share code, notes, and snippets.

@mudssrali
Created November 23, 2022 03:39
Show Gist options
  • Save mudssrali/8f48a3690ef74a705a835b434a94805e to your computer and use it in GitHub Desktop.
Save mudssrali/8f48a3690ef74a705a835b434a94805e to your computer and use it in GitHub Desktop.
Read addresses from Pk-Cashpoints CSV and try to get geo-codes using Geolocation API
const fs = require('fs')
const axios = require('axios');
const Papa = require("papaparse");
fs.readFile('sample.csv', 'utf8', (err, data) => {
if (err) {
return console.error(err)
}
const [headers, ...parsedCSVData] = Papa.parse(data).data
console.debug("done reading csv")
const locations = parsedCSVData.map(item => {
const [
_name,
_city,
address,
_lat,
_lng
] = item
return getGeoCodes(address)
})
Promise.allSettled(locations).then(results => {
// Here we need to treverse each parsed CSV data item
// using index, we will update each item's address from
// fullfilled responses for each address
const cashpoints = parsedCSVData
.map((item, index) => {
const result = results[index]
let location = { lat: '', lng: '' }
if (result.status == 'fulfilled') {
location = result.value
}
const [
name,
city,
address,
_lat,
_lng
] = item
return {
Name: name,
City: city,
Address: address,
Lat: location.lat,
Lng: location.lng
}
})
// unparse
const csv = Papa.unparse(cashpoints, {
columns: headers
})
fs.writeFile('output-with-geocodes.csv', csv, err => {
if (err) {
console.error(err);
}
// file written successfully
});
console.debug("done writing output csv")
})
.catch(error => {
console.log(error)
})
})
const getGeoCodes = async (address) => {
const apiKey = ""
// const encodedAddress = encodeURIComponent(address)
console.debug("getting geocodes for address " + address)
const resp = await axios.get(`https://maps.google.com/maps/api/geocode/json?address=${address}&key=${apiKey}`).then(resp => {
// console the data
// console.debug(resp.data)
const {
status,
results
} = resp.data
// console.log(results)
// CASE 1
//
// If the API results in any of following
//
// {
// "results": [],
// "status": "ZERO_RESULTS"
// },
// {
// "error_message": "The provided API key is invalid. ",
// "results": [],
// "status": "REQUEST_DENIED"
// }
//
// which means there's no location exist for the given address, so simply
// return empty lat and lng
if (status === "ZERO_RESULTS" || status === "REQUEST_DENIED") {
return { lat: '', lng: '' }
}
// If the status is "ok", we will get something in data as below
//
// {
// "results": [...]
// "status": "OK"
// }
// Process the data to get Lat and Lng, tricky part is Geolocation API
// return multiple search results when it does't find exact one So we need to process
// response to get the Lat and Lng
// CASE 2
//
// Results is an array containing object with different properties see *example-address-geolocation-api.json*
// (as a sample output "lady reading hospital peshawar city"). If the result contains only one item (object)
// which we don't need to write any sort of complex logic to pull the matching location (lat, lng).
if (results.length === 1) {
return results[0].geometry.location
}
// CASE 3
//
// If results contains more than one location geolocation information, we need to process according to our
// passing address. Living it empty for now.
}).catch(error => {
console.error(error);
});
// return response
return resp
}
@mudssrali
Copy link
Author

Same input (CSV):

Name,City,Address,Lat,Lng
Mudassar,Lahore,Rabbani Masjid Lahore Cantt,,
CERP,Lahore,29-P mustaq ahmed gurmani road Gulberg-2 Lahore,, 

Output (CSV)

Name,City,Address,Lat,Lng
Mudassar,Lahore,Rabbani Masjid Lahore Cantt,31.4946798,74.3907455
CERP,Lahore,29-P mustaq ahmed gurmani road Gulberg-2 Lahore,31.5235824,74.352457

@mudssrali
Copy link
Author

To run above script, make sure nodejs along with npm is installed on your machine (laptop, computer), then run the following commands.

npm install papaparse axios (install dependencies)
node ./geocodes (to run the script)

Note: Make sure you've put API_KEY in the script code and change input CSV file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment