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

Geospatial - added missing allowDimensionalityMismatch parameter #1531

Merged
merged 11 commits into from
Jan 8, 2024
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Changes are grouped as follows
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [7.5.0] - 2023-11-29
### Added
- `geospatial.search_features` and `geospatial.stream_features` now accept the `allow_dimensionality_mismatch` parameter.

## [7.4.1] - 2023-11-28
### Fixed
- Error in logic when creating a `Transformation`. This is causing when calling `client.transformations.update`.
Expand Down
13 changes: 8 additions & 5 deletions cognite/client/_api/geospatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ def search_features(
limit: int = DEFAULT_LIMIT_READ,
order_by: Sequence[OrderSpec] | None = None,
allow_crs_transformation: bool = False,
allow_dimensionality_mismatch: bool = False,
) -> FeatureList:
"""`Search for features`
<https://developer.cognite.com/api#tag/Geospatial/operation/searchFeatures>
Expand All @@ -542,6 +543,7 @@ def search_features(
limit (int): Maximum number of results
order_by (Sequence[OrderSpec] | None): The order specification
allow_crs_transformation (bool): If true, then input geometries will be transformed into the Coordinate Reference System defined in the feature type specification. When it is false, then requests with geometries in Coordinate Reference System different from the ones defined in the feature type will result in CogniteAPIError exception.
allow_dimensionality_mismatch (bool): Indicating if the spatial filter operators allow input geometries with a different dimensionality than the properties they are applied to.
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved

Returns:
FeatureList: the filtered features
Expand Down Expand Up @@ -632,6 +634,7 @@ def search_features(
"output": {"properties": properties},
"sort": order,
"allowCrsTransformation": (True if allow_crs_transformation else None),
"allowDimensionalityMismatch": (True if allow_dimensionality_mismatch else None),
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved
},
)
return FeatureList.load(res.json()["items"], cognite_client=self._cognite_client)
Expand All @@ -642,6 +645,7 @@ def stream_features(
filter: dict[str, Any] | None = None,
properties: dict[str, Any] | None = None,
allow_crs_transformation: bool = False,
allow_dimensionality_mismatch: bool = False,
) -> Iterator[Feature]:
"""`Stream features`
<https://developer.cognite.com/api#tag/Geospatial/operation/searchFeaturesStreaming>
Expand All @@ -655,7 +659,7 @@ def stream_features(
filter (dict[str, Any] | None): the search filter
properties (dict[str, Any] | None): the output property selection
allow_crs_transformation (bool): If true, then input geometries will be transformed into the Coordinate Reference System defined in the feature type specification. When it is false, then requests with geometries in Coordinate Reference System different from the ones defined in the feature type will result in CogniteAPIError exception.

allow_dimensionality_mismatch (bool): Indicating if the spatial filter operators allow input geometries with a different dimensionality than the properties they are applied to.
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved
Yields:
Feature: a generator for the filtered features

Expand Down Expand Up @@ -690,11 +694,10 @@ def stream_features(
payload = {
"filter": filter or {},
"output": {"properties": properties, "jsonStreamFormat": "NEW_LINE_DELIMITED"},
"allowCrsTransformation": "true" if allow_crs_transformation else "false",
"allowDimensionalityMismatch": "true" if allow_dimensionality_mismatch else "false",
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved
}
params = {"allowCrsTransformation": "true"} if allow_crs_transformation else None
res = self._do_request(
"POST", url_path=resource_path, json=payload, timeout=self._config.timeout, stream=True, params=params
)
res = self._do_request("POST", url_path=resource_path, json=payload, timeout=self._config.timeout, stream=True)

try:
for line in res.iter_lines():
Expand Down
2 changes: 1 addition & 1 deletion cognite/client/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import annotations

__version__ = "7.4.1"
__version__ = "7.5.0"
__api_subversion__ = "V20220125"
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "cognite-sdk"

version = "7.4.1"
version = "7.5.0"
description = "Cognite Python SDK"
readme = "README.md"
documentation = "https://cognite-sdk-python.readthedocs-hosted.com"
Expand Down
56 changes: 56 additions & 0 deletions tests/tests_integration/test_api/test_geospatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,34 @@ def test_search_single_feature(self, cognite_client, test_feature_type, test_fea
)
assert len(res) == 0

def test_search_feature_dimensionality_mismatch(self, cognite_client, test_feature_type, test_feature):
polygon_z = "POLYGONZ((2.276 48.858 3,2.278 48.859 3,2.2759 48.859 3,2.276 48.858 3))"
polygon = "POLYGON((2.276 48.858,2.278 48.859,2.275 48.859,2.276 48.858))"
res = cognite_client.geospatial.search_features(
feature_type_external_id=test_feature_type.external_id,
filter={"stWithin": {"property": "position", "value": {"wkt": polygon}}},
limit=10,
)
assert res[0].external_id == test_feature.external_id

try:
res = cognite_client.geospatial.search_features(
feature_type_external_id=test_feature_type.external_id,
filter={"stWithin": {"property": "position", "value": {"wkt": polygon_z}}},
limit=10,
)
raise pytest.fail("searching features with wrong dimensionality should raise exception")
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved
except CogniteAPIError:
pass
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved
res = cognite_client.geospatial.search_features(
feature_type_external_id=test_feature_type.external_id,
filter={"stWithin": {"property": "position", "value": {"wkt": polygon_z}}},
limit=10,
allow_dimensionality_mismatch=True,
)
assert len(res) == 1
assert res[0].external_id == test_feature.external_id
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved

def test_retrieve_multiple_feature_types_by_external_id(
self, cognite_client, test_feature_type, another_test_feature_type
):
Expand Down Expand Up @@ -449,6 +477,34 @@ def test_stream_features(self, cognite_client, large_feature_type, many_features
feature_list = FeatureList(list(features))
assert len(feature_list) == len(many_features)

def test_stream_features_dimensionality_mismatch(self, cognite_client, test_feature_type, test_feature):
polygon_z = "POLYGONZ((2.276 48.858 3,2.278 48.859 3,2.2759 48.859 3,2.276 48.858 3))"
polygon = "POLYGON((2.276 48.858,2.278 48.859,2.275 48.859,2.276 48.858))"
stream_res = cognite_client.geospatial.stream_features(
feature_type_external_id=test_feature_type.external_id,
filter={"stWithin": {"property": "position", "value": {"wkt": polygon}}},
)
res = [x for x in stream_res]
assert res[0].external_id == test_feature.external_id

try:
stream_res = cognite_client.geospatial.stream_features(
feature_type_external_id=test_feature_type.external_id,
filter={"stWithin": {"property": "position", "value": {"wkt": polygon_z}}},
)
_ = [x for x in stream_res]
raise pytest.fail("searching features with wrong dimensionality should raise exception")
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved
except CogniteAPIError:
pass
stream_res = cognite_client.geospatial.stream_features(
feature_type_external_id=test_feature_type.external_id,
filter={"stWithin": {"property": "position", "value": {"wkt": polygon_z}}},
allow_dimensionality_mismatch=True,
)
res = [x for x in stream_res]
assert len(res) == 1
assert res[0].external_id == test_feature.external_id
skepticalcat marked this conversation as resolved.
Show resolved Hide resolved

def test_list(self, cognite_client, test_feature_type, test_features):
with set_request_limit(cognite_client.geospatial, 2):
res = cognite_client.geospatial.list_features(
Expand Down