Skip to content

Commit 474fc72

Browse files
committed
Added small change to NGD API sync to allow usage of NGDFeatureCollection
1 parent d6d8796 commit 474fc72

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/osdatahub/NGD/ngd_api.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import osdatahub
1111
from osdatahub import Extent
1212
from osdatahub.NGD.crs import get_crs
13+
from osdatahub.NGD.models import NGDFeatureCollection
1314

1415

1516
def _merge_geojsons(gj1: Union[dict], gj2: Union[dict]) -> Union[dict]:
@@ -96,7 +97,8 @@ def query(
9697
filter_crs: Union[str, int, None] = None,
9798
max_results: int = 100,
9899
offset: int = 0,
99-
) -> Union[dict]:
100+
output_as_collection: bool = False
101+
) -> Union[dict, NGDFeatureCollection]:
100102
"""
101103
Retrieves features from a Collection
102104
@@ -121,9 +123,11 @@ def query(
121123
max_results (int, optional): The maximum number of features to return. Defaults to 100
122124
offset (int, optional): The offset number skips past the specified number of features in the collection.
123125
Defaults to 0
124-
125-
Returns:
126-
FeatureCollection: The results of the query in GeoJSON format
126+
output_as_collection (bool, optional): Outputs as a NGDFeatureCollection which is more pythonic and structured.
127+
Defaults to False to ensure backwards compatibility.
128+
Returns either:
129+
Dict: The results of the query in GeoJSON format (if output_as_collection is set to False)
130+
NGDFeatureCollection: The results of the query in structured form (if output_as_collection is set to True)
127131
"""
128132

129133
assert max_results > 0, (
@@ -209,9 +213,16 @@ def query(
209213
else:
210214
n_required -= resp_json["numberReturned"]
211215

216+
# Added to allow the NGD Sync to work with the NGD ASync
217+
if output_as_collection:
218+
data = NGDFeatureCollection.from_dict(data)
219+
212220
return data
213221

214-
def query_feature(self, feature_id: str, crs: Union[str, int] = None) -> dict:
222+
def query_feature(self,
223+
feature_id: str,
224+
crs: Union[str, int] = None
225+
) -> dict:
215226
"""
216227
Retrieves a single feature from a collection
217228

tests/test_ngd.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from osdatahub.NGD.crs import get_crs
77
from osdatahub.NGD.ngd_api import _merge_geojsons, NGD
8+
from osdatahub.NGD.models import NGDFeatureCollection
89

910
from tests.data import ngd_crs_data as crs_data
1011
from tests.data import ngd_merge_geojsons_data as merge_geojsons_data
@@ -94,6 +95,24 @@ def test_ngd_api_call(self, collection, extent, crs, start_datetime, end_datetim
9495

9596
assert len(results["features"]) == max_results
9697

98+
@pytest.mark.skipif(not API_KEY, reason="Test API key not available")
99+
@pytest.mark.parametrize(*query_data.test_ngd_query_live())
100+
def test_ngd_api_call_collection(self, collection, extent, crs, start_datetime, end_datetime, cql_filter, filter_crs,
101+
max_results, offset):
102+
ngd = NGD(key=API_KEY, collection=collection)
103+
results = ngd.query(extent=extent,
104+
crs=crs,
105+
start_datetime=start_datetime,
106+
end_datetime=end_datetime,
107+
cql_filter=cql_filter,
108+
filter_crs=filter_crs,
109+
max_results=max_results,
110+
offset=offset,
111+
output_to_collection=True
112+
)
113+
114+
assert type(results) is NGDFeatureCollection
115+
assert len(results.features) == max_results
97116

98117
class TestNGDGetCollections:
99118
@mock.patch('osdatahub.get')

0 commit comments

Comments
 (0)