@@ -50,6 +50,16 @@ def nodata(self) -> int | float | None:
5050 """The nodata value for the image, if any."""
5151 ...
5252
53+ @property
54+ def width (self ) -> int :
55+ """The width of the image in pixels."""
56+ ...
57+
58+ @property
59+ def height (self ) -> int :
60+ """The height of the image in pixels."""
61+ ...
62+
5363
5464class FetchTileMixin :
5565 """Mixin for fetching tiles from a GeoTIFF.
@@ -61,7 +71,22 @@ async def fetch_tile(
6171 self : HasTiffReference ,
6272 x : int ,
6373 y : int ,
74+ * ,
75+ boundless : bool = True ,
6476 ) -> Tile :
77+ """Fetch a single tile from the GeoTIFF.
78+
79+ Args:
80+ x: The x-coordinate of the tile.
81+ y: The y-coordinate of the tile.
82+
83+ Keyword Args:
84+ boundless: If False, clip edge tiles to the image bounds. Defaults to True.
85+
86+ Returns:
87+ A Tile object containing the fetched tile data.
88+
89+ """
6590 tile_fut = self ._ifd .fetch_tile (x , y )
6691
6792 mask_data : AsyncTiffArray | None = None
@@ -86,6 +111,10 @@ async def fetch_tile(
86111 transform = tile_transform ,
87112 nodata = self .nodata ,
88113 )
114+
115+ if not boundless :
116+ array = _clip_to_image_bounds (self , x , y , array )
117+
89118 return Tile (
90119 x = x ,
91120 y = y ,
@@ -95,12 +124,17 @@ async def fetch_tile(
95124 async def fetch_tiles (
96125 self : HasTiffReference ,
97126 xy : Sequence [tuple [int , int ]],
127+ * ,
128+ boundless : bool = True ,
98129 ) -> list [Tile ]:
99130 """Fetch multiple tiles from this overview.
100131
101132 Args:
102133 xy: The (x, y) coordinates of the tiles.
103134
135+ Keyword Args:
136+ boundless: If False, clip edge tiles to the image bounds.
137+
104138 """
105139 tiles_fut = self ._ifd .fetch_tiles (xy )
106140
@@ -136,6 +170,10 @@ async def fetch_tiles(
136170 transform = tile_transform ,
137171 nodata = self .nodata ,
138172 )
173+
174+ if not boundless :
175+ array = _clip_to_image_bounds (self , x , y , array )
176+
139177 tile = Tile (
140178 x = x ,
141179 y = y ,
@@ -144,3 +182,43 @@ async def fetch_tiles(
144182 final_tiles .append (tile )
145183
146184 return final_tiles
185+
186+
187+ def _clip_to_image_bounds (
188+ self : HasTiffReference ,
189+ x : int ,
190+ y : int ,
191+ array : Array ,
192+ ) -> Array :
193+ """Clip a decoded tile array to the valid image bounds.
194+
195+ Edge tiles in a COG are always encoded at the full tile size, with the
196+ out-of-bounds region zero-padded. When ``boundless=False`` is requested,
197+ this function copies only the valid pixel sub-rectangle into a new array,
198+ returning an Array whose width/height match the actual image content.
199+
200+ Interior tiles (where the tile fits entirely within the image) are
201+ returned unchanged.
202+ """
203+ clipped_width = min ((x + 1 ) * self .tile_width , self .width ) - x * self .tile_width
204+ clipped_height = min ((y + 1 ) * self .tile_height , self .height ) - y * self .tile_height
205+
206+ if clipped_width == self .tile_width and clipped_height == self .tile_height :
207+ return array
208+
209+ # data shape is (bands, height, width)
210+ clipped_data = array .data [:, :clipped_height , :clipped_width ]
211+ clipped_mask = (
212+ array .mask [:clipped_height , :clipped_width ] if array .mask is not None else None
213+ )
214+
215+ return Array (
216+ data = clipped_data ,
217+ mask = clipped_mask ,
218+ width = clipped_width ,
219+ height = clipped_height ,
220+ count = array .count ,
221+ transform = array .transform ,
222+ crs = array .crs ,
223+ nodata = array .nodata ,
224+ )
0 commit comments