Skip to content

Commit 36e071f

Browse files
committed
Port and modernize building density code
1 parent c69f327 commit 36e071f

28 files changed

+636
-1895
lines changed

poetry.lock

Lines changed: 45 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ python = ">=3.10, <3.13"
3434
click = "^8.1.8"
3535
requests = "^2.32.3"
3636
tqdm = "^4.67.1"
37-
rra-tools = "^1.0.17"
37+
rra-tools = "^1.0.20"
3838
geopandas = "^1.0.1"
3939
shapely = "^2.0.6"
4040
contextily = "^1.6.2"
@@ -60,6 +60,9 @@ pytest-github-actions-annotate-failures = "*"
6060
pytest-cov = "*"
6161
python-kacl = "*"
6262
ruff = ">=0.2.0"
63+
types-pyyaml = "^6.0.12.20241230"
64+
types-requests = "^2.32.0.20241016"
65+
types-tqdm = "^4.67.0.20241221"
6366

6467
[build-system]
6568
requires = ["poetry-core>=1.0.0"]
@@ -87,6 +90,9 @@ ignore = [
8790
"RET505", # Else after return, makes a lot of false positives
8891
"E501", # Line too long, this is autoformatted
8992
"PLR0913", # Too many args, bad check for CLIs
93+
"T201", # We're using print for now until I have time to think about logging
94+
"RET504", # Unneccesary assignment before return. I like this pattern for debugging
95+
"PD901", # df is fine
9096
]
9197

