SPlit up
This commit is contained in:
parent
cf39ad754e
commit
f59d01227b
91 changed files with 10370 additions and 7562 deletions
|
|
@ -329,16 +329,24 @@ def _outcode_of_postcode(postcode: str) -> str:
|
|||
|
||||
def _outcode_tree(postcodes_path: Path) -> tuple[cKDTree, list[str]]:
|
||||
"""Build a nearest-neighbour index from postcode coordinates to their outcode, so each
|
||||
street can be tagged with the outcode it sits in (used to disambiguate same-named roads)."""
|
||||
street can be tagged with the outcode it sits in (used to disambiguate same-named roads).
|
||||
|
||||
The tree lives in BNG metres (like `_london_postcode_tree`): in raw degrees
|
||||
1° of longitude is only ~0.6° of latitude at UK latitudes, which biases
|
||||
nearest-postcode picks E-W near outcode boundaries."""
|
||||
df = (
|
||||
pl.read_parquet(
|
||||
postcodes_path, columns=["pcds", "lat", "long", "ctry25cd", "doterm"]
|
||||
postcodes_path,
|
||||
columns=["pcds", "east1m", "north1m", "ctry25cd", "doterm"],
|
||||
)
|
||||
.filter((pl.col("ctry25cd") == ENGLAND_COUNTRY_CODE) & pl.col("doterm").is_null())
|
||||
.filter(_valid_wgs84_expr())
|
||||
.filter(_valid_bng_expr())
|
||||
)
|
||||
coords = np.column_stack(
|
||||
[df["lat"].to_numpy().astype(np.float64), df["long"].to_numpy().astype(np.float64)]
|
||||
[
|
||||
df["east1m"].to_numpy().astype(np.float64),
|
||||
df["north1m"].to_numpy().astype(np.float64),
|
||||
]
|
||||
)
|
||||
outcodes = [_outcode_of_postcode(pc) for pc in df["pcds"].to_list()]
|
||||
return cKDTree(coords), outcodes
|
||||
|
|
@ -354,8 +362,10 @@ def _build_street_places(
|
|||
if not streets:
|
||||
return []
|
||||
|
||||
coords = np.array([[street["lat"], street["lon"]] for street in streets], dtype=np.float64)
|
||||
_, indices = tree.query(coords)
|
||||
lons = np.array([street["lon"] for street in streets], dtype=np.float64)
|
||||
lats = np.array([street["lat"] for street in streets], dtype=np.float64)
|
||||
eastings, northings = WGS84_TO_BNG.transform(lons, lats)
|
||||
_, indices = tree.query(np.column_stack([eastings, northings]))
|
||||
|
||||
grouped: dict[tuple[str, str], dict] = {}
|
||||
for street, postcode_idx in zip(streets, indices):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue