Skip to content

Commit

Permalink
Simplify routes prior to sending to Django template (#124)
Browse files Browse the repository at this point in the history
* Add layer toggle to map

Following official Leaflet tutorial closely:
https://leafletjs.com/examples/layers-control/

Separate out commands to instantiate map layers from commands
to add them to map.

Create a "control" object whose "overlayMaps" parameter
determines which features can be toggled on or off.

For now, set all "stations" as one toggle-able feature, and
all "routes" as another.

TODO: separate out different station/route layers based on
mode of transit and/or line of interest. This would likely
involve initializing distinct layers (e.g. busStations,
railStations, busRoutes...) and then using conditional logic
(perhaps relying on passing in the `route_type` GTFS number)
to determine what goes where

* Respond to black failure notification by running black on two files

Weirdly these weren't files I edited in any way prior to previous
commit

* Get simplified routes working

---------

Co-authored-by: Matthew Jackson <mbjackson@Matthews-MacBook-Air.local>
  • Loading branch information
mbjackson-capp and Matthew Jackson committed May 17, 2024
1 parent e42d7c1 commit 5648e50
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions app/route_rangers_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils import timezone
from django.core.serializers import serialize
from django.templatetags.static import static
from django.contrib.gis.geos import GEOSGeometry, MultiLineString, LineString


from app.route_rangers_api.utils.city_mapping import CITY_CONTEXT
Expand Down Expand Up @@ -35,12 +36,18 @@ def dashboard(request, city: str):
# get commute

# get paths
routes = TransitRoute.objects.filter(city=CITY_CONTEXT[city]["DB_Name"]) # .values(
# MultiLineString needs to be serialized into a GeoJson object for Leaflet to
# work with it.
# to serialize into GeoJson, need to get out entire Django model object, not just
# the .values("geo_representation", "route_name", "color")
# with .values() you get "AttributeError: 'dict' has no component 'meta'"
routes = TransitRoute.objects.filter(city=CITY_CONTEXT[city]["DB_Name"])
# reduce load time and data transfer size by overwriting model attribute
TOLERANCE = 0.00005
for route in routes:
simple_geo_representation = route.geo_representation.simplify(
tolerance=TOLERANCE, preserve_topology=True
)
# simplify() might alter the GEOS type; can't allow that
if isinstance(simple_geo_representation, LineString):
simple_geo_representation = MultiLineString(simple_geo_representation)
route.geo_representation = simple_geo_representation

routes_json = serialize(
"geojson",
routes,
Expand All @@ -49,6 +56,8 @@ def dashboard(request, city: str):
)

# stations
# TODO: consider passing these in with serialized geoJSON instead of
# creating lst_coords separately
stations = TransitStation.objects.values().filter(
city=CITY_CONTEXT[city]["DB_Name"]
)
Expand Down

0 comments on commit 5648e50

Please sign in to comment.