9298
[tool.ruff.lint.per-file-ignores]
@@ -131,17 +137,18 @@ exclude_lines = [
131137
# This is the global mypy configuration.
132138
# Avoid changing this!
133139
strict = true # See all the enabled flags `mypy --help | grep -A 10 'Strict mode'`
134-
disallow_any_unimported = true
140+
disallow_any_unimported = false
135141

136142
# If you need to ignore something for some specific module,
137143
# add overrides for them. Avoid changing the global config!
138144
# For example:
139-
# [[tool.mypy.overrides]]
140-
# module = [
141-
# "my_unpyted_dependency1.*",
142-
# "my_unpyted_dependency2.*"
143-
# ]
144-
# ignore_missing_imports = true
145+
[[tool.mypy.overrides]]
146+
module = [
147+
"affine.*",
148+
"geopandas.*",
149+
"shapely.*",
150+
]
151+
ignore_missing_imports = true
145152

146153
# [[tool.mypy.overrides]]
147154
# module = [

src/rra_building_density/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from rra_building_density import (
44
extract,
5-
format,
65
postprocess,
6+
process,
77
)
88

99

@@ -17,7 +17,7 @@ def bdtask() -> None:
1717
"""Run an individual modeling task in the building density pipeline."""
1818

1919

20-
for module in [extract, format, postprocess]:
20+
for module in [extract, process, postprocess]:
2121
runners = getattr(module, "RUNNERS", {})
2222
task_runners = getattr(module, "TASK_RUNNERS", {})
2323

src/rra_building_density/cli_options.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def with_time_point(
8080
convert=choices is not None and allow_all,
8181
)
8282

83+
8384
def with_version(
8485
choices: Collection[str],
8586
*,
@@ -93,24 +94,27 @@ def with_version(
9394
convert=allow_all,
9495
)
9596

97+
9698
def with_tile_size() -> ClickOption[_P, _T]:
97-
return ClickOption(
99+
return click.option(
98100
"--tile-size",
99101
type=int,
100102
default=512,
101103
help="The number of pixels in each tile dimension.",
102104
show_default=True,
103105
)
104106

107+
105108
def with_block_size() -> ClickOption[_P, _T]:
106-
return ClickOption(
109+
return click.option(
107110
"--block-size",
108111
type=int,
109112
default=16,
110113
help="The number of tiles in each block dimension",
111114
show_default=True,
112115
)
113116

117+
114118
def with_block_key() -> ClickOption[_P, _T]:
115119
return click.option(
116120
"--block-key",
@@ -120,8 +124,9 @@ def with_block_key() -> ClickOption[_P, _T]:
120124
help="Block key of block to run.",
121125
)
122126

127+
123128
def with_resolution(
124-
choices: Collection[int],
129+
choices: Collection[str],
125130
*,
126131
allow_all: bool = False,
127132
) -> ClickOption[_P, _T]:

src/rra_building_density/constants.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import itertools
22
from pathlib import Path
3-
import pyproj
43

4+
import pyproj
55
from pydantic import BaseModel, model_validator
66

7-
87
RRA_ROOT = Path("/mnt/team/rapidresponse/")
98
RRA_CREDENTIALS_ROOT = RRA_ROOT / "priv" / "shared" / "credentials"
109
RRA_BINARIES_ROOT = RRA_ROOT / "priv" / "shared" / "bin"
@@ -26,30 +25,34 @@
2625
GHSL_TIME_POINTS = [f"{y}q1" for y in GHSL_YEARS]
2726

2827
MICROSOFT_TIME_POINTS = {
29-
"2": [f"{y}q{q}" for q, y in itertools.product(range(1, 5), range(2018, 2024))][:-1],
28+
"2": [f"{y}q{q}" for q, y in itertools.product(range(1, 5), range(2018, 2024))][
29+
:-1
30+
],
3031
"3": ["2023q3"],
3132
"4": ["2023q4"],
3233
}
3334
MICROSOFT_VERSIONS = list(MICROSOFT_TIME_POINTS)
3435

35-
ALL_TIME_POINTS = sorted(set(GHSL_TIME_POINTS) | set().union(*MICROSOFT_TIME_POINTS.values()))
36+
ALL_TIME_POINTS = sorted(
37+
set(GHSL_TIME_POINTS) | set().union(*MICROSOFT_TIME_POINTS.values())
38+
)
3639

3740

3841
class CRS(BaseModel):
3942
name: str
40-
bounds: tuple[float, float, float, float] | None = None
43+
bounds: tuple[float, float, float, float]
4144
code: str = ""
4245
proj_string: str = ""
4346

4447
@model_validator(mode="after")
45-
def validate_code_or_proj_string(self):
48+
def validate_code_or_proj_string(self) -> "CRS":
4649
if not self.code and not self.proj_string:
4750
msg = "Either code or proj_string must be provided."
4851
raise ValueError(msg)
4952
return self
5053

5154
@model_validator(mode="after")
52-
def validate_code_and_proj_string(self):
55+
def validate_code_and_proj_string(self) -> "CRS":
5356
if self.code and self.proj_string:
5457
code_proj = pyproj.CRS.from_user_input(self.code)
5558
proj_proj = pyproj.CRS.from_user_input(self.proj_string)
@@ -58,7 +61,7 @@ def validate_code_and_proj_string(self):
5861
raise ValueError(msg)
5962
return self
6063

61-
def to_pyproj(self):
64+
def to_pyproj(self) -> pyproj.CRS:
6265
if self.proj_string:
6366
return pyproj.CRS.from_proj4(self.proj_string)
6467
return pyproj.CRS.from_user_input(self.code)
@@ -69,17 +72,18 @@ def to_pyproj(self):
6972
name="WGS84",
7073
code="EPSG:4326",
7174
proj_string="+proj=longlat +datum=WGS84 +no_defs +type=crs",
72-
bounds=(-180., -90., 180., 90.),
75+
bounds=(-180.0, -90.0, 180.0, 90.0),
7376
),
7477
"wgs84_anti_meridian": CRS(
7578
name="WGS84 Anti-Meridian",
7679
proj_string="+proj=longlat +lon_0=180 +datum=WGS84 +no_defs +type=crs",
77-
bounds=(-180., -90., 180., 90.),
80+
bounds=(-180.0, -90.0, 180.0, 90.0),
7881
),
7982
"itu_anti_meridian": CRS(
8083
name="PDC Mercator",
8184
code="EPSG:3832",
8285
proj_string="+proj=merc +lon_0=150 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs",
86+
bounds=(-5711803.07, -8362698.55, 15807367.69, 10023392.49),
8387
),
8488
"mollweide": CRS(
8589
name="Mollweide",
@@ -107,6 +111,7 @@ def to_pyproj(self):
107111
name="Web Mercator",
108112
code="EPSG:3857",
109113
proj_string="+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs",
114+
bounds=(-20037508.34, -20048966.1, 20037508.34, 20048966.1),
110115
),
111116
}
112117

src/rra_building_density/data.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import itertools
2-
from typing import Any
32
from pathlib import Path
3+
from typing import Any
44

55
import geopandas as gpd
66
import rasterra as rt
@@ -9,7 +9,6 @@
99
from pydantic import BaseModel
1010
from rra_tools.shell_tools import mkdir, touch
1111

12-
1312
from rra_building_density import constants as bdc
1413

1514

@@ -347,4 +346,4 @@ def save_raster_to_cog(
347346
"driver": "COG",
348347
"overview_resampling": resampling,
349348
}
350-
save_raster(raster, output_path, num_cores, **cog_save_params)
349+
save_raster(raster, output_path, num_cores, **cog_save_params)

src/rra_building_density/diagnostics/__init__.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)