| 
									
										
										
										
											2025-09-15 23:25:11 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  | 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'], | 
					
						
							|  |  |  |             "id": row['events_id'] | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2025-09-16 00:46:09 +02:00
										 |  |  |          | 
					
						
							|  |  |  |         # Ensure what property is used instead of description | 
					
						
							|  |  |  |         if 'description' in properties and 'what' not in properties: | 
					
						
							|  |  |  |             properties['what'] = properties.pop('description') | 
					
						
							| 
									
										
										
										
											2025-09-15 23:25:11 +02:00
										 |  |  |         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) | 
					
						
							|  |  |  |         } |