Skip to content

Commit f3a49ea

Browse files
committed
Add docstrings to render extension
1 parent 18a9011 commit f3a49ea

File tree

1 file changed

+167
-16
lines changed

1 file changed

+167
-16
lines changed

pystac/extensions/render.py

Lines changed: 167 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@
2222

2323

2424
class Render:
25+
"""Parameters for creating a rendered view of assets."""
26+
2527
properties: dict[str, Any]
2628

2729
def __init__(self, properties: dict[str, Any]) -> None:
2830
self.properties = properties
2931

3032
@property
3133
def assets(self) -> list[str]:
34+
"""
35+
List of asset keys referencing the assets that are
36+
used to make the rendering.
37+
"""
3238
return get_required(self.properties.get("assets"), self, "assets")
3339

3440
@assets.setter
@@ -37,6 +43,7 @@ def assets(self, v: list[str]) -> None:
3743

3844
@property
3945
def title(self) -> str | None:
46+
"""Title of the rendering"""
4047
return self.properties.get("title")
4148

4249
@title.setter
@@ -48,6 +55,10 @@ def title(self, v: str | None) -> None:
4855

4956
@property
5057
def rescale(self) -> list[list[float]] | None:
58+
"""A list of min/max value pairs to rescale each asset by, e.g.
59+
`[[0, 5000], [0, 7000], [0, 9000]]`. If not provided, the
60+
assets will not be rescaled.
61+
"""
5162
return self.properties.get("rescale")
5263

5364
@rescale.setter
@@ -59,6 +70,7 @@ def rescale(self, v: list[list[float]] | None) -> None:
5970

6071
@property
6172
def nodata(self) -> float | str | None:
73+
"""Nodata value."""
6274
return self.properties.get("nodata")
6375

6476
@nodata.setter
@@ -70,6 +82,9 @@ def nodata(self, v: float | str | None) -> None:
7082

7183
@property
7284
def colormap_name(self) -> str | None:
85+
"""Name of color map to apply to the render.
86+
See: https://matplotlib.org/stable/gallery/color/colormap_reference.html
87+
"""
7388
return self.properties.get("colormap_name")
7489

7590
@colormap_name.setter
@@ -81,6 +96,9 @@ def colormap_name(self, v: str | None) -> None:
8196

8297
@property
8398
def colormap(self) -> dict[str, Any] | None:
99+
"""A dictionary containing a custom colormap definition.
100+
See: https://developmentseed.org/titiler/advanced/rendering/#custom-colormaps
101+
"""
84102
return self.properties.get("colormap")
85103

86104
@colormap.setter
@@ -92,6 +110,12 @@ def colormap(self, v: dict[str, Any] | None) -> None:
92110

93111
@property
94112
def color_formula(self) -> str | None:
113+
"""A string containing a color formula to apply
114+
color corrections to images. Useful for reducing
115+
artefacts like atmospheric haze, dark shadows, or
116+
muted colors.
117+
See: https://developmentseed.org/titiler/advanced/rendering/#color-formula
118+
"""
95119
return self.properties.get("color_formula")
96120

97121
@color_formula.setter
@@ -103,6 +127,10 @@ def color_formula(self, v: str | None) -> None:
103127

104128
@property
105129
def resampling(self) -> str | None:
130+
"""Resampling algorithm to apply to the referenced assets. See GDAL
131+
resampling algorithm for some examples.
132+
See: https://gdal.org/en/latest/programs/gdalwarp.html#cmdoption-gdalwarp-r
133+
"""
106134
return self.properties.get("resampling")
107135

108136
@resampling.setter
@@ -114,6 +142,7 @@ def resampling(self, v: str | None) -> None:
114142

115143
@property
116144
def expression(self) -> str | None:
145+
"""Band arithmetic formula to apply to the referenced assets."""
117146
return self.properties.get("expression")
118147

119148
@expression.setter
@@ -125,6 +154,7 @@ def expression(self, v: str | None) -> None:
125154

126155
@property
127156
def minmax_zoom(self) -> list[int] | None:
157+
"""Zoom level range applicable for the visualization, e.g. `[2, 18]`."""
128158
return self.properties.get("minmax_zoom")
129159

