2908 lines
No EOL
138 KiB
Python
2908 lines
No EOL
138 KiB
Python
"""
|
|
Demo resource for the OpenEventDatabase.
|
|
"""
|
|
|
|
import falcon
|
|
import requests
|
|
import json
|
|
import os
|
|
from collections import defaultdict
|
|
from oedb.utils.logging import logger
|
|
from oedb.utils.db import load_env_from_file
|
|
|
|
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://api.openevent/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/search" target="_blank">/demo/search - Advanced Search</a></li>
|
|
<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="/demo/traffic" target="_blank">/demo/traffic - Report Traffic Jam</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_search(self, req, resp):
|
|
"""
|
|
Handle GET requests to the /demo/search endpoint.
|
|
Returns an HTML page with a form for searching events and displaying results.
|
|
|
|
Args:
|
|
req: The request object.
|
|
resp: The response object.
|
|
"""
|
|
logger.info("Processing GET request to /demo/search")
|
|
|
|
try:
|
|
# Set content type to HTML
|
|
resp.content_type = 'text/html'
|
|
|
|
# Create HTML response with search form
|
|
html = """
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Search Events - 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">
|
|
<script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js"></script>
|
|
<style>
|
|
body {
|
|
margin: 0;
|
|
padding: 20px;
|
|
font-family: Arial, sans-serif;
|
|
background-color: #f5f5f5;
|
|
}
|
|
.container {
|
|
max-width: 1200px;
|
|
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;
|
|
}
|
|
.nav-links {
|
|
margin-bottom: 20px;
|
|
}
|
|
.nav-links a {
|
|
color: #0078ff;
|
|
text-decoration: none;
|
|
margin-right: 15px;
|
|
}
|
|
.nav-links a:hover {
|
|
text-decoration: underline;
|
|
}
|
|
.tabs-container {
|
|
margin-top: 20px;
|
|
}
|
|
.tab-content {
|
|
display: none;
|
|
padding: 20px;
|
|
border: 1px solid #ddd;
|
|
border-top: none;
|
|
}
|
|
.tab-content.active {
|
|
display: block;
|
|
}
|
|
.tab-buttons {
|
|
display: flex;
|
|
border-bottom: 1px solid #ddd;
|
|
}
|
|
.tab-button {
|
|
padding: 10px 20px;
|
|
background-color: #f1f1f1;
|
|
border: 1px solid #ddd;
|
|
border-bottom: none;
|
|
cursor: pointer;
|
|
margin-right: 5px;
|
|
}
|
|
.tab-button.active {
|
|
background-color: white;
|
|
border-bottom: 1px solid white;
|
|
margin-bottom: -1px;
|
|
}
|
|
#map {
|
|
width: 100%;
|
|
height: 500px;
|
|
margin-top: 20px;
|
|
border-radius: 4px;
|
|
}
|
|
.results-table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
margin-top: 20px;
|
|
}
|
|
.results-table th, .results-table td {
|
|
padding: 8px;
|
|
text-align: left;
|
|
border-bottom: 1px solid #ddd;
|
|
}
|
|
.results-table th {
|
|
background-color: #f2f2f2;
|
|
}
|
|
.download-buttons {
|
|
margin-top: 20px;
|
|
text-align: right;
|
|
}
|
|
.download-button {
|
|
display: inline-block;
|
|
padding: 8px 16px;
|
|
background-color: #0078ff;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
margin-left: 10px;
|
|
text-decoration: none;
|
|
}
|
|
.download-button:hover {
|
|
background-color: #0056b3;
|
|
}
|
|
.form-row {
|
|
display: flex;
|
|
gap: 15px;
|
|
margin-bottom: 15px;
|
|
}
|
|
.form-group {
|
|
flex: 1;
|
|
}
|
|
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;
|
|
}
|
|
.note {
|
|
font-size: 12px;
|
|
color: #666;
|
|
margin-top: 5px;
|
|
}
|
|
button {
|
|
background-color: #0078ff;
|
|
color: white;
|
|
border: none;
|
|
padding: 10px 15px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
}
|
|
button:hover {
|
|
background-color: #0056b3;
|
|
}
|
|
#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;
|
|
}
|
|
</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>
|
|
</div>
|
|
|
|
<h1>Search Events</h1>
|
|
|
|
<form id="searchForm">
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="what">Event Type</label>
|
|
<input type="text" id="what" name="what" placeholder="e.g., sport.match.football">
|
|
<div class="note">Category of the event (e.g., sport.match.football, culture.festival)</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="type">Event Type</label>
|
|
<select id="type" name="type">
|
|
<option value="">Any</option>
|
|
<option value="scheduled">Scheduled</option>
|
|
<option value="forecast">Forecast</option>
|
|
<option value="unscheduled">Unscheduled</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="when">When</label>
|
|
<select id="when" name="when">
|
|
<option value="now">Now</option>
|
|
<option value="today">Today</option>
|
|
<option value="yesterday">Yesterday</option>
|
|
<option value="tomorrow">Tomorrow</option>
|
|
<option value="lasthour">Last Hour</option>
|
|
<option value="nexthour">Next Hour</option>
|
|
<option value="last7days">Last 7 Days</option>
|
|
<option value="next7days">Next 7 Days</option>
|
|
<option value="last30days">Last 30 Days</option>
|
|
<option value="next30days">Next 30 Days</option>
|
|
<option value="custom">Custom Range</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group" id="customDateGroup" style="display: none;">
|
|
<label for="start">Start Date</label>
|
|
<input type="datetime-local" id="start" name="start">
|
|
</div>
|
|
|
|
<div class="form-group" id="customDateEndGroup" style="display: none;">
|
|
<label for="stop">End Date</label>
|
|
<input type="datetime-local" id="stop" name="stop">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="near">Near (Longitude, Latitude, Distance in meters)</label>
|
|
<input type="text" id="near" name="near" placeholder="e.g., 2.3522,48.8566,10000">
|
|
<div class="note">Search for events near a specific location (e.g., 2.3522,48.8566,10000 for events within 10km of Paris)</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="bbox">Bounding Box (East, South, West, North)</label>
|
|
<input type="text" id="bbox" name="bbox" placeholder="e.g., -5.0,41.0,10.0,52.0">
|
|
<div class="note">Search for events within a geographic bounding box</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="where_osm">OpenStreetMap ID</label>
|
|
<input type="text" id="where_osm" name="where:osm" placeholder="e.g., R12345">
|
|
<div class="note">Search for events associated with a specific OpenStreetMap ID</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="where_wikidata">Wikidata ID</label>
|
|
<input type="text" id="where_wikidata" name="where:wikidata" placeholder="e.g., Q90">
|
|
<div class="note">Search for events associated with a specific Wikidata ID</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="limit">Result Limit</label>
|
|
<input type="number" id="limit" name="limit" value="200" min="1" max="1000">
|
|
<div class="note">Maximum number of results to return (default: 200)</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="geom">Geometry Detail</label>
|
|
<select id="geom" name="geom">
|
|
<option value="">Default (Centroid)</option>
|
|
<option value="full">Full Geometry</option>
|
|
<option value="only">Geometry Only</option>
|
|
<option value="0.01">Simplified (0.01)</option>
|
|
</select>
|
|
<div class="note">Controls the level of detail in the geometry portion of the response</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Search Area (Draw on Map)</label>
|
|
<div id="map"></div>
|
|
<div class="note">Draw a polygon on the map to define the search area, or use the form fields above</div>
|
|
</div>
|
|
|
|
<button type="submit">Search Events</button>
|
|
</form>
|
|
|
|
<div id="result"></div>
|
|
|
|
<div id="resultsContainer" style="display: none;">
|
|
<h2>Search Results</h2>
|
|
|
|
<div class="tabs-container">
|
|
<div class="tab-buttons">
|
|
<div class="tab-button active" data-tab="map-tab">Map View</div>
|
|
<div class="tab-button" data-tab="table-tab">Table View</div>
|
|
</div>
|
|
|
|
<div id="map-tab" class="tab-content active">
|
|
<div id="resultsMap" style="width: 100%; height: 500px;"></div>
|
|
</div>
|
|
|
|
<div id="table-tab" class="tab-content">
|
|
<table class="results-table" id="resultsTable">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Label</th>
|
|
<th>Type</th>
|
|
<th>What</th>
|
|
<th>Where</th>
|
|
<th>Start</th>
|
|
<th>Stop</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<!-- Results will be added here dynamically -->
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="download-buttons">
|
|
<button id="downloadCsv" class="download-button">Download CSV</button>
|
|
<button id="downloadJson" class="download-button">Download JSON</button>
|
|
</div>
|
|
</div>
|
|
</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 draw controls for polygon
|
|
let drawnPolygon = null;
|
|
let drawingMode = false;
|
|
let points = [];
|
|
let lineString = null;
|
|
let polygonFill = null;
|
|
|
|
// Add a button to toggle drawing mode
|
|
const drawButton = document.createElement('button');
|
|
drawButton.textContent = 'Draw Polygon';
|
|
drawButton.style.position = 'absolute';
|
|
drawButton.style.top = '10px';
|
|
drawButton.style.right = '10px';
|
|
drawButton.style.zIndex = '1';
|
|
drawButton.style.padding = '5px 10px';
|
|
drawButton.style.backgroundColor = '#0078ff';
|
|
drawButton.style.color = 'white';
|
|
drawButton.style.border = 'none';
|
|
drawButton.style.borderRadius = '3px';
|
|
drawButton.style.cursor = 'pointer';
|
|
document.getElementById('map').appendChild(drawButton);
|
|
|
|
drawButton.addEventListener('click', () => {
|
|
drawingMode = !drawingMode;
|
|
drawButton.textContent = drawingMode ? 'Cancel Drawing' : 'Draw Polygon';
|
|
|
|
if (!drawingMode) {
|
|
// Clear the drawing
|
|
points = [];
|
|
if (lineString) {
|
|
map.removeLayer('line-string');
|
|
map.removeSource('line-string');
|
|
lineString = null;
|
|
}
|
|
if (polygonFill) {
|
|
map.removeLayer('polygon-fill');
|
|
map.removeSource('polygon-fill');
|
|
polygonFill = null;
|
|
}
|
|
}
|
|
});
|
|
|
|
// Handle map click events for drawing
|
|
map.on('click', (e) => {
|
|
if (!drawingMode) return;
|
|
|
|
const coords = [e.lngLat.lng, e.lngLat.lat];
|
|
points.push(coords);
|
|
|
|
// If we have at least 3 points, create a polygon
|
|
if (points.length >= 3) {
|
|
const polygonCoords = [...points, points[0]]; // Close the polygon
|
|
|
|
// Create or update the line string
|
|
if (lineString) {
|
|
map.removeLayer('line-string');
|
|
map.removeSource('line-string');
|
|
}
|
|
|
|
lineString = {
|
|
type: 'Feature',
|
|
geometry: {
|
|
type: 'LineString',
|
|
coordinates: polygonCoords
|
|
}
|
|
};
|
|
|
|
map.addSource('line-string', {
|
|
type: 'geojson',
|
|
data: lineString
|
|
});
|
|
|
|
map.addLayer({
|
|
id: 'line-string',
|
|
type: 'line',
|
|
source: 'line-string',
|
|
paint: {
|
|
'line-color': '#0078ff',
|
|
'line-width': 2
|
|
}
|
|
});
|
|
|
|
// Create or update the polygon fill
|
|
if (polygonFill) {
|
|
map.removeLayer('polygon-fill');
|
|
map.removeSource('polygon-fill');
|
|
}
|
|
|
|
polygonFill = {
|
|
type: 'Feature',
|
|
geometry: {
|
|
type: 'Polygon',
|
|
coordinates: [polygonCoords]
|
|
}
|
|
};
|
|
|
|
map.addSource('polygon-fill', {
|
|
type: 'geojson',
|
|
data: polygonFill
|
|
});
|
|
|
|
map.addLayer({
|
|
id: 'polygon-fill',
|
|
type: 'fill',
|
|
source: 'polygon-fill',
|
|
paint: {
|
|
'fill-color': '#0078ff',
|
|
'fill-opacity': 0.2
|
|
}
|
|
});
|
|
|
|
// Store the drawn polygon for search
|
|
drawnPolygon = {
|
|
type: 'Polygon',
|
|
coordinates: [polygonCoords]
|
|
};
|
|
}
|
|
});
|
|
|
|
// Handle custom date range selection
|
|
document.getElementById('when').addEventListener('change', function() {
|
|
const customDateGroup = document.getElementById('customDateGroup');
|
|
const customDateEndGroup = document.getElementById('customDateEndGroup');
|
|
|
|
if (this.value === 'custom') {
|
|
customDateGroup.style.display = 'block';
|
|
customDateEndGroup.style.display = 'block';
|
|
} else {
|
|
customDateGroup.style.display = 'none';
|
|
customDateEndGroup.style.display = 'none';
|
|
}
|
|
});
|
|
|
|
// Handle form submission
|
|
document.getElementById('searchForm').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
// Show loading message
|
|
const resultElement = document.getElementById('result');
|
|
resultElement.textContent = 'Searching...';
|
|
resultElement.className = '';
|
|
resultElement.style.display = 'block';
|
|
|
|
// Get form values
|
|
const formData = new FormData(this);
|
|
const params = new URLSearchParams();
|
|
|
|
// Add form fields to params
|
|
for (const [key, value] of formData.entries()) {
|
|
if (value) {
|
|
params.append(key, value);
|
|
}
|
|
}
|
|
|
|
// Handle custom date range
|
|
if (formData.get('when') === 'custom') {
|
|
params.delete('when');
|
|
} else {
|
|
params.delete('start');
|
|
params.delete('stop');
|
|
}
|
|
|
|
// Prepare the request
|
|
let url = '/event/search';
|
|
let method = 'POST';
|
|
let body = null;
|
|
|
|
// If we have a drawn polygon, use it for the search
|
|
if (drawnPolygon) {
|
|
body = JSON.stringify({
|
|
geometry: drawnPolygon
|
|
});
|
|
} else if (formData.get('near') || formData.get('bbox')) {
|
|
// If we have near or bbox parameters, use GET request
|
|
url = '/event?' + params.toString();
|
|
method = 'GET';
|
|
} else {
|
|
// Default to a simple point search in Paris if no spatial filter is provided
|
|
body = JSON.stringify({
|
|
geometry: {
|
|
type: 'Point',
|
|
coordinates: [2.3522, 48.8566]
|
|
}
|
|
});
|
|
}
|
|
|
|
// Make the request
|
|
fetch(url + (method === 'GET' ? '' : '?' + params.toString()), {
|
|
method: method,
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: method === 'POST' ? body : null
|
|
})
|
|
.then(response => {
|
|
if (response.ok) {
|
|
return response.json();
|
|
} else {
|
|
return response.text().then(text => {
|
|
throw new Error(text || response.statusText);
|
|
});
|
|
}
|
|
})
|
|
.then(data => {
|
|
// Show success message
|
|
resultElement.textContent = `Found ${data.features ? data.features.length : 0} events`;
|
|
resultElement.className = 'success';
|
|
|
|
// Display results
|
|
displayResults(data);
|
|
})
|
|
.catch(error => {
|
|
// Show error message
|
|
resultElement.textContent = `Error: ${error.message}`;
|
|
resultElement.className = 'error';
|
|
|
|
// Hide results container
|
|
document.getElementById('resultsContainer').style.display = 'none';
|
|
});
|
|
});
|
|
|
|
// Function to display search results
|
|
function displayResults(data) {
|
|
// Show results container
|
|
document.getElementById('resultsContainer').style.display = 'block';
|
|
|
|
// Initialize results map
|
|
const resultsMap = new maplibregl.Map({
|
|
container: 'resultsMap',
|
|
style: 'https://demotiles.maplibre.org/style.json',
|
|
center: [2.3522, 48.8566], // Default center (Paris)
|
|
zoom: 4
|
|
});
|
|
|
|
// Add navigation controls to results map
|
|
resultsMap.addControl(new maplibregl.NavigationControl());
|
|
|
|
// Add events to the map
|
|
resultsMap.on('load', function() {
|
|
// Add events as a source
|
|
resultsMap.addSource('events', {
|
|
type: 'geojson',
|
|
data: data
|
|
});
|
|
|
|
// Add a circle layer for events
|
|
resultsMap.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
|
|
if (data.features) {
|
|
data.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 key properties
|
|
if (properties.what) {
|
|
popupContent += `<p><strong>Type:</strong> ${properties.what}</p>`;
|
|
}
|
|
if (properties.where) {
|
|
popupContent += `<p><strong>Where:</strong> ${properties.where}</p>`;
|
|
}
|
|
if (properties.start) {
|
|
popupContent += `<p><strong>Start:</strong> ${properties.start}</p>`;
|
|
}
|
|
if (properties.stop) {
|
|
popupContent += `<p><strong>End:</strong> ${properties.stop}</p>`;
|
|
}
|
|
|
|
// Add link to view full event
|
|
popupContent += `<p><a href="/event/${properties.id}" target="_blank">View Event</a></p>`;
|
|
|
|
popupContent += '</div>';
|
|
|
|
// Create popup
|
|
const popup = new maplibregl.Popup({
|
|
closeButton: true,
|
|
closeOnClick: true
|
|
}).setHTML(popupContent);
|
|
|
|
// Add marker with popup
|
|
new maplibregl.Marker({
|
|
color: '#FF5722'
|
|
})
|
|
.setLngLat(coordinates)
|
|
.setPopup(popup)
|
|
.addTo(resultsMap);
|
|
});
|
|
|
|
// Fit map to events bounds
|
|
if (data.features.length > 0) {
|
|
const bounds = new maplibregl.LngLatBounds();
|
|
|
|
data.features.forEach(feature => {
|
|
bounds.extend(feature.geometry.coordinates);
|
|
});
|
|
|
|
resultsMap.fitBounds(bounds, {
|
|
padding: 50,
|
|
maxZoom: 12
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
// Populate table with results
|
|
const tableBody = document.getElementById('resultsTable').getElementsByTagName('tbody')[0];
|
|
tableBody.innerHTML = '';
|
|
|
|
if (data.features) {
|
|
data.features.forEach(feature => {
|
|
const properties = feature.properties;
|
|
|
|
const row = tableBody.insertRow();
|
|
row.insertCell(0).textContent = properties.id || '';
|
|
row.insertCell(1).textContent = properties.label || '';
|
|
row.insertCell(2).textContent = properties.type || '';
|
|
row.insertCell(3).textContent = properties.what || '';
|
|
row.insertCell(4).textContent = properties.where || '';
|
|
row.insertCell(5).textContent = properties.start || '';
|
|
row.insertCell(6).textContent = properties.stop || '';
|
|
});
|
|
}
|
|
|
|
// Store the data for download
|
|
window.searchResults = data;
|
|
}
|
|
|
|
// Handle tab switching
|
|
document.querySelectorAll('.tab-button').forEach(button => {
|
|
button.addEventListener('click', () => {
|
|
// Remove active class from all buttons and content
|
|
document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('active'));
|
|
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
|
|
|
|
// Add active class to clicked button and corresponding content
|
|
button.classList.add('active');
|
|
document.getElementById(button.dataset.tab).classList.add('active');
|
|
});
|
|
});
|
|
|
|
// Handle CSV download
|
|
document.getElementById('downloadCsv').addEventListener('click', () => {
|
|
if (!window.searchResults || !window.searchResults.features) {
|
|
alert('No search results to download');
|
|
return;
|
|
}
|
|
|
|
// Convert GeoJSON to CSV
|
|
let csv = 'id,label,type,what,where,start,stop,longitude,latitude\\n';
|
|
|
|
window.searchResults.features.forEach(feature => {
|
|
const p = feature.properties;
|
|
const coords = feature.geometry.coordinates;
|
|
|
|
csv += `"${p.id || ''}","${p.label || ''}","${p.type || ''}","${p.what || ''}","${p.where || ''}","${p.start || ''}","${p.stop || ''}",${coords[0]},${coords[1]}\\n`;
|
|
});
|
|
|
|
// Create download link
|
|
const blob = new Blob([csv], { type: 'text/csv' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = 'search_results.csv';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(url);
|
|
});
|
|
|
|
// Handle JSON download
|
|
document.getElementById('downloadJson').addEventListener('click', () => {
|
|
if (!window.searchResults) {
|
|
alert('No search results to download');
|
|
return;
|
|
}
|
|
|
|
// Create download link
|
|
const blob = new Blob([JSON.stringify(window.searchResults, null, 2)], { type: 'application/json' });
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = 'search_results.json';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
URL.revokeObjectURL(url);
|
|
});
|
|
</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/search")
|
|
except Exception as e:
|
|
logger.error(f"Error processing GET request to /demo/search: {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)}"
|
|
|
|
def on_get_traffic(self, req, resp):
|
|
"""
|
|
Handle GET requests to the /demo/traffic endpoint.
|
|
Returns an HTML page with a form for reporting traffic jams.
|
|
|
|
Args:
|
|
req: The request object.
|
|
resp: The response object.
|
|
"""
|
|
logger.info("Processing GET request to /demo/traffic")
|
|
|
|
try:
|
|
# Set content type to HTML
|
|
resp.content_type = 'text/html'
|
|
|
|
# Load environment variables from .env file
|
|
load_env_from_file()
|
|
|
|
# Get OAuth2 configuration parameters
|
|
client_id = os.getenv("CLIENT_ID", "")
|
|
client_secret = os.getenv("CLIENT_SECRET", "")
|
|
client_authorizations = os.getenv("CLIENT_AUTORIZATIONS", "read_prefs")
|
|
client_redirect = os.getenv("CLIENT_REDIRECT", "")
|
|
|
|
# Check if we have an authorization code in the query parameters
|
|
auth_code = req.params.get('code', None)
|
|
auth_state = req.params.get('state', None)
|
|
|
|
# Variables to track authentication state
|
|
is_authenticated = False
|
|
osm_username = ""
|
|
osm_user_id = ""
|
|
|
|
# If we have an authorization code, exchange it for an access token
|
|
if auth_code:
|
|
logger.info(f"Received authorization code: {auth_code}")
|
|
|
|
try:
|
|
# Exchange authorization code for access token
|
|
token_url = "https://www.openstreetmap.org/oauth2/token"
|
|
token_data = {
|
|
"grant_type": "authorization_code",
|
|
"code": auth_code,
|
|
"redirect_uri": client_redirect,
|
|
"client_id": client_id,
|
|
"client_secret": client_secret
|
|
}
|
|
|
|
token_response = requests.post(token_url, data=token_data)
|
|
token_response.raise_for_status()
|
|
token_info = token_response.json()
|
|
|
|
access_token = token_info.get("access_token")
|
|
|
|
if access_token:
|
|
# Use access token to get user information
|
|
user_url = "https://api.openstreetmap.org/api/0.6/user/details.json"
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
|
|
|
user_response = requests.get(user_url, headers=headers)
|
|
user_response.raise_for_status()
|
|
user_info = user_response.json()
|
|
|
|
# Extract user information
|
|
user = user_info.get("user", {})
|
|
osm_username = user.get("display_name", "")
|
|
osm_user_id = user.get("id", "")
|
|
|
|
if osm_username:
|
|
is_authenticated = True
|
|
logger.info(f"User authenticated: {osm_username} (ID: {osm_user_id})")
|
|
else:
|
|
logger.error("Failed to get OSM username from user details")
|
|
else:
|
|
logger.error("Failed to get access token from token response")
|
|
except Exception as e:
|
|
logger.error(f"Error during OAuth2 token exchange: {e}")
|
|
|
|
# Create HTML response with form
|
|
# Start with the common HTML header
|
|
html_header = f"""
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Report Traffic Jam - 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: 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;
|
|
}}
|
|
.geolocation-btn {{
|
|
background-color: #28a745;
|
|
margin-bottom: 10px;
|
|
}}
|
|
.geolocation-btn:hover {{
|
|
background-color: #218838;
|
|
}}
|
|
.loading {{
|
|
display: inline-block;
|
|
width: 20px;
|
|
height: 20px;
|
|
border: 3px solid rgba(255,255,255,.3);
|
|
border-radius: 50%;
|
|
border-top-color: #fff;
|
|
animation: spin 1s ease-in-out infinite;
|
|
margin-right: 10px;
|
|
vertical-align: middle;
|
|
}}
|
|
@keyframes spin {{
|
|
to {{ transform: rotate(360deg); }}
|
|
}}
|
|
.auth-section {{
|
|
background-color: #f8f9fa;
|
|
padding: 15px;
|
|
border-radius: 5px;
|
|
margin-bottom: 20px;
|
|
border: 1px solid #e9ecef;
|
|
}}
|
|
.auth-section h3 {{
|
|
margin-top: 0;
|
|
margin-bottom: 10px;
|
|
}}
|
|
.auth-info {{
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
}}
|
|
.auth-info img {{
|
|
width: 40px;
|
|
height: 40px;
|
|
border-radius: 50%;
|
|
}}
|
|
.osm-login-btn {{
|
|
background-color: #7ebc6f;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}}
|
|
.osm-login-btn:hover {{
|
|
background-color: #6ba75e;
|
|
}}
|
|
.osm-logo {{
|
|
width: 20px;
|
|
height: 20px;
|
|
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="white"><path d="M12 0C5.373 0 0 5.373 0 12s5.373 12 12 12 12-5.373 12-12S18.627 0 12 0zm-1.66 4.322c.944 0 1.71.766 1.71 1.71s-.766 1.71-1.71 1.71-1.71-.766-1.71-1.71.766-1.71 1.71-1.71zm6.482 13.356H4.322v-1.71h12.5v1.71zm0-3.322H4.322v-1.71h12.5v1.71zm0-3.322H4.322v-1.71h12.5v1.71z"/></svg>');
|
|
background-repeat: no-repeat;
|
|
background-position: center;
|
|
}}
|
|
</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>
|
|
</div>
|
|
|
|
<h1>Report Traffic Jam</h1>
|
|
|
|
<div class="auth-section">
|
|
<h3>OpenStreetMap Authentication</h3>
|
|
"""
|
|
|
|
# Add authentication section based on authentication status
|
|
if is_authenticated:
|
|
auth_section = f"""
|
|
<div class="auth-info">
|
|
<div>
|
|
<p>Logged in as <strong>{osm_username}</strong></p>
|
|
<p><a href="https://www.openstreetmap.org/user/{osm_username}" target="_blank">View OSM Profile</a></p>
|
|
<input type="hidden" id="osmUsername" value="{osm_username}">
|
|
<input type="hidden" id="osmUserId" value="{osm_user_id}">
|
|
</div>
|
|
</div>
|
|
"""
|
|
else:
|
|
auth_section = f"""
|
|
<p>Authenticate with your OpenStreetMap account to include your username in the traffic report.</p>
|
|
<a href="https://www.openstreetmap.org/oauth2/authorize?client_id={client_id}&redirect_uri={client_redirect}&response_type=code&scope={client_authorizations}" class="osm-login-btn button">
|
|
<span class="osm-logo"></span>
|
|
Login with OpenStreetMap
|
|
</a>
|
|
"""
|
|
|
|
# Add the rest of the HTML template
|
|
html_footer = """
|
|
</div>
|
|
|
|
<button id="geolocateBtn" class="geolocation-btn">
|
|
<span id="geolocateSpinner" class="loading" style="display: none;"></span>
|
|
Get My Current Location
|
|
</button>
|
|
|
|
<form id="trafficForm">
|
|
<div class="form-group">
|
|
<label for="label" class="required">Traffic Jam Description</label>
|
|
<input type="text" id="label" name="label" placeholder="e.g., Heavy traffic on Highway A1" required value="bouchon">
|
|
</div>
|
|
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label for="severity" class="required">Severity</label>
|
|
<select id="severity" name="severity" required>
|
|
<option value="low">Low (Slow moving)</option>
|
|
<option value="medium" selected>Medium (Very slow)</option>
|
|
<option value="high">High (Standstill)</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="cause">Cause (if known)</label>
|
|
<input type="text" id="cause" name="cause" placeholder="e.g., Accident, Construction, Weather">
|
|
</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">Estimated End Time</label>
|
|
<input type="datetime-local" id="stop" name="stop" required value="">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label for="where">Road/Location Name</label>
|
|
<input type="text" id="where" name="where" placeholder="e.g., Highway A1, Main Street">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label class="required">Location</label>
|
|
<div id="map"></div>
|
|
<div class="note">Click on the map to set the traffic jam location or use the "Get My Current Location" button</div>
|
|
</div>
|
|
|
|
<button type="submit">Report Traffic Jam</button>
|
|
</form>
|
|
|
|
<div id="result"></div>
|
|
</div>
|
|
|
|
<script>
|
|
// Set default date values (current time and +1 hour)
|
|
function setDefaultDates() {
|
|
const now = new Date();
|
|
const nowISO = now.toISOString().slice(0, 16); // Format: YYYY-MM-DDThh:mm
|
|
|
|
// Set start time to current time
|
|
document.getElementById('start').value = nowISO;
|
|
|
|
// Set end time to current time + 1 hour
|
|
const oneHourLater = new Date(now.getTime() + 60 * 60 * 1000);
|
|
document.getElementById('stop').value = oneHourLater.toISOString().slice(0, 16);
|
|
}
|
|
|
|
// Call function to set default dates
|
|
setDefaultDates();
|
|
|
|
// 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 marker for traffic jam location
|
|
let marker = new maplibregl.Marker({
|
|
draggable: true,
|
|
color: '#ff3860' // Red color for traffic jam
|
|
});
|
|
|
|
// Add marker on map click
|
|
map.on('click', function(e) {
|
|
marker.setLngLat(e.lngLat).addTo(map);
|
|
});
|
|
|
|
// Handle geolocation button click
|
|
document.getElementById('geolocateBtn').addEventListener('click', function() {
|
|
// Show loading spinner
|
|
document.getElementById('geolocateSpinner').style.display = 'inline-block';
|
|
this.disabled = true;
|
|
|
|
// Check if geolocation is available
|
|
if (navigator.geolocation) {
|
|
navigator.geolocation.getCurrentPosition(
|
|
// Success callback
|
|
function(position) {
|
|
const lat = position.coords.latitude;
|
|
const lng = position.coords.longitude;
|
|
|
|
// Set marker at current location
|
|
marker.setLngLat([lng, lat]).addTo(map);
|
|
|
|
// Center map on current location
|
|
map.flyTo({
|
|
center: [lng, lat],
|
|
zoom: 14
|
|
});
|
|
|
|
// Hide loading spinner
|
|
document.getElementById('geolocateSpinner').style.display = 'none';
|
|
document.getElementById('geolocateBtn').disabled = false;
|
|
|
|
// Show success message
|
|
showResult('Current location detected successfully', 'success');
|
|
|
|
// Try to get address using reverse geocoding
|
|
fetch(`https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lng}`)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data && data.address) {
|
|
// Extract road name or location
|
|
let location = '';
|
|
if (data.address.road) {
|
|
location = data.address.road;
|
|
if (data.address.city) {
|
|
location += `, ${data.address.city}`;
|
|
}
|
|
} else if (data.address.suburb) {
|
|
location = data.address.suburb;
|
|
if (data.address.city) {
|
|
location += `, ${data.address.city}`;
|
|
}
|
|
}
|
|
|
|
if (location) {
|
|
document.getElementById('where').value = location;
|
|
}
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error getting address:', error);
|
|
});
|
|
},
|
|
// Error callback
|
|
function(error) {
|
|
// Hide loading spinner
|
|
document.getElementById('geolocateSpinner').style.display = 'none';
|
|
document.getElementById('geolocateBtn').disabled = false;
|
|
|
|
// Show error message
|
|
let errorMsg = 'Unable to get your location. ';
|
|
switch(error.code) {
|
|
case error.PERMISSION_DENIED:
|
|
errorMsg += 'You denied the request for geolocation.';
|
|
break;
|
|
case error.POSITION_UNAVAILABLE:
|
|
errorMsg += 'Location information is unavailable.';
|
|
break;
|
|
case error.TIMEOUT:
|
|
errorMsg += 'The request to get your location timed out.';
|
|
break;
|
|
case error.UNKNOWN_ERROR:
|
|
errorMsg += 'An unknown error occurred.';
|
|
break;
|
|
}
|
|
showResult(errorMsg, 'error');
|
|
},
|
|
// Options
|
|
{
|
|
enableHighAccuracy: true,
|
|
timeout: 10000,
|
|
maximumAge: 0
|
|
}
|
|
);
|
|
} else {
|
|
// Hide loading spinner
|
|
document.getElementById('geolocateSpinner').style.display = 'none';
|
|
document.getElementById('geolocateBtn').disabled = false;
|
|
|
|
// Show error message
|
|
showResult('Geolocation is not supported by your browser', 'error');
|
|
}
|
|
});
|
|
|
|
// Handle form submission
|
|
document.getElementById('trafficForm').addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
|
|
// Get form values
|
|
const label = document.getElementById('label').value;
|
|
const severity = document.getElementById('severity').value;
|
|
const cause = document.getElementById('cause').value;
|
|
const start = document.getElementById('start').value;
|
|
const stop = document.getElementById('stop').value;
|
|
const where = document.getElementById('where').value;
|
|
|
|
// Check if marker is set
|
|
if (!marker.getLngLat()) {
|
|
showResult('Please set a location by clicking on the map or using the geolocation button', '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: 'unscheduled', // Traffic jams are typically unscheduled
|
|
what: 'traffic.jam', // Category for traffic jams
|
|
'traffic:severity': severity, // Custom property for severity
|
|
start: start,
|
|
stop: stop
|
|
}
|
|
};
|
|
|
|
// Add optional properties if provided
|
|
if (cause) {
|
|
event.properties['traffic:cause'] = cause;
|
|
}
|
|
|
|
if (where) {
|
|
event.properties.where = where;
|
|
}
|
|
|
|
// Add OSM username if authenticated
|
|
const osmUsername = document.getElementById('osmUsername');
|
|
if (osmUsername && osmUsername.value) {
|
|
event.properties['reporter:osm'] = osmUsername.value;
|
|
console.log(`Including OSM username in report: ${osmUsername.value}`);
|
|
}
|
|
|
|
// Submit event to API
|
|
fetch('/event', {
|
|
method: 'POST',
|
|
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(`Traffic jam reported 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 Report</a> | <a href="/demo">Back to Map</a></p>`;
|
|
|
|
// Reset form
|
|
document.getElementById('trafficForm').reset();
|
|
// Set default dates again
|
|
setDefaultDates();
|
|
// Remove marker
|
|
marker.remove();
|
|
})
|
|
.catch(error => {
|
|
showResult(`Error reporting traffic jam: ${error.message}`, 'error');
|
|
});
|
|
});
|
|
|
|
// 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' });
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
# Concatenate the HTML parts to form the complete template
|
|
html = html_header + auth_section + html_footer
|
|
|
|
# Set the response body and status
|
|
resp.text = html
|
|
resp.status = falcon.HTTP_200
|
|
logger.success("Successfully processed GET request to /demo/traffic")
|
|
except Exception as e:
|
|
logger.error(f"Error processing GET request to /demo/traffic: {e}")
|
|
resp.status = falcon.HTTP_500
|
|
resp.text = f"Error: {str(e)}"
|
|
|
|
# Create a global instance of DemoResource
|
|
demo = DemoResource() |