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.1.tar.gz
(3.3 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.1.tar.gz.
File metadata
- Download URL: fastapi_geojson-0.3.1.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4459501c60b2d399c457824ca30445eb40afc8f0f45b330457bea47a6d8c5de8
|
|
| MD5 |
c6f8cd9a7fd40e413b2695860b63fcf9
|
|
| BLAKE2b-256 |
1418be58e18ffba5ce607fea841c089a0b4259977f4e0a58154576604a0e7a9f
|
File details
Details for the file fastapi_geojson-0.3.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_geojson-0.3.1-py3-none-any.whl
- Upload date:
- Size: 3.3 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 |
93326e0c8eeb518e4a5d6dd07138dcf2c0a8aa94f6b3d5aa55bfb28340aab756
|
|
| MD5 |
9d9ae0729d354b1a70d09b28f41f3f8d
|
|
| BLAKE2b-256 |
1a708e73066218ac0820c20463dda8785621571b7580279bcfde6d5659c7b4e8
|