Spaces:
Sleeping
Sleeping
dn commited on
Commit ·
93255ff
1
Parent(s): 7d5c46d
more local import test
Browse files- notebooks/constants.py +18 -0
- notebooks/sb79map.py +2 -108
- notebooks/src/{arcgis_paginate.py → arcgis.py} +3 -62
- render.yaml +1 -1
- tests/{test_arcgis_paginate.py → test_arcgis.py} +1 -1
notebooks/constants.py
CHANGED
|
@@ -1 +1,19 @@
|
|
| 1 |
SCAG_PARCELS_URL = "https://rdp.scag.ca.gov/mapping/rest/services/Housing/2020_Annual_Land_Use/MapServer/0/query"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
SCAG_PARCELS_URL = "https://rdp.scag.ca.gov/mapping/rest/services/Housing/2020_Annual_Land_Use/MapServer/0/query"
|
| 2 |
+
CA_PARCELS_URL = (
|
| 3 |
+
"https://services1.arcgis.com/jUJYIo9tSA7EHvfZ/ArcGIS/rest/services/"
|
| 4 |
+
"CA_Statewide_Parcels_Public_view/FeatureServer/0/query"
|
| 5 |
+
)
|
| 6 |
+
SCAG_OUT_FIELDS = (
|
| 7 |
+
"APN20,COUNTY,CITY,IL_RATIO,ZN19_CITY,ZN19_SCAG,TCAC_2024,"
|
| 8 |
+
"APPAREL1MI,EDUC1MI,GROCERY1MI,HOSPIT1MI,RESTAUR1MI,JOBS_30MIN,YEAR"
|
| 9 |
+
)
|
| 10 |
+
HALF_MI_M = 804.7
|
| 11 |
+
|
| 12 |
+
ZONE_DENSITIES: dict[tuple[str, int], float] = {
|
| 13 |
+
("200ft", 1): 160,
|
| 14 |
+
("200ft", 2): 140,
|
| 15 |
+
("qtr_mi", 1): 120,
|
| 16 |
+
("qtr_mi", 2): 100,
|
| 17 |
+
("half_mi", 1): 100,
|
| 18 |
+
("half_mi", 2): 80,
|
| 19 |
+
}
|
notebooks/sb79map.py
CHANGED
|
@@ -18,15 +18,14 @@ app = marimo.App()
|
|
| 18 |
with app.setup(hide_code=True):
|
| 19 |
import csv # noqa: I001
|
| 20 |
import io
|
| 21 |
-
import json
|
| 22 |
import tempfile
|
| 23 |
-
import time
|
| 24 |
import zipfile
|
| 25 |
from collections import defaultdict
|
| 26 |
-
from collections.abc import Generator
|
| 27 |
from pathlib import Path
|
| 28 |
from typing import Any
|
| 29 |
from constants import SCAG_PARCELS_URL
|
|
|
|
|
|
|
| 30 |
|
| 31 |
import geopandas as gpd
|
| 32 |
import numpy as np
|
|
@@ -34,7 +33,6 @@ with app.setup(hide_code=True):
|
|
| 34 |
import requests
|
| 35 |
import shapely
|
| 36 |
from shapely import unary_union
|
| 37 |
-
from shapely.geometry import Polygon
|
| 38 |
from typing import Optional
|
| 39 |
|
| 40 |
import folium
|
|
@@ -44,110 +42,6 @@ with app.setup(hide_code=True):
|
|
| 44 |
import matplotlib # noqa: f401
|
| 45 |
|
| 46 |
__generated_with = "0.23.5"
|
| 47 |
-
CA_PARCELS_URL = (
|
| 48 |
-
"https://services1.arcgis.com/jUJYIo9tSA7EHvfZ/ArcGIS/rest/services/"
|
| 49 |
-
"CA_Statewide_Parcels_Public_view/FeatureServer/0/query"
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
SCAG_OUT_FIELDS = (
|
| 53 |
-
"APN20,COUNTY,CITY,IL_RATIO,ZN19_CITY,ZN19_SCAG,TCAC_2024,"
|
| 54 |
-
"APPAREL1MI,EDUC1MI,GROCERY1MI,HOSPIT1MI,RESTAUR1MI,JOBS_30MIN,YEAR"
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
HALF_MI_M = 804.7 # 0.5 mile in metres
|
| 58 |
-
|
| 59 |
-
# Zone densities (du/ac) by buffer zone and Tier
|
| 60 |
-
ZONE_DENSITIES: dict[tuple[str, int], float] = {
|
| 61 |
-
("200ft", 1): 160,
|
| 62 |
-
("200ft", 2): 140,
|
| 63 |
-
("qtr_mi", 1): 120,
|
| 64 |
-
("qtr_mi", 2): 100,
|
| 65 |
-
("half_mi", 1): 100,
|
| 66 |
-
("half_mi", 2): 80,
|
| 67 |
-
}
|
| 68 |
-
print("https://github.com/dknomura/missing_metro/raw/refs/heads/main/notebooks/public/oc-streetcar_gtfs.zip")
|
| 69 |
-
|
| 70 |
-
def shapely_to_esri_json(polygon: Polygon, wkid: int = 3857) -> dict | None:
|
| 71 |
-
"""Convert a Shapely polygon to an ESRI JSON geometry object."""
|
| 72 |
-
if not polygon or polygon.is_empty:
|
| 73 |
-
return None
|
| 74 |
-
coords = list(polygon.exterior.coords)
|
| 75 |
-
rings = [[[x, y] for x, y in coords]]
|
| 76 |
-
return {"rings": rings, "spatialReference": {"wkid": wkid}}
|
| 77 |
-
|
| 78 |
-
def paginate_arcgis(
|
| 79 |
-
url: str,
|
| 80 |
-
geometry: str = None,
|
| 81 |
-
geometry_type: str = "esriGeometryPolygon",
|
| 82 |
-
spatial_rel: str = "esriSpatialRelIntersects",
|
| 83 |
-
in_sr: int = 3310,
|
| 84 |
-
out_fields: str = "*",
|
| 85 |
-
return_geometry: str = "true",
|
| 86 |
-
f: str = "geojson",
|
| 87 |
-
where: str = "1=1",
|
| 88 |
-
max_record_count: int = 2000,
|
| 89 |
-
delay: float = 0,
|
| 90 |
-
) -> Generator[gpd.GeoDataFrame, None, None]:
|
| 91 |
-
"""Paginate through an ArcGIS REST API query and yield GeoDataFrames."""
|
| 92 |
-
offset = 0
|
| 93 |
-
while True:
|
| 94 |
-
params = {
|
| 95 |
-
"geometry": geometry,
|
| 96 |
-
"geometryType": geometry_type,
|
| 97 |
-
"spatialRel": spatial_rel,
|
| 98 |
-
"inSR": str(in_sr),
|
| 99 |
-
"outFields": out_fields,
|
| 100 |
-
"returnGeometry": return_geometry,
|
| 101 |
-
"f": f,
|
| 102 |
-
"where": where,
|
| 103 |
-
"resultOffset": str(offset),
|
| 104 |
-
"resultRecordCount": str(max_record_count),
|
| 105 |
-
}
|
| 106 |
-
resp = requests.get(url, params=params)
|
| 107 |
-
resp.raise_for_status()
|
| 108 |
-
data = resp.json()
|
| 109 |
-
features = data.get("features", [])
|
| 110 |
-
if not features:
|
| 111 |
-
break
|
| 112 |
-
page = gpd.GeoDataFrame.from_features(features)
|
| 113 |
-
yield page
|
| 114 |
-
offset += max_record_count
|
| 115 |
-
time.sleep(delay)
|
| 116 |
-
if not data.get("properties", {}).get("exceededTransferLimit", False):
|
| 117 |
-
break
|
| 118 |
-
|
| 119 |
-
def fetch_from_arcgis(
|
| 120 |
-
url: str,
|
| 121 |
-
geometries: list[Polygon] = None,
|
| 122 |
-
out_fields: str = "*",
|
| 123 |
-
wkid: int = 3310,
|
| 124 |
-
max_record_count: int = 2000,
|
| 125 |
-
delay: float = 0,
|
| 126 |
-
) -> gpd.GeoDataFrame:
|
| 127 |
-
"""Query an ArcGIS parcel endpoint for multiple geometries and concatenate results."""
|
| 128 |
-
if geometries is None:
|
| 129 |
-
geometries = [None]
|
| 130 |
-
all_geometries: list[gpd.GeoDataFrame] = []
|
| 131 |
-
for geom in geometries:
|
| 132 |
-
esri_geom = shapely_to_esri_json(geom, wkid=wkid)
|
| 133 |
-
geometry_json = json.dumps(esri_geom) if esri_geom else None
|
| 134 |
-
for page in paginate_arcgis(
|
| 135 |
-
url=url,
|
| 136 |
-
geometry=geometry_json,
|
| 137 |
-
geometry_type="esriGeometryPolygon",
|
| 138 |
-
spatial_rel="esriSpatialRelIntersects",
|
| 139 |
-
in_sr=wkid,
|
| 140 |
-
out_fields=out_fields,
|
| 141 |
-
return_geometry="true",
|
| 142 |
-
f="geojson",
|
| 143 |
-
where="1=1",
|
| 144 |
-
max_record_count=max_record_count,
|
| 145 |
-
delay=delay,
|
| 146 |
-
):
|
| 147 |
-
all_geometries.append(page)
|
| 148 |
-
if not all_geometries:
|
| 149 |
-
return gpd.GeoDataFrame()
|
| 150 |
-
return gpd.GeoDataFrame(pd.concat(all_geometries, ignore_index=True))
|
| 151 |
|
| 152 |
def parse_gtfs_zip(
|
| 153 |
gtfs_path: str | Path,
|
|
|
|
| 18 |
with app.setup(hide_code=True):
|
| 19 |
import csv # noqa: I001
|
| 20 |
import io
|
|
|
|
| 21 |
import tempfile
|
|
|
|
| 22 |
import zipfile
|
| 23 |
from collections import defaultdict
|
|
|
|
| 24 |
from pathlib import Path
|
| 25 |
from typing import Any
|
| 26 |
from constants import SCAG_PARCELS_URL
|
| 27 |
+
from constants import CA_PARCELS_URL, HALF_MI_M, SCAG_OUT_FIELDS, ZONE_DENSITIES
|
| 28 |
+
from src.arcgis import fetch_from_arcgis
|
| 29 |
|
| 30 |
import geopandas as gpd
|
| 31 |
import numpy as np
|
|
|
|
| 33 |
import requests
|
| 34 |
import shapely
|
| 35 |
from shapely import unary_union
|
|
|
|
| 36 |
from typing import Optional
|
| 37 |
|
| 38 |
import folium
|
|
|
|
| 42 |
import matplotlib # noqa: f401
|
| 43 |
|
| 44 |
__generated_with = "0.23.5"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
def parse_gtfs_zip(
|
| 47 |
gtfs_path: str | Path,
|
notebooks/src/{arcgis_paginate.py → arcgis.py}
RENAMED
|
@@ -11,16 +11,7 @@ from shapely.geometry import Polygon
|
|
| 11 |
|
| 12 |
|
| 13 |
def shapely_to_esri_json(polygon: Polygon, wkid: int = 3857) -> dict | None:
|
| 14 |
-
"""Convert a Shapely polygon to an ESRI JSON geometry object.
|
| 15 |
-
|
| 16 |
-
Args:
|
| 17 |
-
polygon: A Shapely Polygon (or MultiPolygon).
|
| 18 |
-
wkid: The spatial reference WKID for the output geometry.
|
| 19 |
-
|
| 20 |
-
Returns:
|
| 21 |
-
An ESRI JSON geometry dict with ``rings`` and ``spatialReference``,
|
| 22 |
-
or ``None`` if the polygon is empty.
|
| 23 |
-
"""
|
| 24 |
if not polygon or polygon.is_empty:
|
| 25 |
return None
|
| 26 |
coords = list(polygon.exterior.coords)
|
|
@@ -41,34 +32,8 @@ def paginate_arcgis(
|
|
| 41 |
max_record_count: int = 2000,
|
| 42 |
delay: float = 0,
|
| 43 |
) -> Generator[gpd.GeoDataFrame, None, None]:
|
| 44 |
-
"""Paginate through an ArcGIS REST API query and yield GeoDataFrames.
|
| 45 |
-
|
| 46 |
-
Handles ``resultOffset`` / ``resultRecordCount`` pagination and respects
|
| 47 |
-
the server's ``exceededTransferLimit`` flag.
|
| 48 |
-
|
| 49 |
-
Args:
|
| 50 |
-
url: The ArcGIS REST endpoint URL.
|
| 51 |
-
geometry: JSON-encoded ESRI geometry string (e.g. from
|
| 52 |
-
:func:`shapely_to_esri_json`).
|
| 53 |
-
geometry_type: The geometry type parameter (e.g.
|
| 54 |
-
``"esriGeometryPolygon"``).
|
| 55 |
-
spatial_rel: The spatial relationship parameter.
|
| 56 |
-
in_sr: The input spatial reference WKID.
|
| 57 |
-
out_fields: Comma-separated list of fields to return.
|
| 58 |
-
return_geometry: Whether to return geometry (``"true"`` or ``"false"``).
|
| 59 |
-
f: The output format (e.g. ``"geojson"``).
|
| 60 |
-
where: The SQL where clause.
|
| 61 |
-
max_record_count: Number of records to request per page.
|
| 62 |
-
delay: Seconds to wait between requests (for rate limiting).
|
| 63 |
-
|
| 64 |
-
Yields:
|
| 65 |
-
A GeoDataFrame for each page of results.
|
| 66 |
-
|
| 67 |
-
Raises:
|
| 68 |
-
requests.HTTPError: If the server returns a non-2xx status.
|
| 69 |
-
"""
|
| 70 |
offset = 0
|
| 71 |
-
|
| 72 |
while True:
|
| 73 |
params = {
|
| 74 |
"geometry": geometry,
|
|
@@ -82,21 +47,16 @@ def paginate_arcgis(
|
|
| 82 |
"resultOffset": str(offset),
|
| 83 |
"resultRecordCount": str(max_record_count),
|
| 84 |
}
|
| 85 |
-
|
| 86 |
resp = requests.get(url, params=params)
|
| 87 |
resp.raise_for_status()
|
| 88 |
data = resp.json()
|
| 89 |
-
|
| 90 |
features = data.get("features", [])
|
| 91 |
if not features:
|
| 92 |
break
|
| 93 |
-
|
| 94 |
page = gpd.GeoDataFrame.from_features(features)
|
| 95 |
yield page
|
| 96 |
-
|
| 97 |
offset += max_record_count
|
| 98 |
time.sleep(delay)
|
| 99 |
-
|
| 100 |
if not data.get("properties", {}).get("exceededTransferLimit", False):
|
| 101 |
break
|
| 102 |
|
|
@@ -109,30 +69,13 @@ def fetch_from_arcgis(
|
|
| 109 |
max_record_count: int = 2000,
|
| 110 |
delay: float = 0,
|
| 111 |
) -> gpd.GeoDataFrame:
|
| 112 |
-
"""Query an ArcGIS parcel endpoint for multiple geometries and concatenate results.
|
| 113 |
-
|
| 114 |
-
For each geometry in ``geometries``, this calls :func:`paginate_arcgis`
|
| 115 |
-
and concatenates all returned pages into a single GeoDataFrame.
|
| 116 |
-
|
| 117 |
-
Args:
|
| 118 |
-
url: The ArcGIS REST endpoint URL.
|
| 119 |
-
geometries: A list of Shapely Polygon geometries to query.
|
| 120 |
-
out_fields: Comma-separated list of fields to return.
|
| 121 |
-
wkid: The spatial reference WKID for the input geometry.
|
| 122 |
-
max_record_count: Number of records to request per page.
|
| 123 |
-
delay: Seconds to wait between requests (for rate limiting).
|
| 124 |
-
|
| 125 |
-
Returns:
|
| 126 |
-
A single GeoDataFrame containing all results from all geometries.
|
| 127 |
-
"""
|
| 128 |
if geometries is None:
|
| 129 |
geometries = [None]
|
| 130 |
all_geometries: list[gpd.GeoDataFrame] = []
|
| 131 |
-
|
| 132 |
for geom in geometries:
|
| 133 |
esri_geom = shapely_to_esri_json(geom, wkid=wkid)
|
| 134 |
geometry_json = json.dumps(esri_geom) if esri_geom else None
|
| 135 |
-
|
| 136 |
for page in paginate_arcgis(
|
| 137 |
url=url,
|
| 138 |
geometry=geometry_json,
|
|
@@ -147,8 +90,6 @@ def fetch_from_arcgis(
|
|
| 147 |
delay=delay,
|
| 148 |
):
|
| 149 |
all_geometries.append(page)
|
| 150 |
-
|
| 151 |
if not all_geometries:
|
| 152 |
return gpd.GeoDataFrame()
|
| 153 |
-
|
| 154 |
return gpd.GeoDataFrame(pd.concat(all_geometries, ignore_index=True))
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def shapely_to_esri_json(polygon: Polygon, wkid: int = 3857) -> dict | None:
|
| 14 |
+
"""Convert a Shapely polygon to an ESRI JSON geometry object."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if not polygon or polygon.is_empty:
|
| 16 |
return None
|
| 17 |
coords = list(polygon.exterior.coords)
|
|
|
|
| 32 |
max_record_count: int = 2000,
|
| 33 |
delay: float = 0,
|
| 34 |
) -> Generator[gpd.GeoDataFrame, None, None]:
|
| 35 |
+
"""Paginate through an ArcGIS REST API query and yield GeoDataFrames."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
offset = 0
|
|
|
|
| 37 |
while True:
|
| 38 |
params = {
|
| 39 |
"geometry": geometry,
|
|
|
|
| 47 |
"resultOffset": str(offset),
|
| 48 |
"resultRecordCount": str(max_record_count),
|
| 49 |
}
|
|
|
|
| 50 |
resp = requests.get(url, params=params)
|
| 51 |
resp.raise_for_status()
|
| 52 |
data = resp.json()
|
|
|
|
| 53 |
features = data.get("features", [])
|
| 54 |
if not features:
|
| 55 |
break
|
|
|
|
| 56 |
page = gpd.GeoDataFrame.from_features(features)
|
| 57 |
yield page
|
|
|
|
| 58 |
offset += max_record_count
|
| 59 |
time.sleep(delay)
|
|
|
|
| 60 |
if not data.get("properties", {}).get("exceededTransferLimit", False):
|
| 61 |
break
|
| 62 |
|
|
|
|
| 69 |
max_record_count: int = 2000,
|
| 70 |
delay: float = 0,
|
| 71 |
) -> gpd.GeoDataFrame:
|
| 72 |
+
"""Query an ArcGIS parcel endpoint for multiple geometries and concatenate results."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if geometries is None:
|
| 74 |
geometries = [None]
|
| 75 |
all_geometries: list[gpd.GeoDataFrame] = []
|
|
|
|
| 76 |
for geom in geometries:
|
| 77 |
esri_geom = shapely_to_esri_json(geom, wkid=wkid)
|
| 78 |
geometry_json = json.dumps(esri_geom) if esri_geom else None
|
|
|
|
| 79 |
for page in paginate_arcgis(
|
| 80 |
url=url,
|
| 81 |
geometry=geometry_json,
|
|
|
|
| 90 |
delay=delay,
|
| 91 |
):
|
| 92 |
all_geometries.append(page)
|
|
|
|
| 93 |
if not all_geometries:
|
| 94 |
return gpd.GeoDataFrame()
|
|
|
|
| 95 |
return gpd.GeoDataFrame(pd.concat(all_geometries, ignore_index=True))
|
render.yaml
CHANGED
|
@@ -2,7 +2,7 @@ services:
|
|
| 2 |
- type: web
|
| 3 |
name: sb79-map
|
| 4 |
runtime: python
|
| 5 |
-
buildCommand:
|
| 6 |
startCommand: marimo run notebooks/sb79map.py --host 0.0.0.0 --port $PORT
|
| 7 |
envVars:
|
| 8 |
- key: PYTHON_VERSION
|
|
|
|
| 2 |
- type: web
|
| 3 |
name: sb79-map
|
| 4 |
runtime: python
|
| 5 |
+
buildCommand: uv sync --frozen && uv cache prune --ci
|
| 6 |
startCommand: marimo run notebooks/sb79map.py --host 0.0.0.0 --port $PORT
|
| 7 |
envVars:
|
| 8 |
- key: PYTHON_VERSION
|
tests/{test_arcgis_paginate.py → test_arcgis.py}
RENAMED
|
@@ -6,7 +6,7 @@ import geopandas as gpd
|
|
| 6 |
import pytest
|
| 7 |
import responses
|
| 8 |
from shapely.geometry import Polygon, box
|
| 9 |
-
from src.
|
| 10 |
fetch_from_arcgis,
|
| 11 |
paginate_arcgis,
|
| 12 |
shapely_to_esri_json,
|
|
|
|
| 6 |
import pytest
|
| 7 |
import responses
|
| 8 |
from shapely.geometry import Polygon, box
|
| 9 |
+
from src.arcgis import (
|
| 10 |
fetch_from_arcgis,
|
| 11 |
paginate_arcgis,
|
| 12 |
shapely_to_esri_json,
|