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.1.tar.gz (3.3 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.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

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

Hashes for fastapi_geojson-0.3.1.tar.gz
Algorithm Hash digest
SHA256 4459501c60b2d399c457824ca30445eb40afc8f0f45b330457bea47a6d8c5de8
MD5 c6f8cd9a7fd40e413b2695860b63fcf9
BLAKE2b-256 1418be58e18ffba5ce607fea841c089a0b4259977f4e0a58154576604a0e7a9f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_geojson-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93326e0c8eeb518e4a5d6dd07138dcf2c0a8aa94f6b3d5aa55bfb28340aab756
MD5 9d9ae0729d354b1a70d09b28f41f3f8d
BLAKE2b-256 1a708e73066218ac0820c20463dda8785621571b7580279bcfde6d5659c7b4e8

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