maps.chandlerswift.com/layers/dot-cams/travel-iq/get_data.py

115 lines
3.3 KiB
Python
Executable File

#!/usr/bin/python3
import requests
import json
states = {
"Alaska": "https://511.alaska.gov/",
"Georgia": "https://511ga.org/",
"Louisiana": "https://www.511la.org/",
"Nevada": "https://www.nvroads.com/",
"NewYork": "https://www.511ny.org/",
"Wisconsin": "https://511wi.gov/"
}
for state, baseURL in states.items():
query={
"columns": [ # no clue what any of this is, so here it stays
{
"data": None,
"name": "",
},
{
"name": "sortId",
"s": True,
},
{
"name": "region",
"s": True,
},
{
"name": "county",
"s": True,
},
{
"name": "roadway",
"s": True,
},
{
"name": "description1",
},
{
"data": 6,
"name": "",
},
],
"start": 0,
"length": 100,
}
video_camera_count = 0
still_camera_count = 0
cameras = []
available_cameras = 999_999 # lots
while len(cameras) < available_cameras:
res = requests.get(f"{baseURL}/List/GetData/Cameras", {
"query": json.dumps(query),
"lang": "en",
})
res.raise_for_status()
res = res.json()
available_cameras = res['recordsTotal']
for c in res['data']:
if isinstance(c['videoUrl'], list): # LA returns multiple (identical?) streams
src = c['videoUrl'][0]
video_camera_count += 1
elif c['videoUrl']:
src = c['videoUrl']
video_camera_count += 1
else:
src = baseURL + "map/Cctv/" + c['id']
still_camera_count += 1
cameras.append({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [c['longitude'], c['latitude']], # yes, [lon, lat] since it's [x, y]
},
"properties": {
'name': c['displayName'],
'views': [{
'hasVideo': bool(c['videoUrl']),
'src': src,
}],
},
})
query['start'] += 100
geojson = {
"type": "FeatureCollection",
"features": cameras,
}
with open(f"data/{state}.geojson", "w") as f:
f.write(json.dumps(geojson))
print(f"{len(cameras)} locations found for {state}")
print(f"{state}: {still_camera_count} photo + {video_camera_count} video cameras")
# hack hack hack
#
# If I write this to one big file, I can't take advantage of any lazy loading
# for performance reasons, so I'm constrained to having a bunch of files. I
# can't programmatically import those, since es6 imports don't allow for that.
# So, codegen it is (and fairly gross codegen at that!).
with open('data/states.js', 'w') as f:
for state in states:
f.write(f"import {state} from './{state}.geojson?url';\n")
f.write('\nexport default {\n')
for state in states:
f.write(f" {state}: {state},\n")
f.write("};\n")