Skip to content

Commit 4b8a65b

Browse files
authored
Merge pull request stac-utils#19 from azavea/sk/itemcollections
add itemcollections
2 parents 6c8d64f + 90f9107 commit 4b8a65b

File tree

9 files changed

+918
-3
lines changed

9 files changed

+918
-3
lines changed

pystac/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class STACError(Exception):
99
from pystac.catalog import (Catalog, CatalogType)
1010
from pystac.collection import (Collection, Extent, SpatialExtent, TemporalExtent, Provider)
1111
from pystac.item import (Item, Asset)
12+
from pystac.item_collection import ItemCollection
13+
from pystac.single_file import SingleFileSTAC
1214
from pystac.eo import *
1315
from pystac.label import *
1416

pystac/item_collection.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import json
2+
import os
3+
4+
from pystac.io import STAC_IO
5+
from pystac.item import Item
6+
from pystac.link import Link, LinkType
7+
8+
9+
class ItemCollection:
10+
DEFAULT_FILE_NAME = "item_collection.json"
11+
12+
def __init__(self, features, links=[], type='FeatureCollection'):
13+
self.type = type
14+
self.features = features
15+
self.links = links
16+
17+
if [l for l in self.links if l.rel == 'self'] == []:
18+
self.links.append(Link('self', './{}'.format(self.DEFAULT_FILE_NAME), link_type=LinkType.RELATIVE))
19+
20+
def __repr__(self):
21+
return '<ItemCollection item ids={}>'.format([f.id for f in self.features])
22+
23+
@staticmethod
24+
def from_dict(d):
25+
features = [Item.from_dict(feature) for feature in d['features']]
26+
links = []
27+
if 'links' in d.keys():
28+
links = [Link.from_dict(link) for link in d['links']]
29+
return ItemCollection(features, links, d['type'])
30+
31+
@staticmethod
32+
def from_file(uri):
33+
d = json.loads(STAC_IO.read_text(uri))
34+
c = ItemCollection.from_dict(d)
35+
return c
36+
37+
def to_dict(self, include_self_link=False):
38+
links = self.links
39+
if not include_self_link:
40+
links = filter(lambda l: l.rel != 'self', links)
41+
42+
d = {
43+
'type': self.type,
44+
'features': [f.to_dict() for f in self.features],
45+
'links': [l.to_dict() for l in links]
46+
}
47+
48+
return d
49+
50+
def get_self_href(self):
51+
self_link = next((l for l in self.links if l.rel == 'self'), None)
52+
if self_link:
53+
return self_link.target
54+
return self_link
55+
56+
def get_items(self):
57+
return self.features
58+
59+
def save(self, include_self_link=True):
60+
STAC_IO.save_json(self.get_self_href(), self.to_dict(include_self_link = include_self_link))

pystac/single_file.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import json
2+
import os
3+
4+
from pystac.collection import Collection
5+
from pystac.io import STAC_IO
6+
from pystac.item import Item
7+
from pystac.item_collection import ItemCollection
8+
9+
10+
class SingleFileSTAC(ItemCollection):
11+
def __init__(self, type, features, collections, search=None):
12+
self.type = type
13+
self.features = features
14+
self.collections = collections
15+
self.search = search
16+
17+
def __repr__(self):
18+
return '<SingleFile collection ids={}>'.format([c.id for c in self.collections])
19+
20+
@staticmethod
21+
def from_file(uri):
22+
d = json.loads(STAC_IO.read_text(uri))
23+
c = SingleFileSTAC.from_dict(d)
24+
return c
25+
26+
@staticmethod
27+
def from_dict(d):
28+
type = d['type']
29+
features = [Item.from_dict(feature) for feature in d['features']]
30+
collections = [Collection.from_dict(c) for c in d['collections']]
31+
search_obj = None
32+
if 'search' in d.keys():
33+
sd = d['search']
34+
search_obj = Search(sd['endpoint'], sd['parameters'])
35+
return SingleFileSTAC(type, features, collections, search_obj)
36+
37+
def to_dict(self):
38+
d = {}
39+
40+
d['type'] = self.type
41+
d['features'] = [f.to_dict() for f in self.features]
42+
d['collections'] = [c.to_dict() for c in self.collections]
43+
if self.search:
44+
d['search'] = self.search.to_dict()
45+
46+
return d
47+
48+
def save(self, uri):
49+
STAC_IO.save_json(os.path.abspath(uri), self.to_dict())
50+
51+
class Search:
52+
def __init__(self, endpoint=None, parameters=None):
53+
self.endpoint = endpoint
54+
self.parameters = parameters
55+
56+
@staticmethod
57+
def from_dict(d):
58+
return Search(d['endpoint'], d['parameters'])
59+
60+
def to_dict(self):
61+
return {'endpoint': self.endpoint, 'parameters': self.parameters}

0 commit comments

Comments
 (0)