Skip to main content

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

  • GeoJSONResponse for FastAPI endpoints
  • Create GeoJSON Features:
    • feature_from_point
    • feature_from_linestring
    • feature_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)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_geojson-0.3.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

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

Hashes for fastapi_geojson-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5eb4b360b9880c07e281b59c14a8c30d8bb2db2ffdd98c284d850e99fa09f356
MD5 9568ab37b80fb4cdb864a14f59c7f1e8
BLAKE2b-256 940ae2376f107eb934e37aa1f0531464492ede941b5c607c82a321fe782a1857

See more details on using hashes here.

File details

Details for the file fastapi_geojson-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_geojson-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b6962cd3cfe02f477f9f580791928e9ecb1095086835d509f1d49ef1be30494e
MD5 482b52b5d6a82d4c28ce5a1a6063fb4e
BLAKE2b-256 ab397476cf2e29fd9cd5867fe4cd785e265b2465cd5ed6f264c5052e6a42aaa9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page