Ejemplo n.º 1
0
def gather_data():
    
    
    cloud_classifier = get_s2_pixel_cloud_detector(average_over=2, dilation_size=1, all_bands=False)
    add_clm = AddCloudMaskTask(cloud_classifier, 'BANDS-S2CLOUDLESS', cm_size_y='80m', cm_size_x='80m', 
                           cmask_feature='CLM', # cloud mask name
                           cprobs_feature='CLP' # cloud prob. map name
                          )
    ndvi = NormalizedDifferenceIndex('NDVI', 'BANDS/3', 'BANDS/2')
    add_sh_valmask = AddValidDataMaskTask(SentinelHubValidData(), 
                                      'IS_VALID')
    layer = 'BANDS-S2-L1C'
    custom_script = 'return [B02, B03];'
    input_task = S2L1CWCSInput(layer=layer,
                           feature=(FeatureType.DATA, 'BANDS'), 
    custom_url_params={CustomUrlParam.EVALSCRIPT: custom_script},
                           resx='10m', resy='10m',
                           maxcc=.8)
    add_ndvi = S2L1CWCSInput(layer='NDVI')
    save = SaveToDisk('io_example', overwrite_permission=2)#compress_level=1
    workflow = LinearWorkflow(
       input_task,
        add_clm,
        add_ndvi,
        add_sh_valmask,
    
        )
    time_interval = ('2017-01-01', '2017-12-31')
    result = workflow.execute({input_task: {'bbox': roi_bbox, 'time_interval': time_interval},
                           save: {'eopatch_folder': 'eopatch'}})
    return list(result.values())[0].data['NDVI'], list(result.values())[-1].mask['IS_VALID'],  np.array(list(result.values())[0].timestamp)
Ejemplo n.º 2
0
 def _get_eopatch(self, bbox, time_interval):
     # request L2A images
     add_l2a = S2L2AWCSInput(self.config.l2a_field,
                             resx=self.config.resx,
                             resy=self.config.resy,
                             maxcc=self.config.maxcc,
                             time_difference=datetime.timedelta(hours=2))
     # filter frames with valid data = 1
     filter_task = SimpleFilterTask(
         (FeatureType.MASK, 'IS_DATA'), ValidDataFractionPredicate(1),
         (FeatureType.DATA, self.config.l2a_field))
     # randomly select one temporal frame
     sel_task = SelectRandomFrameTask([(FeatureType.DATA,
                                        self.config.l2a_field),
                                       (FeatureType.MASK, 'IS_DATA')])
     # request corresponding L1C
     add_l1c = S2L1CWCSInput(self.config.l1c_field,
                             resx=self.config.resx,
                             resy=self.config.resy,
                             maxcc=self.config.maxcc,
                             time_difference=datetime.timedelta(hours=2))
     # execute workflow
     try:
         eop = add_l1c.execute(
             sel_task.execute(
                 filter_task.execute(
                     add_l2a.execute(time_interval=time_interval,
                                     bbox=bbox))))
         executed = True
     except (HTTPError, IndexError, DownloadFailedException, RuntimeError,
             ValueError, TypeError, NameError):
         logging.error("Exception thrown in obtaining EOPatch")
         eop = None
         executed = False
     return eop, executed
Ejemplo n.º 3
0
    def get_elevation(self, bounds):
        INSTANCE_ID = '4aaea2ec-3a2c-4e1c-8a51-851e220d0273'
        roi = BBox(bbox=bounds, crs=CRS.WGS84)
        layer = 'MAPZEN_DEM'
        time_interval = ('2019-01-01', '2019-06-01')
        add_dem = DEMWCSInput(layer=layer, instance_id=INSTANCE_ID)
        input_task = S2L1CWCSInput(layer=layer,
                                   resx='30m',
                                   resy='30m',
                                   instance_id=INSTANCE_ID)
        workflow = LinearWorkflow(input_task, add_dem)
        result = workflow.execute(
            {input_task: {
                'bbox': roi,
                'time_interval': time_interval
            }})
        eopatch = list(result.values())[0]

        return eopatch
Ejemplo n.º 4
0
def download_patches(path, shp, bbox_list, indexes):
    add_data = S2L1CWCSInput(
        layer='BANDS-S2-L1C',
        feature=(FeatureType.DATA, 'BANDS'),  # save under name 'BANDS'
        resx='10m',  # resolution x
        resy='10m',  # resolution y
        maxcc=0.8,  # maximum allowed cloud cover of original ESA tiles
    )
    path_out = path + '/Slovenia/'
    if not os.path.isdir(path_out):
        os.makedirs(path_out)
    save = SaveTask(path_out,
                    overwrite_permission=OverwritePermission.OVERWRITE_PATCH)

    workflow = LinearWorkflow(add_data, save)

    time_interval = ['2017-01-01',
                     '2017-12-31']  # time interval for the SH request

    execution_args = []
    for idx, bbox in zip(indexes, bbox_list[indexes]):
        execution_args.append({
            add_data: {
                'bbox': bbox,
                'time_interval': time_interval
            },
            save: {
                'eopatch_folder': 'eopatch_{}'.format(idx)
            }
        })

    start_time = time.time()
    executor = EOExecutor(workflow, execution_args, save_logs=True)
    executor.run(workers=1, multiprocess=False)
    file = open('timing.txt', 'a')
    running = str(
        dt.datetime.now()) + ' Running time: {}\n'.format(time.time() -
                                                          start_time)
    print(running)
    file.write(running)
    file.close()
Ejemplo n.º 5
0
lulc_cmap = mpl.colors.ListedColormap([entry.color for entry in LULC])
lulc_norm = mpl.colors.BoundaryNorm(np.arange(-0.5, 6, 1), lulc_cmap.N)

#%%

# TASK FOR BAND DATA
# add a request for B(B02), G(B03), R(B04), NIR (B08), SWIR1(B11), SWIR2(B12)
# from default layer 'ALL_BANDS' at 10m resolution
# Here we also do a simple filter of cloudy scenes. A detailed cloud cover
# detection is performed in the next step
custom_script = 'return [B02, B03, B04, B08, B11, B12];'
add_data = S2L1CWCSInput(
    layer='BANDS-S2-L1C',
    feature=(FeatureType.DATA, 'BANDS'),  # save under name 'BANDS'
    custom_url_params={CustomUrlParam.EVALSCRIPT:
                       custom_script},  # custom url for 6 specific bands
    resx='10m',  # resolution x
    resy='10m',  # resolution y
    maxcc=0.8,  # maximum allowed cloud cover of original ESA tiles
)

# TASK FOR CLOUD INFO
# cloud detection is performed at 80m resolution
# and the resulting cloud probability map and mask
# are scaled to EOPatch's resolution
cloud_classifier = get_s2_pixel_cloud_detector(average_over=2,
                                               dilation_size=1,
                                               all_bands=False)
