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

Fix #186 and related issues #221

Merged
merged 4 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 6 additions & 8 deletions pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ def __init__(self,
self.extra_fields = extra_fields

self._resolved_objects = ResolvedObjectCache()
self._resolved_objects.cache(self)

self.add_link(Link.root(self))

if href is not None:
self.set_self_href(href)

self._resolved_objects.cache(self)

def __repr__(self):
return '<Catalog id={}>'.format(self.id)

Expand Down Expand Up @@ -637,18 +638,15 @@ def from_dict(cls, d, href=None, root=None):
description=description,
title=title,
stac_extensions=stac_extensions,
extra_fields=d)
extra_fields=d,
href=href)

has_self_link = False
for link in links:
has_self_link |= link['rel'] == 'self'
if link['rel'] == 'root':
# Remove the link that's generated in Catalog's constructor.
cat.remove_links('root')

cat.add_link(Link.from_dict(link))

if not has_self_link and href is not None:
cat.add_link(Link.self_href(href))
if link['rel'] != 'self' or href is None:
cat.add_link(Link.from_dict(link))

return cat
35 changes: 4 additions & 31 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,6 @@ def __init__(self,
self.properties = properties
self.summaries = summaries

def set_self_href(self, href):
"""Sets the absolute HREF that is represented by the ``rel == 'self'``
:class:`~pystac.Link`.

Args:
str: The absolute HREF of this object. If the given HREF
is not absolute, it will be transformed to an absolute
HREF based on the current working directory.

Note:
Overridden for collections so that the root's ResolutionObjectCache can properly
update the HREF cache.
"""
root_link = self.get_root_link()
if root_link is not None and root_link.is_resolved():
root_link.target._resolved_objects.remove(self)

super().set_self_href(href)

if root_link is not None and root_link.is_resolved():
root_link.target._resolved_objects.cache(self)

return self

def __repr__(self):
return '<Collection id={}>'.format(self.id)

Expand Down Expand Up @@ -187,19 +163,16 @@ def from_dict(cls, d, href=None, root=None):
keywords=keywords,
providers=providers,
properties=properties,
summaries=summaries)
summaries=summaries,
href=href)

has_self_link = False
for link in links:
has_self_link |= link['rel'] == 'self'
if link['rel'] == 'root':
# Remove the link that's generated in Catalog's constructor.
collection.remove_links('root')

collection.add_link(Link.from_dict(link))

if not has_self_link and href is not None:
collection.add_link(Link.self_href(href))
if link['rel'] != 'self' or href is None:
collection.add_link(Link.from_dict(link))

return collection

Expand Down
7 changes: 6 additions & 1 deletion pystac/serialization/common_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def merge_common_properties(item_dict, collection_cache=None, json_href=None):
if type(stac_extensions) is list:
if 'commons' not in stac_extensions:
return False
else:
return False

# Try the cache if we have a collection ID.
if 'collection' in item_dict:
Expand Down Expand Up @@ -71,10 +73,13 @@ def merge_common_properties(item_dict, collection_cache=None, json_href=None):
if isinstance(collection, Collection):
collection_id = collection.id
collection_props = collection.properties
else:
elif type(collection) is dict:
collection_id = collection['id']
if 'properties' in collection:
collection_props = collection['properties']
else:
raise ValueError('{} is expected to be a Collection or '
'dict but is neither.'.format(collection))