130160
@minmax_zoom.setter
@@ -147,6 +177,41 @@ def apply(
147177
expression: str | None = None,
148178
minmax_zoom: list[int] | None = None,
149179
) -> None:
180+
"""Set the properties for a new Render.
181+
182+
Args:
183+
assets:
184+
List of asset keys referencing the assets that are
185+
used to make the rendering.
186+
title:
187+
Title of the rendering.
188+
rescale:
189+
A list of min/max value pairs to rescale each asset by, e.g.
190+
`[[0, 5000], [0, 7000], [0, 9000]]`. If not provided, the
191+
assets will not be rescaled.
192+
nodata:
193+
Nodata value.
194+
colormap_name:
195+
Name of color map to apply to the render.
196+
https://matplotlib.org/stable/gallery/color/colormap_reference.html
197+
colormap:
198+
A dictionary containing a custom colormap definition.
199+
https://developmentseed.org/titiler/advanced/rendering/#custom-colormaps
200+
color_formula:
201+
A string containing a color formula to apply
202+
color corrections to images. Useful for reducing
203+
artefacts like atmospheric haze, dark shadows, or
204+
muted colors.
205+
https://developmentseed.org/titiler/advanced/rendering/#color-formula
206+
resampling:
207+
Resampling algorithm to apply to the referenced assets. See GDAL
208+
resampling algorithm for some examples.
209+
https://gdal.org/en/latest/programs/gdalwarp.html#cmdoption-gdalwarp-r
210+
expression:
211+
Band arithmetic formula to apply to the referenced assets.
212+
minmax_zoom:
213+
Zoom level range applicable for the visualization, e.g. `[2, 18]`.
214+
"""
150215
self.assets = assets
151216
self.title = title
152217
self.rescale = rescale
@@ -172,6 +237,41 @@ def create(
172237
expression: str | None = None,
173238
minmax_zoom: list[int] | None = None,
174239
) -> Render:
240+
"""Create a new Render.
241+
242+
Args:
243+
assets:
244+
List of asset keys referencing the assets that are
245+
used to make the rendering.
246+
title:
247+
Title of the rendering.
248+
rescale:
249+
A list of min/max value pairs to rescale each asset by, e.g.
250+
`[[0, 5000], [0, 7000], [0, 9000]]`. If not provided, the
251+
assets will not be rescaled.
252+
nodata:
253+
Nodata value.
254+
colormap_name:
255+
Name of color map to apply to the render.
256+
https://matplotlib.org/stable/gallery/color/colormap_reference.html
257+
colormap:
258+
A dictionary containing a custom colormap definition.
259+
https://developmentseed.org/titiler/advanced/rendering/#custom-colormaps
260+
color_formula:
261+
A string containing a color formula to apply
262+
color corrections to images. Useful for reducing
263+
artefacts like atmospheric haze, dark shadows, or
264+
muted colors.
265+
https://developmentseed.org/titiler/advanced/rendering/#color-formula
266+
resampling:
267+
Resampling algorithm to apply to the referenced assets. See GDAL
268+
resampling algorithm for some examples.
269+
https://gdal.org/en/latest/programs/gdalwarp.html#cmdoption-gdalwarp-r
270+
expression:
271+
Band arithmetic formula to apply to the referenced assets.
272+
minmax_zoom:
273+
Zoom level range applicable for the visualization, e.g. `[2, 18]`.
274+
"""
175275
c = cls({})
176276
c.apply(
177277
assets=assets,
@@ -211,16 +311,68 @@ class RenderExtension(
211311
PropertiesExtension,
212312
ExtensionManagementMixin[pystac.Item | pystac.Collection],
213313
):
314+
"""An abstract class that can be used to extend the properties of a
315+
:class:`~pystac.Collection` or :class:`~pystac.Item` with
316+
properties from the :stac-ext:`Render Extension <render>`. This class is
317+
generic over the type of STAC Object to be extended (e.g. :class:`~pystac.Item`,
318+
:class:`~pystac.Collection`).
319+
320+
To create a concrete instance of :class:`RenderExtension`, use the
321+
:meth:`RenderExtension.ext` method. For example:
322+
323+
.. code-block:: python
324+
325+
>>> item: pystac.Item = ...
326+
>>> xr_ext = RenderExtension.ext(item)
327+
328+
"""
329+
214330
name: Literal["render"] = "render"
215331

332+
@classmethod
333+
def get_schema_uri(cls) -> str:
334+
return SCHEMA_URI_PATTERN.format(version=DEFAULT_VERSION)
335+
336+
@classmethod
337+
def ext(cls, obj: T, add_if_missing: bool = False) -> RenderExtension[T]:
338+
"""Extend the given STAC Object with properties from the
339+
:stac-ext:`Render Extension <render>`.
340+
341+
This extension can be applied to instances of :class:`~pystac.Collection`
342+
or :class:`~pystac.Item`.
343+
344+
Raises:
345+
pystac.ExtensionTypeError : If an invalid object type is passed.
346+
"""
347+
if isinstance(obj, pystac.Collection):
348+
cls.ensure_has_extension(obj, add_if_missing)
349+
return CollectionRenderExtension(obj)
350+
elif isinstance(obj, pystac.Item):
351+
cls.ensure_has_extension(obj, add_if_missing)
352+
return ItemRenderExtension(obj)
353+
else:
354+
raise pystac.ExtensionTypeError(
355+
f"RenderExtension does not apply to type '{type(obj).__name__}'"
356+
)
357+
216358
def apply(
217359
self,
218360
renders: dict[str, Render],
219361
) -> None:
362+
"""Applies the render extension fields to the extended
363+
object.
364+
365+
Args:
366+
renders: a dictionary mapping render names to
367+
:class: `~pystac.extension.render.Render` objects.
368+
"""
220369
self.renders = renders
221370

222371
@property
223372
def renders(self) -> dict[str, Render]:
373+
"""A dictionary where each key is the name of a render and each
374+
value is a :class:`~Render` object.
375+
"""
224376
renders: dict[str, dict[str, Any]] = get_required(
225377
self._get_property(RENDERS_PROP, dict[str, dict[str, Any]]),
226378
self,
@@ -236,25 +388,16 @@ def renders(self, v: dict[str, Render]) -> None:
236388
pop_if_none=False,
237389
)
238390

239-
@classmethod
240-
def get_schema_uri(cls) -> str:
241-
return SCHEMA_URI_PATTERN.format(version=DEFAULT_VERSION)
242391

243-
@classmethod
244-
def ext(cls, obj: T, add_if_missing: bool = False) -> RenderExtension[T]:
245-
if isinstance(obj, pystac.Collection):
246-
cls.ensure_has_extension(obj, add_if_missing)
247-
return CollectionRenderExtension(obj)
248-
elif isinstance(obj, pystac.Item):
249-
cls.ensure_has_extension(obj, add_if_missing)
250-
return ItemRenderExtension(obj)
251-
else:
252-
raise pystac.ExtensionTypeError(
253-
f"RenderExtension does not apply to type '{type(obj).__name__}'"
254-
)
392+
class CollectionRenderExtension(RenderExtension[pystac.Collection]):
393+
"""A concrete implementation of :class:`RenderExtension` on a
394+
:class:`~pystac.Collection` that extends the properties of the Collection to include
395+
properties defined in the :stac-ext:`Render Extension <xarray>`.
255396
397+
This class should generally not be instantiated directly. Instead, call
398+
:meth:`RenderExtension.ext` on an :class:`~pystac.Collection` to extend it.
399+
"""
256400

257-
class CollectionRenderExtension(RenderExtension[pystac.Collection]):
258401
def __init__(self, collection: pystac.Collection):
259402
self.collection = collection
260403
self.properties = collection.extra_fields
@@ -264,6 +407,14 @@ def __repr__(self) -> str:
264407

265408

266409
class ItemRenderExtension(RenderExtension[pystac.Item]):
410+
"""A concrete implementation of :class:`RenderExtension` on a
411+
:class:`~pystac.Item` that extends the properties of the Item to include
412+
properties defined in the :stac-ext:`Render Extension <xarray>`.
413+
414+
This class should generally not be instantiated directly. Instead, call
415+
:meth:`RenderExtension.ext` on an :class:`~pystac.Item` to extend it.
416+
"""
417+
267418
def __init__(self, item: pystac.Item):
268419
self.item = item
269420
self.properties = item.properties

0 commit comments

Comments
 (0)