GeoJSON helpers for FastAPI with Feature creation and GeoJSONResponse
Project description
FastAPI GeoJSON
A lightweight Python package to generate GeoJSON responses easily in FastAPI applications.
Features
GeoJSONResponsefor FastAPI endpoints- Create GeoJSON Features:
feature_from_pointfeature_from_linestringfeature_from_polygon
- Supports FeatureCollection out-of-the-box
- Fully compatible with FastAPI response models
Installation
pip install fastapi-geojson
Usage
Points
from fastapi import FastAPI
from fastapi_geojson import (
GeoJSONResponse,
FeatureCollectionModel,
feature_from_point,
)
app = FastAPI()
locations = [
{"name": "Cairo", "lat": 30.0444, "lon": 31.2357},
{"name": "Alexandria", "lat": 31.2001, "lon": 29.9187},
]
@app.get("/geojson/points", response_class=GeoJSONResponse)
def get_points():
features = [
feature_from_point(loc["lat"], loc["lon"], {"name": loc["name"]})
for loc in locations
]
return FeatureCollectionModel(features=features).model_dump()
Lines
from fastapi_geojson import GeoJSONResponse, FeatureCollectionModel, feature_from_linestring
roads = [
{"name": "Road 1", "coords": [[31.2357, 30.0444], [31.2400, 30.0500]]},
{"name": "Road 2", "coords": [[29.9187, 31.2001], [29.9250, 31.2100]]},
]
@app.get("/geojson/lines", response_class=GeoJSONResponse)
def get_lines():
features = [
feature_from_linestring(road["coords"], {"name": road["name"]})
for road in roads
]
return FeatureCollectionModel(features=features).model_dump()
Polygons
from fastapi_geojson import GeoJSONResponse, FeatureCollectionModel, feature_from_polygon
areas = [
{
"name": "Park",
"coords": [[[31.2300, 30.0400], [31.2350, 30.0400], [31.2350, 30.0450], [31.2300, 30.0450], [31.2300, 30.0400]]],
}
]
@app.get("/geojson/polygons", response_class=GeoJSONResponse)
def get_polygons():
features = [
feature_from_polygon(area["coords"], {"name": area["name"]})
for area in areas
]
return FeatureCollectionModel(features=features).model_dump()
Response Format
All endpoints return a valid GeoJSON FeatureCollection with Content-Type: application/geo+json:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [31.2357, 30.0444]
},
"properties": {
"name": "Cairo"
}
}
]
}
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
fastapi_geojson-0.3.0.tar.gz
(3.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_geojson-0.3.0.tar.gz.
File metadata
- Download URL: fastapi_geojson-0.3.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5eb4b360b9880c07e281b59c14a8c30d8bb2db2ffdd98c284d850e99fa09f356
|
|
| MD5 |
9568ab37b80fb4cdb864a14f59c7f1e8
|
|
| BLAKE2b-256 |
940ae2376f107eb934e37aa1f0531464492ede941b5c607c82a321fe782a1857
|
File details
Details for the file fastapi_geojson-0.3.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_geojson-0.3.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b6962cd3cfe02f477f9f580791928e9ecb1095086835d509f1d49ef1be30494e
|
|
| MD5 |
482b52b5d6a82d4c28ce5a1a6063fb4e
|
|
| BLAKE2b-256 |
ab397476cf2e29fd9cd5867fe4cd785e265b2465cd5ed6f264c5052e6a42aaa9
|