Skip to content

Commit

Permalink
Merge pull request #85 from sat-utils/develop
Browse files Browse the repository at this point in the history
release 0.2.2
  • Loading branch information
matthewhanson authored Sep 24, 2019
2 parents d81e477 + 40f8c29 commit 48f00dc
Show file tree
Hide file tree
Showing 15 changed files with 476 additions and 304 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [v0.2.2] - 2019-09-20

### Changed
- Parser module now handles reading JSON from file
- sat-stac dependency bumped to 0.3.0 - tests updated

### Fixed
- Fixed issue with some comparison ops not being evaluated

## [v0.2.1] - 2019-02-14

Expand Down Expand Up @@ -36,6 +44,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
Initial Release

[Unreleased]: https://github.com/sat-utils/sat-search/compare/master...develop
[v0.2.2]: https://github.com/sat-utils/sat-search/compare/0.2.1...v0.2.2
[v0.2.1]: https://github.com/sat-utils/sat-search/compare/0.2.0...v0.2.1
[v0.2.0]: https://github.com/sat-utils/sat-search/compare/0.1.0...v0.2.0
[v0.1.0]: https://github.com/sat-utils/sat-search/tree/0.1.0
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ pip install .
```

#### Versions
The latest version of sat-search is 0.2.0, which uses the STAC spec v0.6.0. To install other versions of sat-search, specify the version in the call to pip.
The latest version of sat-search is 0.2.2, which uses [STAC v0.7.0](https://github.com/radiantearth/stac-spec/tree/v0.7.0). To install other versions of sat-search, specify the version in the call to pip.

```bash
pip install sat-search==0.2.0
Expand All @@ -32,8 +32,8 @@ The table below shows the corresponding versions between sat-search and STAC. Ad

| sat-search | STAC |
| -------- | ---- |
| 0.1.x | 0.5.0 |
| 0.2.x | 0.6.0 |
| 0.1.x | 0.5.x - 0.6.x |
| 0.2.x | 0.5.x - 0.7.x |


