Innovation Friday

another weird subject?

Last time I talked about GIS (geographical information system)

Today I will talk about a tiny part of GIS

Geocoding

kezako?

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

Boring !

I agree

Then why not creating an API ?

Let's do it

We only have ten minutes

Let's crack on

we need a plan

not a map, a plan

an architecture diagram

Simple

architecture

Todo

get a Google API key (done)

get an AWS account (done)

create a project

Hurry up!

Create a project


$ npx serverless create --template aws-nodejs --path whereis
$ cd whereis
$ npm init -y
$ npm i -S axios dotenv
# $ npm i -D serverless serverless-offline
				

Hack the serverless file


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
				

We always need an env file


$ echo "GOOGLE_API_KEY=$YOUR-GOOGLE-API-KEY" > .env
        

Let's rock now

send an error


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,
    })
  };
};
          

send a success


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)
  };
};
          

Google API


const GOOGLE_GEOCODE_URL = 'https://maps.googleapis.com/maps/api/geocode/json';

const geocodeGoogle = async params => axios.get(GOOGLE_GEOCODE_URL, {
  params
});
          

handler


module.exports.whereIs = async (event) => {
  // read queryStringParameter

  // if latlng : reverseGeocode

  // if address : geocode

};
          

queryStringParameter


// read queryStringParameter
const query = event.queryStringParameters;
if (!query) {
  return badRequest('Missing input parameters');
}
          

reverse Geocode


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}`;
}
          

Geocode


if (query.address) {
  // if address : geocode
  console.log("geocode with address", query.address);
  params.address = query.address;
}
          

Call Google Maps


params.key = process.env.GOOGLE_API_KEY;
try {
  const result = await geocodeGoogle(params);
  return success(result.data);
} catch (err) {
  return badRequest(err);
}
          

Done !

deploy it


$ npx serverless deploy
          

Try it

- where is 51.518991,-0.2100558
- where is lyon
- where is W10 5XL

C'est fini

Merci

resources

- Github repo for the slides and sources