smomtaz commited on
Commit
154c09f
·
verified ·
1 Parent(s): 1dd5e98

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +63 -1
utils.py CHANGED
@@ -1,5 +1,6 @@
1
  from pathlib import Path
2
- #import pandas as pd
 
3
  import faicons as fa
4
  from shiny import ui
5
  from shared import app_dir
@@ -7,9 +8,69 @@ import matplotlib.pyplot as plt
7
  import numpy as np
8
 
9
 
 
 
10
  #app_dir = Path(__file__).parent
11
  #tips = pd.read_csv(app_dir / "tips.csv")
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  def create_nav_button(button_icon, button_label_text, button_link, button_cls = "btn btn-primary"):
15
  return ui.tags.a(
@@ -102,3 +163,4 @@ def create_stacked_bar_chart(categories, intra_napa, into_napa, out_napa):
102
  plt.tight_layout()
103
  return fig
104
 
 
 
1
  from pathlib import Path
2
+ import pandas as pd
3
+ import geopandas as gpd
4
  import faicons as fa
5
  from shiny import ui
6
  from shared import app_dir
 
8
  import numpy as np
9
 
10
 
11
+
12
+
13
  #app_dir = Path(__file__).parent
14
  #tips = pd.read_csv(app_dir / "tips.csv")
15
 
16
+ def load_data():
17
+ """
18
+ Load zones shapefile and trips data.
19
+ Returns:
20
+ zones (GeoDataFrame): GeoDataFrame of zones.
21
+ trips (DataFrame): DataFrame of trip data.
22
+ """
23
+ zones = gpd.read_file(app_dir / "data" / "zone" / "Napa_TBS_2024_Zone_System.shp") # Replace with your shapefile path
24
+ zones = zones[zones.is_valid & ~zones.is_empty]
25
+ zones = zones[["FPID", "geometry"]] # Keep only essential columns
26
+ zones["geometry"] = zones["geometry"].simplify(0.001, preserve_topology=True) # Simplify geometries
27
+
28
+ #trips = pd.read_csv(app_dir / "data" / "od_table.csv") # Replace with your trip data path
29
+ trips = pd.read_parquet(app_dir / "data" / "od_table.parquet") # Replace with your trip data path
30
+ return zones, trips
31
+
32
+
33
+ def filter_trips(trips, zones, origin_filter, destination_filter):
34
+ """
35
+ Filter trips based on selected origin or destination.
36
+
37
+ Args:
38
+ trips (DataFrame): Trip data with origin and destination zones.
39
+ zones (GeoDataFrame): GeoDataFrame of zones.
40
+ origin_filter (str): Selected origin zone ID.
41
+ destination_filter (str): Selected destination zone ID.
42
+
43
+ Returns:
44
+ filtered_origins (GeoDataFrame): Filtered origin zones with trip counts.
45
+ filtered_destinations (GeoDataFrame): Filtered destination zones with trip counts.
46
+ """
47
+ if origin_filter and destination_filter:
48
+ filtered_trips = trips[
49
+ (trips["start_zone_id"] == origin_filter) &
50
+ (trips["end_zone_id"] == destination_filter)
51
+ ]
52
+ elif origin_filter:
53
+ filtered_trips = trips[trips["start_zone_id"] == origin_filter]
54
+ elif destination_filter:
55
+ filtered_trips = trips[trips["end_zone_id"] == destination_filter]
56
+ else:
57
+ filtered_trips = trips
58
+
59
+
60
+ # Aggregate trip counts for origins and destinations
61
+ filtered_origins = zones.merge(
62
+ #filtered_trips.groupby("start_zone_id").size().reset_index(name="trip_count"),
63
+ filtered_trips.groupby("start_zone_id")["trips"].sum().reset_index(name="trip_count"),
64
+ left_on="FPID", right_on="start_zone_id", how="left"
65
+ ).fillna(0)
66
+
67
+ filtered_destinations = zones.merge(
68
+ filtered_trips.groupby("end_zone_id")["trips"].sum().reset_index(name="trip_count"),
69
+ left_on="FPID", right_on="end_zone_id", how="left"
70
+ ).fillna(0)
71
+
72
+ return filtered_origins, filtered_destinations
73
+
74
 
75
  def create_nav_button(button_icon, button_label_text, button_link, button_cls = "btn btn-primary"):
76
  return ui.tags.a(
 
163
  plt.tight_layout()
164
  return fig
165
 
166
+