if collection_props is not None:
for k in collection_props:
Expand Down
4 changes: 3 additions & 1 deletion tests/data-files/examples/example-info.csv
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@
"sentinel-0.6.0/sentinel-2-l1c/9/V/XK/catalog.json","CATALOG","0.6.0","",""
"sentinel-0.6.0/sentinel-2-l1c/9/V/catalog.json","CATALOG","0.6.0","",""
"sentinel-0.6.0/sentinel-2-l1c/9/catalog.json","CATALOG","0.6.0","",""
"sentinel-0.6.0/sentinel-2-l1c/catalog.json","COLLECTION","0.6.0","",""
"sentinel-0.6.0/sentinel-2-l1c/catalog.json","COLLECTION","0.6.0","",""
"hand-0.9.0/collection.json","COLLECTION","0.9.0","",""
"hand-0.8.1/collection.json","COLLECTION","0.8.1","",""
66 changes: 66 additions & 0 deletions tests/data-files/examples/hand-0.8.1/010100/010100.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"type": "Feature",
"stac_version": "0.8.1",
"id": "010100",
"properties": {
"datetime": "2020-06-01T00:00:00Z"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-69.840087890625,
45.92822950933618
],
[
-67.2802734375,
45.92822950933618
],
[
-67.2802734375,
47.52461999690651
],
[
-69.840087890625,
47.52461999690651
],
[
-69.840087890625,
45.92822950933618
]
]
]
},
"bbox": [
-70.4171738459305,
45.73847961442135,
-66.60498877163946,
48.09932887950686
],
"links": [
{
"rel": "collection",
"href": "../collection.json",
"type": "application/json"
},
{
"rel": "root",
"href": "../collection.json",
"type": "application/json"
},
{
"rel": "parent",
"href": "../collection.json",
"type": "application/json"
}
],
"assets": {
"hand": {
"href": "s3://hand-data/010100/010100hand.tif",
"type": "image/tiff; application=geotiff",
"description": "HAND raster, buffer removed, final result"
}
},
"collection": "hand_021"
}
37 changes: 37 additions & 0 deletions tests/data-files/examples/hand-0.8.1/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"id": "hand_021",
"stac_version": "0.8.1",
"description": "The continental flood inundation mapping (CFIM) framework is a high-performance computing (HPC)-based computational framework for the Height Above Nearest Drainage (HAND)-based inundation mapping methodology...",
"links": [
{
"rel": "root",
"href": "./collection.json",
"type": "application/json"
},
{
"rel": "item",
"href": "./010100/010100.json",
"type": "application/json"
}
],
"title": "HAND and the Hydraulic Property Table version 0.2.1",
"extent": {
"spatial": {
"bbox": [[
-124.90220439990048,
48.57285024335522,
-123.0335319132177,
52.88065372625198
]]
},
"temporal": {
"interval": [
[
"2020-06-01T00:00:00Z",
null
]
]
}
},
"license": "CC-BY-4.0"
}
66 changes: 66 additions & 0 deletions tests/data-files/examples/hand-0.9.0/010100/010100.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"type": "Feature",
"stac_version": "0.9.0",
"id": "010100",
"properties": {
"datetime": "2020-06-01T00:00:00Z"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-69.840087890625,
45.92822950933618
],
[
-67.2802734375,
45.92822950933618
],
[
-67.2802734375,
47.52461999690651
],
[
-69.840087890625,
47.52461999690651
],
[
-69.840087890625,
45.92822950933618
]
]
]
},
"bbox": [
-70.4171738459305,
45.73847961442135,
-66.60498877163946,
48.09932887950686
],
"links": [
{
"rel": "collection",
"href": "../collection.json",
"type": "application/json"
},
{
"rel": "root",
"href": "../collection.json",
"type": "application/json"
},
{
"rel": "parent",
"href": "../collection.json",
"type": "application/json"
}
],
"assets": {
"hand": {
"href": "s3://hand-data/010100/010100hand.tif",
"type": "image/tiff; application=geotiff",
"description": "HAND raster, buffer removed, final result"
}
},
"collection": "hand_021"
}
37 changes: 37 additions & 0 deletions tests/data-files/examples/hand-0.9.0/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"id": "hand_021",
"stac_version": "0.9.0",
"description": "The continental flood inundation mapping (CFIM) framework is a high-performance computing (HPC)-based computational framework for the Height Above Nearest Drainage (HAND)-based inundation mapping methodology...",
"links": [
{
"rel": "root",
"href": "./collection.json",
"type": "application/json"
},
{
"rel": "item",
"href": "./010100/010100.json",
"type": "application/json"
}
],
"title": "HAND and the Hydraulic Property Table version 0.2.1",
"extent": {
"spatial": {
"bbox": [[
-124.90220439990048,
48.57285024335522,
-123.0335319132177,
52.88065372625198
]]
},
"temporal": {
"interval": [
[
"2020-06-01T00:00:00Z",
null
]
]
}
},
"license": "CC-BY-4.0"
}
8 changes: 8 additions & 0 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,14 @@ def test_handles_children_with_same_id(self):

self.assertEqual(len(items), 1)

def test_catalog_with_href_caches_by_href(self):
cat = TestCases.test_case_1()
cache = cat._resolved_objects

# Since all of our STAC objects have HREFs, everything should be
# cached only by HREF
self.assertEqual(len(cache.id_keys_to_objects), 0)


class FullCopyTest(unittest.TestCase):
def check_link(self, link, tag):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ def test_supplying_href_in_init_does_not_fail(self):

self.assertEqual(collection.get_self_href(), test_href)

def test_reading_0_8_1_collection_as_catalog_throws_correct_exception(self):
cat = pystac.Catalog.from_file(
TestCases.get_path('data-files/examples/hand-0.8.1/collection.json'))
with self.assertRaises(ValueError):
list(cat.get_all_items())

def test_collection_with_href_caches_by_href(self):
collection = pystac.read_file(
TestCases.get_path('data-files/examples/hand-0.8.1/collection.json'))
cache = collection._resolved_objects

# Since all of our STAC objects have HREFs, everything should be
# cached only by HREF
self.assertEqual(len(cache.id_keys_to_objects), 0)


class ExtentTest(unittest.TestCase):
def test_spatial_allows_single_bbox(self):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ def test_null_geometry(self):
with self.assertRaises(KeyError):
item_dict['bbox']

def test_0_9_item_with_no_extensions_does_not_read_collection_data(self):
item_json = pystac.STAC_IO.read_json(
TestCases.get_path('data-files/examples/hand-0.9.0/010100/010100.json'))
assert item_json.get('stac_extensions') is None
assert item_json.get('stac_version') == '0.9.0'

did_merge = pystac.serialization.common_properties.merge_common_properties(item_json)
self.assertFalse(did_merge)


class CommonMetadataTest(unittest.TestCase):
def setUp(self):
Expand Down