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

Add CLI support for geometry input as string #381

Merged
merged 27 commits into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c27f5e6
Add support for geometry input as string. Going to need some more wor…
thomasyoung-audet Jul 7, 2020
16cbdd9
Black formatting
thomasyoung-audet Jul 7, 2020
c7da386
Small syntax changes
thomasyoung-audet Jul 7, 2020
7d166b5
Merge branch 'master' into iss350
thomasyoung-audet Jul 7, 2020
05a94cd
Add information about running tests
thomasyoung-audet Jul 8, 2020
15d1ee8
Merge branch 'iss350' of https://github.com/thomasyoung-audet/sentine…
thomasyoung-audet Jul 8, 2020
e4d1026
Change assertion so it matches the output
thomasyoung-audet Jul 8, 2020
c029eec
Fix the test_location_cli test
thomasyoung-audet Jul 9, 2020
e2c009c
Black formatting. Is there not a way to automatically run black on ev…
thomasyoung-audet Jul 9, 2020
9456a16
Update --geography help string to reflect new capabilities. Restrict …
thomasyoung-audet Jul 10, 2020
36668f6
Change is_wkt() validation to regex. Switch eval() to json.loads() fo…
thomasyoung-audet Jul 13, 2020
e69da42
Fix code that might have made WKT evaluation code unreachable
thomasyoung-audet Jul 13, 2020
4fee5a4
Change logger message level so it will be more useful to the user
thomasyoung-audet Jul 13, 2020
3c9af06
Fix tests so that they now pass
thomasyoung-audet Jul 13, 2020
7d16c7d
Black formatting
thomasyoung-audet Jul 13, 2020
a5fd8a8
Add chenges to the changelog
thomasyoung-audet Jul 20, 2020
a3f58b4
Add test to cover bad GeoJSON string input
thomasyoung-audet Jul 30, 2020
001c57b
Change help string for --geometry. Change JSON test to produce more a…
thomasyoung-audet Jul 30, 2020
a633506
Add black formatting. Change mechanism for stopping code after invali…
thomasyoung-audet Jul 31, 2020
687ecf6
Fix failing test assertion
thomasyoung-audet Jul 31, 2020
a8909bb
Fix Travis failed build (hopefully)
thomasyoung-audet Aug 18, 2020
f64aa72
Fix Travis failed build part 2 (hopefully)
thomasyoung-audet Aug 18, 2020
c0313e4
Add black formatting
thomasyoung-audet Aug 18, 2020
ddc8b26
Merge branch 'master' into iss350
thomasyoung-audet Aug 18, 2020
48f208d
Fix merge issue
thomasyoung-audet Aug 18, 2020
641ba1e
Black formatting
thomasyoung-audet Aug 18, 2020
2d3fb0e
Merge branch 'master' into iss350
thomasyoung-audet Jan 31, 2021
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
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ All notable changes to ``sentinelsat`` will be listed here.
Added
~~~~~
* Display DHuS server version with CLI flag ``--info`` (#367 @thomasyoung-audet)
* Added seraching by placenames with the CLI flag ``--location`` (#372 @thomasyoung-audet)
* Added searching by placenames with the CLI flag ``--location`` (#372 @thomasyoung-audet)
* Added CLI support for ``--geometry`` input as a string (#381 @thomasyoung-audet)

Changed
~~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions CONTRIBUTE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ To run the tests

pytest -v

You can run individual tests with the syntax:

.. code-block:: console

pytest -v /tests/test_file.py::test_you_want_to_run

This can be useful for recording or modifying individual vcr cassettes.

By default, prerecorded responses to Copernicus Open Access Hub queries are used to not be affected by its downtime.
Furthermore, any network accesses are blocked as well (by raising a ``pytest_socket.SocketBlockedError: A test tried to use socket.socket`` exception) to guarantee that all tests are indeed correctly covered by recorded queries.
Expand Down
41 changes: 38 additions & 3 deletions sentinelsat/scripts/cli.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
import json
import logging
import os
from json import JSONDecodeError

import click
import geojson as gj
import requests.utils

from sentinelsat import __version__ as sentinelsat_version
from sentinelsat.sentinel import SentinelAPI, geojson_to_wkt, read_geojson, placename_to_wkt

from sentinelsat.sentinel import (
SentinelAPI,
SentinelAPIError,
geojson_to_wkt,
read_geojson,
placename_to_wkt,
is_wkt,
)

from sentinelsat.exceptions import InvalidKeyError


logger = logging.getLogger("sentinelsat")


Expand Down Expand Up @@ -69,7 +81,10 @@ def convert(self, value, param, ctx):
help="End date of the query in the format YYYYMMDD.",
)
@click.option(
"--geometry", "-g", type=click.Path(exists=True), help="Search area geometry as GeoJSON file."
"--geometry",
"-g",
type=str,
help="Search area geometry as GeoJSON file, a GeoJSON string, or a WKT string. ",
)
@click.option(
"--uuid",
Expand Down Expand Up @@ -217,7 +232,27 @@ def cli(
search_kwargs["area"] = wkt

if geometry is not None:
search_kwargs["area"] = geojson_to_wkt(read_geojson(geometry))
# check if the value is an existing path
if os.path.exists(geometry):
search_kwargs["area"] = geojson_to_wkt(read_geojson(geometry))
# check if the value is a GeoJSON
else:
if geometry.startswith("{"):
try:
geometry = json.loads(geometry)
search_kwargs["area"] = geojson_to_wkt(geometry)
except JSONDecodeError:
logger.error("geometry string starts with '{' but is not a valid GeoJSON.")
exit(1)
thomasyoung-audet marked this conversation as resolved.
Show resolved Hide resolved
# check if the value is a WKT
elif is_wkt(geometry):
search_kwargs["area"] = geometry
else:
logger.error(
"The geometry input is neither a GeoJSON file with a valid path, "
"a GeoJSON String or a WKT string."
)
exit(1)

if uuid is not None:
uuid_list = [x.strip() for x in uuid]
Expand Down
5 changes: 5 additions & 0 deletions sentinelsat/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,3 +1457,8 @@ def placename_to_wkt(placename):
footprint = "ENVELOPE({}, {}, {}, {})".format(minX, maxX, maxY, minY)
placeinfo = feature["properties"]["display_name"]
return footprint, placeinfo


def is_wkt(possible_wkt):
thomasyoung-audet marked this conversation as resolved.
Show resolved Hide resolved
pattern = r"^((MULTI)?(POINT|LINESTRING|POLYGON)|GEOMETRYCOLLECTION|ENVELOPE)\s*\(.+\)$"
return re.match(pattern, possible_wkt.strip().upper()) is not None
50 changes: 50 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,56 @@ def geojson_path():
return path


@pytest.fixture(scope="session")
def geojson_string():
string = """{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-66.26953125,
-8.05922962720018
],
[
-66.26953125,
0.7031073524364909
],
[
-57.30468749999999,
0.7031073524364909
],
[
-57.30468749999999,
-8.05922962720018
],
[
-66.26953125,
-8.05922962720018
]
]
]
}
}
]
}"""
return string


@pytest.fixture(scope="session")
def wkt_string():
string = (
"POLYGON((-78.046875 46.377254205100286,-75.76171874999999 43.32517767999295,-71.279296875 "
"46.55886030311717,-78.046875 46.377254205100286))"
)
return string


@pytest.fixture(scope="session")
def test_wkt(geojson_path):
return geojson_to_wkt(read_geojson(geojson_path))
Expand Down
Loading