Geographic Data API¶
The Geographic API handles location-based data and provides endpoints for retrieving geographic information and regional data.
Router Implementation¶
Geographic API endpoints for the CulicidaeLab server.
This module provides FastAPI router endpoints for retrieving geographic data and spatial information related to arthropod vectors and vector-borne diseases. The endpoints support various layer types and filtering options for spatial analysis and mapping applications.
The module includes the following endpoints: - GET /geo/{layer_type}: Retrieve geographic features for a specific layer type with optional spatial, temporal, and species-based filtering
All endpoints return GeoJSON-compliant data structures suitable for mapping applications and geographic information systems (GIS). The endpoints support bounding box filtering, date range filtering, and species-specific queries.
VALID_LAYER_TYPES = ['distribution', 'observations', 'modeled', 'breeding_sites'] module-attribute ¶
Built-in mutable sequence.
If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.
GeoJSONFeatureCollection ¶
GeoJSON FeatureCollection model.
Represents a collection of GeoJSON Features for batch operations and API responses containing multiple geographic features.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
get_geographic_layer(layer_type: str = Path(PydanticUndefined), db: DBConnection = Depends(get_db), species: str | None = Query(None), bbox: str | None = Query(None), start_date: str | None = Query(None), end_date: str | None = Query(None)) async ¶
Retrieve geographic features for a specific layer type with optional filtering.
This endpoint provides access to various geographic data layers related to arthropod vectors and vector-borne diseases. Currently supports observation data with spatial, temporal, and species-based filtering capabilities.
The endpoint returns GeoJSON FeatureCollection data that can be directly consumed by mapping libraries and GIS applications for visualization and spatial analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_type | str | The type of geographic layer to retrieve. Must be one of: 'distribution', 'observations', 'modeled', 'breeding_sites'. | Path(PydanticUndefined) |
db | DBConnection | Database connection for querying geographic data. | Depends(get_db) |
species | str | None | Comma-separated list of species scientific names to filter by. If provided, only features for the specified species will be returned. Example: "Aedes aegypti,Anopheles gambiae,Culex quinquefasciatus". | Query(None) |
bbox | str | None | Bounding box filter in the format "min_lon,min_lat,max_lon,max_lat". Filters features to only include those within the specified geographic bounds. Example: "-122.4194,37.7749,-122.0808,37.9128" (San Francisco area). | Query(None) |
start_date | str | None | Start date for temporal filtering in YYYY-MM-DD format. Only features observed on or after this date will be included. Example: "2023-01-01". | Query(None) |
end_date | str | None | End date for temporal filtering in YYYY-MM-DD format. Only features observed on or before this date will be included. Example: "2023-12-31". | Query(None) |
Returns:
| Name | Type | Description |
|---|---|---|
GeoJSONFeatureCollection | A GeoJSON FeatureCollection containing the requested geographic features. Each feature includes geometry (Point) and properties with observation metadata such as species information, observation dates, and location details. |
Raises:
| Type | Description |
|---|---|
HTTPException | If layer_type is invalid (400) or if bbox format is incorrect (400). |
Examples:
Basic usage - retrieve all observation features:
Filter by species:
Filter by geographic bounds (San Francisco):
Filter by date range:
Combined filters - species and location:
Combined filters - all parameters:
GET /geo/observations?species=Culex%20quinquefasciatus
&bbox=-118.5,34.0,-118.1,34.3
&start_date=2023-03-01&end_date=2023-09-30
Source code in backend\routers\geo.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
Data Schemas¶
The Geographic API uses Pydantic schemas for geographic data validation:
backend.schemas.geo_schemas ¶
Pydantic models for geographic data and GeoJSON structures.
This module defines the schema models used for handling geographic data, GeoJSON structures, and map layer responses.
GeoJSONGeometry ¶
GeoJSON geometry model.
Represents the geometry component of a GeoJSON Feature, containing type and coordinates information.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
GeoJSONFeature ¶
GeoJSON Feature model.
Represents a complete GeoJSON Feature with type, properties, and geometry. Used for representing geographic features with associated data.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
GeoJSONFeatureCollection ¶
GeoJSON FeatureCollection model.
Represents a collection of GeoJSON Features for batch operations and API responses containing multiple geographic features.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
MapLayerResponse ¶
Response model for map layer data.
Contains layer metadata and GeoJSON data for rendering map layers in the frontend application.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
Service Layer¶
The Geographic API integrates with geographic service layers:
backend.services.geo_service ¶
Geographic data service for handling spatial queries and filtering.
This module provides functionality for querying and filtering geographic data, particularly observation data with spatial and temporal filtering capabilities. It supports bounding box filtering, date range filtering, and species-based filtering for geographic visualization.
Example
from backend.services.geo_service import get_geo_layer from backend.services.database import get_db db = get_db() features = get_geo_layer(db, "observations", species_list=["Aedes aegypti"])
GeoJSONFeature ¶
GeoJSON Feature model.
Represents a complete GeoJSON Feature with type, properties, and geometry. Used for representing geographic features with associated data.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
GeoJSONFeatureCollection ¶
GeoJSON FeatureCollection model.
Represents a collection of GeoJSON Features for batch operations and API responses containing multiple geographic features.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
GeoJSONGeometry ¶
GeoJSON geometry model.
Represents the geometry component of a GeoJSON Feature, containing type and coordinates information.
model_config = {} class-attribute ¶
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
is_valid_date_str(date_str: str) -> bool ¶
Validate if a string represents a valid YYYY-MM-DD date format.
This helper function checks if the provided string can be parsed as a date in the expected format used throughout the application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_str | str | The date string to validate. | required |
Returns:
| Name | Type | Description |
|---|---|---|
bool | bool | True if the string is a valid YYYY-MM-DD date, False otherwise. |
Example
is_valid_date_str("2023-12-25") True is_valid_date_str("invalid-date") False
get_geo_layer(db: DBConnection, layer_type: str, species_list: list[str] | None = None, bbox_filter: tuple[float, float, float, float] | None = None, start_date_str: str | None = None, end_date_str: str | None = None, limit: int = 10000) -> GeoJSONFeatureCollection ¶
Retrieve geographic features for a specific layer with optional filtering.
This function queries observation data and applies multiple filters including species, bounding box, and date range filters. It returns GeoJSON formatted features suitable for mapping applications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db | DBConnection | The database connection object. | required |
layer_type | str | The type of layer to retrieve. Currently supports "observations". Other types return empty collections. | required |
species_list | list[str] | None | List of species scientific names to filter by. If None, no species filtering is applied. | None |
bbox_filter | tuple[float, float, float, float] | None | A bounding box as (min_lon, min_lat, max_lon, max_lat). If None, no spatial filtering is applied. | None |
start_date_str | str | None | Start date in YYYY-MM-DD format. If None, no start date filtering is applied. | None |
end_date_str | str | None | End date in YYYY-MM-DD format. If None, no end date filtering is applied. | None |
limit | int | Maximum number of records to return. Defaults to 10000. | 10000 |
Returns:
| Name | Type | Description |
|---|---|---|
GeoJSONFeatureCollection | GeoJSONFeatureCollection | A GeoJSON FeatureCollection containing the filtered observation features with their properties and geometry. |
Example
from backend.services.database import get_db db = get_db()
Get all Aedes aegypti observations in a specific region¶
features = get_geo_layer( ... db, ... "observations", ... species_list=["Aedes aegypti"], ... bbox_filter=(-74.1, 40.6, -71.9, 41.3), # NYC area ... start_date_str="2023-01-01", ... end_date_str="2023-12-31" ... ) print(len(features.features)) # Number of observations
get_table(db: DBConnection, table_name: str) ¶
Retrieve a specific table from the LanceDB database.
Opens and returns a reference to the specified table in the database. This function validates that the table exists and is accessible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db | DBConnection | The database connection object. | required |
table_name | str | The name of the table to retrieve. | required |
Returns:
| Type | Description |
|---|---|
| lancedb.table.Table: A table object for the specified table name. |
Raises:
| Type | Description |
|---|---|
ValueError | If the table is not found or cannot be opened. |
Example
db = get_db() observations_table = get_table(db, "observations") results = observations_table.search().limit(10).to_list()
Example Usage¶
Geographic Data Retrieval¶
import httpx
async with httpx.AsyncClient() as client:
# Get regional data
response = await client.get(
"http://localhost:8000/api/v1/regions",
params={"lang": "en"}
)
regions = response.json()
# Get country information
response = await client.get(
"http://localhost:8000/api/v1/countries",
params={"lang": "en"}
)
countries = response.json()