def test_api_query_format_sets(): query = SentinelAPI.format_query(orbitnumber={16301, 16302, 16303}) assert query == "orbitnumber:(16301 OR 16302 OR 16303)" query = SentinelAPI.format_query( ingestiondate={date(2017, 1, 1), "20170203"}) assert query == "ingestiondate:(2017-01-01T00:00:00Z OR 2017-02-03T00:00:00Z)"
def test_api_query_format_escape_spaces(api): query = SentinelAPI.format_query(ingestiondate=('NOW-1DAY', 'NOW')) assert query == 'ingestiondate:[NOW-1DAY TO NOW]' query = SentinelAPI.format_query(ingestiondate='[NOW-1DAY TO NOW]') assert query == 'ingestiondate:[NOW-1DAY TO NOW]' query = SentinelAPI.format_query(ingestiondate=' [NOW-1DAY TO NOW] ') assert query == 'ingestiondate:[NOW-1DAY TO NOW]' query = SentinelAPI.format_query(relativeorbitnumber=' {101 TO 103} ') assert query == 'relativeorbitnumber:{101 TO 103}' query = SentinelAPI.format_query(filename='S3A_OL_2* ') assert query == 'filename:S3A_OL_2*' query = SentinelAPI.format_query(timeliness='Non Time Critical') assert query == r'timeliness:Non\ Time\ Critical' query = SentinelAPI.format_query(timeliness='Non\tTime\tCritical') assert query == r'timeliness:Non\ Time\ Critical' assert api.count(timeliness='Non Time Critical') > 0 # Allow for regex weirdness query = SentinelAPI.format_query(timeliness='.+ Critical') assert query == r'timeliness:.+\ Critical' assert api.count(timeliness='.+ Critical') > 0 query = SentinelAPI.format_query(identifier='/S[123 ]A.*/') assert query == r'identifier:/S[123 ]A.*/' assert api.count(identifier='/S[123 ]A.*/') > 0
def test_api_query_format_escape_spaces(api): query = SentinelAPI.format_query(ingestiondate=("NOW-1DAY", "NOW")) assert query == "ingestiondate:[NOW-1DAY TO NOW]" query = SentinelAPI.format_query(ingestiondate="[NOW-1DAY TO NOW]") assert query == "ingestiondate:[NOW-1DAY TO NOW]" query = SentinelAPI.format_query(ingestiondate=" [NOW-1DAY TO NOW] ") assert query == "ingestiondate:[NOW-1DAY TO NOW]" query = SentinelAPI.format_query(relativeorbitnumber=" {101 TO 103} ") assert query == "relativeorbitnumber:{101 TO 103}" query = SentinelAPI.format_query(filename="S3A_OL_2* ") assert query == "filename:S3A_OL_2*" query = SentinelAPI.format_query(timeliness="Non Time Critical") assert query == r"timeliness:Non\ Time\ Critical" query = SentinelAPI.format_query(timeliness="Non\tTime\tCritical") assert query == r"timeliness:Non\ Time\ Critical" assert api.count(timeliness="Non Time Critical") > 0 # Allow for regex weirdness query = SentinelAPI.format_query(timeliness=".+ Critical") assert query == r"timeliness:.+\ Critical" assert api.count(timeliness=".+ Critical") > 0 query = SentinelAPI.format_query(identifier="/S[123 ]A.*/") assert query == r"identifier:/S[123 ]A.*/" assert api.count(identifier="/S[123 ]A.*/") > 0
def raw_products(): """A fixture for tests that need some non-specific set of products in the form of a raw response as input.""" api = SentinelAPI(**_api_auth) raw_products = api._load_query( api.format_query(geojson_to_wkt(read_geojson('tests/map.geojson')), "20151219", "20151228")) return raw_products
def raw_products(api_kwargs, vcr, test_wkt): """A fixture for tests that need some non-specific set of products in the form of a raw response as input.""" with vcr.use_cassette('products_fixture', decode_compressed_response=False): api = SentinelAPI(**api_kwargs) raw_products = api._load_query( api.format_query(test_wkt, ("20151219", "20151228")) )[0] return raw_products
def raw_products(api_kwargs, vcr, test_wkt): """A fixture for tests that need some non-specific set of products in the form of a raw response as input.""" with vcr.use_cassette("products_fixture", decode_compressed_response=False): api = SentinelAPI(**api_kwargs) raw_products = api._load_query( api.format_query(test_wkt, ("20151219", "20151228")))[0] return raw_products
def raw_products(): """A fixture for tests that need some non-specific set of products in the form of a raw response as input.""" api = SentinelAPI(**_api_auth) raw_products = api._load_query(api.format_query( geojson_to_wkt(read_geojson('tests/map.geojson')), "20151219", "20151228") ) return raw_products
def raw_products(): """A fixture for tests that need some non-specific set of products in the form of a raw response as input.""" api = SentinelAPI(**_api_auth) with my_vcr.use_cassette('products_fixture', decode_compressed_response=False): raw_products = api._load_query( api.format_query( geojson_to_wkt(read_geojson(FIXTURES_DIR + '/map.geojson')), ("20151219", "20151228")))[0] return raw_products
def test_api_query_format(): api = SentinelAPI("mock_user", "mock_password") wkt = 'POLYGON((0 0,1 1,0 1,0 0))' now = datetime.now() last_24h = _format_query_date(now - timedelta(hours=24)) query = api.format_query(wkt, initial_date=last_24h, end_date=now) assert query == '(beginPosition:[%s TO %s]) ' % (last_24h, _format_query_date(now)) + \ 'AND (footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))")' query = api.format_query(wkt, end_date=now, producttype='SLC') assert query == '(beginPosition:[NOW-1DAY TO %s]) ' % (_format_query_date(now)) + \ 'AND (footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))") ' + \ 'AND (producttype:SLC)' query = api.format_query() assert query == '(beginPosition:[NOW-1DAY TO NOW])' query = api.format_query(area=None, initial_date=None, end_date=None) assert query == ''
def test_api_query_format_sets(): query = SentinelAPI.format_query(orbitnumber={16301, 16302, 16303}) assert query == "(orbitnumber:16301 OR orbitnumber:16302 OR orbitnumber:16303)" query = SentinelAPI.format_query(orbitnumber=set()) assert query == "" query = SentinelAPI.format_query(ingestiondate={(date(2017, 1, 1), "20170203")}) assert query == '(ingestiondate:["2017-01-01T00:00:00Z" TO "2017-02-03T00:00:00Z"])' with pytest.raises(ValueError): SentinelAPI.format_query(ingestiondate={}) with pytest.raises(ValueError): SentinelAPI.format_query(ingestiondate={""}) with pytest.raises(ValueError): SentinelAPI.format_query(ingestiondate={date(2017, 1, 1), "20170203"})
def test_api_query_format(): wkt = 'POLYGON((0 0,1 1,0 1,0 0))' now = datetime.now() last_24h = format_query_date(now - timedelta(hours=24)) query = SentinelAPI.format_query(wkt, (last_24h, now)) assert query == 'beginPosition:[%s TO %s] ' % (last_24h, format_query_date(now)) + \ 'footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))"' query = SentinelAPI.format_query(wkt, date=(last_24h, "NOW"), producttype='SLC', raw='IW') assert query == 'beginPosition:[%s TO NOW] ' % (format_query_date(last_24h)) + \ 'producttype:SLC IW footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))"' query = SentinelAPI.format_query(wkt, producttype='SLC', raw='IW') assert query == 'producttype:SLC IW footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))"' query = SentinelAPI.format_query(area=None, date=None) assert query == '' query = SentinelAPI.format_query() assert query == '' query = SentinelAPI.format_query(raw='test') assert query == 'test'
def test_api_query_format(): wkt = "POLYGON((0 0,1 1,0 1,0 0))" now = datetime.now() last_24h = format_query_date(now - timedelta(hours=24)) query = SentinelAPI.format_query(wkt, (last_24h, now)) assert (query == "beginPosition:[%s TO %s] " % (last_24h, format_query_date(now)) + 'footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))"') query = SentinelAPI.format_query(wkt, date=(last_24h, "NOW"), producttype="SLC", raw="IW") assert ( query == "beginPosition:[%s TO NOW] " % (format_query_date(last_24h)) + 'producttype:SLC IW footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))"' ) query = SentinelAPI.format_query(wkt, producttype="SLC", raw="IW") assert query == 'producttype:SLC IW footprint:"Intersects(POLYGON((0 0,1 1,0 1,0 0)))"' query = SentinelAPI.format_query(area=None, date=None) assert query == "" query = SentinelAPI.format_query() assert query == "" query = SentinelAPI.format_query(raw="test") assert query == "test"
def test_api_query_format_ranges(): query = SentinelAPI.format_query(cloudcoverpercentage=(0, 30)) assert query == 'cloudcoverpercentage:[0 TO 30]' query = SentinelAPI.format_query(cloudcoverpercentage=[0, 30]) assert query == 'cloudcoverpercentage:[0 TO 30]' with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[]) with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[0]) with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[0, 1, 2])
def test_api_query_format_with_duplicates(): with pytest.raises(ValueError) as excinfo: SentinelAPI.format_query(date=('NOW-1DAY', 'NOW'), beginPosition=('NOW-3DAY', 'NOW')) assert 'duplicate' in str(excinfo.value) with pytest.raises(ValueError) as excinfo: SentinelAPI.format_query(ingestiondate=('NOW-1DAY', 'NOW'), ingestionDate=('NOW-3DAY', 'NOW')) assert 'duplicate' in str(excinfo.value) with pytest.raises(ValueError) as excinfo: SentinelAPI.format_query(area='POINT(0, 0)', footprint='POINT(0, 0)') assert 'duplicate' in str(excinfo.value)
def test_api_query_format_with_duplicates(): with pytest.raises(ValueError) as excinfo: SentinelAPI.format_query(date=("NOW-1DAY", "NOW"), beginPosition=("NOW-3DAY", "NOW")) assert "duplicate" in str(excinfo.value) with pytest.raises(ValueError) as excinfo: SentinelAPI.format_query(ingestiondate=("NOW-1DAY", "NOW"), ingestionDate=("NOW-3DAY", "NOW")) assert "duplicate" in str(excinfo.value) with pytest.raises(ValueError) as excinfo: SentinelAPI.format_query(area="POINT(0, 0)", footprint="POINT(0, 0)") assert "duplicate" in str(excinfo.value)
def test_api_query_format_dates(): query = SentinelAPI.format_query(ingestiondate=('NOW-1DAY', 'NOW')) assert query == 'ingestiondate:[NOW-1DAY TO NOW]' query = SentinelAPI.format_query(ingestiondate=(date(2017, 1, 1), '20170203')) assert query == 'ingestiondate:[2017-01-01T00:00:00Z TO 2017-02-03T00:00:00Z]' query = SentinelAPI.format_query(ingestiondate='[NOW-1DAY TO NOW]') assert query == 'ingestiondate:[NOW-1DAY TO NOW]' with pytest.raises(ValueError): SentinelAPI.format_query(date="NOW") with pytest.raises(ValueError): SentinelAPI.format_query(date=["NOW"]) with pytest.raises(ValueError): SentinelAPI.format_query(ingestiondate=[]) with pytest.raises(ValueError): SentinelAPI.format_query(ingestiondate=[None, 'NOW'])
import sentinelsat import json from sentinelsat import SentinelAPI import geojson from shapely.geometry import shape with open("resources/canada.geojson", 'r+') as file: nwt = geojson.load(file)["features"][0] """ Some sample code to construct the OpenSearch Query for our products. """ simplified = shape(nwt.geometry).simplify(0.5, preserve_topology=False) print(simplified.to_wkt()) api = SentinelAPI('test', 'test') query = api.format_query(date=("20170617", "20170628"), producttype='SLC', platformname='Sentinel-1', polarisationmode='HH HV', orbitdirection='DESCENDING', geometry=simplified.to_wkt()) print(query) products = api.query(simplified.to_wkt(), date=("20170617", "20170628"), producttype='SLC', platformname='Sentinel-1', orbitdirection='DESCENDING', polarisationmode='HH HV')
def test_api_query_format_ranges(): query = SentinelAPI.format_query(cloudcoverpercentage=(0, 30)) assert query == 'cloudcoverpercentage:[0 TO 30]' query = SentinelAPI.format_query(cloudcoverpercentage=[0, 30]) assert query == 'cloudcoverpercentage:[0 TO 30]' query = SentinelAPI.format_query(cloudcoverpercentage=[None, 30]) assert query == 'cloudcoverpercentage:[* TO 30]' query = SentinelAPI.format_query(orbitnumber=(16302, None)) assert query == 'orbitnumber:[16302 TO *]' query = SentinelAPI.format_query(orbitnumber=(16302, '*')) assert query == 'orbitnumber:[16302 TO *]' for value in [(None, None), ('*', None), (None, '*'), ('*', '*')]: query = SentinelAPI.format_query(orbitnumber=value) assert query == '' with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[]) with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[0]) with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[0, 1, 2])
def test_api_query_format_ranges(): query = SentinelAPI.format_query(cloudcoverpercentage=(0, 30)) assert query == "cloudcoverpercentage:[0 TO 30]" query = SentinelAPI.format_query(cloudcoverpercentage=[0, 30]) assert query == "cloudcoverpercentage:[0 TO 30]" query = SentinelAPI.format_query(cloudcoverpercentage=[None, 30]) assert query == "cloudcoverpercentage:[* TO 30]" query = SentinelAPI.format_query(orbitnumber=(16302, None)) assert query == "orbitnumber:[16302 TO *]" query = SentinelAPI.format_query(orbitnumber=(16302, "*")) assert query == "orbitnumber:[16302 TO *]" for value in [(None, None), ("*", None), (None, "*"), ("*", "*")]: query = SentinelAPI.format_query(orbitnumber=value) assert query == "" with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[]) with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[0]) with pytest.raises(ValueError): SentinelAPI.format_query(cloudcoverpercentage=[0, 1, 2])
def test_api_query_format_dates(): query = SentinelAPI.format_query(ingestiondate=("NOW-1DAY", "NOW")) assert query == "ingestiondate:[NOW-1DAY TO NOW]" query = SentinelAPI.format_query(ingestiondate=(date(2017, 1, 1), "20170203")) assert query == "ingestiondate:[2017-01-01T00:00:00Z TO 2017-02-03T00:00:00Z]" query = SentinelAPI.format_query(ingestiondate="[NOW-1DAY TO NOW]") assert query == "ingestiondate:[NOW-1DAY TO NOW]" query = SentinelAPI.format_query(ingestiondate=[None, "NOW"]) assert query == "ingestiondate:[* TO NOW]" for value in [(None, None), ("*", None), (None, "*"), ("*", "*")]: query = SentinelAPI.format_query(ingestiondate=value) assert query == "" with pytest.raises(ValueError): SentinelAPI.format_query(date="NOW") with pytest.raises(ValueError): SentinelAPI.format_query(date=["NOW"]) with pytest.raises(ValueError): SentinelAPI.format_query(ingestiondate=[])
def test_api_query_format_escape_spaces(api): query = SentinelAPI.format_query(ingestiondate=("NOW-1DAY", "NOW")) assert query == 'ingestiondate:["NOW-1DAY" TO "NOW"]' assert api.count(ingestiondate=("NOW-1DAY", "NOW")) > 0 query = SentinelAPI.format_query(ingestiondate=("NOW-1DAY", None)) assert query == 'ingestiondate:["NOW-1DAY" TO *]' assert api.count(ingestiondate=("NOW-1DAY", None)) > 0 query = SentinelAPI.format_query(ingestiondate="[NOW-1DAY TO NOW]") assert query == "ingestiondate:[NOW-1DAY TO NOW]" query = SentinelAPI.format_query(ingestiondate=" [NOW-1DAY TO NOW] ") assert query == "ingestiondate:[NOW-1DAY TO NOW]" query = SentinelAPI.format_query(relativeorbitnumber="{101 TO 103}") assert query == "relativeorbitnumber:{101 TO 103}" assert api.count(relativeorbitnumber="{101 TO 103}") > 0 query = SentinelAPI.format_query(filename="S3A_OL_2* ") assert query == "filename:S3A_OL_2*" query = SentinelAPI.format_query(filename="S3A_OL_2?2") assert query == "filename:S3A_OL_2?2" query = SentinelAPI.format_query(timeliness="Non Time Critical") assert query == r'timeliness:"Non Time Critical"' query = SentinelAPI.format_query(timeliness='"Non Time Critical"') assert query == r'timeliness:"Non Time Critical"' query = SentinelAPI.format_query(timeliness="Non\tTime\tCritical") assert query == r'timeliness:"Non Time Critical"' assert api.count(timeliness="Near Real Time") > 0 # Allow for regex weirdness query = SentinelAPI.format_query(timeliness=".+ Critical") assert query == r'timeliness:".+ Critical"' assert api.count(timeliness=".+ Critical") > 0 query = SentinelAPI.format_query(identifier="/S[123 ]A.*/") assert query == "identifier:/S[123 ]A.*/" kwargs = dict(beginposition=("20180101", "20180130"), area="POINT(14.20 59.96)") regex_count = api.count(identifier="/S[12 ]A.*/", **kwargs) c1 = api.count(identifier="S1A*", **kwargs) c2 = api.count(identifier="S2A*", **kwargs) assert min(regex_count, c1, c2) > 0 assert regex_count == c1 + c2