Giter VIP home page Giter VIP logo

lambda-rds-postgis's Introduction

Lambda RDS PostGIS

This is an Amazon Lambda function written in node.js that accepts an array of encoded polylines and returns a list of US counties that they intersect. The response also includes the US state for each county.

For example, you can turn this:

["cqepF~dgzUd@~BqDvA_C|@qCdAmBx@YLORg@sC[kB{Bz@yChAwEzAoC~@{@^m@P"]

into this:

[
  {
    "county": "Washoe",
    "state": "Nevada",
    "id": "32031"
  }
]

This function depends on an external PostGIS database of counties to do the lookup. This database could be hosted on Amazon RDS or anywhere else you'd like.

With Amazon API Gateway, it's easy to expose this as an API microservice. This could allow a website to access this function via a POST request.

This project uses Amazon Lambda, Amazon RDS, Amazon API Gateway and PostGIS.

Setup

Collect Data

Download US Counties

Download US States

Setup database

Create a postgres database called counties

CREATE DATABASE counties;

Enable POSTGIS:

CREATE EXTENSION postgis;

This database could be created on Amazon RDS. Follow these steps to create a database on Amazon RDS.

Import Data

Using shp2pgsql.

County:

shp2pgsql -W LATIN1 -d -s 4269 cb_2014_us_county_500k/cb_2014_us_county_500k counties | psql -U username -h host counties

State:

shp2pgsql -W LATIN1 -d -s 4269 cb_2014_us_state_500k/cb_2014_us_state_500k states | psql -U username -h host counties

Create Configuration File

Copy config-sample.json to config.json. Add your postgres credentials as POSTGRES_URL in config.json.

Upload code to Amazon Lambda

Zip the project:

sh build.sh

This will create a file called process.zip that you can upload to Amazon Lambda.

Create an API Endpoint

If you want to be able to access this via HTTP POST, create an Amazon API Gateway endpoint.

If you want to be able to access this endpoint from the browser, enable CORS.

Example request

Add your Amazon API Gateway endpoint

curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '["cqepF~dgzUd@~BqDvA_C|@qCdAmBx@YLORg@sC[kB{Bz@yChAwEzAoC~@{@^m@P","qq`nFvv~yU@{FcGAyKAkF?sCABdDA|F@rAw@AeA?iDAkHAuXI}DBIKc@@{ALiBZuAZ{Bt@sAj@kB~@yKpFyIrEcI~DyIpEkCtAqAp@IEGmM@sByB@iE?{IG]|@?xF?vB@xG?pHDn@pHwDtAs@"]' https://1vcbxgt3at.execute-api.us-east-1.amazonaws.com/prod

Response:

[
  {
    "county": "Washoe",
    "state": "Nevada",
    "id": "32031"
  },
  {
    "county": "Carson City",
    "state": "Nevada",
    "id": "32510"
  }
]

lambda-rds-postgis's People

Contributors

brendannee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.