Created
September 28, 2021 14:07
-
-
Save alecmce/1c13483caf458a4d0e64c8069db4fafe to your computer and use it in GitHub Desktop.
A very rough and ready file for requesting elevation data in a square grid pattern from Google Maps
This file contains hidden or 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 axios = require('axios'); | |
const fs = require('fs') | |
const API_KEY = '[your google API key]' | |
const COMMA = '%2C' | |
const UNIT = 0.0001 // roughly 11.1 meters, +/- 5.55 m | |
const queue = makeLocations([55.9446035, -3.1940965], 10, UNIT * 8) | |
fs.writeFileSync('./data.csv', 'lat,lng,elevation\n') | |
dequeue() | |
// This this requests one location at a time, because the size of the request for all locations | |
// was too big and I didn't want to play around with finding the right chunk size that Google accepted | |
// but it wouldn't take too much work to shift a bunch of locations here and pipe-separate them, then | |
// to map through the results in `parseResponse`. | |
function dequeue() { | |
const location = queue.shift() | |
return makeRequest(location) | |
.then(function (response) { | |
const data = parseResponse(response.data) | |
fs.appendFileSync('./data.csv', data) | |
console.log(data) | |
queue.length && dequeue() | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}) | |
} | |
function makeRequest(location) { | |
return axios({ method: 'get', url: makeUrl(location), headers: {} }) | |
} | |
function makeLocations(center, size, step) { | |
const minLat = center[0] - step * size / 2 | |
const minLng = center[1] - step * size / 2 | |
const locations = [] | |
for (let y = 0; y < size; y++) { | |
for (let x = 0; x < size; x++) { | |
locations.push(encodeLocation(minLat + x * step, minLng + y * step)) | |
} | |
} | |
return locations | |
} | |
function encodeLocation(lat, lng) { | |
return `${lat}${COMMA}${lng}` | |
} | |
function makeUrl(location) { | |
return `https://maps.googleapis.com/maps/api/elevation/json?locations=${location}&key=${API_KEY}` | |
} | |
function parseResponse(response) { | |
const [{ location: { lat, lng }, elevation }] = response.results | |
return `${[lat, lng, elevation].join(',')}\n` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment