This REST API helps you keep track of Service Areas and determine whether a geo point falls within any of your service areas.
- PostgreSQL with PostGIS extensions
- FastAPI as the web framework
- SQLAlchemy as the ORM
- GeoAlchemy2 for managing geo data format
- Alembic for database schema migrations
- Python 3.10
- Docker & Docker Compose
You can quickly get started by running a single command: docker-compose up -d
.
This will launch a Postgres database with PostGIS installed, apply Alembic migrations on the database and serve the API.
If you wish to run the API locally for the sake of easier development and debugging, you can proceed as follows:
- Run the Postgres Database:
docker-compose up -d db pgadmin
- Install Python dependencies:
pip install -r requirements.txt
- Set up environment variables:
cp .env.sample .env
- Apply database migrations:
alembic upgrade head
- Run the application:
uvicorn src.main:app --reload
Providers:
GET api/v1/providers
: List all providersPOST api/v1/providers
: Create a new providerGET api/v1/providers/{id}
: Get details of a providerPUT api/v1/providers/{id}
: Update a providerDELETE /providers/{id}
: Delete a provider
Service Areas:
GET api/v1/service-areas
: List all service areasPOST api/v1/service-areas
: Create a new service areaGET api/v1/service-areas/{id}
: Get details of a service areaPUT api/v1/service-areas/{id}
: Update a service areaDELETE api/v1/service-areas/{id}
: Delete a service area
Point Geospatial Querying:
GET api/v1/service-areas/search?lat={latitude}&lng={longitude}
: Find service areas containing a specific geo point
To test using curl
commands, you can try creating a Service Area, and check if a latitude/longitude pair is contained in the registered Service Areas:
curl -X 'POST' 'http://127.0.0.1:8000/api/v1/serviceareas/' -H 'Content-Type: application/json' -d '{
"name": "Downtown Area 2",
"price": 25.50,
"geojson": "{\"type\": \"Polygon\", \"coordinates\": [[[30.0, 10.0], [40.0, 40.0], [20.0, 40.0], [10.0, 20.0], [30.0, 10.0]]]}"
}'
curl -X GET "http://localhost:8000/api/v1/serviceareas/check?lat=30&lng=20"
To run the unit tests:
pytest
We recommend you stick to the Docker Copose setup to deploy the API on an AWS EC2 instance: docker-compose build -d
.
This will include the Postgres database, run the Alembic migrations to create the database schema, and serve the PolygonFinder API.
You can find an example hosted online at http://13.51.79.167:8000
.
You can try it out with the following curl
commands:
curl -X 'POST' 'http://13.51.79.167:8000/api/v1/serviceareas/' -H 'Content-Type: application/json' -d '{
"name": "Downtown Area",
"price": 25.50,
"geojson": "{\"type\": \"Polygon\", \"coordinates\": [[[30.0, 10.0], [40.0, 40.0], [20.0, 40.0], [10.0, 20.0], [30.0, 10.0]]]}"
}'
curl -X GET "http://13.51.79.167:8000/api/v1/serviceareas/check?lat=30&lng=200"