Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/sdf94/sentinelsat into sd…
Browse files Browse the repository at this point in the history
…f94-master
  • Loading branch information
kr-stn committed Nov 3, 2019
2 parents 5434f62 + 34b4603 commit 3b6f002
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/common_issues.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Standard GeoJSON specification contains only WGS84 format, check if your data co
Maybe there are no images for the specified time period, by default
``sentinelsat`` will query the last 24 hours only.

.. rubric:: My search just quits or I got a warning describing my query complexity.

Have you checked your query length? It might be that your query is too complex or specific, causing the library
to behave poorly. A suggestion in this case would be to create a query with fewer parameters.

.. rubric:: Anything else?

Expand Down
4 changes: 4 additions & 0 deletions sentinelsat/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,10 @@ def check_query_length(query):
"""
# The server uses the Java's URLEncoder implementation internally, which we are replicating here
effective_length = len(quote_plus(query, safe="-_.*").replace("~", "%7E"))
if effective_length / 3938 > 1.0:
warnings.warn(
"Your query, {}, is complex and may cause a bad SciHub response.".format(query)
)
return effective_length / 3938

def _query_names(self, names):
Expand Down
32 changes: 32 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest
import yaml
import warnings
from pytest_socket import disable_socket
from vcr import VCR

Expand Down Expand Up @@ -188,3 +189,34 @@ def large_query():
area="POLYGON((0 0,0 10,10 10,10 0,0 0))",
date=(datetime(2015, 12, 1), datetime(2015, 12, 31)),
)


@pytest.fixture(scope="session")
def test_check_query_length(**api_kwargs):
api = SentinelAPI(**api_kwargs)
# short query to make sure it does not show the warning
query = api.format_query(
date=("20170801", "20170830"), platformname="Sentinel-2", cloudcoverpercentage=(0, 100)
)

# super long query to make sure it does show the warning
query1 = api.format_query(
date=("20170801", "20170830"),
platformname="Sentinel-3",
producttype="Sentinel-3: SR_1_SRA___, SR_1_SRA_A, SR_1_SRA_BS, SR_2_LAN___, OL_1_EFR___, OL_1_ERR___, OL_2_LFR___, OL_2_LRR___, SL_1_RBT___, SL_2_LST___, SY_2_SYN___, SY_2_V10___, SY_2_VG1___, SY_2_VGP___.",
area_relation="intersects",
footprint="Intersects(POLYGON((-4.53 29.85, 26.75 29.85, 26.75 46.80,-4.53 46.80,-4.53 29.85)))",
cloudcoverpercentage=(0, 100),
timeliness="Near Real Time",
)

with warnings.catch_warnings(record=True) as w:
# Cause all warnings to always be triggered.
warnings.simplefilter("always")
w = api.check_query_length(query)
# Activate the function which should or should not cause a warning.
assert len(w) == 0
w1 = api.check_query_length(query1)

assert len(w1) == 1
assert "Your query" in str(w1[-1].message)

0 comments on commit 3b6f002

Please sign in to comment.