Skip to content

Commit 7cf53c5

Browse files
authored
Merge pull request #287 from OpenGeoscience/mbta-ridership
MBTA ridership data
2 parents dee9bf9 + 5c95725 commit 7cf53c5

File tree

3 files changed

+61
-17
lines changed

3 files changed

+61
-17
lines changed

geoinsight/core/tasks/networks.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ def create_network(vector_data, network_options):
9595
and str(v).lower() != "nan"
9696
},
9797
)
98-
from_node_obj.vector_feature = VectorFeature.objects.create(
99-
vector_data=vector_data,
100-
geometry=from_node_obj.location,
101-
properties=from_node_obj.metadata,
102-
)
103-
from_node_obj.save()
98+
if from_node_obj.vector_feature is None:
99+
from_node_obj.vector_feature = VectorFeature.objects.create(
100+
vector_data=vector_data,
101+
geometry=from_node_obj.location,
102+
properties=from_node_obj.metadata,
103+
)
104+
from_node_obj.save()
104105

105106
if i < len(route_nodes) - 1:
106107
next_node = route_nodes.iloc[i + 1]
@@ -130,12 +131,13 @@ def create_network(vector_data, network_options):
130131
and str(v).lower() != "nan"
131132
},
132133
)
133-
to_node_obj.vector_feature = VectorFeature.objects.create(
134-
vector_data=vector_data,
135-
geometry=to_node_obj.location,
136-
properties=to_node_obj.metadata,
137-
)
138-
to_node_obj.save()
134+
if to_node_obj.vector_feature is None:
135+
to_node_obj.vector_feature = VectorFeature.objects.create(
136+
vector_data=vector_data,
137+
geometry=to_node_obj.location,
138+
properties=to_node_obj.metadata,
139+
)
140+
to_node_obj.save()
139141

140142
route_points_start_index = route_points_reprojected.index[
141143
route_points_reprojected["geometry"] == cutoff_points[current_node_name]

sample_data/boston_floods/ingest_mbta.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from __future__ import annotations
22

3-
from geoinsight.core.models import VectorFeature
3+
import io
4+
5+
import pandas as pd
6+
import requests
7+
8+
from geoinsight.core.models import NetworkNode, VectorData, VectorFeature
49

510
LINE_COLORS = {
611
"RED": "#D31414",
@@ -10,6 +15,14 @@
1015
"SILVER": "#7B8B86",
1116
}
1217

18+
RIDERSHIP_DATA_URL = "https://data.kitware.com/api/v1/item/69938a5d92fec64197f41dc3/download"
19+
20+
21+
STATION_NAME_ABBREVIATIONS = {
22+
"Northeastern University": "Northeastern",
23+
"Massachusetts Avenue": "Massachusetts Ave",
24+
}
25+
1326

1427
def convert_dataset(dataset, options):
1528
# Run standard conversion task first
@@ -21,6 +34,7 @@ def convert_dataset(dataset, options):
2134
)
2235

2336
# Post-processing
37+
2438
# For each associated vector feature, add style properties
2539
# To color features by the LINE property
2640
for feature in VectorFeature.objects.filter(vector_data__dataset=dataset):
@@ -35,3 +49,29 @@ def convert_dataset(dataset, options):
3549
}
3650
feature.properties = feature.properties | default_style
3751
feature.save()
52+
53+
# Add ridership data to stations
54+
response = requests.get(RIDERSHIP_DATA_URL, timeout=1000)
55+
ridership_data = pd.read_csv(io.StringIO(response.text.replace("\r", "")))
56+
for _, station in ridership_data.iterrows():
57+
station_name = station.loc["stop_name"].replace("'", "")
58+
if station_name in STATION_NAME_ABBREVIATIONS:
59+
station_name = STATION_NAME_ABBREVIATIONS[station_name]
60+
total_offs = int(station.loc["total_offs"])
61+
node_matches = NetworkNode.objects.filter(
62+
network__vector_data__dataset=dataset, metadata__STATION__iexact=station_name
63+
)
64+
if node_matches.count():
65+
new = {"total_ridership": total_offs}
66+
node = node_matches.first()
67+
node.metadata = node.metadata | new
68+
node.save()
69+
feature = node.vector_feature
70+
feature.properties = feature.properties | new
71+
feature.save()
72+
else:
73+
print(f"Could not find node for {station_name}")
74+
75+
# Update vector data summary
76+
for vector in VectorData.objects.filter(dataset=dataset):
77+
vector.get_summary(cache=False)

web/src/components/sidebars/LegendPanel.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ function getColormapPreviews(layer: Layer) {
4646
]
4747
}
4848
}
49-
if (discrete && colorBy && colormap && colorConfig.colormap) {
50-
const propsSpec = vector?.summary?.properties;
51-
if (propsSpec) {
52-
const colorByProp = propsSpec[colorBy]
49+
const propsSpec = vector?.summary?.properties;
50+
if (propsSpec && colorBy) {
51+
const colorByProp = propsSpec[colorBy]
52+
if (discrete && colormap && colorConfig.colormap) {
5353
const valueSet = Array.from(new Set(colorByProp.value_set.filter((v) => v)))
5454
const sortedValues = valueSet.sort((a: any, b: any) => a - b)
5555
const markers = colormapMarkersSubsample(colormap, colorConfig.colormap, nColors)
@@ -61,6 +61,8 @@ function getColormapPreviews(layer: Layer) {
6161
}
6262
}
6363
})
64+
} else if (!discrete && colorByProp.range) {
65+
range = colorByProp.range
6466
}
6567
}
6668
return {

0 commit comments

Comments
 (0)