server.py: Backend Server for MobilityDB/PostgreSQL Data
This Python script implements a Flask server that provides RESTful APIs to query and retrieve data from a MobilityDB database. It leverages the pymeos library to interact with the database and provides endpoints for retrieving points, routes, and trips.
1. Dependencies
The server relies on the following Python libraries:
flask: A lightweight web framework for building RESTful APIs.flask_cors: Enables Cross-Origin Resource Sharing (CORS) to allow requests from different origins.pymeos: A Python library for interacting with MobilityDB databases.collections: Provides data structures likedefaultdictfor efficient data organization.rdp: Implements the Douglas-Peucker algorithm for line simplification.numpy: A library for numerical computing, used for array operations.
2. Database Connection
The server establishes a connection to a MobilityDB database using the pymeos.db.psycopg.MobilityDB class. The connection details (host, port, database name, username, password) are specified in the connect method.
3. API Endpoints
The server exposes three main API endpoints:
3.1. /points
- Method: GET
- Parameters:
start_time: The starting timestamp for the query (in milliseconds).end_time: The ending timestamp for the query (in milliseconds).min_lon: The minimum longitude for the query.max_lon: The maximum longitude for the query.min_lat: The minimum latitude for the query.max_lat: The maximum latitude for the query.max_entries: The maximum number of entries to return.
- Response: A JSON array containing point data within the specified time and spatial bounds. Each point object includes the following fields:
order_id: The identifier of the order.time_stamp: The timestamp of the point (in milliseconds).altitude: The altitude of the point.longitude: The longitude of the point.latitude: The latitude of the point.vendor: The vendor of the device.sn: The serial number of the device.
3.2. /routes
- Method: GET
- Parameters:
start_time: The starting timestamp for the query (in milliseconds).end_time: The ending timestamp for the query (in milliseconds).min_lon: The minimum longitude for the query.max_lon: The maximum longitude for the query.min_lat: The minimum latitude for the query.max_lat: The maximum latitude for the query.max_entries: The maximum number of entries to return.threshold: The threshold distance for line simplification (in meters).
- Response: A JSON array containing route segments within the specified time and spatial bounds. Each segment object includes the following fields:
source_longitude: The longitude of the starting point of the segment.source_latitude: The latitude of the starting point of the segment.source_altitude: The altitude of the starting point of the segment.target_longitude: The longitude of the ending point of the segment.target_latitude: The latitude of the ending point of the segment.target_altitude: The altitude of the ending point of the segment.bounding_volume: The volume of the bounding cylinder for the segment.sn: The serial number of the device.order_id: The identifier of the order.threshold: The threshold distance used for line simplification.start_time_stamp: The timestamp of the starting point of the segment (in milliseconds).end_time_stamp: The timestamp of the ending point of the segment (in milliseconds).vendor: The vendor of the device.
3.3. /trips
- Method: GET
- Parameters:
start_time: The starting timestamp for the query (in milliseconds).end_time: The ending timestamp for the query (in milliseconds).min_lon: The minimum longitude for the query.max_lon: The maximum longitude for the query.min_lat: The minimum latitude for the query.max_lat: The maximum latitude for the query.max_entries: The maximum number of entries to return.
- Response: A JSON object representing a GeoJSON FeatureCollection containing trip data within the specified time and spatial bounds. Each trip is represented as a Feature with the following properties:
type: "Trips"geometry: A GeoJSON LineString object representing the trip trajectory.vendor: The vendor of the device.sn: The serial number of the device.
4. Error Handling
The server includes error handling mechanisms to gracefully handle exceptions during database queries and data processing. If an error occurs, the server returns a JSON response with an error message and a corresponding HTTP status code.
5. PyMEOS Initialization and Finalization
The server initializes the pymeos library at startup and finalizes it when the application exits. This ensures proper resource management and avoids potential issues related to library state.
6. Usage
To run the server, execute the following command in the terminal:
npm run serverThe server will listen on port 5000 by default. You can access the API endpoints using a web browser or a REST client.
7. Example Queries
The following are example queries to retrieve data from the server:
Get Points:
GET http://localhost:5000/points?start_time=1697600500000&end_time=1697624000000&min_lon=114.010&max_lon=114.020&min_lat=22.669&max_lat=22.670&max_entries=1000Get Routes:
GET http://localhost:5000/routes?start_time=1697600500000&end_time=1697624000000&min_lon=114.010&max_lon=114.020&min_lat=22.660&max_lat=22.670&max_entries=1000&threshold=10Get Trips:
GET http://localhost:5000/trips?start_time=1697600500000&end_time=1697624000000&min_lon=113.010&max_lon=115.100&min_lat=22.500&max_lat=22.780&max_entries=1000