Skip to content

Commit bbee0ca

Browse files
committed
adjust types and remove pyproj
1 parent 66e69f7 commit bbee0ca

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

titiler/openeo/reader.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from urllib.parse import urlparse
77

88
import attr
9-
import pyproj
109
import rasterio
1110
from morecantile import TileMatrixSet
1211
from openeo_pg_parser_networkx.pg_schema import BoundingBox
1312
from rasterio.errors import RasterioIOError
1413
from rasterio.transform import array_bounds
14+
from rasterio.warp import transform_bounds
1515
from rio_tiler.constants import WEB_MERCATOR_TMS, WGS84_CRS
1616
from rio_tiler.errors import (
1717
AssetAsBandError,
@@ -25,8 +25,16 @@
2525
from rio_tiler.tasks import multi_arrays
2626
from rio_tiler.types import AssetInfo, BBox, Indexes
2727
from rio_tiler.utils import cast_to_sequence
28+
from typing_extension import TypedDict
2829

29-
from .processes.implementations.utils import to_rasterio_crs
30+
31+
class Dims(TypedDict):
32+
"""Estimate Dimensions."""
33+
34+
width: int
35+
height: int
36+
crs: rasterio.crs.CRS
37+
bbox: List[float]
3038

3139

3240
def _estimate_output_dimensions(
@@ -35,7 +43,7 @@ def _estimate_output_dimensions(
3543
bands: Optional[list[str]],
3644
width: Optional[int] = None,
3745
height: Optional[int] = None,
38-
) -> Dict[str, Any]:
46+
) -> Dims:
3947
"""
4048
Estimate output dimensions based on items and spatial extent.
4149
@@ -50,7 +58,6 @@ def _estimate_output_dimensions(
5058
Dictionary containing:
5159
- width: Estimated or specified width
5260
- height: Estimated or specified height
53-
- item_crs: CRS of the items
5461
- crs: Target CRS to use
5562
- bbox: Bounding box as a list [west, south, east, north]
5663
"""
@@ -70,7 +77,7 @@ def _estimate_output_dimensions(
7077
raise ValueError("Missing required input: bands")
7178

7279
# Extract CRS and resolution information from items
73-
item_crs = None
80+
item_crs: rasterio.crs.CRS = None
7481
x_resolutions = []
7582
y_resolutions = []
7683

@@ -87,14 +94,15 @@ def _estimate_output_dimensions(
8794
if src_dst.transform:
8895
x_resolutions.append(abs(src_dst.transform.a))
8996
y_resolutions.append(abs(src_dst.transform.e))
97+
9098
# If no transform, check for assets metadata
9199
else:
92100
for _, asset in item.get("assets", {}).items():
93101
# Get resolution from asset metadata
94-
asset_transform = asset.get("proj:transform")
95-
if asset_transform:
102+
if asset_transform := asset.get("proj:transform"):
96103
x_resolutions.append(abs(asset_transform[0]))
97104
y_resolutions.append(abs(asset_transform[4]))
105+
98106
else:
99107
# Default to 1024x1024 if no resolution is found
100108
x_resolutions.append(1024)
@@ -105,8 +113,7 @@ def _estimate_output_dimensions(
105113
y_resolution = min(y_resolutions) if y_resolutions else None
106114

107115
# Get target CRS and bounds
108-
projcrs = pyproj.crs.CRS(spatial_extent.crs or "epsg:4326")
109-
crs = to_rasterio_crs(projcrs)
116+
crs = rasterio.crs.CRS.from_user_input(spatial_extent.crs or "epsg:4326")
110117

111118
# Convert bounds to the same CRS if needed
112119
bbox = [
@@ -119,11 +126,6 @@ def _estimate_output_dimensions(
119126
# If item CRS is different from spatial_extent CRS, we need to reproject the resolution
120127
if item_crs and item_crs != crs:
121128
# Calculate approximate resolution in target CRS
122-
transformer = pyproj.Transformer.from_crs(
123-
item_crs,
124-
crs,
125-
always_xy=True,
126-
)
127129
# Get reprojected resolution using a small 1x1 degree box at the center of the bbox
128130
center_x = (bbox[0] + bbox[2]) / 2
129131
center_y = (bbox[1] + bbox[3]) / 2
@@ -133,7 +135,8 @@ def _estimate_output_dimensions(
133135
center_x + x_resolution,
134136
center_y + y_resolution,
135137
]
136-
dst_box = transformer.transform_bounds(*src_box)
138+
dst_box = transform_bounds(item_crs, crs, *src_box, densify_pts=21)
139+
137140
x_resolution = abs(dst_box[2] - dst_box[0])
138141
y_resolution = abs(dst_box[3] - dst_box[1])
139142

@@ -155,13 +158,12 @@ def _estimate_output_dimensions(
155158
)
156159

157160
# Return all information needed for rendering
158-
return {
159-
"width": width,
160-
"height": height,
161-
"item_crs": item_crs,
162-
"crs": crs,
163-
"bbox": bbox,
164-
}
161+
return Dims(
162+
width=width,
163+
height=height,
164+
crs=crs,
165+
bbox=bbox,
166+
)
165167

166168

167169
def _reader(item: Dict[str, Any], bbox: BBox, **kwargs: Any) -> ImageData:

titiler/openeo/stacapi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ def load_collection(
531531
assets=bands,
532532
bounds_crs=crs,
533533
dst_crs=crs,
534-
width=int(width) if width else width,
535-
height=int(height) if height else height,
534+
width=width if width else width,
535+
height=height if height else height,
536536
buffer=float(tile_buffer) if tile_buffer is not None else tile_buffer,
537537
)
538538
# Return a LazyRasterStack that will only execute the tasks when accessed

0 commit comments

Comments
 (0)