GeoJSON export
All checks were successful
Amarillo/amarillo-gitea/amarillo-grfs-export/pipeline/head This commit looks good
All checks were successful
Amarillo/amarillo-gitea/amarillo-grfs-export/pipeline/head This commit looks good
This commit is contained in:
parent
7bca8d6cd0
commit
7016ba22bf
23
amarillo/plugins/grfs_export/geojson_export.py
Normal file
23
amarillo/plugins/grfs_export/geojson_export.py
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
import gtfs_kit
|
||||||
|
import json
|
||||||
|
import zipfile
|
||||||
|
|
||||||
|
def convert_to_geojson(gtfs_zip, output_filename):
|
||||||
|
feed = gtfs_kit.read_feed(gtfs_zip, dist_units='km')
|
||||||
|
|
||||||
|
geojson_data = [
|
||||||
|
("routes.geojson", feed.routes_to_geojson),
|
||||||
|
("shapes.geojson", feed.shapes_to_geojson),
|
||||||
|
("stop_times.geojson", feed.stop_times_to_geojson),
|
||||||
|
("stops.geojson", feed.stops_to_geojson),
|
||||||
|
("trips.geojson", feed.trips_to_geojson)
|
||||||
|
]
|
||||||
|
|
||||||
|
with zipfile.ZipFile(output_filename, "w") as zipf:
|
||||||
|
for filename, data_fn in geojson_data:
|
||||||
|
try:
|
||||||
|
zipf.writestr(filename, json.dumps(data_fn()))
|
||||||
|
# some files may be empty in which case they get read as None
|
||||||
|
# shapes being None raises a ValueError
|
||||||
|
except (AttributeError, ValueError):
|
||||||
|
pass
|
||||||
|
|
@ -7,6 +7,7 @@ from amarillo.utils.container import container
|
||||||
from amarillo.plugins.grfs_export.router import router
|
from amarillo.plugins.grfs_export.router import router
|
||||||
from amarillo.plugins.enhancer.configuration import configure_enhancer_services
|
from amarillo.plugins.enhancer.configuration import configure_enhancer_services
|
||||||
from amarillo.utils.utils import assert_folder_exists
|
from amarillo.utils.utils import assert_folder_exists
|
||||||
|
from amarillo.plugins.grfs_export.geojson_export import convert_to_geojson
|
||||||
from glob import glob
|
from glob import glob
|
||||||
import json
|
import json
|
||||||
import schedule
|
import schedule
|
||||||
|
|
@ -58,7 +59,9 @@ def generate_gtfs():
|
||||||
container['trips_store'],
|
container['trips_store'],
|
||||||
container['stops_store'],
|
container['stops_store'],
|
||||||
region.bbox)
|
region.bbox)
|
||||||
exporter.export(f"data/grfs/amarillo.{region.id}.gtfs.zip", "data/tmp/grfs")
|
gtfs_path = f"data/grfs/amarillo.{region.id}.gtfs.zip"
|
||||||
|
exporter.export(gtfs_path, "data/tmp/grfs")
|
||||||
|
convert_to_geojson(gtfs_path, f"data/grfs/amarillo.{region.id}.geojson.zip")
|
||||||
|
|
||||||
def generate_gtfs_rt():
|
def generate_gtfs_rt():
|
||||||
logger.info("Generate GRFS-RT")
|
logger.info("Generate GRFS-RT")
|
||||||
|
|
|
||||||
|
|
@ -49,6 +49,24 @@ async def get_file(region_id: str, user: str = Depends(verify_admin_api_key)):
|
||||||
pass
|
pass
|
||||||
return FileResponse(f'data/grfs/amarillo.{region_id}.gtfs.zip')
|
return FileResponse(f'data/grfs/amarillo.{region_id}.gtfs.zip')
|
||||||
|
|
||||||
|
@router.get("/region/{region_id}/grfs-geojson",
|
||||||
|
summary="Return GRFS Feed for this region in GeoJSON",
|
||||||
|
response_description="GRFS-Feed (zip-file)",
|
||||||
|
response_class=FileResponse,
|
||||||
|
responses={
|
||||||
|
status.HTTP_404_NOT_FOUND: {"description": "Region not found"},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
async def get_file(region_id: str, user: str = Depends(verify_admin_api_key)):
|
||||||
|
_assert_region_exists(region_id)
|
||||||
|
try:
|
||||||
|
from amarillo.plugins.metrics import increment_grfs_download_counter
|
||||||
|
increment_grfs_download_counter()
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
return FileResponse(f'data/grfs/amarillo.{region_id}.geojson.zip')
|
||||||
|
|
||||||
|
|
||||||
@router.get("/region/{region_id}/grfs-rt/",
|
@router.get("/region/{region_id}/grfs-rt/",
|
||||||
summary="Return GRFS-RT Feed for this region",
|
summary="Return GRFS-RT Feed for this region",
|
||||||
response_description="GRFS-RT-Feed",
|
response_description="GRFS-RT-Feed",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue