예제 #1
0
def downloadSentinel(user, pw, aoi, start, stop):
    # For image before November 16th, 2015
    curl = pycurl.Curl()
    curl.setopt(pycurl.CAINFO, certifi.where())
    curl.setopt(pycurl.URL, 'https://scihub.copernicus.eu/dhus')
    curl.perform()
    # For image before November 16th, 2015
    api = SentinelAPI(user, pw, 'https://scihub.copernicus.eu/dhus')
    AOI = KMLtoGeoJason.kml2geojson(aoi)
    api.query(get_coordinates(AOI), start, stop, producttype='GRD')
# footprint generation of all found images:
    a=api.get_footprints()
    name = AOI[:-8]+"_S1footprint.geojson"
    foot = open(name, "w")
    foot.write(dumps(a, indent=2) + "\n")
    foot.close()
##
##    with open(name) as f:
##      contents = f.read()
##      display(contents)
# selected image download and unzip:
    imageId = raw_input("Insert Sentinel-1 image id: ")
    output_img = 'C:\Users\ithaca\Documents\Magda\Tool_MIE\SENTINEL-1_TOOL\Immagini_grandi'
    s1 = api.download(imageId, output_img)
    path = os.path.dirname(s1)
    with zipfile.ZipFile(s1, "r") as z:
        z.extractall(path)
예제 #2
0
    def query_and_create(self, start=None, end=None):
        '''Query the last scene available on period filtered by start time
        and end dates. By default start is current time and end is 7 days ago.'''

        end = end or datetime.utcnow()
        start = start or datetime.utcnow() - timedelta(days=7)
        coords = ','.join([('%f %f' % coord) for coord in self.geom.coords[0]])

        scenes_created = []

        try:
            api = SentinelAPI(settings.SENTINEL_USER, settings.SENTINEL_PASSWORD, settings.SENTINEL_API_URL)
            print('sentinel initialized on %s, with %s - %s' %
                  (settings.SENTINEL_API_URL, settings.SENTINEL_USER, settings.SENTINEL_PASSWORD))
        except AttributeError:
            api = SentinelAPI(settings.SENTINEL_USER, settings.SENTINEL_PASSWORD)
            print('sentinel initialized on %s, with %s - %s' %
                  (settings.SENTINEL_API_URL, ettings.SENTINEL_USER, settings.SENTINEL_PASSWORD))

        print('sentinelsat query -s %s -e %s coords %s q %s' % (start, end, coords, self.query))

        if self.query:
            query = dict([i.split('=') for i in self.query.split(',')])
            api.query(coords, start, end, **query)
        else:
            api.query(coords, start, end)

        features = api.get_footprints()['features']
        print('%s features found' % len(features))

        for feature in features:
            product_id = feature['properties']['product_id']

            try:
                Scene.objects.get(product=product_id)
                print('Scene of product %s already exists' % product_id)

            except Scene.DoesNotExist:
                print('Creating scene with data: %s' % feature)

                scene = Scene.objects.create(
                    product=product_id,
                    identifier=feature['properties']['identifier'],
                    date=datetime.strptime(
                        feature['properties']['date_beginposition'],
                        '%Y-%m-%dT%H:%M:%S.%fZ'
                    ),
                    polarisation=feature['properties']['polarisationmode'],
                    orbit_direction=feature['properties']['orbitdirection'],
                    sensor_mode=feature['properties']['sensoroperationalmode'],
                    product_type=feature['properties']['producttype'],
                    sat=feature['properties']['platformname'],
                    geom=Polygon(feature['geometry']['coordinates'][0]))
                
                print('Scene of product %s created' % product_id)
                
                scenes_created.append(scene)
        return scenes_created
예제 #3
0
def test_footprints_s2():
    api = SentinelAPI(**_api_auth)
    api.query(
        get_coordinates('tests/map.geojson'),
        "20151219", "20151228", platformname="Sentinel-2"
    )

    with open('tests/expected_search_footprints_s2.geojson', 'r') as geojson_file:
        expected_footprints = geojson.loads(geojson_file.read())
        # to compare unordered lists (JSON objects) they need to be sorted or changed to sets
        assert set(api.get_footprints()) == set(expected_footprints)
예제 #4
0
def test_footprints_s1():
    api = SentinelAPI(**_api_auth)
    api.query(
        get_coordinates('tests/map.geojson'),
        datetime(2014, 10, 10), datetime(2014, 12, 31), producttype="GRD"
    )

    with open('tests/expected_search_footprints_s1.geojson', 'r') as geojson_file:
        expected_footprints = geojson.loads(geojson_file.read())
        # to compare unordered lists (JSON objects) they need to be sorted or changed to sets
        assert set(api.get_footprints()) == set(expected_footprints)
예제 #5
0
def test_footprints():
    api = SentinelAPI(
    environ.get('SENTINEL_USER'),
    environ.get('SENTINEL_PASSWORD')
    )
    api.query(get_coordinates('tests/map.geojson'), datetime(2014, 10, 10), datetime(2014, 12, 31), producttype="GRD")

    expected_footprints = geojson.loads(open('tests/expected_search_footprints.geojson', 'r').read())

    # to compare unordered lists (JSON objects) they need to be sorted or changed to sets
    assert set(api.get_footprints()) == set(expected_footprints)
예제 #6
0
def test_footprints_s2():
    api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD'])
    api.query(get_coordinates('tests/map.geojson'),
              "20151219",
              "20151228",
              platformname="Sentinel-2")

    with open('tests/expected_search_footprints_s2.geojson',
              'r') as geojson_file:
        expected_footprints = geojson.loads(geojson_file.read())
        # to compare unordered lists (JSON objects) they need to be sorted or changed to sets
        assert set(api.get_footprints()) == set(expected_footprints)
예제 #7
0
def test_footprints_s1():
    api = SentinelAPI(environ['SENTINEL_USER'], environ['SENTINEL_PASSWORD'])
    api.query(get_coordinates('tests/map.geojson'),
              datetime(2014, 10, 10),
              datetime(2014, 12, 31),
              producttype="GRD")

    with open('tests/expected_search_footprints_s1.geojson',
              'r') as geojson_file:
        expected_footprints = geojson.loads(geojson_file.read())
        # to compare unordered lists (JSON objects) they need to be sorted or changed to sets
        assert set(api.get_footprints()) == set(expected_footprints)
예제 #8
0
def search(
        user, password, tile, geojson, start, end, download, md5,
        sentinel1, sentinel2, cloud, footprints, path, query, url):
    """Search for Sentinel products and, optionally, download all the results
    and/or create a geojson file with the search result footprints.
    Beyond your SciHub user and password, you must pass a geojson file
    containing the polygon of the area you want to search for. If you
    don't specify the start and end dates, it will search in the last 24 hours.
    """
    api = SentinelAPI(user, password, url)

    search_kwargs = {}
    if cloud:
        search_kwargs.update(
            {"platformname": "Sentinel-2",
            "cloudcoverpercentage": "[0 TO %s]" % cloud})
    elif sentinel2:
        search_kwargs.update({"platformname": "Sentinel-2"})
    elif sentinel1:
        search_kwargs.update({"platformname": "Sentinel-1"})
        
    if query is not None:
        search_kwargs.update(dict([i.split('=') for i in query.split(',')]))

    if tile:
        api.query(point = get_coordinates(tile = tile), initial_date = start, end_date = end, **search_kwargs)
    elif geojson:
        api.query(area = get_coordinates(geojson_file = geojson), initial_date = start, end_date = end, **search_kwargs)
    else:
        raise ValueError("Either a --geojson or --tile arguments must be given.")
    
    if footprints is True:
        footprints_geojson = api.get_footprints()
        with open(os.path.join(path, "search_footprints.geojson"), "w") as outfile:
            outfile.write(gj.dumps(footprints_geojson))

    if download is True:
        result = api.download_all(path, checksum=md5)
        if md5 is True:
            corrupt_scenes = [(path, info["id"]) for path, info in result.items() if info is not None]
            if len(corrupt_scenes) > 0:
                with open(os.path.join(path, "corrupt_scenes.txt"), "w") as outfile:
                    for corrupt_tuple in corrupt_scenes:
                        outfile.write("%s : %s\n" % corrupt_tuple)
    else:
        for product in api.get_products():
            print('Product %s - %s' % (product['id'], product['summary']))
        print('---')
        print(
            '%s scenes found with a total size of %.2f GB' %
            (len(api.get_products()), api.get_products_size()))
예제 #9
0
def search(
        user, password, geojson, start, end, download, md5,
        sentinel1, sentinel2, cloud, footprints, path, query, url):
    """Search for Sentinel products and, optionally, download all the results
    and/or create a geojson file with the search result footprints.
    Beyond your SciHub user and password, you must pass a geojson file
    containing the polygon of the area you want to search for. If you
    don't specify the start and end dates, it will search in the last 24 hours.
    """
    api = SentinelAPI(user, password, url)

    search_kwargs = {}
    if cloud:
        search_kwargs.update(
            {"platformname": "Sentinel-2",
            "cloudcoverpercentage": "[0 TO %s]" % cloud})
    elif sentinel2:
        search_kwargs.update({"platformname": "Sentinel-2"})
    elif sentinel1:
        search_kwargs.update({"platformname": "Sentinel-1"})

    if query is not None:
        search_kwargs.update(dict([i.split('=') for i in query.split(',')]))

    api.query(get_coordinates(geojson), start, end, **search_kwargs)

    if footprints is True:
        footprints_geojson = api.get_footprints()
        with open(os.path.join(path, "search_footprints.geojson"), "w") as outfile:
            outfile.write(gj.dumps(footprints_geojson))

    if download is True:
        result = api.download_all(path, checksum=md5)
        if md5 is True:
            corrupt_scenes = [(path, info["id"]) for path, info in result.items() if info is not None]
            if len(corrupt_scenes) > 0:
                with open(os.path.join(path, "corrupt_scenes.txt"), "w") as outfile:
                    for corrupt_tuple in corrupt_scenes:
                        outfile.write("%s : %s\n" % corrupt_tuple)
    else:
        for product in api.get_products():
            print('Product %s - %s' % (product['id'], product['summary']))
        print('---')
        print(
            '%s scenes found with a total size of %.2f GB' %
            (len(api.get_products()), api.get_products_size()))
예제 #10
0
파일: cli.py 프로젝트: gijs/sentinelsat
def search(user, password, geojson, start, end, download, footprints, path, query):
    """Search for Sentinel-1 products and, optionally, download all the results
    and/or create a geojson file with the search result footprints.
    Beyond your SciHub user and password, you must pass a geojson file
    containing the polygon of the area you want to search for. If you
    don't specify the start and end dates, it will search in the last 24 hours.
    """
    api = SentinelAPI(user, password)
    if query is not None:
        query = dict([i.split('=') for i in query.split(',')])
        api.query(get_coordinates(geojson), start, end, **query)
    else:
        api.query(get_coordinates(geojson), start, end)

    if footprints is True:
        footprints_geojson = api.get_footprints()
        with open(os.path.join(path, "search_footprints.geojson"), "w") as outfile:
            outfile.write(gj.dumps(footprints_geojson))

    if download is True:
        api.download_all(path)
    else:
        for product in api.get_products():
            print('Product %s - %s' % (product['id'], product['summary']))
예제 #11
0
#api.download(<product_id>)

# search by polygon, time, and SciHub query keywords
#products = api.query(,'20151219', date(2015, 12, 29), platformname = 'Sentinel-2', cloudcoverpercentage = '[0 TO 30]'})
products = api.query(get_coordinates('map.geojson'),
                     initial_date=datetime(2017, 2, 24),
                     end_date=datetime(2017, 2, 25))

#api.get_products_size(products)
print(api.get_products_size(products))

# download all results from the search
#api.download_all(products)

# GeoJSON FeatureCollection containing footprints and metadata of the scenes
fp = api.get_footprints(products)

with open('footprints.json', 'w') as outfile:
    #json.dump({'numbers':n, 'strings':s, 'x':x, 'y':y}, outfile, indent=4)
    json.dump(fp, outfile, indent=4)

with open('product.json', 'w') as outfile:
    #json.dump({'numbers':n, 'strings':s, 'x':x, 'y':y}, outfile, indent=4)
    json.dump(products, outfile, indent=4)

#print(fp["features"][0]["properties"]["identifier"])

i = 0
#2017-02-07T10:42:00.746Z
datetime_old = format_date("20170101")
print datetime_old