trackintel

repository·master·Indexed 42 days ago

https://github.com/mie-lab/trackintel

A Python library for the analysis of spatio-temporal tracking data focused on human mobility. It features a hierarchical data model (positionfixes, staypoints, locations, triplegs, trips, and tours) and tools for the full analysis lifecycle, including preprocessing, semantic enrichment, and visualization. The framework supports data import/export via CSV, GeoPandas, and PostGIS, and provides utilities for calculating mobility metrics like radius of gyration and jump length, as well as predicting transport modes.

Tokens
5.8K
Snippets
24
Records
43
Agent score
54%

What's inside trackintel

  1. Overview of the trackintel framework

    master

    trackintel is a framework for human mobility data analysis. It provides tools for data quality enhancement, multi-source data integration, quantitative analysis, mining, and visualization. The framework is organized into a processing pipeline that operates on data adhering to the trackintel data model:

    • Preprocessing: Filtering, outlier detection, and imputation of missing values.
    • Contextual Augmentation: Map matching and trajectory algebra-based context addition.
    • Analysis: Extraction of mobility metrics, descriptors, preferences, and systematic mobility.
    • Visualization and Communication: Generation of maps, charts, and other visual outputs.

    Additionally, the framework includes packages for user mobility profiling and trajectory-based learning analytics. Note that non-standardized methods are explicitly marked as experimental.

  2. Overview of example datasets

    master

    The examples/data directory contains several datasets used to demonstrate different tracking sources and formats:

    • gpsies_trajectory.csv: Manually generated via GPSies.
    • posmo_trajectory.csv: Automatically tracked using POSMO Segments.
    • geolife_trajectory.csv and pfs_tutorial.geojson: From the Microsoft Geolife trajectory dataset.
    • google_trajectory.csv: A random excerpt from a Google Maps location track.
  3. Use trackintel classes as GeoPandas DataFrames

    master

    trackintel provides 5 core classes that subclass pandas.DataFrame or geopandas.GeoDataFrame. This means you can use all standard GeoPandas operations on trackintel objects, while also benefiting from built-in data validation and specialized mobility analysis methods.

    To begin processing data, you can load a CSV into a Positionfixes object, which then grants access to generation methods like generate_staypoints().

    import trackintel
    
    # Load raw data into a Positionfixes object
    df = trackintel.read_positionfixes_csv('data.csv')
    
    # Access trackintel-specific methods
    df.generate_staypoints()
  4. Understand the trackintel data model hierarchy

    master

    The trackintel framework organizes tracking data into a hierarchy of classes that represent different levels of mobility abstraction. Data is typically generated sequentially starting from raw GPS points:

    1. positionfixes: The base layer containing raw GPS data (at minimum: user_id, tracked_at, longitude, latitude).
    2. staypoints: Locations where a user remained for a minimum duration.
    3. triplegs: Segments of movement covered using a single mode of transport.
    4. locations: Meaningful places formed by clustering staypoints (can be generated by one or multiple users).
    5. trips: Segments of movement between two consecutive activity staypoints (staypoints where activity=True).
    6. tours: Sequences of trips that start and end at the same location. If the journey column is True, this location is considered home.
  5. Understand the trackintel hierarchical data model

    master

    The trackintel framework is built around a hierarchical data model for movement data. Understanding these abstractions is key to using the library's aggregation and enrichment features:

    • positionfixes: Raw tracking points (e.g., GPS recordings or check-ins).
    • staypoints: Locations where a user spent time without moving. These can be:
      • activity staypoints: Have a semantic purpose (e.g., workplace, cafe).
      • non-activity staypoints: No explicit purpose (e.g., waiting for a bus).
    • locations: Important places visited more than once (e.g., home or work), often generated by clustering staypoints.
    • triplegs (or stages): Continuous movement without changing mode, vehicle, or stopping for too long (e.g., a taxi trip).
    • trips: The sequence of all triplegs between two consecutive activity staypoints.
    • tours: A collection of sequential trips that return to the same location.
  6. Identify locations using various methods

    master

    The trackintel.analysis module provides several tools for identifying significant locations in mobility data:

    • location_identifier: The primary function for identifying locations.
    • pre_filter_locations: Used to clean or filter location data before identification.
    • freq_method: A method for location identification based on frequency.
    • osna_method: A method for location identification (likely referring to a specific algorithmic approach).
  7. Run trackintel examples

    master

    You can run the provided examples directly using Python. These examples demonstrate common use cases such as preprocessing trajectories, importing/exporting data to PostGIS, and setting up example databases. Some examples, like preprocess_trajectories.py, will automatically generate output plots in the examples/out directory.

    If an example requires a database connection, you can customize the connection settings by modifying the config.json file.

    python preprocess_trajectories.py
    python import_export_postgis.py
    python setup_example_database.py
  8. Install trackintel via conda or pip

    master

    You can install trackintel using either conda-forge (recommended) or pip. If using pip, ensure you have GeoPandas installed in your environment first.

    # Recommended: Install via conda-forge
    conda install -c conda-forge trackintel
    
    # Alternative: Install via pip
    pip install trackintel
  9. Deploy trackintel data model to PostGIS

    master

    To set up the trackintel data model in a PostGIS database, you can use the provided SQL commands or run the initialization script available in the repository. This ensures your database schema is compatible with the framework's data model requirements.

    https://github.com/mie-lab/trackintel/blob/master/sql/create_tables_pg.sql
  10. Set up trackintel with a PostGIS database

    master

    For data persistence, it is recommended to use a PostGIS database.

    1. Execute the provided SQL script (sql/create_tables_pg.sql) in a new or empty PostGIS database to create the necessary tables.
    2. Use a PostgreSQL connection string to interact with the database via trackintel functions.
  11. Parse Geolife and GPX datasets

    master

    trackintel includes predefined readers for common datasets to simplify the ingestion process.

    Geolife

    Supports easy parsing of the Geolife dataset, including the ability to add available mode labels.

    • read_geolife: Parses the Geolife dataset.
    • geolife_add_modes_to_triplegs: Adds mode labels to triplegs derived from Geolife data.

    GPX

    Used to load multiple tracks belonging to the same user.

    • read_gpx: Loads GPX track data.
    import trackintel
    
    # Load Geolife data
    geolife_data = trackintel.read_geolife(path_to_geolife)
    
    # Load GPX tracks
    gpx_data = trackintel.read_gpx(path_to_gpx)