class VegetationIndex: ''' Product level interface that communicate with models and core modules Responsible for product logic to construct vegetation indexes (NDVI, NDWI, BAI, etc) from AOI ''' def __init__(self): self.gaia = Gaia() self.athena = Athena() self.theia = Theia() self.fs = FileSystemStorage() def manage_index_creation(self, params): ''' Main logic of vegetation index asset creation. Given the desired AOI and date to analyze, creating parallel tasks to generate asset. Params Input: 1. aoi_id 2. start_date 3. end_date 4. cloud_cover threshold Steps: 1. Simplify AOI 2. Find available planet's item for given input params 3. Queue tasks to activate planet's item ''' try: aoi_id = params['aoi_id'] start_date = str(params['start_date']) + 'T00:00:00.000Z' end_date = str(params['end_date']) + 'T00:00:00.000Z' cloud_cover = params.get('cloud_cover', 0.1) except Exception as e: return (None, 'AOI, Start Date, & Planet Item ID must be given') aoi = Aoi.objects.get_by_id(aoi_id) if not aoi: return (None, 'AOI not found.') elif len(aoi.coordinates) > 110: print('simplyfying coordinates for AOI: ' + str(aoi.id)) coordinates = aoi.coordinates while len(coordinates) > 110: coordinates = self.gaia.simplify_coordinates(coordinates) aoi.coordinates = coordinates aoi.save() filter_params = { 'date_filter': { 'start': start_date, 'end': end_date }, 'cloud_threshold': cloud_cover } available_planet_assets = self.gaia.find_available_asset([aoi.coordinates], filter_params) for ass in available_planet_assets.get('features'): properties = ass.get('properties') date = properties.get('acquired').split('T')[0] payload = { 'aoi_id': aoi_id, 'planet_item_id': ass.get('id'), 'date': date } activate_clipped_asset.apply_async((payload,), countdown=1) print('Queued :' + date + ' -> ' + ass.get('id'))
class VegetationIndex: ''' Product level interface that communicate with models and core modules Responsible for product logic to construct vegetation indexes (NDVI, NDWI, BAI, etc) from AOI ''' def __init__(self): self.gaia = Gaia() self.athena = Athena() self.theia = Theia() self.fs = FileSystemStorage() self.PLANET_API_KEY = getattr(settings, "PLANET_API_KEY", None) def manage_index_creation(self, params): ''' Main logic of vegetation index asset creation. Given the desired AOI and date to analyze, creating parallel tasks to generate asset. Params Input: 1. aoi_id 2. start_date 3. end_date 4. cloud_cover threshold Steps: 1. Simplify AOI 2. Find available planet's item for given input params 3. Queue tasks to activate planet's item ''' try: aoi_id = params['aoi_id'] start_date = str(params['start_date']) + 'T00:00:00.000Z' end_date = str(params['end_date']) + 'T00:00:00.000Z' # API search on the relevant coordinates, but allow 50% cloud cover cloud_cover = params.get('cloud_cover', 0.5) except Exception as e: return (None, 'AOI, Start Date, & Planet Item ID must be given') aoi = Aoi.objects.get_by_id(aoi_id) if not aoi: return (None, 'AOI not found.') elif len(aoi.coordinates) > 110: print('simplyfying coordinates for AOI: ' + str(aoi.id)) coordinates = aoi.coordinates while len(coordinates) > 110: coordinates = self.gaia.simplify_coordinates(coordinates) aoi.coordinates = coordinates aoi.save() filter_params = { 'date_filter': { 'start': start_date, 'end': end_date }, 'cloud_threshold': cloud_cover } available_planet_assets = self.gaia.find_available_asset([aoi.coordinates], filter_params, item_types='PSScene4Band') if available_planet_assets.get('features'): for ass in available_planet_assets.get('features'): properties = ass.get('properties') date = properties.get('acquired').split('T')[0] print('ass.get', ass.get('id')) payload = { 'aoi_id': aoi_id, 'planet_item_id': ass.get('id'), 'date': date, 'PLANET_API_KEY': self.PLANET_API_KEY, 'item_type': "PSScene4Band", 'asset_type': "udm", 'pixels_usability_test_pass': 0 } activate_clipped_asset.apply_async((payload,), countdown=1) print('Queued :' + date + ' -> ' + ass.get('id')) else: print('No available_planet_assets!') def check_update_asset(self): today = datetime.now().date() start_date = str((today - timedelta(weeks=1))) + 'T00:00:00.000Z' end_date = str(today) + 'T00:00:00.000Z' # API search on the relevant coordinates, but allow 50% cloud cover cloud_cover = 0.5 filter_params = { 'date_filter': { 'start': start_date, 'end': end_date }, 'cloud_threshold': cloud_cover } # since A represents 'Active' Aoi active_aois = Aoi.objects.filter(status='D') for active_aoi in active_aois: aoi_id = active_aoi.id available_planet_assets = self.gaia.find_available_asset([active_aoi.coordinates], filter_params, item_types='PSScene4Band') if available_planet_assets.get('features'): already_download_assets = Asset.objects.filter(aoi__plot_id=active_aoi.id) for ass in available_planet_assets.get('features'): if already_download_assets: for already_download_asset in already_download_assets: ''' here check if the already_download_asset.planet_item_id is not equal to the newly searched item_id then download that asset and process as regular flow otherwise download that asset and process ''' if ass.get('id') != already_download_asset.planet_item_id: self.start_download_updated_asset(aoi_id, ass) else: self.start_download_updated_asset(aoi_id, ass) def start_download_updated_asset(self, aoi_id, ass): properties = ass.get('properties') date = properties.get('acquired').split('T')[0] print('ass.get', ass.get('id')) payload = { 'aoi_id': aoi_id, 'planet_item_id': ass.get('id'), 'date': date, 'PLANET_API_KEY': self.PLANET_API_KEY, 'item_type': "PSScene4Band", 'asset_type': "udm", 'pixels_usability_test_pass': 0 } activate_clipped_asset.apply_async((payload,), countdown=1) print('Queued :' + date + ' -> ' + ass.get('id'))