Skip to content

Commit bf6bc0b

Browse files
add get_feature functionality
1 parent 9479932 commit bf6bc0b

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

planet/cli/features.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from planet.cli.io import echo_json
77
from planet.clients.features import FeaturesClient
8+
from planet.geojson import split_ref
89

910
from .cmds import coro, translate_exceptions
1011
from .options import pretty, compact
@@ -122,6 +123,41 @@ async def items_list(ctx, collection_id, pretty):
122123
echo_json([f async for f in results], pretty)
123124

124125

126+
@features.command() # type: ignore
127+
@click.pass_context
128+
@translate_exceptions
129+
@coro
130+
@click.argument("collection_id")
131+
@click.argument("feature_id", required=False)
132+
@pretty
133+
async def item_get(ctx, collection_id, feature_id, pretty):
134+
"""Get a feature in a collection.
135+
136+
You may supply either a collection ID and a feature ID, or
137+
a feature reference.
138+
139+
Example:
140+
141+
planet features item-get my-collection-123 item123
142+
planet features item-get pl:features/my/my-collection-123/item123"
143+
"""
144+
145+
# ensure that either collection_id and feature_id were supplied, or that
146+
# a feature ref was supplied as a single value.
147+
if not ((collection_id and feature_id) or
148+
("pl:features" in collection_id)):
149+
raise ClickException(
150+
"Must supply either collection_id and feature_id, or a valid feature reference."
151+
)
152+
153+
if collection_id.startswith("pl:features"):
154+
collection_id, feature_id = split_ref(collection_id)
155+
156+
async with features_client(ctx) as cl:
157+
feature = await cl.get_feature(collection_id, feature_id)
158+
echo_json(feature, pretty)
159+
160+
125161
@features.command() # type: ignore
126162
@click.pass_context
127163
@translate_exceptions

planet/clients/features.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async def get_collection(self, collection_id: str) -> dict:
116116

117117
async def list_features(
118118
self,
119-
collection_id,
119+
collection_id: str,
120120
limit: int = 10,
121121
) -> AsyncIterator[Feature]:
122122
"""
@@ -164,6 +164,15 @@ def _next_link(self, page):
164164
limit=limit):
165165
yield Feature(**feat)
166166

167+
async def get_feature(self, collection_id: str,
168+
feature_id: str) -> Feature:
169+
"""
170+
Return metadata for a single feature in a collection
171+
"""
172+
url = f'{self._base_url}/collections/{collection_id}/items/{feature_id}'
173+
response = await self._session.request(method='GET', url=url)
174+
return Feature(**response.json())
175+
167176
async def create_collection(self,
168177
title: str,
169178
description: Optional[str] = None) -> str:

planet/geojson.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
LOGGER = logging.getLogger(__name__)
3030

3131

32+
def split_ref(ref: str) -> typing.Tuple[str, str]:
33+
"""split a feature ref into a tuple of the form (collection_id, feature_id)"""
34+
validate_ref(ref)
35+
36+
# after validating the ref, return the collection_id and
37+
# feature_id parts at the end.
38+
parts = ref.split("/", 4)
39+
path = parts[-2:]
40+
return path[0], path[1]
41+
42+
3243
def as_geom_or_ref(data) -> dict:
3344
"""Extract the geometry from GeoJSON and validate.
3445

planet/sync/features.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ def list_features(self, collection_id: str) -> Iterator[Feature]:
115115
except StopAsyncIteration:
116116
pass
117117

118+
def get_feature(self, collection_id: str, feature_id: str) -> Feature:
119+
"""
120+
Return metadata for a single feature in a collection
121+
"""
122+
return self._client._call_sync(
123+
self._client.get_feature(collection_id, feature_id))
124+
118125
def add_features(self,
119126
collection_id: str,
120127
feature: Union[dict, GeoInterface],

0 commit comments

Comments
 (0)