Add Viarail layer

This commit is contained in:
Chandler Swift 2024-02-01 01:10:18 -06:00
parent 60d46c4b62
commit ff77bc31e7
Signed by: chandlerswift
GPG key ID: A851D929D52FB93F
3 changed files with 74 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import Stamen from 'ol/source/Stamen.js';
import chandlerLayer from './chandler/layer.js'; import chandlerLayer from './chandler/layer.js';
import amtrakLayer from './amtrak/layer.js'; import amtrakLayer from './amtrak/layer.js';
import viarailLayer from './viarail/layer.js';
import arenasLayer from './nhl-arenas/layer.js'; import arenasLayer from './nhl-arenas/layer.js';
import bikepackingLayer from './bikepacking/layer.js'; import bikepackingLayer from './bikepacking/layer.js';
import chains from './chains/index.js'; import chains from './chains/index.js';
@ -68,6 +69,10 @@ const layerCategories = [
name: "Amtrak Routes", name: "Amtrak Routes",
layer: amtrakLayer, layer: amtrakLayer,
}, },
{
name: "Via Rail Routes",
layer: viarailLayer,
},
{ {
name: "NHL Arenas", name: "NHL Arenas",
layer: arenasLayer, layer: arenasLayer,

43
layers/viarail/get_data.py Executable file
View file

@ -0,0 +1,43 @@
#!/usr/bin/python3
import csv
import io
import json
import zipfile
import urllib.request
shapes = {}
# https://stackoverflow.com/a/5711095
resp = urllib.request.urlopen("https://www.viarail.ca/sites/all/files/gtfs/viarail.zip")
with zipfile.ZipFile(io.BytesIO(resp.read())).open('shapes.txt') as f:
reader = csv.DictReader(io.TextIOWrapper(f))
for row in reader:
# they look like they're probably all in order, but the spec doesn't
# actually say that they have to be, so we're going to bucket them by
# route and then sort each bucket's contents to be on the safe side.
# It's not that much data, and I don't run this download frequently, so
# the extra CPU cost shouldn't be too outrageous :)
if row['shape_id'] not in shapes:
shapes[row['shape_id']] = []
shapes[row['shape_id']].append(row)
geojson = {
"type": "FeatureCollection",
"features": [],
}
for _, shape in shapes.items():
shape.sort(key=lambda c: int(c['shape_pt_sequence']))
geojson['features'].append({
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[float(l['shape_pt_lon']), float(l['shape_pt_lat'])] for l in shape
]
},
})
with open('data.geojson', 'w') as f:
f.write(json.dumps(geojson))

26
layers/viarail/layer.js Normal file
View file

@ -0,0 +1,26 @@
import VectorLayer from 'ol/layer/Vector';
import {Vector as VectorSource} from 'ol/source.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import {Style, Stroke} from 'ol/style.js';
import url from './data.geojson?url'; // TODO: remove `?url`?
const colors = '255,0,0'; // "Canadian red"...is just red? https://en.wikipedia.org/wiki/National_colours_of_Canada#Reproduction
const vectorLayer = new VectorLayer({
source: new VectorSource({
url: url,
format: new GeoJSON,
}),
style: function(feature, resolution){
return new Style({
stroke: new Stroke({
color: `rgba(${colors},${Math.min(1, Math.pow(resolution/10, 1/4))})`,
width: 10/Math.pow(resolution, 1/4),
})
});
},
});
export default vectorLayer;