diff --git a/planet/clients/features.py b/planet/clients/features.py index 4aa3483b..4635edd5 100644 --- a/planet/clients/features.py +++ b/planet/clients/features.py @@ -300,3 +300,49 @@ async def add_items(self, json=feature, params=params) return list(resp.json()) + + async def update_item(self, collection_id: str, feature_id: str, feature: Union[dict, GeoInterface]) -> Feature: + """ + Update an existing Feature identified by `feature_id` and `collection_id`. + Returns a Feature. + + Note the Features API only accepts Polygon and MultiPolygon. + + collection_id: the collection to add the feature to + feature: a dict containing a geojson Feature or FeatureCollection, or an + instance of a class that implements __geo_interface__ (e.g. a Shapely or + GeoPandas geometry object) + + Example: + + ``` + updated_feature = await features_client.update_feature( + collection_id="my-collection", + feature_id="my-feature" + feature=feature, + ) + ``` + + note: if a geojson Geometry is supplied, it will be converted + to a Feature. However, we recommend doing this conversion yourself + so that you can set a title and description property. + """ + url = f'{self._base_url}/collections/{collection_id}/items/{feature_id}' + + if isinstance(feature, GeoInterface): + # we expect __geo_interface__ to return a Geometry (not a Feature), so + # we're using the name `feature` liberally. We'll convert to a feature + # at the next step. + feature = feature.__geo_interface__ + + # convert a geojson geometry into geojson feature + if feature.get("type", "").lower() == "featurecollection": + raise ClientError("FeatureCollection is not supported, must provide a single feature") + if feature.get("type", "").lower() != "feature": + feature = {"type": "Feature", "geometry": feature} + + response = await self._session.request(method='PUT', + url=url, + json=feature) + + return Feature(**response.json()) \ No newline at end of file diff --git a/planet/sync/features.py b/planet/sync/features.py index c4cbee69..7681a041 100644 --- a/planet/sync/features.py +++ b/planet/sync/features.py @@ -190,3 +190,32 @@ def add_items(self, property_id) return self._client._call_sync(uploaded_features) + + def update_item(self, collection_id: str, feature_id: str, feature: Union[dict, GeoInterface]) -> Feature: + """ + Update an existing Feature identified by `feature_id` and `collection_id`. + Returns a Feature. + + Note the Features API only accepts Polygon and MultiPolygon. + + collection_id: the collection to add the feature to + feature: a dict containing a geojson Feature or FeatureCollection, or an + instance of a class that implements __geo_interface__ (e.g. a Shapely or + GeoPandas geometry object) + + Example: + + ``` + updated_feature = await features_client.update_feature( + collection_id="my-collection", + feature_id="my-feature" + feature=feature, + ) + ``` + + note: if a geojson Geometry is supplied, it will be converted + to a Feature. However, we recommend doing this conversion yourself + so that you can set a title and description property. + """ + return self._client._call_sync( + self._client.update_item(collection_id, feature_id, feature))