Data Formatter
calculateBearing(lat1, lon1, lat2, lon2)
¶
Computes the initial bearing (forward azimuth) from one geographic coordinate to another. This bearing is measured clockwise from true north and returned as a value between 0 and 360 degrees.
How It Works
- Converts both starting (lat1, lon1) and ending (lat2, lon2) coordinates to radians.
- Uses the difference in longitudes (dlon) and trigonometric functions to calculate the bearing in radians.
- Converts the bearing from radians to degrees.
- Normalizes the result to ensure it falls within the range of [0, 360).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lat1
|
float
|
Latitude of the start location (in decimal degrees). |
required |
lon1
|
float
|
Longitude of the start location (in decimal degrees). |
required |
lat2
|
float
|
Latitude of the end location (in decimal degrees). |
required |
lon2
|
float
|
Longitude of the end location (in decimal degrees). |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The initial bearing in degrees, between 0 and 360. |
Example
bearing = calculateBearing(12.9716, 77.5946, 13.0827, 80.2707) print(bearing) 76.123456789 # Example output
Source code in meowmotion/data_formatter.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
calculateStraightnessIndex(df)
¶
Calculates a trip’s "straightness index" by comparing the total distance traveled to the straight-line distance between the first and last points. The resulting ratio (straight-line distance ÷ actual path distance) measures how directly a traveler moved from start to end.
How It Works
- Summarizes the total distance covered (
distance_covered
). - Calculates the straight-line (haversine) distance between the first and last coordinates in the trip.
- Divides the straight-line distance by the actual path length.
- Returns that value for every row in the DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
A DataFrame representing a single trip, containing at least
- |
required |
Returns:
Type | Description |
---|---|
List[float]
|
List[float]: A list (the same length as |
List[float]
|
repeated for each row. If the path length is 0 or NaN, returns |
Example
import pandas as pd from haversine import haversine df = pd.DataFrame({ ... "lat": [12.9716, 13.0827], ... "lng": [77.5946, 80.2707], ... "distance_covered": [0, 35000] # for example ... }) result = calculateStraightnessIndex(df) print(result) [0.5, 0.5] # Indicates the path is half as direct as a straight line
Source code in meowmotion/data_formatter.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
|
checkIfAtGrrenSpace(df, sdf)
¶
Checks whether a trip has at least five data points that fall within the specified green space polygons. If it does, the entire trip is marked with 1 for every row. Otherwise, it returns 0 for each row.
Note
- This function uses a threshold of five detections by default, but you can customize this threshold as needed.
How It Works
- Iterates through all points in
df
(each representing a trajectory point in the trip). - For each point, checks if it intersects any of the polygons in
sdf
. - If at least five points from the trip are found in a green space, returns a list
of 1s (one per row in
df
). - Otherwise, returns a list of 0s.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing trip data with at least |
required |
sdf
|
GeoDataFrame
|
GeoDataFrame representing one or more green space polygons. |
required |
Returns:
Type | Description |
---|---|
List[int]
|
List[int]: A list of integers (1 or 0) for each row in |
Example
df = pd.DataFrame({ ... "lat": [12.9716, 12.9780, 12.9825, 12.9850, 12.9900], ... "lng": [77.5946, 77.5949, 77.5953, 77.5960, 77.5965] ... }) green_spaces = gpd.read_file("greenspaces.shp") # Example file result = checkIfAtGrrenSpace(df, green_spaces) print(result) [1, 1, 1, 1, 1] # indicates at least 5 points are inside a green space
Source code in meowmotion/data_formatter.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
checkIfNearStop(df, sdf)
¶
Determines whether the first and/or last point of a trip lies within a given
polygon area (e.g., bus stop, train station, or metro station). It returns a list
of length equal to df.shape[0]
, with each element indicating whether:
- Both the first and last points intersect the polygon(s): 2
- Only one of the points intersects the polygon(s): 1
- Neither the first nor the last point intersects the polygon(s): 0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing trip data, including |
required |
sdf
|
GeoDataFrame
|
GeoDataFrame representing polygons for stops or stations (e.g., bus stops, train stations, metro stations). |
required |
Returns:
Type | Description |
---|---|
List[int]
|
List[int]: A list of integers (2, 1, or 0) indicating the presence of the |
List[int]
|
first/last point in the polygon(s). |
Example
import pandas as pd import geopandas as gpd from shapely.geometry import Polygon
Example DataFrame with two points¶
df = pd.DataFrame({ ... 'lat': [12.9716, 12.9760], ... 'lng': [77.5946, 77.5950] ... })
Example GeoDataFrame representing a stop¶
polygons = [Polygon([(77.5940, 12.9710), (77.5950, 12.9710), ... (77.5950, 12.9720), (77.5940, 12.9720)])] sdf = gpd.GeoDataFrame(geometry=polygons, crs="EPSG:4326") result = checkIfNearStop(df, sdf) print(result) [1, 1] # Only the first point intersects the polygon
Source code in meowmotion/data_formatter.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
|
featureEngineering(trip_df, shape_files, cpu_cores=max(1, int(cpu_count() // 2)))
¶
Performs feature engineering on raw trip data by partitioning it and processing each partition in parallel. This includes calculating advanced trip features such as speed, acceleration, angular deviation, and straightness index, as well as identifying whether a trip starts or ends near transport stops or green spaces.
This function distributes work across the specified number of CPU cores, calls the processData
child function on each chunk, and then merges all processed chunks into a single DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trip_df
|
DataFrame
|
A DataFrame containing raw trip information, including columns for
user ID, trip ID, latitude ( |
required |
shape_files
|
List[GeoDataFrame]
|
A list of GeoDataFrames representing various geographic layers (e.g., bus stops, train stops, metro stops, green spaces). These are used to check if trips start/end near these points or areas. |
required |
cpu_cores
|
int
|
Number of CPU cores to use for parallel processing. Defaults to half of the available cores. |
max(1, int(cpu_count() // 2))
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: A concatenated DataFrame containing the enhanced feature set for all trips. |
DataFrame
|
Features include: - Speed, acceleration, jerk, and angular deviation - Straightness index - Indicators for whether a trip begins or ends near transport stops or in green spaces - Filtered trips based on minimum impressions |
Example
Suppose 'trip_df' is a DataFrame of trips and 'shapes' is a list of GeoDataFrames¶
from your_module import featureEngineering enhanced_df = featureEngineering(trip_df, shapes, cores=4) print(enhanced_df.head())
Source code in meowmotion/data_formatter.py
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
|
generateTrajStats(df)
¶
Aggregates and summarizes key trip-level statistics (e.g., median/percentile speeds,
accelerations, jerk, angular deviation, distance) from enhanced trip data. This function
operates on data that has already undergone feature engineering (e.g., via featureEngineering
),
and creates consolidated columns reflecting various trip metrics. A progress bar is displayed
during calculation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
A DataFrame containing enhanced trip data, including columns such as
|
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing aggregated statistics for each trip, including:
- Speed median, 95th percentile, and standard deviation
- Acceleration median, 95th percentile, and standard deviation
- Jerk median, 95th percentile, and standard deviation
- Angular deviation median, 95th percentile, and standard deviation
- Straightness index
- Total distance covered (km)
- Indicators for whether the trip starts/ends near specific transport stops or green spaces
- Weekend/hour categories
- A placeholder for |
Example
import pandas as pd data = { ... "uid": [1, 1, 1, 2, 2], ... "trip_id": [10, 10, 10, 20, 20], ... "new_speed": [3.0, 5.5, 2.0, 4.0, 4.5], ... "accelaration": [0.1, 0.2, 0.3, 0.1, 0.05], ... "jerk": [0.01, 0.02, 0.03, 0.01, 0.02], ... "angular_deviation": [5, 10, 15, 3, 4], ... } df = pd.DataFrame(data) result = generateTrajStats(df) result.head() datetime uid trip_id speed_median ... hour_category transport_mode 0 NaT 1 10 3.5 ... 0 NaN 1 NaT 1 10 3.5 ... 0 NaN 2 NaT 1 10 3.5 ... 0 NaN 3 NaT 2 20 4.25 ... 0 NaN 4 NaT 2 20 4.25 ... 0 NaN
Source code in meowmotion/data_formatter.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 |
|
processData(df, shape_files)
¶
Cleans and enriches raw trip-point data with motion-related features (speed, acceleration, jerk, bearing, angular deviation, straightness index) and contextual flags indicating proximity to public-transport stops or green spaces.
The function operates per trip (uid
–trip_id
):
1. Removes duplicate timestamps and drops trips with fewer than
five distinct observations (num_of_impressions
< 5).
2. Computes time deltas, inter-point distance (haversine), speed,
speed z-scores, acceleration, and jerk, replacing extreme
speed outliers (|z| ≥ 3) with the median speed.
3. Derives temporal attributes—calendar month, hour of day,
weekend flag, and a four-level hour_category
(0 Night, 1 Morning, 2 Afternoon, 3 Evening).
4. For each trip, determines whether the first and/or last point
lies within
• a bus stop (shape_files[0])
• a train station (shape_files[1])
• a metro station (shape_files[2])
and whether ≥ 5 points fall inside a green space
polygon (shape_files[3]).
5. Calculates a straightness index (straight-line ÷ actual path
length) and removes trips with an index > 1 (spurious data).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
Point-level trip data containing at least
|
required |
shape_files
|
List[GeoDataFrame]
|
A list of four
GeoDataFrames in this order:
|
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The cleaned and feature-rich DataFrame, one row |
DataFrame
|
per retained point, including new columns such as |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Example
processed = processData(raw_trip_df, [ ... bus_stops_gdf, train_stops_gdf, metro_stops_gdf, ... green_space_gdf ... ]) processed.head()
Source code in meowmotion/data_formatter.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
|
processTripData(trip_point_df, na_flow_df, raw_df)
¶
Processes trip-level data by expanding stored trip-point coordinates, merging in origin-destination flows, and then attaching timestamps from a raw dataset. The result is a single DataFrame containing trip points (latitude, longitude, and timestamps) and corresponding origin/destination information.
Key Steps
- Expands list-based trip points in
trip_point_df
into individual rows for each (lat, lng) point. - Joins the expanded trip points to
na_flow_df
to retrieve origin, destination, and timing fields. - Filters trips to ensure total travel time does not exceed 24 hours.
- Merges
raw_df
to add precise timestamps for each (lat, lng) point and ensures each point is within the trip's time window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
trip_point_df
|
DataFrame
|
Contains user IDs, trip IDs, and a column of list-based trip points.
Must have columns |
required |
na_flow_df
|
DataFrame
|
Non-aggregated OD flow data containing origin/destination coordinates
and timestamps ( |
required |
raw_df
|
DataFrame
|
The raw dataset with columns |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: A cleaned and merged DataFrame with columns for each trip's |
DataFrame
|
user ID, trip ID, origin/destination coordinates, and per-point latitude, |
DataFrame
|
longitude, and timestamps. |
Example
Suppose you already have three DataFrames: trip_point_df, na_flow_df, raw_df¶
result_df = processTripData(trip_point_df, na_flow_df, raw_df) print(result_df.head()) uid trip_id lat lng datetime org_lat org_lng ... 0 1 10 12.9716 77.59460 2023-01-01 ... 12.970 77.5940 ... 1 1 10 12.9720 77.59470 2023-01-01 ... 12.970 77.5940 ... ...
Source code in meowmotion/data_formatter.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
readRawData(data_dir, cpu_cores=max(1, cpu_count() // 2))
¶
Reads and compiles raw JSON data files for a given year and city by parallel processing multiple monthly files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cpu_cores
|
int
|
The number of CPU cpu_cores to be used for parallel processing. By default, it uses half of the available cpu_cores. |
max(1, cpu_count() // 2)
|
data_dir
|
str
|
The directory where the raw data files are stored. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: A DataFrame containing compiled raw data from all monthly files. |
Example
df = readRawData(2023, "path_to_root/city/year") print(df.head())
Source code in meowmotion/data_formatter.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
removeOutlier(group)
¶
Filters outliers in the speed
column by replacing high z-score values (≥ 3) with
the median speed. This function is typically applied to each group within a larger
grouped DataFrame (e.g., a single trip trajectory).
How It Works
- Calculates the median speed within the group.
- Identifies rows where
speed_z_score
is ≥ 3. - Replaces those outlier
speed
values with the median speed. - Returns the modified group DataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
group
|
DataFrame
|
Subset of a larger DataFrame, typically representing
one trip. Must contain at least:
- |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The same DataFrame with outlier speeds replaced by the median. |
Example
import pandas as pd import numpy as np data = { ... 'speed': [5.0, 120.0, 6.0], ... 'speed_z_score': [0.2, 3.5, 0.3] ... } df = pd.DataFrame(data) print(df) speed speed_z_score 0 5.0 0.20 1 120.0 3.50 2 6.0 0.30
cleaned = removeOutlier(df) print(cleaned) speed speed_z_score 0 5.0 0.20 1 5.5 3.50 # replaced with median (5.5) 2 6.0 0.30
Source code in meowmotion/data_formatter.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
|