Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement classification v2.0.0 #1359

Merged
merged 8 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

### Changed

- Allow object ID as input for getting APILayoutStrategy hrefs and add `items`, `collections`, `search`, `conformance`, `service_desc` and `service_doc` href methods. ([#1335](https://github.com/stac-utils/pystac/pull/1335))
- Update docstring of `name` argument to `Classification.apply` and `Classification.create` to agree with extension specification. ([#1356](https://github.com/stac-utils/pystac/pull/1356))
- Allow object ID as input for getting APILayoutStrategy hrefs and add `items`, `collections`, `search`, `conformance`, `service_desc` and `service_doc` href methods ([#1335](https://github.com/stac-utils/pystac/pull/1335))
- Updated classification extension to v2.0.0 ([#1359](https://github.com/stac-utils/pystac/pull/1359))
- Update docstring of `name` argument to `Classification.apply` and `Classification.create` to agree with extension specification ([#1356](https://github.com/stac-utils/pystac/pull/1356))

## [v1.10.1] - 2024-05-03

Expand Down
106 changes: 85 additions & 21 deletions pystac/extensions/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
SCHEMA_URI_PATTERN: str = (
"https://stac-extensions.github.io/classification/v{version}/schema.json"
)
DEFAULT_VERSION: str = "1.1.0"
SUPPORTED_VERSIONS: list[str] = ["1.1.0", "1.0.0"]
DEFAULT_VERSION: str = "2.0.0"
SUPPORTED_VERSIONS: list[str] = ["2.0.0", "1.1.0", "1.0.0"]

# Field names
PREFIX: str = "classification:"
Expand All @@ -58,32 +58,37 @@
def apply(
self,
value: int,
description: str,
name: str | None = None,
name: str,
gadomski marked this conversation as resolved.
Show resolved Hide resolved
description: str | None = None,
color_hint: str | None = None,
nodata: bool | None = None,
gadomski marked this conversation as resolved.
Show resolved Hide resolved
percentage: float | None = None,
count: int | None = None,
) -> None:
"""
Set the properties for a new Classification.

Args:
value: The integer value corresponding to this class
description: The description of this class
name: Short name of the class for machine readability. Must consist only
of letters, numbers, -, and _ characters.
description: The description of this class
color_hint: An optional hexadecimal string-encoded representation of the
RGB color that is suggested to represent this class (six hexadecimal
characters, all capitalized)
nodata: If set to true classifies a value as a no-data value,
defaults to false
percentage: The percentage of data values that belong to this class
in comparison to all data values, in percent (0-100).
count: The number of data values that belong to this class.
"""
self.value = value
self.name = name
self.description = description
self.color_hint = color_hint

if color_hint is not None:
match = COLOR_HINT_PATTERN.match(color_hint)
assert (
color_hint is None or match is not None and match.group() == color_hint
), "Must format color hints as '^([0-9A-F]{6})$'"
self.nodata = nodata
self.percentage = percentage
self.count = count

if color_hint is not None:
match = COLOR_HINT_PATTERN.match(color_hint)
Expand All @@ -95,9 +100,12 @@
def create(
cls,
value: int,
description: str,
name: str | None = None,
name: str,
description: str | None = None,
color_hint: str | None = None,
nodata: bool | None = None,
jsignell marked this conversation as resolved.
Show resolved Hide resolved
percentage: float | None = None,
count: int | None = None,
) -> Classification:
"""
Create a new Classification.
Expand All @@ -110,13 +118,21 @@
color_hint: An optional hexadecimal string-encoded representation of the
RGB color that is suggested to represent this class (six hexadecimal
characters, all capitalized)
nodata: If set to true classifies a value as a no-data value,
defaults to false
percentage: The percentage of data values that belong to this class
in comparison to all data values, in percent (0-100).
count: The number of data values that belong to this class.
"""
c = cls({})
c.apply(
value=value,
name=name,
description=description,
color_hint=color_hint,
nodata=nodata,
percentage=percentage,
count=count,
)
return c

Expand All @@ -140,11 +156,14 @@
Returns:
str
"""
return get_required(self.properties.get("description"), self, "description")
return self.properties.get("description")

@description.setter
def description(self, v: str) -> None:
self.properties["description"] = v
def description(self, v: str | None) -> None:
if v is not None:
self.properties["description"] = v
else:
self.properties.pop("description", None)

@property
def name(self) -> str | None:
Expand All @@ -153,14 +172,11 @@
Returns:
Optional[str]
"""
return self.properties.get("name")
return get_required(self.properties.get("name"), self, "name")

@name.setter
def name(self, v: str | None) -> None:
if v is not None:
self.properties["name"] = v
else:
self.properties.pop("name", None)
self.properties["name"] = v
gadomski marked this conversation as resolved.
Show resolved Hide resolved

@property
def color_hint(self) -> str | None:
Expand All @@ -185,6 +201,54 @@
else:
self.properties.pop("color_hint", None)

@property
def nodata(self) -> bool | None:
"""Get or set the nodata value for this class.

Returns:
Optional[bool]
gadomski marked this conversation as resolved.
Show resolved Hide resolved
"""
return self.properties.get("nodata")

Check warning on line 211 in pystac/extensions/classification.py

View check run for this annotation

Codecov / codecov/patch

pystac/extensions/classification.py#L211

Added line #L211 was not covered by tests

@nodata.setter
def nodata(self, v: bool | None) -> None:
if v is not None:
self.properties["nodata"] = v

Check warning on line 216 in pystac/extensions/classification.py

View check run for this annotation

Codecov / codecov/patch

pystac/extensions/classification.py#L216

Added line #L216 was not covered by tests
else:
self.properties.pop("nodata", None)

@property
def percentage(self) -> float | None:
"""Get or set the percentage value for this class.

Returns:
Optional[float]
"""
return self.properties.get("percentage")

Check warning on line 227 in pystac/extensions/classification.py

View check run for this annotation

Codecov / codecov/patch

pystac/extensions/classification.py#L227

Added line #L227 was not covered by tests

@percentage.setter
def percentage(self, v: float | None) -> None:
if v is not None:
self.properties["percentage"] = v

Check warning on line 232 in pystac/extensions/classification.py

View check run for this annotation

Codecov / codecov/patch

pystac/extensions/classification.py#L232

Added line #L232 was not covered by tests
else:
self.properties.pop("percentage", None)

@property
def count(self) -> int | None:
"""Get or set the count value for this class.

Returns:
Optional[int]
"""
return self.properties.get("count")

Check warning on line 243 in pystac/extensions/classification.py

View check run for this annotation

Codecov / codecov/patch

pystac/extensions/classification.py#L243

Added line #L243 was not covered by tests

@count.setter
def count(self, v: int | None) -> None:
if v is not None:
self.properties["count"] = v

Check warning on line 248 in pystac/extensions/classification.py

View check run for this annotation

Codecov / codecov/patch

pystac/extensions/classification.py#L248

Added line #L248 was not covered by tests
else:
self.properties.pop("count", None)

def to_dict(self) -> dict[str, Any]:
"""Returns the dictionary encoding of this class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@
"https://stac-extensions.github.io/projection/v1.1.0/schema.json",
"https://stac-extensions.github.io/eo/v1.1.0/schema.json",
"https://stac-extensions.github.io/raster/v1.1.0/schema.json",
"https://stac-extensions.github.io/classification/v1.1.0/schema.json"
"https://stac-extensions.github.io/classification/v2.0.0/schema.json"
]
}
Loading
Loading