69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
![]() |
"""
|
||
|
Event models for the OpenEventDatabase.
|
||
|
"""
|
||
|
|
||
|
import json
|
||
|
from oedb.utils.logging import logger
|
||
|
|
||
|
class BaseEvent:
|
||
|
"""
|
||
|
Base class for event resources.
|
||
|
Provides common methods for converting database rows to GeoJSON features.
|
||
|
"""
|
||
|
|
||
|
def row_to_feature(self, row, geom_only=False):
|
||
|
"""
|
||
|
Convert a database row to a GeoJSON feature.
|
||
|
|
||
|
Args:
|
||
|
row: The database row.
|
||
|
geom_only: Whether to return only the geometry and event ID.
|
||
|
|
||
|
Returns:
|
||
|
dict: A GeoJSON feature.
|
||
|
"""
|
||
|
logger.debug(f"Converting row to feature (geom_only={geom_only})")
|
||
|
|
||
|
# Only return geometry and event id
|
||
|
if geom_only:
|
||
|
return {
|
||
|
"type": "Feature",
|
||
|
"geometry": json.loads(row['geometry']),
|
||
|
"properties": {"id": row['events_id']}
|
||
|
}
|
||
|
|
||
|
properties = dict(row['events_tags'])
|
||
|
properties.update({
|
||
|
'createdate': row['createdate'],
|
||
|
'lastupdate': row['lastupdate'],
|
||
|
'lon': row['lon'],
|
||
|
'lat': row['lat'],
|
||
|
"id": row['events_id']
|
||
|
})
|
||
|
if 'secret' in properties: # hide secret in results
|
||
|
del properties['secret']
|
||
|
if "distance" in row:
|
||
|
properties['distance'] = row['distance']
|
||
|
return {
|
||
|
"type": "Feature",
|
||
|
"geometry": json.loads(row['geometry']),
|
||
|
"properties": properties
|
||
|
}
|
||
|
|
||
|
def rows_to_collection(self, rows, geom_only=False):
|
||
|
"""
|
||
|
Convert multiple database rows to a GeoJSON FeatureCollection.
|
||
|
|
||
|
Args:
|
||
|
rows: The database rows.
|
||
|
geom_only: Whether to return only the geometry and event ID.
|
||
|
|
||
|
Returns:
|
||
|
dict: A GeoJSON FeatureCollection.
|
||
|
"""
|
||
|
logger.debug(f"Converting {len(rows)} rows to collection")
|
||
|
return {
|
||
|
"type": "FeatureCollection",
|
||
|
"features": [self.row_to_feature(r, geom_only) for r in rows],
|
||
|
"count": len(rows)
|
||
|
}
|