Spaces:
Sleeping
Sleeping
dn commited on
Commit ·
107b207
1
Parent(s): 4c2ea61
optimize compute_dwelling_units, compute_scag_density, and clip_to_buffer_rings
Browse files- shared/pipelines/sb79.py +121 -122
- shared/utils/geoprocessing.py +5 -41
shared/pipelines/sb79.py
CHANGED
|
@@ -80,93 +80,95 @@ def _empty_result() -> gpd.GeoDataFrame:
|
|
| 80 |
|
| 81 |
|
| 82 |
def compute_scag_density(scag_parcels: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
|
| 83 |
-
"""Add ``current_density_du_per_ac`` to SCAG parcels using LA / non-LA logic.
|
| 84 |
-
|
| 85 |
-
For LA parcels, first tries ``ZN19_CITY`` zone codes. If no match is found,
|
| 86 |
-
falls through to try ``ZN19_SCAG`` codes. Parcels that don't match any code
|
| 87 |
-
get ``NaN`` (excluded from downstream calculations).
|
| 88 |
-
|
| 89 |
-
Parameters
|
| 90 |
-
----------
|
| 91 |
-
scag_parcels:
|
| 92 |
-
Must have columns ``ZN19_CITY``, ``ZN19_SCAG``, ``CITY``, and a valid
|
| 93 |
-
geometry for area calculations.
|
| 94 |
-
|
| 95 |
-
Returns
|
| 96 |
-
-------
|
| 97 |
-
gpd.GeoDataFrame
|
| 98 |
-
Same as input with an added ``current_density_du_per_ac`` column.
|
| 99 |
-
"""
|
| 100 |
df = scag_parcels.copy()
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
(df["ZN19_CITY"].str.contains("C3", na=False), 110),
|
| 134 |
-
(df["ZN19_CITY"].str.contains("C4", na=False), 200),
|
| 135 |
-
]
|
| 136 |
-
)
|
| 137 |
|
| 138 |
zn19_scag = df["ZN19_SCAG"].astype(str)
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
return df
|
| 171 |
|
| 172 |
|
|
@@ -175,10 +177,18 @@ def compute_dwelling_units(
|
|
| 175 |
scag_density: gpd.GeoDataFrame,
|
| 176 |
) -> gpd.GeoDataFrame:
|
| 177 |
stops_trimmed = stops_gdf[["stop_id", "Tier", "geometry"]]
|
| 178 |
-
|
| 179 |
scag_clean = scag_density.drop(columns=["Tier"], errors="ignore")
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
buffer_distances = [200, 1320, 2640]
|
|
|
|
| 182 |
rings = clip_to_buffer_rings(
|
| 183 |
features_gdf=scag_clean,
|
| 184 |
sources_gdf=stops_trimmed,
|
|
@@ -191,59 +201,48 @@ def compute_dwelling_units(
|
|
| 191 |
ring["buffer_zone_id"] = f"{distance} ft"
|
| 192 |
|
| 193 |
buffed_parcels = gpd.GeoDataFrame(pd.concat(rings, ignore_index=True))
|
| 194 |
-
|
| 195 |
buffed_parcels["area_sqft"] = buffed_parcels.area
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
{
|
| 199 |
-
("200 ft", 1): 160,
|
| 200 |
-
("200 ft", 2): 140,
|
| 201 |
-
("1320 ft", 1): 120,
|
| 202 |
-
("1320 ft", 2): 100,
|
| 203 |
-
("2640 ft", 1): 100,
|
| 204 |
-
("2640 ft", 2): 80,
|
| 205 |
-
}
|
| 206 |
)
|
| 207 |
-
buffed_parcels["zone_density"] = buffed_parcels.set_index(["buffer_zone_id", "Tier"]).index.map(zone_densities_series)
|
| 208 |
-
|
| 209 |
-
area_agg = buffed_parcels.groupby("APN20")["area_sqft"].sum().rename("area_sqft_total")
|
| 210 |
-
area_acres = area_agg / 43560
|
| 211 |
-
|
| 212 |
buffed_parcels["area_x_density"] = buffed_parcels["area_sqft"] * buffed_parcels["zone_density"]
|
| 213 |
-
weighted_sum = buffed_parcels.groupby("APN20")["area_x_density"].sum()
|
| 214 |
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
"Tier",
|
| 221 |
-
"ZN19_CITY",
|
| 222 |
-
"ZN19_SCAG",
|
| 223 |
-
"COUNTY",
|
| 224 |
-
"CITY",
|
| 225 |
-
"buffer_zone_id",
|
| 226 |
-
]
|
| 227 |
-
]
|
| 228 |
weighted_density = np.where(
|
| 229 |
-
(
|
|
|
|
|
|
|
| 230 |
)
|
| 231 |
|
| 232 |
by_apn = gpd.GeoDataFrame(
|
| 233 |
{
|
| 234 |
-
"APN20":
|
| 235 |
"geometry": geom_agg.values,
|
| 236 |
"area_acres": area_acres.values,
|
| 237 |
"new_density_du_per_ac": weighted_density,
|
| 238 |
"new_dwelling_units": weighted_density * area_acres.values,
|
| 239 |
-
"current_dwelling_units":
|
| 240 |
-
"current_density_du_per_ac":
|
| 241 |
-
"Tier":
|
| 242 |
-
"ZN19_CITY":
|
| 243 |
-
"ZN19_SCAG":
|
| 244 |
-
"COUNTY":
|
| 245 |
-
"CITY":
|
| 246 |
-
"buffer_zone_id":
|
| 247 |
},
|
| 248 |
geometry="geometry",
|
| 249 |
crs=buffed_parcels.crs,
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
def compute_scag_density(scag_parcels: gpd.GeoDataFrame) -> gpd.GeoDataFrame:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
df = scag_parcels.copy()
|
| 84 |
+
is_la = df["CITY"] == "Los Angeles"
|
| 85 |
|
| 86 |
+
SCAG_FIXED = {
|
| 87 |
+
"1123": 18,
|
| 88 |
+
"1124": 60,
|
| 89 |
+
"1125": 80,
|
| 90 |
+
"1131": 6,
|
| 91 |
+
"1150": 1,
|
| 92 |
+
"1600": 40,
|
| 93 |
+
"1610": 40,
|
| 94 |
+
"1620": 30,
|
| 95 |
+
"1220": 47,
|
| 96 |
+
"1221": 47,
|
| 97 |
+
"1222": 47,
|
| 98 |
+
"2000": 0.2,
|
| 99 |
+
"2100": 0.2,
|
| 100 |
+
"2200": 0.2,
|
| 101 |
+
"2300": 0.2,
|
| 102 |
+
"2400": 0.2,
|
| 103 |
+
"2500": 0.2,
|
| 104 |
+
"2600": 0.2,
|
| 105 |
+
"2700": 0.2,
|
| 106 |
+
"1900": 0,
|
| 107 |
+
"7777": 0,
|
| 108 |
+
"1500": 0,
|
| 109 |
+
"1233": 0,
|
| 110 |
+
"1210": 0,
|
| 111 |
+
"1211": 0,
|
| 112 |
+
"1212": 0,
|
| 113 |
+
"1213": 0,
|
| 114 |
+
"1247": 0,
|
| 115 |
+
}
|
| 116 |
+
SCAG_AREA_BASED = {"1110": 1, "1111": 1, "1112": 1, "1113": 1, "1121": 3, "1122": 3, "1140": 3}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
zn19_scag = df["ZN19_SCAG"].astype(str)
|
| 119 |
+
scag_density = zn19_scag.map(SCAG_FIXED)
|
| 120 |
+
|
| 121 |
+
area_mask = zn19_scag.isin(SCAG_AREA_BASED)
|
| 122 |
+
if area_mask.any():
|
| 123 |
+
area_acres = df.loc[area_mask].to_crs(SCAG_FT_CRS).area / 43560
|
| 124 |
+
area_acres = area_acres.replace(0, np.nan)
|
| 125 |
+
scag_density.loc[area_mask] = zn19_scag[area_mask].map(SCAG_AREA_BASED) / area_acres
|
| 126 |
+
|
| 127 |
+
LA_ZONES = [
|
| 128 |
+
("RD1.5", 43560 / 1500),
|
| 129 |
+
("RD2", 43560 / 2000),
|
| 130 |
+
("RD3", 43560 / 3000),
|
| 131 |
+
("RD4", 43560 / 4000),
|
| 132 |
+
("RD5", 43560 / 5000),
|
| 133 |
+
("RD6", 43560 / 6000),
|
| 134 |
+
("RMP", 43560 / 20000),
|
| 135 |
+
("RAS3", 43560 / 800),
|
| 136 |
+
("RAS4", 43560 / 400),
|
| 137 |
+
("R3", 43560 / 800),
|
| 138 |
+
("R4", 43560 / 400),
|
| 139 |
+
("R5", 43560 / 200),
|
| 140 |
+
("RE40", 43560 / 40000),
|
| 141 |
+
("RE20", 43560 / 20000),
|
| 142 |
+
("RE15", 43560 / 15000),
|
| 143 |
+
("RE11", 43560 / 11000),
|
| 144 |
+
("RE9", 43560 / 9000),
|
| 145 |
+
("RS", 43560 / 7500),
|
| 146 |
+
("R1", 43560 / 5000),
|
| 147 |
+
("RU", 43560 / 3500),
|
| 148 |
+
("RZ2.5", 43560 / 2500),
|
| 149 |
+
("RZ3", 43560 / 3000),
|
| 150 |
+
("RZ4", 43560 / 4000),
|
| 151 |
+
("RW1", 43560 / 2300),
|
| 152 |
+
("R2", 43560 / 2500),
|
| 153 |
+
("RW2", 43560 / 1150),
|
| 154 |
+
("C1", 100),
|
| 155 |
+
("C2", 43560 / 400),
|
| 156 |
+
("C3", 110),
|
| 157 |
+
("C4", 200),
|
| 158 |
+
]
|
| 159 |
|
| 160 |
+
if is_la.any():
|
| 161 |
+
la_zones = df.loc[is_la, "ZN19_CITY"]
|
| 162 |
+
la_density = pd.Series(np.nan, index=la_zones.index)
|
| 163 |
+
for code, density in LA_ZONES:
|
| 164 |
+
unmatched = la_density.isna()
|
| 165 |
+
if not unmatched.any():
|
| 166 |
+
break
|
| 167 |
+
mask = unmatched & la_zones.str.contains(code, na=False, regex=False)
|
| 168 |
+
la_density.loc[mask] = density
|
| 169 |
+
scag_density.loc[is_la] = la_density
|
| 170 |
+
|
| 171 |
+
df["current_density_du_per_ac"] = pd.to_numeric(scag_density, errors="coerce")
|
| 172 |
return df
|
| 173 |
|
| 174 |
|
|
|
|
| 177 |
scag_density: gpd.GeoDataFrame,
|
| 178 |
) -> gpd.GeoDataFrame:
|
| 179 |
stops_trimmed = stops_gdf[["stop_id", "Tier", "geometry"]]
|
|
|
|
| 180 |
scag_clean = scag_density.drop(columns=["Tier"], errors="ignore")
|
| 181 |
|
| 182 |
+
zone_densities = {
|
| 183 |
+
("200 ft", 1): 160,
|
| 184 |
+
("200 ft", 2): 140,
|
| 185 |
+
("1320 ft", 1): 120,
|
| 186 |
+
("1320 ft", 2): 100,
|
| 187 |
+
("2640 ft", 1): 100,
|
| 188 |
+
("2640 ft", 2): 80,
|
| 189 |
+
}
|
| 190 |
buffer_distances = [200, 1320, 2640]
|
| 191 |
+
|
| 192 |
rings = clip_to_buffer_rings(
|
| 193 |
features_gdf=scag_clean,
|
| 194 |
sources_gdf=stops_trimmed,
|
|
|
|
| 201 |
ring["buffer_zone_id"] = f"{distance} ft"
|
| 202 |
|
| 203 |
buffed_parcels = gpd.GeoDataFrame(pd.concat(rings, ignore_index=True))
|
|
|
|
| 204 |
buffed_parcels["area_sqft"] = buffed_parcels.area
|
| 205 |
+
buffed_parcels["zone_density"] = buffed_parcels.set_index(["buffer_zone_id", "Tier"]).index.map(
|
| 206 |
+
pd.Series(zone_densities)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
buffed_parcels["area_x_density"] = buffed_parcels["area_sqft"] * buffed_parcels["zone_density"]
|
|
|
|
| 209 |
|
| 210 |
+
agg = buffed_parcels.groupby("APN20").agg(
|
| 211 |
+
area_sqft_total=("area_sqft", "sum"),
|
| 212 |
+
area_x_density_sum=("area_x_density", "sum"),
|
| 213 |
+
current_density_du_per_ac=("current_density_du_per_ac", "first"),
|
| 214 |
+
Tier=("Tier", "first"),
|
| 215 |
+
ZN19_CITY=("ZN19_CITY", "first"),
|
| 216 |
+
ZN19_SCAG=("ZN19_SCAG", "first"),
|
| 217 |
+
COUNTY=("COUNTY", "first"),
|
| 218 |
+
CITY=("CITY", "first"),
|
| 219 |
+
buffer_zone_id=("buffer_zone_id", "first"),
|
| 220 |
+
)
|
| 221 |
|
| 222 |
+
geom_agg = buffed_parcels[["APN20", "geometry"]].dissolve(by="APN20")["geometry"]
|
| 223 |
+
|
| 224 |
+
area_acres = agg["area_sqft_total"] / 43560
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
weighted_density = np.where(
|
| 226 |
+
(agg["area_sqft_total"] > 0) & (agg["current_density_du_per_ac"].notna()),
|
| 227 |
+
agg["area_x_density_sum"] / agg["area_sqft_total"],
|
| 228 |
+
np.nan,
|
| 229 |
)
|
| 230 |
|
| 231 |
by_apn = gpd.GeoDataFrame(
|
| 232 |
{
|
| 233 |
+
"APN20": agg.index,
|
| 234 |
"geometry": geom_agg.values,
|
| 235 |
"area_acres": area_acres.values,
|
| 236 |
"new_density_du_per_ac": weighted_density,
|
| 237 |
"new_dwelling_units": weighted_density * area_acres.values,
|
| 238 |
+
"current_dwelling_units": agg["current_density_du_per_ac"].values * area_acres.values,
|
| 239 |
+
"current_density_du_per_ac": agg["current_density_du_per_ac"].values,
|
| 240 |
+
"Tier": agg["Tier"].values,
|
| 241 |
+
"ZN19_CITY": agg["ZN19_CITY"].values,
|
| 242 |
+
"ZN19_SCAG": agg["ZN19_SCAG"].values,
|
| 243 |
+
"COUNTY": agg["COUNTY"].values,
|
| 244 |
+
"CITY": agg["CITY"].values,
|
| 245 |
+
"buffer_zone_id": agg["buffer_zone_id"].values,
|
| 246 |
},
|
| 247 |
geometry="geometry",
|
| 248 |
crs=buffed_parcels.crs,
|
shared/utils/geoprocessing.py
CHANGED
|
@@ -12,38 +12,8 @@ def clip_to_buffer_rings(
|
|
| 12 |
buffer_crs: str = SCAG_FT_CRS,
|
| 13 |
donut_how: Literal["difference", "union"] = "difference",
|
| 14 |
) -> list[gpd.GeoDataFrame]:
|
| 15 |
-
"""Clip features to concentric donut rings buffered around source geometries.
|
| 16 |
-
|
| 17 |
-
Parameters
|
| 18 |
-
----------
|
| 19 |
-
features_gdf:
|
| 20 |
-
Features to clip.
|
| 21 |
-
sources_gdf:
|
| 22 |
-
Source geometries to buffer. All non-geometry columns are carried
|
| 23 |
-
through to the result.
|
| 24 |
-
buffer_distances:
|
| 25 |
-
Buffer distances in the units of ``buffer_crs``. Rings are built
|
| 26 |
-
from smallest to largest — e.g. ``[200, 1320, 2640]`` produces a
|
| 27 |
-
200ft disc, a 200–1320ft donut, and a 1320–2640ft donut.
|
| 28 |
-
buffer_crs:
|
| 29 |
-
CRS to reproject into before buffering and spatial operations.
|
| 30 |
-
Defaults to EPSG:2229 (California State Plane, feet).
|
| 31 |
-
donut_how:
|
| 32 |
-
How to subtract the inner buffer from the outer clip when forming
|
| 33 |
-
donuts. ``"difference"`` produces smooth donut rings clipped to the
|
| 34 |
-
buffer boundary; ``"union"`` retains the full parcel geometry within
|
| 35 |
-
the outer ring without cutting at the inner boundary.
|
| 36 |
-
|
| 37 |
-
Returns
|
| 38 |
-
-------
|
| 39 |
-
list[gpd.GeoDataFrame]
|
| 40 |
-
One GeoDataFrame per distance in ``buffer_distances``, in ascending
|
| 41 |
-
order. The first entry is the innermost disc; subsequent entries are
|
| 42 |
-
donuts. Results are returned in ``buffer_crs``.
|
| 43 |
-
"""
|
| 44 |
distances = sorted(buffer_distances)
|
| 45 |
ring_definitions = [(dist, distances[i - 1] if i > 0 else None) for i, dist in enumerate(distances)]
|
| 46 |
-
|
| 47 |
sources_reprojected = sources_gdf.to_crs(buffer_crs).geometry
|
| 48 |
|
| 49 |
buffers = {
|
|
@@ -57,21 +27,15 @@ def clip_to_buffer_rings(
|
|
| 57 |
|
| 58 |
features_reproj = features_gdf.to_crs(buffer_crs)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
how="intersection",
|
| 65 |
-
)
|
| 66 |
-
for dist, buf in buffers.items()
|
| 67 |
-
}
|
| 68 |
|
| 69 |
rings = []
|
| 70 |
for outer, inner in ring_definitions:
|
| 71 |
outer_clip = clipped[outer]
|
| 72 |
-
if inner is None:
|
| 73 |
-
rings.append(outer_clip)
|
| 74 |
-
elif outer_clip.empty:
|
| 75 |
rings.append(outer_clip)
|
| 76 |
else:
|
| 77 |
rings.append(gpd.overlay(outer_clip, buffers[inner][["geometry"]], how=donut_how))
|
|
|
|
| 12 |
buffer_crs: str = SCAG_FT_CRS,
|
| 13 |
donut_how: Literal["difference", "union"] = "difference",
|
| 14 |
) -> list[gpd.GeoDataFrame]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
distances = sorted(buffer_distances)
|
| 16 |
ring_definitions = [(dist, distances[i - 1] if i > 0 else None) for i, dist in enumerate(distances)]
|
|
|
|
| 17 |
sources_reprojected = sources_gdf.to_crs(buffer_crs).geometry
|
| 18 |
|
| 19 |
buffers = {
|
|
|
|
| 27 |
|
| 28 |
features_reproj = features_gdf.to_crs(buffer_crs)
|
| 29 |
|
| 30 |
+
largest = buffers[distances[-1]]
|
| 31 |
+
candidates = gpd.sjoin(features_reproj, largest, how="inner", predicate="intersects")
|
| 32 |
+
|
| 33 |
+
clipped = {dist: gpd.overlay(candidates, buf[["geometry"]], how="intersection") for dist, buf in buffers.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
rings = []
|
| 36 |
for outer, inner in ring_definitions:
|
| 37 |
outer_clip = clipped[outer]
|
| 38 |
+
if inner is None or outer_clip.empty:
|
|
|
|
|
|
|
| 39 |
rings.append(outer_clip)
|
| 40 |
else:
|
| 41 |
rings.append(gpd.overlay(outer_clip, buffers[inner][["geometry"]], how=donut_how))
|