Last time I talked about GIS (geographical information system)
Today I will talk about a tiny part of GIS
Geocoding is the process of transforming an address to a location
on the Earth's surface.
Reverse geocoding, on the other hand, converts geographic
coordinates to a description of a location, usually the name of a
place or an addressable location.
wikipedia
we need a plan
not a map, a plan
an architecture diagram
get a Google API key (done)
get an AWS account (done)
create a project
Hurry up!
$ npx serverless create --template aws-nodejs --path whereis
$ cd whereis
$ npm init -y
$ npm i -S axios dotenv
# $ npm i -D serverless serverless-offline
service: whereis # NOTE: update this with your service name
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: eu-west-2
plugins:
- serverless-offline # serverless-offline needs to be last in the list
functions:
whereIs:
handler: handler.whereIs
events:
- http:
path: whereis
method: get
cors: true
$ echo "GOOGLE_API_KEY=$YOUR-GOOGLE-API-KEY" > .env
const badRequest = data => {
console.warn('bad request:', data);
return {
statusCode: 400,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
'Access-Control-Allow-Credentials': true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({
error: 400,
message: data,
})
};
};
const success = data => {
return {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
'Access-Control-Allow-Credentials': true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify(data)
};
};
const GOOGLE_GEOCODE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';
const geocodeGoogle = async params => axios.get(GOOGLE_GEOCODE_URL, {
params
});
module.exports.whereIs = async (event) => {
// read queryStringParameter
// if latlng : reverseGeocode
// if address : geocode
};
// read queryStringParameter
const query = event.queryStringParameters;
if (!query) {
return badRequest('Missing input parameters');
}
const params = {};
if (query.lat || query.lng) {
// if latlng : reverseGeocode
console.log('reverse geocode with coordinates', query.lat, query.lng);
params.latlng = `${query.lat},${query.lng}`;
}
if (query.address) {
// if address : geocode
console.log("geocode with address", query.address);
params.address = query.address;
}
params.key = process.env.GOOGLE_API_KEY;
try {
const result = await geocodeGoogle(params);
return success(result.data);
} catch (err) {
return badRequest(err);
}
$ npx serverless deploy
- where is 51.518991,-0.2100558
- where is lyon
- where is W10 5XL
- Github repo for the slides and sources