-
Notifications
You must be signed in to change notification settings - Fork 0
Ambulance data #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AtticusTarleton
wants to merge
18
commits into
main
Choose a base branch
from
ambulance-data
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ambulance data #67
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
39c2e29
created files for ambulance service area data, and made it work so th…
AtticusTarleton b5a3c9c
fixed a problem preventing mapping from working
AtticusTarleton 47943da
fixing a gitignore issue
AtticusTarleton c0d2055
Merge branch 'main' into ambulance-data
AtticusTarleton 0dbcdb1
Merge branch 'main' into ambulance-data
FWJK1 0459324
fixed ruff errors
AtticusTarleton e0d81e5
fixed a pathing error
AtticusTarleton 34d738f
fixed prettier errors, and some sql errors (I believe in CDC.py
AtticusTarleton fc457e9
for some reason my ruff check was not finding the 2 spaces between fu…
AtticusTarleton 72a87c6
added ruff to uv
AtticusTarleton 869eb34
checking if the automatic ruff check on commits works
AtticusTarleton bbdb051
Merge branch 'main' into ambulance-data
AtticusTarleton a841b81
fixed a ruff error
AtticusTarleton 802980c
small changes to meet ruff requirements
FWJK1 9b9733f
Merge branch 'main' into ambulance-data
FWJK1 f580311
added to main.py, slight refactoring to work with main, removed unrel…
FWJK1 020248c
added parquet
FWJK1 fc74974
added a legend to ambulance data and made a switch that sets a thicke…
AtticusTarleton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
AtticusTarleton marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| from fastapi import APIRouter, Response | ||
|
|
||
| from api.core_functions import request_to_source, spec_to_source | ||
| from api.models import FilterRequest, FilterSpec | ||
| from query import ( | ||
| get_ambulance_geojson, | ||
| get_ambulance_legend, | ||
| ) | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.post("/load/mapping/ambulance/service_area") | ||
| async def ambulance_info_geojson(request: FilterRequest): | ||
| source = request_to_source(request, "ambulance_ambulance_info", "default") | ||
| data = get_ambulance_geojson([source]) | ||
| return Response(content=data, media_type="application/json") | ||
|
|
||
|
|
||
| @router.post("/load/mapping/ambulance/service_area_new") | ||
| async def ambulance_info_geojson_new(specs: list[FilterSpec]): | ||
| sources = [spec_to_source(spec, "default") for spec in specs] | ||
| data = get_ambulance_geojson(sources) | ||
| return Response(content=data, media_type="application/json") | ||
|
|
||
|
|
||
| @router.get("/load/mapping/ambulance/ambulance_legend") | ||
| async def ambulance_legend(): | ||
| data = get_ambulance_legend() | ||
| return Response(content=data, media_type="application/json") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
AtticusTarleton marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| """ | ||
| **Author**: | ||
| Atticus Tarleton | ||
| **Created**: | ||
| 2026-07-20 | ||
| **Description**: | ||
| Build script to convert the ambulance service area files into SQL tables. | ||
| """ | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
|
|
||
| import duckdb | ||
|
|
||
| _project_root = Path.cwd() | ||
| while not (_project_root / "api").exists(): | ||
| _project_root = _project_root.parent | ||
| os.chdir(_project_root) | ||
| print(os.getcwd()) | ||
|
|
||
| # globals | ||
| con = duckdb.connect() | ||
| proc_dir = Path("Data/_Processed/ambulance") | ||
| data_dir = Path("Data/ambulance/ambulance_service_areas.parquet") | ||
|
|
||
| # hardcoded specifics: | ||
| ambulance_info_cols = [ | ||
| "OBJECTID", | ||
| "Serv_Name", | ||
| "Cert_Level", | ||
| "Address", | ||
| "Street_1", | ||
| "Street_2", | ||
| "City", | ||
| "State", | ||
| "Zip_Code", | ||
| "Total_Tran", | ||
| "Per_No_Tran", | ||
| "Re_Per_Tran", | ||
| "Cost_Per", | ||
| "Cost_Call", | ||
| ] | ||
|
|
||
| ambulance_geom_cols = [ | ||
| "OBJECTID", | ||
| "Shape__Area", | ||
| "Shape__Length", | ||
| "geometry", | ||
| ] | ||
|
|
||
|
|
||
| # functions: | ||
| def load_ambulance_data(): | ||
| con.execute("LOAD spatial") | ||
|
|
||
| con.execute(f"""--sql | ||
| CREATE OR REPLACE VIEW ambulance_service_areas AS | ||
| SELECT * FROM read_parquet('{data_dir}') | ||
| """) | ||
|
|
||
|
|
||
| def build_ambulance_info_table(): | ||
| info_string = ", ".join(ambulance_info_cols) | ||
|
|
||
| con.execute(f"""--sql | ||
| CREATE OR REPLACE VIEW ambulance_info AS | ||
| SELECT {info_string} | ||
| FROM ambulance_service_areas | ||
| """) | ||
|
|
||
|
|
||
| def build_ambulance_geom_table(): | ||
| geom_string = ", ".join(ambulance_geom_cols) | ||
|
|
||
| con.execute(f"""--sql | ||
| CREATE OR REPLACE VIEW ambulance_geom AS | ||
| SELECT {geom_string} | ||
| FROM ambulance_service_areas | ||
| """) | ||
|
|
||
|
|
||
| def build_ambulance_color_table(): | ||
| con.execute("""--sql | ||
| CREATE TABLE ambulance_colors ( | ||
| certification_level TEXT PRIMARY KEY, | ||
| hex_color TEXT NOT NULL, | ||
| rgba TEXT NOT NULL -- '[255,127,14,180]' as JSON-ish text | ||
| ); | ||
|
|
||
| INSERT INTO ambulance_colors VALUES | ||
| ('Paramedic', '#2ca02c', '[44, 160, 44, 180]'), | ||
| ('Advanced EMT', '#ffcc00', '[255, 204, 0, 180]'), | ||
| ('Paramedic - Critical Care Endorsement', '#fd7e14', '[253, 126, 20, 180]') | ||
| """) | ||
|
|
||
|
|
||
| def save_ambulance_tables(): | ||
| load_ambulance_data() | ||
| build_ambulance_info_table() | ||
| build_ambulance_geom_table() | ||
| build_ambulance_color_table() | ||
|
|
||
| for table in ["ambulance_info", "ambulance_geom", "ambulance_colors"]: | ||
| con.execute( | ||
| f"COPY (SELECT * FROM {table}) TO '{proc_dir / f'{table}.parquet'}' " | ||
| ) | ||
|
|
||
|
|
||
| ## Putting everything together | ||
| def main(): | ||
| proc_dir.mkdir(parents=True, exist_ok=True) | ||
| con.execute("LOAD spatial") | ||
| save_ambulance_tables() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
AtticusTarleton marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import pandas as pd | ||
| import requests | ||
| from io import BytesIO | ||
| from pyogrio import read_dataframe | ||
|
|
||
| AMBULANCE_SERVICE_AREA = "https://services1.arcgis.com/BkFxaEFNwHqX3tAw/arcgis/rest/services/FS_VCGI_OPENDATA_Emergency_AmbulanceServiceAreas_SP_v1/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson" | ||
| STORAGE_LOCATION = "../Data/ambulance" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Ambulance API fetch | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| # Fetch ambulance data from goverment arcgis website (geojson files) | ||
|
|
||
|
|
||
| def fetch_service_areas() -> pd.DataFrame | None: | ||
| r = requests.get(AMBULANCE_SERVICE_AREA, timeout=30) | ||
| r.raise_for_status() | ||
| df = read_dataframe(BytesIO(r.content)) | ||
| return df | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Main scrape runner | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| def run_ambulance_scrape() -> None: | ||
| """ | ||
| Fetch Wastewater data and save as parquet files. | ||
| """ | ||
| service_areas = fetch_service_areas() | ||
|
|
||
| if service_areas is not None: | ||
| service_areas.to_parquet( | ||
| f"{STORAGE_LOCATION}/ambulance_service_areas.parquet", index=False | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run_ambulance_scrape() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.