1541 lines
No EOL
72 KiB
Python
1541 lines
No EOL
72 KiB
Python
"""
|
|
Demo resource for the OpenEventDatabase.
|
|
"""
|
|
|
|
import falcon
|
|
import requests
|
|
import json
|
|
from collections import defaultdict
|
|
from oedb.utils.logging import logger
|
|
|
|
class DemoResource:
|
|
"""
|
|
Resource for the demo endpoint.
|
|
Handles the /demo endpoint and related demo pages.
|
|
"""
|
|
|
|
def on_get_edit(self, req, resp, id=None):
|
|
"""
|
|
Handle GET requests to the /demo/edit endpoint.
|
|
Returns an HTML page with a form for editing an existing event.
|
|
|
|
Args:
|
|
req: The request object.
|
|
resp: The response object.
|
|
id: The event ID to edit.
|
|
"""
|
|
logger.info(f"Processing GET request to /demo/edit for event ID: {id}")
|
|
|
|
if id is None:
|
|
resp.status = falcon.HTTP_400
|
|
resp.text = "Event ID is required"
|
|
return
|
|
|
|
try:
|
|
# Set content type to HTML
|
|
resp.content_type = 'text/html'
|
|
|
|
# Fetch the event data from the API
|
|
response = requests.get(f'http://localhost/event/{id}')
|
|
|
|
if response.status_code != 200:
|
|
resp.status = falcon.HTTP_404
|
|
resp.text = f"Event with ID {id} not found"
|
|
return
|
|
|
|
event_data = response.json()
|
|
|
|
# Create HTML response with form
|
|
html = f"""
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Edit Event - OpenEventDatabase</title>
|
|
<script src="https://unpkg.com/maplibre-gl@3.0.0/dist/maplibre-gl.js"></script>
|
|
<link href="https://unpkg.com/maplibre-gl@3.0.0/dist/maplibre-gl.css" rel="stylesheet" />
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
|
|
<style>
|
|
body {{
|
|
margin: 0;
|
|
padding: 20px;
|
|
font-family: Arial, sans-serif;
|
|
background-color: #f5f5f5;
|
|
}}
|
|
.container {{
|
|
max-width: 1000px;
|
|
margin: 0 auto;
|
|
background-color: white;
|
|
padding: 20px;
|
|
border-radius: 5px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
}}
|
|
h1 {{
|
|
margin-top: 0;
|
|
color: #333;
|
|
}}
|
|
.form-group {{
|
|
margin-bottom: 15px;
|
|
}}
|
|
label {{
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
font-weight: bold;
|
|
}}
|
|
input[type="text"],
|
|
input[type="datetime-local"],
|
|
select,
|
|
textarea {{
|
|
width: 100%;
|
|
padding: 8px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
box-sizing: border-box;
|
|
font-size: 14px;
|
|
}}
|
|
.required:after {{
|
|
content: " *";
|
|
color: red;
|
|
}}
|
|
.form-row {{
|
|
display: flex;
|
|
gap: 15px;
|
|
}}
|
|
.form-row .form-group {{
|
|
flex: 1;
|
|
}}
|
|
button {{
|
|
background-color: #0078ff;
|
|
color: white;
|
|
border: none;
|
|
padding: 10px 15px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
}}
|
|
button:hover {{
|
|
background-color: #0056b3;
|
|
}}
|
|
.note {{
|
|
font-size: 12px;
|
|
color: #666;
|
|
margin-top: 5px;
|
|
}}
|
|
#map {{
|
|
width: 100%;
|
|
height: 300px;
|
|
margin-bottom: 15px;
|
|
border-radius: 4px;
|
|
}}
|
|
#result {{
|
|
margin-top: 20px;
|
|
padding: 10px;
|
|
border-radius: 4px;
|
|
display: none;
|
|
}}
|
|
#result.success {{
|
|
background-color: #d4edda;
|
|
border: 1px solid #c3e6cb;
|
|
color: #155724;
|
|
}}
|
|
#result.error {{
|
|
background-color: #f8d7da;
|
|
border: 1px solid #f5c6cb;
|
|
color: #721c24;
|
|
}}
|
|
.nav-links {{
|
|
margin-bottom: 20px;
|
|
}}
|
|
.nav-links a {{
|
|
color: #0078ff;
|
|
text-decoration: none;
|
|
margin-right: 15px;
|
|
}}
|
|
.nav-links a:hover {{
|
|
text-decoration: underline;
|
|
}}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="nav-links">
|
|
<a href="/demo">← Back to Map</a>
|
|
<a href="/">API Information</a>
|
|
<a href="/event">View Events</a>
|
|
<a href="https://source.cipherbliss.com/tykayn/oedb-backend" target="_blank" title="View Source Code on Cipherbliss">
|
|
<i class="fas fa-code-branch"></i> Source
|
|
</a>
|
|
</div>
|
|
|
|
<h1>Edit Event</h1>
|
|
|
|
<form id="eventForm">
|
|
<input type="hidden" id="eventId" value="{id}">
|
|
|
|
<div class="form-group">
|
|
<label for="label" class="required">Event Name</label>
|
|
<input type="text" id="label" name="label" required>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="type" class="required">Event Type</label>
|
|
<select id="type" name="type" required>
|
|
<option value="scheduled">Scheduled</option>
|
|
<option value="forecast">Forecast</option>
|
|
<option value="unscheduled">Unscheduled</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="what" class="required">What</label>
|
|
<input type="text" id="what" name="what" placeholder="e.g., sport.match.football" required>
|
|
<div class="note">Category of the event (e.g., sport.match.football, culture.festival)</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="what_series">What: Series</label>
|
|
<input type="text" id="what_series" name="what_series" placeholder="e.g., Euro 2024">
|
|
<div class="note">Series or group the event belongs to (e.g., Euro 2024, Summer Festival 2023)</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="where">Where</label>
|
|
<input type="text" id="where" name="where" placeholder="e.g., Stadium Name">
|
|
<div class="note">Specific location name (e.g., Eiffel Tower, Wembley Stadium)</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="start" class="required">Start Time</label>
|
|
<input type="datetime-local" id="start" name="start" required value="">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="stop" class="required">End Time</label>
|
|
<input type="datetime-local" id="stop" name="stop" required value="">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="required">Location</label>
|
|
<div id="map"></div>
|
|
<div class="note">Click on the map to set the event location</div>
|
|
</div>
|
|
|
|
<button type="submit">Update Event</button>
|
|
</form>
|
|
|
|
<div id="result"></div>
|
|
</div>
|
|
|
|
<script>
|
|
// Initialize the map
|
|
const map = new maplibregl.Map({{
|
|
container: 'map',
|
|
style: 'https://demotiles.maplibre.org/style.json',
|
|
center: [2.2137, 46.2276], // Default center (center of metropolitan France)
|
|
zoom: 5
|
|
}});
|
|
|
|
// Add navigation controls
|
|
map.addControl(new maplibregl.NavigationControl());
|
|
|
|
// Add attribution control with OpenStreetMap attribution
|
|
map.addControl(new maplibregl.AttributionControl({{
|
|
customAttribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
|
|
}}));
|
|
|
|
// Add marker for event location
|
|
let marker = new maplibregl.Marker({{
|
|
draggable: true
|
|
}});
|
|
|
|
// Event data from API
|
|
const eventData = {event_data};
|
|
|
|
// Function to populate form with event data
|
|
function populateForm() {{
|
|
if (!eventData || !eventData.properties) {{
|
|
showResult('Error loading event data', 'error');
|
|
return;
|
|
}}
|
|
|
|
const properties = eventData.properties;
|
|
|
|
// Set form values
|
|
document.getElementById('label').value = properties.label || '';
|
|
document.getElementById('type').value = properties.type || 'scheduled';
|
|
document.getElementById('what').value = properties.what || '';
|
|
|
|
// Handle optional fields
|
|
if (properties['what:series']) {{
|
|
document.getElementById('what_series').value = properties['what:series'];
|
|
}}
|
|
|
|
if (properties.where) {{
|
|
document.getElementById('where').value = properties.where;
|
|
}}
|
|
|
|
// Format dates for datetime-local input
|
|
if (properties.start) {{
|
|
const startDate = new Date(properties.start);
|
|
document.getElementById('start').value = startDate.toISOString().slice(0, 16);
|
|
}}
|
|
|
|
if (properties.stop) {{
|
|
const stopDate = new Date(properties.stop);
|
|
document.getElementById('stop').value = stopDate.toISOString().slice(0, 16);
|
|
}}
|
|
|
|
// Set marker on map
|
|
if (eventData.geometry && eventData.geometry.coordinates) {{
|
|
const coords = eventData.geometry.coordinates;
|
|
marker.setLngLat(coords).addTo(map);
|
|
|
|
// Center map on event location
|
|
map.flyTo({{
|
|
center: coords,
|
|
zoom: 10
|
|
}});
|
|
}}
|
|
}}
|
|
|
|
// Call function to populate form
|
|
populateForm();
|
|
|
|
// Add marker on map click
|
|
map.on('click', function(e) {{
|
|
marker.setLngLat(e.lngLat).addTo(map);
|
|
}});
|
|
|
|
// Function to show result message
|
|
function showResult(message, type) {{
|
|
const resultElement = document.getElementById('result');
|
|
resultElement.textContent = message;
|
|
resultElement.className = type;
|
|
resultElement.style.display = 'block';
|
|
|
|
// Scroll to result
|
|
resultElement.scrollIntoView({{ behavior: 'smooth' }});
|
|
}}
|
|
|
|
// Handle form submission
|
|
document.getElementById('eventForm').addEventListener('submit', function(e) {{
|
|
e.preventDefault();
|
|
|
|
// Get event ID
|
|
const eventId = document.getElementById('eventId').value;
|
|
|
|
// Get form values
|
|
const label = document.getElementById('label').value;
|
|
const type = document.getElementById('type').value;
|
|
const what = document.getElementById('what').value;
|
|
const what_series = document.getElementById('what_series').value;
|
|
const where = document.getElementById('where').value;
|
|
const start = document.getElementById('start').value;
|
|
const stop = document.getElementById('stop').value;
|
|
|
|
// Check if marker is set
|
|
if (!marker.getLngLat()) {{
|
|
showResult('Please set a location by clicking on the map', 'error');
|
|
return;
|
|
}}
|
|
|
|
// Get marker coordinates
|
|
const lngLat = marker.getLngLat();
|
|
|
|
// Create event object
|
|
const event = {{
|
|
type: 'Feature',
|
|
geometry: {{
|
|
type: 'Point',
|
|
coordinates: [lngLat.lng, lngLat.lat]
|
|
}},
|
|
properties: {{
|
|
label: label,
|
|
type: type,
|
|
what: what,
|
|
start: start,
|
|
stop: stop
|
|
}}
|
|
}};
|
|
|
|
// Add optional properties if provided
|
|
if (what_series) {{
|
|
event.properties['what:series'] = what_series;
|
|
}}
|
|
|
|
if (where) {{
|
|
event.properties.where = where;
|
|
}}
|
|
|
|
// Submit event to API
|
|
fetch(`/event/${{eventId}}`, {{
|
|
method: 'PUT',
|
|
headers: {{
|
|
'Content-Type': 'application/json'
|
|
}},
|
|
body: JSON.stringify(event)
|
|
}})
|
|
.then(response => {{
|
|
if (response.ok) {{
|
|
return response.json();
|
|
}} else {{
|
|
return response.text().then(text => {{
|
|
throw new Error(text || response.statusText);
|
|
}});
|
|
}}
|
|
}})
|
|
.then(data => {{
|
|
showResult(`Event updated successfully with ID: ${{data.id}}`, 'success');
|
|
|
|
// Add link to view the event
|
|
const resultElement = document.getElementById('result');
|
|
resultElement.innerHTML += `<p><a href="/event/${{data.id}}" target="_blank">View Event</a> | <a href="/demo">Back to Map</a></p>`;
|
|
}})
|
|
.catch(error => {{
|
|
showResult(`Error: ${{error.message}}`, 'error');
|
|
}});
|
|
}});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
# Set the response body and status
|
|
resp.text = html.replace('{event_data}', json.dumps(event_data))
|
|
resp.status = falcon.HTTP_200
|
|
logger.success(f"Successfully processed GET request to /demo/edit for event ID: {id}")
|
|
except Exception as e:
|
|
logger.error(f"Error processing GET request to /demo/edit: {e}")
|
|
resp.status = falcon.HTTP_500
|
|
resp.text = f"Error: {str(e)}"
|
|
|
|
def on_get(self, req, resp):
|
|
"""
|
|
Handle GET requests to the /demo endpoint.
|
|
Returns an HTML page with a MapLibre map showing current events.
|
|
|
|
Args:
|
|
req: The request object.
|
|
resp: The response object.
|
|
"""
|
|
logger.info("Processing GET request to /demo")
|
|
|
|
try:
|
|
# Set content type to HTML
|
|
resp.content_type = 'text/html'
|
|
|
|
# Create HTML response with MapLibre map
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>OpenEventDatabase Demo</title>
|
|
<script src="https://unpkg.com/maplibre-gl@3.0.0/dist/maplibre-gl.js"></script>
|
|
<link href="https://unpkg.com/maplibre-gl@3.0.0/dist/maplibre-gl.css" rel="stylesheet" />
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.4/css/bulma.min.css">
|
|
<script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js"></script>
|
|
<style>
|
|
body { margin: 0; padding: 0; font-family: Arial, sans-serif; }
|
|
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
|
|
.map-overlay {
|
|
position: absolute;
|
|
top: 10px;
|
|
left: 10px;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
max-width: 300px;
|
|
}
|
|
.map-style-control {
|
|
position: absolute;
|
|
top: 10px;
|
|
right: 10px;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
z-index: 1;
|
|
}
|
|
.map-style-control button {
|
|
display: block;
|
|
margin-bottom: 5px;
|
|
padding: 5px 10px;
|
|
background: #fff;
|
|
border: 1px solid #ddd;
|
|
border-radius: 3px;
|
|
cursor: pointer;
|
|
width: 100%;
|
|
text-align: left;
|
|
}
|
|
.map-style-control button:hover {
|
|
background: #f5f5f5;
|
|
}
|
|
.map-style-control button.active {
|
|
background: #0078ff;
|
|
color: white;
|
|
border-color: #0056b3;
|
|
}
|
|
h2 { margin-top: 0; }
|
|
ul { padding-left: 20px; }
|
|
a { color: #0078ff; text-decoration: none; }
|
|
a:hover { text-decoration: underline; }
|
|
.event-popup { max-width: 300px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="map"></div>
|
|
<div class="map-overlay">
|
|
<h2>OpenEventDatabase Demo</h2>
|
|
<p>This map shows current events from the OpenEventDatabase.</p>
|
|
<h3>API Endpoints:</h3>
|
|
<ul>
|
|
<li><a href="/" target="_blank">/ - API Information</a></li>
|
|
<li><a href="/event" target="_blank">/event - Get Events</a></li>
|
|
<li><a href="/stats" target="_blank">/stats - Database Statistics</a></li>
|
|
</ul>
|
|
<h3>Demo Pages:</h3>
|
|
<ul>
|
|
<li><a href="/demo/by-what" target="_blank">/demo/by-what - Events by Type</a></li>
|
|
<li><a href="/demo/map-by-what" target="_blank">/demo/map-by-what - Map by Event Type</a></li>
|
|
<li><a href="/event?what=music" target="_blank">Search Music Events</a></li>
|
|
<li><a href="/event?what=sport" target="_blank">Search Sport Events</a></li>
|
|
</ul>
|
|
<p><a href="/demo/add" class="add-event-btn" style="display: block; text-align: center; margin-top: 15px; padding: 8px; background-color: #0078ff; color: white; border-radius: 4px; font-weight: bold;">+ Add New Event</a></p>
|
|
<p style="text-align: center; margin-top: 10px;">
|
|
<a href="https://source.cipherbliss.com/tykayn/oedb-backend" target="_blank" title="View Source Code on Cipherbliss" style="font-size: 24px;">
|
|
<i class="fas fa-code-branch"></i>
|
|
</a>
|
|
</p>
|
|
</div>
|
|
<div class="map-style-control">
|
|
<h4 style="margin-top: 0; margin-bottom: 10px;">Map Style</h4>
|
|
<button id="style-default" class="active">Default</button>
|
|
<button id="style-osm-vector">OSM Vector</button>
|
|
<button id="style-osm-raster">OSM Raster</button>
|
|
</div>
|
|
|
|
<script>
|
|
// Map style URLs
|
|
const mapStyles = {
|
|
default: 'https://demotiles.maplibre.org/style.json',
|
|
osmVector: 'https://cdn.jsdelivr.net/gh/openmaptiles/osm-bright-gl-style@master/style-cdn.json',
|
|
osmRaster: {
|
|
version: 8,
|
|
sources: {
|
|
'osm-raster': {
|
|
type: 'raster',
|
|
tiles: [
|
|
'https://tile.openstreetmap.org/{z}/{x}/{y}.png'
|
|
],
|
|
tileSize: 256,
|
|
attribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
|
|
}
|
|
},
|
|
layers: [
|
|
{
|
|
id: 'osm-raster-layer',
|
|
type: 'raster',
|
|
source: 'osm-raster',
|
|
minzoom: 0,
|
|
maxzoom: 19
|
|
}
|
|
]
|
|
}
|
|
};
|
|
|
|
// Initialize the map with default style
|
|
const map = new maplibregl.Map({
|
|
container: 'map',
|
|
style: mapStyles.default,
|
|
center: [2.3522, 48.8566], // Default center (Paris)
|
|
zoom: 4
|
|
});
|
|
|
|
// Add navigation controls
|
|
map.addControl(new maplibregl.NavigationControl());
|
|
|
|
// Add attribution control with OpenStreetMap attribution
|
|
map.addControl(new maplibregl.AttributionControl({
|
|
customAttribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
|
|
}));
|
|
|
|
// Style switcher functionality
|
|
let currentStyle = 'default';
|
|
let eventsData = null;
|
|
|
|
document.getElementById('style-default').addEventListener('click', () => {
|
|
if (currentStyle !== 'default') {
|
|
switchMapStyle('default');
|
|
}
|
|
});
|
|
|
|
document.getElementById('style-osm-vector').addEventListener('click', () => {
|
|
if (currentStyle !== 'osmVector') {
|
|
switchMapStyle('osmVector');
|
|
}
|
|
});
|
|
|
|
document.getElementById('style-osm-raster').addEventListener('click', () => {
|
|
if (currentStyle !== 'osmRaster') {
|
|
switchMapStyle('osmRaster');
|
|
}
|
|
});
|
|
|
|
function switchMapStyle(styleName) {
|
|
// Update active button
|
|
document.querySelectorAll('.map-style-control button').forEach(button => {
|
|
button.classList.remove('active');
|
|
});
|
|
document.getElementById(`style-${styleName.replace('osm', 'osm-')}`).classList.add('active');
|
|
|
|
// Save current center and zoom
|
|
const center = map.getCenter();
|
|
const zoom = map.getZoom();
|
|
|
|
// Save events data if already loaded
|
|
if (map.getSource('events')) {
|
|
try {
|
|
eventsData = map.getSource('events')._data;
|
|
} catch (e) {
|
|
console.error('Error saving events data:', e);
|
|
}
|
|
}
|
|
|
|
// Apply new style
|
|
map.setStyle(mapStyles[styleName]);
|
|
|
|
// Restore center and zoom after style is loaded
|
|
map.once('style.load', () => {
|
|
map.setCenter(center);
|
|
map.setZoom(zoom);
|
|
|
|
// Restore events data if available
|
|
if (eventsData) {
|
|
addEventsToMap(eventsData);
|
|
} else {
|
|
fetchEvents();
|
|
}
|
|
});
|
|
|
|
currentStyle = styleName;
|
|
}
|
|
|
|
// Fetch events when the map is loaded
|
|
map.on('load', function() {
|
|
fetchEvents();
|
|
});
|
|
|
|
// Function to fetch events from the API
|
|
function fetchEvents() {
|
|
// Fetch events from the API - using default behavior to get currently active events
|
|
fetch('/event')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.features && data.features.length > 0) {
|
|
// Add events to the map
|
|
addEventsToMap(data);
|
|
|
|
// Fit map to events bounds
|
|
fitMapToBounds(data);
|
|
} else {
|
|
console.log('No events found');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching events:', error);
|
|
});
|
|
}
|
|
|
|
// Function to add events to the map
|
|
function addEventsToMap(geojson) {
|
|
// Add a GeoJSON source for events
|
|
map.addSource('events', {
|
|
type: 'geojson',
|
|
data: geojson
|
|
});
|
|
|
|
// Add a circle layer for events
|
|
map.addLayer({
|
|
id: 'events-circle',
|
|
type: 'circle',
|
|
source: 'events',
|
|
paint: {
|
|
'circle-radius': 8,
|
|
'circle-color': '#FF5722',
|
|
'circle-stroke-width': 2,
|
|
'circle-stroke-color': '#FFFFFF'
|
|
}
|
|
});
|
|
|
|
// Add popups for events
|
|
geojson.features.forEach(feature => {
|
|
const coordinates = feature.geometry.coordinates.slice();
|
|
const properties = feature.properties;
|
|
|
|
// Create popup content
|
|
let popupContent = '<div class="event-popup">';
|
|
popupContent += `<h3>${properties.label || 'Event'}</h3>`;
|
|
|
|
// Display all properties
|
|
popupContent += '<div style="max-height: 300px; overflow-y: auto;">';
|
|
popupContent += '<table style="width: 100%; border-collapse: collapse;">';
|
|
|
|
// Sort properties alphabetically for better organization
|
|
const sortedKeys = Object.keys(properties).sort();
|
|
|
|
for (const key of sortedKeys) {
|
|
// Skip the label as it's already displayed as the title
|
|
if (key === 'label') continue;
|
|
|
|
const value = properties[key];
|
|
let displayValue;
|
|
|
|
// Format the value based on its type
|
|
if (value === null || value === undefined) {
|
|
displayValue = '<em>null</em>';
|
|
} else if (typeof value === 'object') {
|
|
displayValue = `<pre style="margin: 0; white-space: pre-wrap;">${JSON.stringify(value, null, 2)}</pre>`;
|
|
} else if (typeof value === 'string' && value.startsWith('http')) {
|
|
displayValue = `<a href="${value}" target="_blank">${value}</a>`;
|
|
} else {
|
|
displayValue = String(value);
|
|
}
|
|
|
|
popupContent += `
|
|
<tr style="border-bottom: 1px solid #eee;">
|
|
<td style="padding: 4px; font-weight: bold; vertical-align: top;">${key}:</td>
|
|
<td style="padding: 4px;">${displayValue}</td>
|
|
</tr>`;
|
|
}
|
|
|
|
popupContent += '</table>';
|
|
popupContent += '</div>';
|
|
|
|
// Add edit link
|
|
popupContent += `<div style="margin-top: 10px; text-align: center;">
|
|
<a href="/demo/edit/${properties.id}" class="edit-event-btn" style="display: inline-block; padding: 5px 10px; background-color: #0078ff; color: white; border-radius: 4px; text-decoration: none; font-weight: bold;">Edit Event</a>
|
|
</div>`;
|
|
|
|
popupContent += '</div>';
|
|
|
|
// Create popup
|
|
const popup = new maplibregl.Popup({
|
|
closeButton: true,
|
|
closeOnClick: true
|
|
}).setHTML(popupContent);
|
|
|
|
// Get event type for icon selection
|
|
const eventType = properties.what || 'unknown';
|
|
|
|
// Define icon based on event type
|
|
let iconClass = 'info-circle'; // Default icon
|
|
let iconColor = '#0078ff'; // Default color
|
|
|
|
// Map event types to icons
|
|
if (eventType.startsWith('weather')) {
|
|
iconClass = 'cloud';
|
|
iconColor = '#00d1b2'; // Teal
|
|
} else if (eventType.startsWith('traffic')) {
|
|
iconClass = 'car';
|
|
iconColor = '#ff3860'; // Red
|
|
} else if (eventType.startsWith('sport')) {
|
|
iconClass = 'futbol';
|
|
iconColor = '#3273dc'; // Blue
|
|
} else if (eventType.startsWith('culture')) {
|
|
iconClass = 'theater-masks';
|
|
iconColor = '#ffdd57'; // Yellow
|
|
} else if (eventType.startsWith('health')) {
|
|
iconClass = 'heartbeat';
|
|
iconColor = '#ff3860'; // Red
|
|
} else if (eventType.startsWith('education')) {
|
|
iconClass = 'graduation-cap';
|
|
iconColor = '#3273dc'; // Blue
|
|
} else if (eventType.startsWith('politics')) {
|
|
iconClass = 'landmark';
|
|
iconColor = '#209cee'; // Light blue
|
|
} else if (eventType.startsWith('nature')) {
|
|
iconClass = 'leaf';
|
|
iconColor = '#23d160'; // Green
|
|
}
|
|
|
|
// Create custom HTML element for marker
|
|
const el = document.createElement('div');
|
|
el.className = 'marker';
|
|
el.innerHTML = `<span class="icon" style="background-color: white; border-radius: 50%; padding: 8px; box-shadow: 0 0 5px rgba(0,0,0,0.3);">
|
|
<i class="fas fa-${iconClass}" style="color: ${iconColor}; font-size: 16px;"></i>
|
|
</span>`;
|
|
|
|
// Add marker with popup
|
|
new maplibregl.Marker(el)
|
|
.setLngLat(coordinates)
|
|
.setPopup(popup)
|
|
.addTo(map);
|
|
});
|
|
}
|
|
|
|
// Function to fit map to events bounds
|
|
function fitMapToBounds(geojson) {
|
|
if (geojson.features.length === 0) return;
|
|
|
|
// Create a bounds object
|
|
const bounds = new maplibregl.LngLatBounds();
|
|
|
|
// Extend bounds with each feature
|
|
geojson.features.forEach(feature => {
|
|
bounds.extend(feature.geometry.coordinates);
|
|
});
|
|
|
|
// Fit map to bounds with padding
|
|
map.fitBounds(bounds, {
|
|
padding: 50,
|
|
maxZoom: 12
|
|
});
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
# Set the response body and status
|
|
resp.text = html
|
|
resp.status = falcon.HTTP_200
|
|
logger.success("Successfully processed GET request to /demo")
|
|
except Exception as e:
|
|
logger.error(f"Error processing GET request to /demo: {e}")
|
|
resp.status = falcon.HTTP_500
|
|
resp.text = f"Error: {str(e)}"
|
|
|
|
def on_get_by_what(self, req, resp):
|
|
"""
|
|
Handle GET requests to the /demo/by-what endpoint.
|
|
Returns an HTML page with links to events organized by their "what" type.
|
|
|
|
Args:
|
|
req: The request object.
|
|
resp: The response object.
|
|
"""
|
|
logger.info("Processing GET request to /demo/by-what")
|
|
|
|
try:
|
|
# Set content type to HTML
|
|
resp.content_type = 'text/html'
|
|
|
|
# Fetch events from the API
|
|
try:
|
|
response = requests.get('/event?limit=1000')
|
|
if response.status_code == 200 and response.text:
|
|
events_data = response.json()
|
|
else:
|
|
logger.error(f"Error fetching events: Status code {response.status_code}, Response: {response.text}")
|
|
events_data = {"features": []}
|
|
except json.JSONDecodeError as e:
|
|
logger.error(f"Error parsing JSON response: {e}")
|
|
events_data = {"features": []}
|
|
except Exception as e:
|
|
logger.error(f"Error fetching events: {e}")
|
|
events_data = {"features": []}
|
|
|
|
# Group events by "what" type
|
|
events_by_what = defaultdict(list)
|
|
|
|
if events_data.get('features'):
|
|
for feature in events_data['features']:
|
|
properties = feature.get('properties', {})
|
|
what = properties.get('what', 'Unknown')
|
|
events_by_what[what].append({
|
|
'id': properties.get('id'),
|
|
'label': properties.get('label', 'Unnamed Event'),
|
|
'coordinates': feature.get('geometry', {}).get('coordinates', [0, 0])
|
|
})
|
|
|
|
# Create HTML response
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Events by Type - OpenEventDatabase</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
line-height: 1.6;
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
}
|
|
h1 { color: #333; }
|
|
h2 {
|
|
color: #0078ff;
|
|
margin-top: 30px;
|
|
padding-bottom: 5px;
|
|
border-bottom: 1px solid #eee;
|
|
}
|
|
ul { padding-left: 20px; }
|
|
li { margin-bottom: 8px; }
|
|
a { color: #0078ff; text-decoration: none; }
|
|
a:hover { text-decoration: underline; }
|
|
.nav {
|
|
background-color: #f5f5f5;
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
margin-bottom: 20px;
|
|
}
|
|
.nav a {
|
|
margin-right: 15px;
|
|
}
|
|
.event-count {
|
|
color: #666;
|
|
font-size: 0.9em;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="nav">
|
|
<a href="/">Home</a>
|
|
<a href="/demo">Demo Map</a>
|
|
<a href="/demo/map-by-what">Map by Event Type</a>
|
|
</div>
|
|
|
|
<h1>Events by Type</h1>
|
|
<p>This page lists all events from the OpenEventDatabase organized by their type.</p>
|
|
"""
|
|
|
|
# Add event types and their events
|
|
if events_by_what:
|
|
# Sort event types alphabetically
|
|
sorted_what_types = sorted(events_by_what.keys())
|
|
|
|
# Add quick navigation
|
|
html += "<h2>Quick Navigation</h2><ul>"
|
|
for what_type in sorted_what_types:
|
|
event_count = len(events_by_what[what_type])
|
|
html += f'<li><a href="#what-{what_type.replace(" ", "-")}">{what_type}</a> <span class="event-count">({event_count} events)</span></li>'
|
|
html += "</ul>"
|
|
|
|
# Add sections for each event type
|
|
for what_type in sorted_what_types:
|
|
events = events_by_what[what_type]
|
|
html += f'<h2 id="what-{what_type.replace(" ", "-")}">{what_type} <span class="event-count">({len(events)} events)</span></h2>'
|
|
html += "<ul>"
|
|
|
|
# Sort events by label
|
|
sorted_events = sorted(events, key=lambda x: x.get('label', ''))
|
|
|
|
for event in sorted_events:
|
|
event_id = event.get('id')
|
|
event_label = event.get('label', 'Unnamed Event')
|
|
coordinates = event.get('coordinates', [0, 0])
|
|
|
|
html += f'<li><a href="/event/{event_id}" target="_blank">{event_label}</a> '
|
|
html += f'<small>[<a href="https://www.openstreetmap.org/?mlat={coordinates[1]}&mlon={coordinates[0]}&zoom=15" target="_blank">map</a>]</small></li>'
|
|
|
|
html += "</ul>"
|
|
else:
|
|
html += "<p>No events found in the database.</p>"
|
|
|
|
html += """
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
# Set the response body and status
|
|
resp.text = html
|
|
resp.status = falcon.HTTP_200
|
|
logger.success("Successfully processed GET request to /demo/by-what")
|
|
except Exception as e:
|
|
logger.error(f"Error processing GET request to /demo/by-what: {e}")
|
|
resp.status = falcon.HTTP_500
|
|
resp.text = f"Error: {str(e)}"
|
|
|
|
def on_get_map_by_what(self, req, resp):
|
|
"""
|
|
Handle GET requests to the /demo/map-by-what endpoint.
|
|
Returns an HTML page with a MapLibre map showing events filtered by "what" type.
|
|
|
|
Args:
|
|
req: The request object.
|
|
resp: The response object.
|
|
"""
|
|
logger.info("Processing GET request to /demo/map-by-what")
|
|
|
|
try:
|
|
# Set content type to HTML
|
|
resp.content_type = 'text/html'
|
|
|
|
# Create HTML response with MapLibre map and filtering controls
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Map by Event Type - OpenEventDatabase</title>
|
|
<script src="https://unpkg.com/maplibre-gl@3.0.0/dist/maplibre-gl.js"></script>
|
|
<link href="https://unpkg.com/maplibre-gl@3.0.0/dist/maplibre-gl.css" rel="stylesheet" />
|
|
<style>
|
|
body { margin: 0; padding: 0; font-family: Arial, sans-serif; }
|
|
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
|
|
|
|
.map-overlay {
|
|
position: absolute;
|
|
top: 10px;
|
|
left: 10px;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
max-width: 300px;
|
|
max-height: 90vh;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.filter-overlay {
|
|
position: absolute;
|
|
top: 10px;
|
|
right: 10px;
|
|
background: rgba(255, 255, 255, 0.9);
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
|
|
max-width: 300px;
|
|
max-height: 90vh;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
h2, h3 { margin-top: 0; }
|
|
ul { padding-left: 20px; }
|
|
a { color: #0078ff; text-decoration: none; }
|
|
a:hover { text-decoration: underline; }
|
|
.event-popup { max-width: 300px; }
|
|
|
|
.filter-list {
|
|
list-style: none;
|
|
padding: 0;
|
|
margin: 0;
|
|
}
|
|
|
|
.filter-item {
|
|
margin-bottom: 8px;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.filter-item input {
|
|
margin-right: 8px;
|
|
}
|
|
|
|
.filter-item label {
|
|
cursor: pointer;
|
|
flex-grow: 1;
|
|
}
|
|
|
|
.filter-count {
|
|
color: #666;
|
|
font-size: 0.8em;
|
|
margin-left: 5px;
|
|
}
|
|
|
|
.color-dot {
|
|
display: inline-block;
|
|
width: 12px;
|
|
height: 12px;
|
|
border-radius: 50%;
|
|
margin-right: 5px;
|
|
}
|
|
|
|
.nav {
|
|
margin-bottom: 15px;
|
|
}
|
|
|
|
.nav a {
|
|
margin-right: 15px;
|
|
}
|
|
|
|
button {
|
|
background-color: #0078ff;
|
|
color: white;
|
|
border: none;
|
|
padding: 5px 10px;
|
|
border-radius: 3px;
|
|
cursor: pointer;
|
|
margin-right: 5px;
|
|
margin-bottom: 5px;
|
|
}
|
|
|
|
button:hover {
|
|
background-color: #0056b3;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="map"></div>
|
|
|
|
<div class="map-overlay">
|
|
<h2>Map by Event Type</h2>
|
|
<div class="nav">
|
|
<a href="/">Home</a>
|
|
<a href="/demo">Demo Map</a>
|
|
<a href="/demo/by-what">Events by Type</a>
|
|
<a href="https://source.cipherbliss.com/tykayn/oedb-backend" target="_blank" title="View Source Code on Cipherbliss">
|
|
<i class="fas fa-code-branch"></i> Source
|
|
</a>
|
|
</div>
|
|
<p>This map shows events from the OpenEventDatabase filtered by their type.</p>
|
|
<p>Use the filter panel on the right to show/hide different event types.</p>
|
|
<div id="event-info">
|
|
<p>Loading events...</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="filter-overlay">
|
|
<h3>Filter by Event Type</h3>
|
|
<div>
|
|
<button id="select-all">Select All</button>
|
|
<button id="deselect-all">Deselect All</button>
|
|
</div>
|
|
<ul id="filter-list" class="filter-list">
|
|
<li>Loading event types...</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<script>
|
|
// Initialize the map
|
|
const map = new maplibregl.Map({
|
|
container: 'map',
|
|
style: 'https://demotiles.maplibre.org/style.json',
|
|
center: [2.3522, 48.8566], // Default center (Paris)
|
|
zoom: 4
|
|
});
|
|
|
|
// Add navigation controls
|
|
map.addControl(new maplibregl.NavigationControl());
|
|
|
|
// Add attribution control with OpenStreetMap attribution
|
|
map.addControl(new maplibregl.AttributionControl({
|
|
customAttribution: '© <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
|
|
}));
|
|
|
|
// Store all events and their types
|
|
let allEvents = null;
|
|
let eventTypes = new Set();
|
|
let eventsByType = {};
|
|
let markersByType = {};
|
|
let colorsByType = {};
|
|
|
|
// Generate a color for each event type
|
|
function getColorForType(type, index) {
|
|
// Predefined colors for better visual distinction
|
|
const colors = [
|
|
'#FF5722', '#E91E63', '#9C27B0', '#673AB7', '#3F51B5',
|
|
'#2196F3', '#03A9F4', '#00BCD4', '#009688', '#4CAF50',
|
|
'#8BC34A', '#CDDC39', '#FFEB3B', '#FFC107', '#FF9800'
|
|
];
|
|
|
|
return colors[index % colors.length];
|
|
}
|
|
|
|
// Fetch events when the map is loaded
|
|
map.on('load', function() {
|
|
fetchEvents();
|
|
});
|
|
|
|
// Function to fetch events from the API
|
|
function fetchEvents() {
|
|
// Update event info
|
|
document.getElementById('event-info').innerHTML = '<p>Loading events...</p>';
|
|
|
|
// Fetch events from the API - using limit=1000 to get more events
|
|
fetch('/event?limit=1000')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.features && data.features.length > 0) {
|
|
// Store all events
|
|
allEvents = data;
|
|
|
|
// Process events by type
|
|
processEventsByType(data);
|
|
|
|
// Create filter UI
|
|
createFilterUI();
|
|
|
|
// Add all events to the map initially
|
|
addAllEventsToMap();
|
|
|
|
// Fit map to events bounds
|
|
fitMapToBounds(data);
|
|
|
|
// Update event info
|
|
document.getElementById('event-info').innerHTML =
|
|
`<p>Found ${data.features.length} events across ${eventTypes.size} different types.</p>`;
|
|
} else {
|
|
document.getElementById('event-info').innerHTML = '<p>No events found.</p>';
|
|
document.getElementById('filter-list').innerHTML = '<li>No event types available.</li>';
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error fetching events:', error);
|
|
document.getElementById('event-info').innerHTML =
|
|
`<p>Error loading events: ${error.message}</p>`;
|
|
});
|
|
}
|
|
|
|
// Process events by their "what" type
|
|
function processEventsByType(data) {
|
|
eventTypes = new Set();
|
|
eventsByType = {};
|
|
|
|
// Group events by their "what" type
|
|
data.features.forEach(feature => {
|
|
const properties = feature.properties;
|
|
const what = properties.what || 'Unknown';
|
|
|
|
// Add to set of event types
|
|
eventTypes.add(what);
|
|
|
|
// Add to events by type
|
|
if (!eventsByType[what]) {
|
|
eventsByType[what] = [];
|
|
}
|
|
eventsByType[what].push(feature);
|
|
});
|
|
|
|
// Assign colors to each type
|
|
let index = 0;
|
|
eventTypes.forEach(type => {
|
|
colorsByType[type] = getColorForType(type, index);
|
|
index++;
|
|
});
|
|
}
|
|
|
|
// Create the filter UI
|
|
function createFilterUI() {
|
|
const filterList = document.getElementById('filter-list');
|
|
filterList.innerHTML = '';
|
|
|
|
// Sort event types alphabetically
|
|
const sortedTypes = Array.from(eventTypes).sort();
|
|
|
|
// Create a checkbox for each event type
|
|
sortedTypes.forEach(type => {
|
|
const count = eventsByType[type].length;
|
|
const color = colorsByType[type];
|
|
|
|
const li = document.createElement('li');
|
|
li.className = 'filter-item';
|
|
|
|
const checkbox = document.createElement('input');
|
|
checkbox.type = 'checkbox';
|
|
checkbox.id = `filter-${type}`;
|
|
checkbox.checked = true;
|
|
checkbox.addEventListener('change', () => {
|
|
toggleEventType(type, checkbox.checked);
|
|
});
|
|
|
|
const colorDot = document.createElement('span');
|
|
colorDot.className = 'color-dot';
|
|
colorDot.style.backgroundColor = color;
|
|
|
|
const label = document.createElement('label');
|
|
label.htmlFor = `filter-${type}`;
|
|
label.appendChild(colorDot);
|
|
label.appendChild(document.createTextNode(type));
|
|
|
|
const countSpan = document.createElement('span');
|
|
countSpan.className = 'filter-count';
|
|
countSpan.textContent = `(${count})`;
|
|
label.appendChild(countSpan);
|
|
|
|
li.appendChild(checkbox);
|
|
li.appendChild(label);
|
|
filterList.appendChild(li);
|
|
});
|
|
|
|
// Add event listeners for select/deselect all buttons
|
|
document.getElementById('select-all').addEventListener('click', selectAllEventTypes);
|
|
document.getElementById('deselect-all').addEventListener('click', deselectAllEventTypes);
|
|
}
|
|
|
|
// Add all events to the map
|
|
function addAllEventsToMap() {
|
|
// Clear existing markers
|
|
clearAllMarkers();
|
|
|
|
// Add markers for each event type
|
|
Object.keys(eventsByType).forEach(type => {
|
|
addEventsOfTypeToMap(type);
|
|
});
|
|
}
|
|
|
|
// Add events of a specific type to the map
|
|
function addEventsOfTypeToMap(type) {
|
|
if (!markersByType[type]) {
|
|
markersByType[type] = [];
|
|
}
|
|
|
|
const events = eventsByType[type];
|
|
const color = colorsByType[type];
|
|
|
|
events.forEach(feature => {
|
|
const coordinates = feature.geometry.coordinates.slice();
|
|
const properties = feature.properties;
|
|
|
|
// Create popup content
|
|
let popupContent = '<div class="event-popup">';
|
|
popupContent += `<h3>${properties.label || 'Event'}</h3>`;
|
|
popupContent += `<p><strong>Type:</strong> ${type}</p>`;
|
|
|
|
// Display all properties
|
|
popupContent += '<div style="max-height: 300px; overflow-y: auto;">';
|
|
popupContent += '<table style="width: 100%; border-collapse: collapse;">';
|
|
|
|
// Sort properties alphabetically for better organization
|
|
const sortedKeys = Object.keys(properties).sort();
|
|
|
|
for (const key of sortedKeys) {
|
|
// Skip the label as it's already displayed as the title
|
|
if (key === 'label') continue;
|
|
|
|
const value = properties[key];
|
|
let displayValue;
|
|
|
|
// Format the value based on its type
|
|
if (value === null || value === undefined) {
|
|
displayValue = '<em>null</em>';
|
|
} else if (typeof value === 'object') {
|
|
displayValue = `<pre style="margin: 0; white-space: pre-wrap;">${JSON.stringify(value, null, 2)}</pre>`;
|
|
} else if (typeof value === 'string' && value.startsWith('http')) {
|
|
displayValue = `<a href="${value}" target="_blank">${value}</a>`;
|
|
} else {
|
|
displayValue = String(value);
|
|
}
|
|
|
|
popupContent += `
|
|
<tr style="border-bottom: 1px solid #eee;">
|
|
<td style="padding: 4px; font-weight: bold; vertical-align: top;">${key}:</td>
|
|
<td style="padding: 4px;">${displayValue}</td>
|
|
</tr>`;
|
|
}
|
|
|
|
popupContent += '</table>';
|
|
popupContent += '</div>';
|
|
|
|
popupContent += '</div>';
|
|
|
|
// Create popup
|
|
const popup = new maplibregl.Popup({
|
|
closeButton: true,
|
|
closeOnClick: true
|
|
}).setHTML(popupContent);
|
|
|
|
// Add marker with popup
|
|
const marker = new maplibregl.Marker({
|
|
color: color
|
|
})
|
|
.setLngLat(coordinates)
|
|
.setPopup(popup)
|
|
.addTo(map);
|
|
|
|
// Store marker reference
|
|
markersByType[type].push(marker);
|
|
});
|
|
}
|
|
|
|
// Toggle visibility of events by type
|
|
function toggleEventType(type, visible) {
|
|
if (!markersByType[type]) return;
|
|
|
|
markersByType[type].forEach(marker => {
|
|
if (visible) {
|
|
marker.addTo(map);
|
|
} else {
|
|
marker.remove();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Select all event types
|
|
function selectAllEventTypes() {
|
|
const checkboxes = document.querySelectorAll('#filter-list input[type="checkbox"]');
|
|
checkboxes.forEach(checkbox => {
|
|
checkbox.checked = true;
|
|
const type = checkbox.id.replace('filter-', '');
|
|
toggleEventType(type, true);
|
|
});
|
|
}
|
|
|
|
// Deselect all event types
|
|
function deselectAllEventTypes() {
|
|
const checkboxes = document.querySelectorAll('#filter-list input[type="checkbox"]');
|
|
checkboxes.forEach(checkbox => {
|
|
checkbox.checked = false;
|
|
const type = checkbox.id.replace('filter-', '');
|
|
toggleEventType(type, false);
|
|
});
|
|
}
|
|
|
|
// Clear all markers from the map
|
|
function clearAllMarkers() {
|
|
Object.keys(markersByType).forEach(type => {
|
|
if (markersByType[type]) {
|
|
markersByType[type].forEach(marker => marker.remove());
|
|
}
|
|
});
|
|
markersByType = {};
|
|
}
|
|
|
|
// Function to fit map to events bounds
|
|
function fitMapToBounds(geojson) {
|
|
if (geojson.features.length === 0) return;
|
|
|
|
// Create a bounds object
|
|
const bounds = new maplibregl.LngLatBounds();
|
|
|
|
// Extend bounds with each feature
|
|
geojson.features.forEach(feature => {
|
|
bounds.extend(feature.geometry.coordinates);
|
|
});
|
|
|
|
// Fit map to bounds with padding
|
|
map.fitBounds(bounds, {
|
|
padding: 50,
|
|
maxZoom: 12
|
|
});
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
# Set the response body and status
|
|
resp.text = html
|
|
resp.status = falcon.HTTP_200
|
|
logger.success("Successfully processed GET request to /demo/map-by-what")
|
|
except Exception as e:
|
|
logger.error(f"Error processing GET request to /demo/map-by-what: {e}")
|
|
resp.status = falcon.HTTP_500
|
|
resp.text = f"Error: {str(e)}"
|
|
events_by_what = defaultdict(list)
|
|
|
|
if events_data.get('features'):
|
|
for feature in events_data['features']:
|
|
properties = feature.get('properties', {})
|
|
what = properties.get('what', 'Unknown')
|
|
events_by_what[what].append({
|
|
'id': properties.get('id'),
|
|
'label': properties.get('label', 'Unnamed Event'),
|
|
'coordinates': feature.get('geometry', {}).get('coordinates', [0, 0])
|
|
})
|
|
|
|
# Create HTML response
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Events by Type - OpenEventDatabase</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
line-height: 1.6;
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
}
|
|
h1 { color: #333; }
|
|
h2 {
|
|
color: #0078ff;
|
|
margin-top: 30px;
|
|
padding-bottom: 5px;
|
|
border-bottom: 1px solid #eee;
|
|
}
|
|
ul { padding-left: 20px; }
|
|
li { margin-bottom: 8px; }
|
|
a { color: #0078ff; text-decoration: none; }
|
|
a:hover { text-decoration: underline; }
|
|
.nav {
|
|
background-color: #f5f5f5;
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
margin-bottom: 20px;
|
|
}
|
|
.nav a {
|
|
margin-right: 15px;
|
|
}
|
|
.event-count {
|
|
color: #666;
|
|
font-size: 0.9em;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="nav">
|
|
<a href="/">Home</a>
|
|
<a href="/demo">Demo Map</a>
|
|
<a href="/demo/map-by-what">Map by Event Type</a>
|
|
</div>
|
|
|
|
<h1>Events by Type</h1>
|
|
<p>This page lists all events from the OpenEventDatabase organized by their type.</p>
|
|
"""
|
|
|
|
# Add event types and their events
|
|
if events_by_what:
|
|
# Sort event types alphabetically
|
|
sorted_what_types = sorted(events_by_what.keys())
|
|
|
|
# Add quick navigation
|
|
html += "<h2>Quick Navigation</h2><ul>"
|
|
for what_type in sorted_what_types:
|
|
event_count = len(events_by_what[what_type])
|
|
html += f'<li><a href="#what-{what_type.replace(" ", "-")}">{what_type}</a> <span class="event-count">({event_count} events)</span></li>'
|
|
html += "</ul>"
|
|
|
|
# Add sections for each event type
|
|
for what_type in sorted_what_types:
|
|
events = events_by_what[what_type]
|
|
html += f'<h2 id="what-{what_type.replace(" ", "-")}">{what_type} <span class="event-count">({len(events)} events)</span></h2>'
|
|
html += "<ul>"
|
|
|
|
# Sort events by label
|
|
sorted_events = sorted(events, key=lambda x: x.get('label', ''))
|
|
|
|
for event in sorted_events:
|
|
event_id = event.get('id')
|
|
event_label = event.get('label', 'Unnamed Event')
|
|
coordinates = event.get('coordinates', [0, 0])
|
|
|
|
html += f'<li><a href="/event/{event_id}" target="_blank">{event_label}</a> '
|
|
html += f'<small>[<a href="https://www.openstreetmap.org/?mlat={coordinates[1]}&mlon={coordinates[0]}&zoom=15" target="_blank">map</a>]</small></li>'
|
|
|
|
html += "</ul>"
|
|
else:
|
|
html += "<p>No events found in the database.</p>"
|
|
|
|
html += """
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
# Set the response body and status
|
|
resp.text = html
|
|
resp.status = falcon.HTTP_200
|
|
logger.success("Successfully processed GET request to /demo/by-what")
|
|
except Exception as e:
|
|
logger.error(f"Error processing GET request to /demo/by-what: {e}")
|
|
resp.status = falcon.HTTP_500
|
|
resp.text = f"Error: {str(e)}"
|
|
|
|
# Create a global instance of DemoResource
|
|
demo = DemoResource() |