add_clm = AddCloudMaskTask(
    cloud_classifier,
    'BANDS-S2CLOUDLESS',
Ejemplo n.º 6
0
# inflate the BBOX
inflate_bbox = 5
minx, miny, maxx, maxy = dam_nominal.bounds
delx = maxx - minx
dely = maxy - miny

minx = minx - delx * inflate_bbox
maxx = maxx + delx * inflate_bbox
miny = miny - dely * inflate_bbox
maxy = maxy + dely * inflate_bbox

dam_bbox = BBox(bbox=[minx, miny, maxx, maxy], crs=CRS.WGS84)

input_task = S2L1CWCSInput('BANDS-S2-L1C',
                           resx='20m',
                           resy='20m',
                           maxcc=1.,
                           time_difference=datetime.timedelta(hours=2),
                           instance_id=WMS_INSTANCE)
add_ndwi = S2L1CWCSInput('NDWI', instance_id=WMS_INSTANCE)

gdf = gpd.GeoDataFrame(crs={'init': 'epsg:4326'}, geometry=[dam_nominal])
gdf.plot()
add_nominal_water = VectorToRaster(
    (FeatureType.MASK_TIMELESS, 'NOMINAL_WATER'), gdf, 1,
    (FeatureType.MASK, 'IS_DATA'), np.uint8)

cloud_classifier = get_s2_pixel_cloud_detector(average_over=2,
                                               dilation_size=1,
                                               all_bands=False)
cloud_det = AddCloudMaskTask(cloud_classifier,
                             'BANDS-S2CLOUDLESS',
Ejemplo n.º 7
0
def download_data(path_save,
                  coords_top,
                  coords_bot,
                  patch_n,
                  s_date,
                  e_date,
                  debug=False):
    # before moving onto actual tasks, check setup
    check_sentinel_cfg()

    [lat_left_top, lon_left_top] = coords_top
    [lat_right_bot, lon_right_bot] = coords_bot
    # TASK FOR BAND DATA
    # add a request for B(B02), G(B03), R(B04), NIR (B08), SWIR1(B11), SWIR2(B12)
    # from default layer 'ALL_BANDS' at 10m resolution
    # Here we also do a simple filter of cloudy scenes. A detailed cloud cover
    # detection is performed in the next step
    custom_script = "return [B02, B03, B04, B08, B11, B12];"
    add_data = S2L1CWCSInput(
        layer="BANDS-S2-L1C",
        feature=(FeatureType.DATA, "BANDS"),  # save under name 'BANDS'
        # custom url for 6 specific bands
        custom_url_params={CustomUrlParam.EVALSCRIPT: custom_script},
        resx="10m",  # resolution x
        resy="10m",  # resolution y
        maxcc=0.1,  # maximum allowed cloud cover of original ESA tiles
    )

    # TASK FOR CLOUD INFO
    # cloud detection is performed at 80m resolution
    # and the resulting cloud probability map and mask
    # are scaled to EOPatch's resolution
    cloud_classifier = get_s2_pixel_cloud_detector(average_over=2,
                                                   dilation_size=1,
                                                   all_bands=False)
    add_clm = AddCloudMaskTask(
        cloud_classifier,
        "BANDS-S2CLOUDLESS",
        cm_size_y="80m",
        cm_size_x="80m",
        cmask_feature="CLM",  # cloud mask name
        cprobs_feature="CLP",  # cloud prob. map name
    )

    # TASKS FOR CALCULATING NEW FEATURES
    # NDVI: (B08 - B04)/(B08 + B04)
    # NDWI: (B03 - B08)/(B03 + B08)
    # NORM: sqrt(B02^2 + B03^2 + B04^2 + B08^2 + B11^2 + B12^2)
    ndvi = NormalizedDifferenceIndex("NDVI", "BANDS/3", "BANDS/2")
    ndwi = NormalizedDifferenceIndex("NDWI", "BANDS/1", "BANDS/3")
    norm = EuclideanNorm("NORM", "BANDS")

    # TASK FOR VALID MASK
    # validate pixels using SentinelHub's cloud detection mask and region of acquisition
    add_sh_valmask = AddValidDataMaskTask(
        SentinelHubValidData(),
        "IS_VALID"  # name of output mask
    )

    # TASK FOR COUNTING VALID PIXELS
    # count number of valid observations per pixel using valid data mask
    count_val_sh = CountValid(
        "IS_VALID",
        "VALID_COUNT"  # name of existing mask  # name of output scalar
    )

    # TASK FOR SAVING TO OUTPUT (if needed)
    path_save = Path(path_save)
    path_save.mkdir(exist_ok=True)
    # if not os.path.isdir(path_save):
    #     os.makedirs(path_save)
    save = SaveToDisk(path_save,
                      overwrite_permission=OverwritePermission.OVERWRITE_PATCH)

    # Define the workflow
    workflow = LinearWorkflow(add_data, add_clm, ndvi, ndwi, norm,
                              add_sh_valmask, count_val_sh, save)
    # Execute the workflow

    # time interval for the SH request
    # TODO: need to check if specified time interval is valid
    time_interval = [s_date, e_date]

    # define additional parameters of the workflow
    execution_args = []

    path_EOPatch = path_save / f"eopatch_{patch_n}"

    execution_args.append({
        add_data: {
            "bbox":
            BBox(
                ((lon_left_top, lat_left_top), (lon_right_bot, lat_right_bot)),
                crs=CRS.WGS84,
            ),
            "time_interval":
            time_interval,
        },
        save: {
            "eopatch_folder": path_EOPatch.stem
        },
    })

    executor = EOExecutor(workflow, execution_args, save_logs=True)
    if debug:
        print("Downloading Satellite data ...")

    executor.run(workers=2, multiprocess=False)
    if executor.get_failed_executions():
        raise RuntimeError("EOExecutor failed in finishing tasks!")

    if debug:
        executor.make_report()
    if debug:
        print("Satellite data is downloaded")
    return path_EOPatch
Ejemplo n.º 8
0
from eolearn.io import S2L1CWCSInput, SentinelHubInputTask, S2L2AWCSInput

input_task = S2L2AWCSInput('BANDS-S2-L1C', resx='10m', resy='10m', maxcc=0.8)
add_l2a = S2L2AWCSInput(layer='BANDS-S2-L2A')
true_color = S2L1CWCSInput('TRUE-COLOR-S2-L1C')
SENT_SAT_CLASSICATIONS = S2L2AWCSInput("SCENE_CLASSIFICATION")
# dam_bbox = BBox(bbox=[106.898236, 11.040099, 107.319836, 11.316956], crs=CRS.WGS84)

# BBox(((106.95406121147886, 11.05524688042548), (107.32241244845807, 11.297316867404723)), crs=EPSG:4326)

print(dam_bbox)

# ### Step 1: Intialize (and implement workflow specific) EOTasks

# #### Create an EOPatch and add all EO features (satellite imagery data)

# In[5]:

input_task = S2L1CWCSInput('TRUE-COLOR-S2-L1C',
                           resx='20m',
                           resy='20m',
                           maxcc=0.5,
                           instance_id=None)
add_ndwi = S2L1CWCSInput('NDWI')

# #### Burn in the nominal water extent
#
# The `VectorToRaster` task expects the vectorised dataset in geopandas dataframe.

# In[6]:

gdf = gpd.GeoDataFrame(crs={'init': 'epsg:4326'}, geometry=[dam_nominal])

# In[8]:

add_nominal_water = VectorToRaster(