def _apply(self, widget, event, data): """download the dataset using the given parameters""" folder = Path(ee.data.getAssetRoots()[0]["id"]) # check if a dataset is existing if self.dataset == None or self.geometry == None: return self # set the parameters name = self.name or dt.now().strftime("%Y-%m-%d_%H-%M-%S") export_params = { "image": self.dataset, "description": name, "scale": self.w_scale.v_model, "region": self.geometry, "maxPixels": 1e13, } # launch the task if self.w_method.v_model == "gee": export_params.update(assetId=str(folder / name)) task = ee.batch.Export.image.toAsset(**export_params) task.start() self.alert.add_msg( "the task have been launched in your GEE acount", "success") elif self.w_method.v_model == "sepal": gdrive = cs.gdrive() files = gdrive.get_files(name) if files == []: task = ee.batch.Export.image.toDrive(**export_params) task.start() gee.wait_for_completion(name, self.alert) files = gdrive.get_files(name) # save everything in the same folder as the json file # no need to create it it's created when the recipe is saved result_dir = cp.result_dir / aoi_name tile_list = gdrive.download_files(files, result_dir) gdrive.delete_files(files) # add the colormap to each tile colormap = {} for code, color in enumerate(no_data_color + cp.gradient(5)): colormap[i] = tuple(int(c * 255) for c in to_rgba(color)) for tile in tile_list: with rio.open(tile) as f: dst_f.write_colormap(self.band, colormap) self.alert.add_msg("map exported", "success") return self
def _apply(self, widget, event, data): """download the dataset using the given parameters""" folder = Path(ee.data.getAssetRoots()[0]["id"]) # check if a dataset is existing if self.datasets == [] or self.geometry == None: return self for name in self.w_datasets.v_model: description = su.normalize_str(f"{self.w_prefix.v_model}_{name}") # set the parameters export_params = { "image": self.datasets[name], "description": description, "scale": self.w_scale.v_model, "region": self.geometry, } # launch the task if self.w_method.v_model == "gee": export_params.update(assetId=str(folder / description)) task = ee.batch.Export.image.toAsset(**export_params) task.start() self.alert.add_msg( "the task have been launched in your GEE acount", "success") elif self.w_method.v_model == "sepal": gdrive = cs.gdrive() files = gdrive.get_files(description) if files == []: task = ee.batch.Export.image.toDrive(**export_params) task.start() gee.wait_for_completion(description, self.alert) files = gdrive.get_files(description) gdrive.download_files(files, cp.result_dir) gdrive.delete_files(files) self.alert.add_msg("map exported", "success") return self
def test_is_task(self): # create an output alert alert = sw.Alert() task = self._create_fake_task() # wait for the task to finish gee.wait_for_completion(self.DESCRIPTION, alert) # check if it exist res = gee.is_task(self.DESCRIPTION) self.assertNotEqual(res, None) # delete the asset ee.data.deleteAsset(self.ASSET_ID.format(self.DESCRIPTION)) return
def test_wait_for_completion(self): # create an output alert alert = sw.Alert() task = self._create_fake_task() res = gee.wait_for_completion(self.DESCRIPTION, alert) self.assertEqual(res, 'COMPLETED') self.assertEqual(alert.type, 'success') self.assertEqual(ms.status.format('COMPLETED'), alert.children[1].children[0]) ee.data.deleteAsset(self.ASSET_ID.format(self.DESCRIPTION)) #check that an error is raised when trying to overwrite a existing asset description = 'france' task = self._create_fake_task(description, False) with self.assertRaises(Exception): res = gee.wait_for_completion(description, alert) return
def run_aoi_selection(file_input, file_name, country_selection, asset_name, drawing_method, widget_alert, list_method, drawn_feat): """ create an gee asset according to the user inputs Args: file_input (str): the file to retreive from the user folder. It must be a .shp file file_name (str): name of the aoi that will be used troughout the process country_selection (str): a country name in english available in LSIB 2017 asset_name (str): the assetId of a existing asset drawing_method (str): the name of the method selected to create the asset widget_alert (v.Alert): the widget used to display the process informations list_method ([str]): list of the method use to select an AOI drawn_feat (ee.FeatureCollection): the last drawn object on the map Returns: asset (str) : the AssetId of the AOI """ #go to the glad folder in gee assets folder = ee.data.getAssetRoots()[0]['id'] + '/' #check the drawing method if drawing_method == None: #not selected utils.displayIO(widget_alert, ms.NO_SELECTION, 'warning') asset = None elif drawing_method == list_method[0]: #use a country boundary if country_selection == None: utils.displayIO(widget_alert, ms.NO_COUNTRY, 'warning') asset = None return asset country_code = utils.create_FIPS_dic()[country_selection] asset_descripsion = ms.FILE_PATTERN.format(country_code) asset = folder + asset_descripsion #check asset existence if isAsset(asset_descripsion, folder): utils.displayIO(widget_alert, ms.ASSET_ALREADY_EXIST.format(asset), 'success') return asset country = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter( ee.Filter.eq('country_co', country_code)) #create and launch the task task_config = { 'collection': country, 'description': asset_descripsion, 'assetId': asset } task = ee.batch.Export.table.toAsset(**task_config) task.start() gee.wait_for_completion(asset_descripsion, widget_alert) utils.displayIO(widget_alert, ms.ASSET_CREATED.format(asset), 'success') elif drawing_method == list_method[1]: #draw a shape aoi = drawn_feat asset_name = ms.FILE_PATTERN.format( re.sub('[^a-zA-Z\d\-\_]', '_', file_name)) #check if something is drawn if drawn_feat == None: asset = None utils.displayIO(widget_alert, ms.NO_SHAPE, 'error') return asset #check asset existence if isAsset(asset_name, folder): asset = None utils.displayIO(widget_alert, ms.NAME_USED, 'error') return asset asset = folder + asset_name #create and launch the task task_config = { 'collection': aoi, 'description': asset_name, 'assetId': asset } task = ee.batch.Export.table.toAsset(**task_config) task.start() gee.wait_for_completion(asset_name, widget_alert) utils.displayIO(widget_alert, ms.ASSET_CREATED.format(asset), 'success') elif drawing_method == list_method[3]: #use GEE asset #verify that there is an asset if asset_name == '' or asset_name == None: asset = None utils.displayIO(widget_alert, ms.NO_ASSET, 'error') else: asset = asset_name utils.displayIO(widget_alert, ms.CHECK_IF_ASSET, 'info') elif drawing_method == list_method[2]: #upload file if not os.path.isfile(file_input): utils.displayIO(widget_alert, ms.ERROR_OCCURED, 'error') asset = None return asset ee_object = geemap.shp_to_ee(file_input) name = os.path.split(file_input)[1] asset_name = ms.FILE_PATTERN.format( re.sub('[^a-zA-Z\d\-\_]', '_', name)) #check asset's name if isAsset(asset_name, folder): asset = None utils.displayIO(widget_alert, ms.NAME_USED, 'error') else: asset = folder + asset_name #launch the task task_config = { 'collection': ee_object, 'description': asset_name, 'assetId': asset } task = ee.batch.Export.table.toAsset(**task_config) task.start() gee.wait_for_completion(asset_name, widget_alert) utils.displayIO(widget_alert, ms.ASSET_CREATED.format(asset), 'success') return asset
def create_grid(self, widget, data, event): # toggle the btn widget.toggle_loading() # read the data aoi = self.aoi_io grid_size = float(self.size_select.v_model) grid_name = self.grid_name.v_model grid_batch = int(self.batch_size.v_model) #check the vars if not self.output.check_input(aoi.get_aoi_name(), ms.no_aoi): return widget.toggle_loading() if not self.output.check_input(grid_size, ms.no_size): return widget.toggle_loading() if not self.output.check_input(grid_batch, ms.no_size): return widget.toggle_loading() if not self.output.check_input(grid_name, ms.no_name): return widget.toggle_loading() try: grid = set_grid(aoi.get_aoi_ee(), grid_size, grid_batch, self.output) # get exportation parameters folder = ee.data.getAssetRoots()[0]['id'] asset = os.path.join(folder, grid_name) # export if not isAsset(grid_name, folder): task_config = { 'collection': grid, 'description': grid_name, 'assetId': asset } task = ee.batch.Export.table.toAsset(**task_config) task.start() gee.wait_for_completion(grid_name, self.output) self.assetId = asset # remove the preview square from the map for layer in self.m.layers: if layer.name == 'preview square size': self.m.remove_layer(layer) # display the asset on the map self.m.addLayer(ee.FeatureCollection(asset), {'color': v.theme.themes.dark.accent}, 'grid') display_asset(self.output, asset) except Exception as e: self.output.add_live_msg(str(e), 'error') # toggle the loading widget.toggle_loading() return