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.
Download US Counties
Download US States
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.
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
Copy config-sample.json
to config.json
. Add your postgres credentials as POSTGRES_URL
in config.json
.
Zip the project:
sh build.sh
This will create a file called process.zip
that you can upload to Amazon Lambda.
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.
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"
}
]