diff --git a/amarillo/plugins/grfs_export/geojson_export.py b/amarillo/plugins/grfs_export/geojson_export.py new file mode 100644 index 0000000..b1b43bb --- /dev/null +++ b/amarillo/plugins/grfs_export/geojson_export.py @@ -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 \ No newline at end of file diff --git a/amarillo/plugins/grfs_export/gtfs_generator.py b/amarillo/plugins/grfs_export/gtfs_generator.py index 4ccf4a0..a63bf0d 100644 --- a/amarillo/plugins/grfs_export/gtfs_generator.py +++ b/amarillo/plugins/grfs_export/gtfs_generator.py @@ -7,6 +7,7 @@ from amarillo.utils.container import container from amarillo.plugins.grfs_export.router import router from amarillo.plugins.enhancer.configuration import configure_enhancer_services from amarillo.utils.utils import assert_folder_exists +from amarillo.plugins.grfs_export.geojson_export import convert_to_geojson from glob import glob import json import schedule @@ -58,7 +59,9 @@ def generate_gtfs(): container['trips_store'], container['stops_store'], 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(): logger.info("Generate GRFS-RT") diff --git a/amarillo/plugins/grfs_export/router.py b/amarillo/plugins/grfs_export/router.py index 8af84e0..67681d9 100644 --- a/amarillo/plugins/grfs_export/router.py +++ b/amarillo/plugins/grfs_export/router.py @@ -49,6 +49,24 @@ async def get_file(region_id: str, user: str = Depends(verify_admin_api_key)): pass 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/", summary="Return GRFS-RT Feed for this region", response_description="GRFS-RT-Feed",