## Using sat-search
Expand All @@ -42,7 +42,7 @@ With sat-search you can search a STAC compliant API with full querying support (

Sat-search is a Python 3 library that can incorporated into other applications. A [Jupyter notebook tutorial](tutorial-1.ipynb) is included that covers all the main features of the library.

Sat-search also comes with a Command Line Interface (CLI), which is exaplained more below.
Sat-search also comes with a Command Line Interface (CLI), which is explained more below.

#### The CLI
The sat-search CLI has an extensive online help that can be printed with the `-h` switch.
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sat-stac~=0.1.2
sat-stac~=0.3.0
70 changes: 66 additions & 4 deletions satsearch/parser.py → satsearch/cli.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from copy import deepcopy
import argparse
import json
import logging
import os
import sys
import logging
import argparse

import satsearch.config as config

from satstac.utils import dict_merge
from .version import __version__
from satsearch import Search
from satstac import ItemCollection
from satstac.utils import dict_merge


class SatUtilsParser(argparse.ArgumentParser):
Expand Down Expand Up @@ -61,6 +63,12 @@ def parse_args(self, *args, **kwargs):
if 'filename' in args:
config.FILENAME = args.pop('filename')

# if a filename, read the GeoJSON file
if 'intersects' in args:
if os.path.exists(args['intersects']):
with open(args['intersects']) as f:
args['intersects'] = json.loads(f.read())

return args

@classmethod
Expand Down Expand Up @@ -96,3 +104,57 @@ def __call__(self, parser, namespace, values, option_string=None):
for val in values:
n, v = val.split('=')
setattr(namespace, n, {'eq': v})


def main(items=None, printmd=None, printcal=False, found=False,
save=None, download=None, requestor_pays=False, **kwargs):
""" Main function for performing a search """

if items is None:
## if there are no items then perform a search
search = Search.search(**kwargs)
if found:
num = search.found()
print('%s items found' % num)
return num
items = search.items()
else:
# otherwise, load a search from a file
items = ItemCollection.load(items)

print('%s items found' % len(items))

# print metadata
if printmd is not None:
print(items.summary(printmd))

# print calendar
if printcal:
print(items.calendar())

# save all metadata in JSON file
if save is not None:
items.save(filename=save)

# download files given `download` keys
if download is not None:
if 'ALL' in download:
# get complete set of assets
download = set([k for i in items for k in i.assets])
for key in download:
items.download(key=key, path=config.DATADIR, filename=config.FILENAME, requestor_pays=requestor_pays)

return items


def cli():
parser = SatUtilsParser.newbie(description='sat-search (v%s)' % __version__)
kwargs = parser.parse_args(sys.argv[1:])

cmd = kwargs.pop('command', None)
if cmd is not None:
main(**kwargs)


if __name__ == "__main__":
cli()
2 changes: 1 addition & 1 deletion satsearch/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

# API URL
API_URL = os.getenv('SATUTILS_API_URL', 'https://sat-api.developmentseed.org')
API_URL = os.getenv('SATUTILS_API_URL', 'https://earth-search.aws.element84.com')

# data directory to store downloaded imagery
DATADIR = os.getenv('SATUTILS_DATADIR', '${collection}/${date}')
Expand Down
69 changes: 0 additions & 69 deletions satsearch/main.py

This file was deleted.

19 changes: 12 additions & 7 deletions satsearch/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import satsearch.config as config

from satstac import Collection, Item, Items
from satstac import Collection, Item, ItemCollection
from satstac.utils import dict_merge
from urllib.parse import urljoin

Expand All @@ -19,6 +19,8 @@ class SatSearchError(Exception):

class Search(object):
""" One search query (possibly multiple pages) """
search_op_list = ['>=', '<=', '=', '>', '<']
search_op_to_stac_op = {'>=': 'gte', '<=': 'lte', '=': 'eq', '>': 'gt', '<': 'lt'}

def __init__(self, **kwargs):
""" Initialize a Search object with parameters """
Expand All @@ -36,14 +38,13 @@ def search(cls, **kwargs):
kwargs['property'] = []
kwargs['property'].append(q)
del kwargs['collection']
symbols = {'=': 'eq', '>': 'gt', '<': 'lt', '>=': 'gte', '<=': 'lte'}
if 'property' in kwargs and isinstance(kwargs['property'], list):
queries = {}
for prop in kwargs['property']:
for s in symbols:
for s in Search.search_op_list:
parts = prop.split(s)
if len(parts) == 2:
queries = dict_merge(queries, {parts[0]: {symbols[s]: parts[1]}})
queries = dict_merge(queries, {parts[0]: {Search.search_op_to_stac_op[s]: parts[1]}})
break
del kwargs['property']
kwargs['query'] = queries
Expand Down Expand Up @@ -95,13 +96,13 @@ def items_by_id(cls, ids, collection):
""" Return Items from collection with matching ids """
col = cls.collection(collection)
items = []
base_url = urljoin(config.API_URL, 'collections/%s/items' % collection)
base_url = urljoin(config.API_URL, 'collections/%s/items/' % collection)
for id in ids:
try:
items.append(Item(cls.query(urljoin(base_url, id))))
except SatSearchError as err:
pass
return Items(items, collections=[col])
return ItemCollection(items, collections=[col])

def items(self, limit=10000):
""" Return all of the Items and Collections for this search """
Expand Down Expand Up @@ -140,4 +141,8 @@ def items(self, limit=10000):
# item = dict_merge(item, collections[item['properties']['collection']])
# _items.append(Item(item))

return Items(items, collections=collections, search=self.kwargs)
search = {
'endpoint': config.API_URL,
'parameters': self.kwargs
}
return ItemCollection(items, collections=collections, search=search)
2 changes: 1 addition & 1 deletion satsearch/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.2.1'
__version__ = '0.2.2'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
],
keywords='',
entry_points={
'console_scripts': ['sat-search=satsearch.main:cli'],
'console_scripts': ['sat-search=satsearch.cli:cli'],
},
packages=find_packages(exclude=['docs', 'tests*']),
include_package_data=True,
Expand Down
Loading

0 comments on commit 48f00dc

Please sign in to comment.