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 get_clipped_asset(params, asset_activation): ''' Second step is to download the item to the desired path ''' gaia = Gaia() filepath = params['filepath'] print('filepath ', str(filepath)) success = gaia.get_clipped_asset(asset_activation, filepath) if success == 0: get_clipped_asset.apply_async((params, asset_activation), countdown=20) return False elif success == 1: if params['pixels_usability_test_pass'] == 0: udm_filename = glob.glob(filepath + '*udm_clip.tif')[0] athena = Athena() is_cloudy_udm = athena.is_cloudy_udm(udm_filename) if is_cloudy_udm > 0: # remove that asset and end the process for this asset shutil.rmtree(filepath) print('Fail cloud test ') return # if udm is > 2 % cloudy then skip processing else: # remove the current content <croped UDM>and download the remaining asset # and then Start the analysis Process print('Successfully pass the Cloudy Test') params['pixels_usability_test_pass'] = 1 params['pixels_ratio'] = is_cloudy_udm # Gather directory contents contents = [os.path.join(filepath, i) for i in os.listdir(filepath)] # Iterate and remove each item in the appropriate manner [os.remove(i) if os.path.isfile(i) or os.path.islink(i) else shutil.rmtree(i) for i in contents] # Update the params['asset_type'] to analytic params['asset_type'] = 'analytic' activate_clipped_asset.apply_async((params,), countdown=1) elif params['pixels_usability_test_pass'] == 1: generate_analytic_assets.delay(params) return True else: return False
def populate_alert_table(self, params): ''' if percentage of '1' pixels is more than 10% of total number of valid pixels then ==> Create Alert in Alert Model ''' try: # pixels_ratio = params['pixels_ratio'] # print('pixels_ratio', str(pixels_ratio)) filepath = params['filepath'] udm_filename = glob.glob(filepath + '*udm_clip.tif')[0] weed_alert_array = params['weed_alert_array'] athena = Athena() ''' Create an Weed Alert in the Alerts data base if percentage of '1' pixels is more than 10% of total number of valid pixels (as per the udm) ''' is_qualify_create_alert = athena.qualify_create_alert( udm_filename, weed_alert_array) if is_qualify_create_alert: # fill this parameters geojson_file_path = params['geojson_file'] type = params['type'] aoi = Aoi.objects.get(id=params['aoi_id']) plot = Plots.objects.get(id=aoi.plot.id) area = aoi.coordinates status = params['status'] # Create into Alert table Alert.objects.create(alert_date=date.today(), file_path=geojson_file_path, type=type, plot=plot, status=status, notes='', area=area) return 1 except: return 'No path for asset '
def __init__(self): self.gaia = Gaia() self.athena = Athena() self.theia = Theia() self.fs = FileSystemStorage()
def generate_analytic_assets(params): ''' Third step is to do calculation, generate the asset, store it S3 and our DB ''' gaia = Gaia() theia = Theia() athena = Athena() date = params['date'] aoi_id = params['aoi_id'] filepath = params['filepath'] planet_item_id = params['planet_item_id'] aoi = Aoi.objects.get_by_id(aoi_id) udm_filename = glob.glob(filepath + '*udm_clip.tif')[0] raw_filename = glob.glob(filepath + '*_AnalyticMS_clip.tif')[0] metadata = glob.glob(filepath + '*_AnalyticMS_metadata_clip.xml')[0] udm, err = gaia.read_band(udm_filename) metadata_xml, err = gaia.parse_xml(metadata) band_file, err = gaia.read_band(raw_filename) clip_udm = athena.create_unusable_clip_mask(band_file, udm) is_hazy = athena.is_hazy(band_file, metadata_xml, clip_udm) ndvi, err = athena.calculate_NDVI(band_file, metadata_xml) ndwi, err = athena.calculate_NDWI(band_file, metadata_xml) bai, err = athena.calculate_BAI(band_file, metadata_xml) rvi, err = athena.calculate_RVI(band_file, metadata_xml) gndvi, err = athena.calculate_GNDVI(band_file, metadata_xml) msavi, err = athena.calculate_MSAVI(band_file, metadata_xml) dirt, err = athena.calculate_DIRT(band_file, metadata_xml) evi, err = athena.calculate_EVI(band_file, metadata_xml) usability_score = athena.calculate_usability_score(udm) ndvi = np.where(clip_udm, -1, ndvi) ndwi = np.where(clip_udm, -1, ndwi) bai = np.where(clip_udm, -1, bai) rvi = np.where(clip_udm, -1, rvi) gndvi = np.where(clip_udm, -1, gndvi) msavi = np.where(clip_udm, -1, msavi) dirt = np.where(clip_udm, -1, dirt) evi = np.where(clip_udm, -1, evi) mean_ndvi = np.ma.array(data = ndvi, mask = clip_udm).mean() mean_ndwi = np.ma.array(data = ndwi, mask = clip_udm).mean() mean_bai = np.ma.array(data = bai, mask = clip_udm).mean() mean_rvi = np.ma.array(data = rvi, mask = clip_udm).mean() mean_gndvi = np.ma.array(data = gndvi, mask = clip_udm).mean() mean_msavi = np.ma.array(data = msavi, mask = clip_udm).mean() mean_dirt = np.ma.array(data = dirt, mask = clip_udm).mean() mean_evi = np.ma.array(data = evi, mask = clip_udm).mean() print('-----') print(date) print("ndvi : " + str(mean_ndvi)) print("ndwi : " + str(mean_ndwi)) print("bai : " + str(mean_bai)) print("rvi : " + str(mean_rvi)) print("gndvi : " + str(mean_gndvi)) print("msavi : " + str(mean_msavi)) print("dirt : " + str(mean_dirt)) print("evi : " + str(mean_evi)) print('-----') bands_arr = (ndvi, ndwi, bai, rvi, gndvi, msavi, dirt, evi) file_profile = band_file.profile file_profile['count'] = len(bands_arr) file_profile['dtype'] = 'float64' gaia.write_band_to_tiff({ 'bands' : bands_arr, 'profile' : file_profile, 'filepath' : filepath, 'filename' : 'output-analytics.tif' }) theia.create_cmap_asset(ndvi, filepath, 'output-cmap-ndvi') theia.create_cmap_asset(ndwi, filepath, 'output-cmap-ndwi', cmap_name='NDWI') theia.create_cmap_asset(bai, filepath, 'output-cmap-bai') theia.create_cmap_asset(rvi, filepath, 'output-cmap-rvi', cmap_name='RVI', vmin=-3, vmax=3) theia.create_cmap_asset(gndvi, filepath, 'output-cmap-gndvi') theia.create_cmap_asset(msavi, filepath, 'output-cmap-msavi', cmap_name='MSAVI') theia.create_cmap_asset(dirt, filepath, 'output-cmap-dirt', cmap_name='DIRT') theia.create_cmap_asset(evi, filepath, 'output-cmap-evi') ## Dump to csv; pls remove if not needed. The size is huge np.savetxt(filepath + "ndvi_dump.csv", ndvi, delimiter=",") np.savetxt(filepath + "ndwi_dump.csv", ndwi, delimiter=",") np.savetxt(filepath + "bai_dump.csv", bai, delimiter=",") np.savetxt(filepath + "rvi_dump.csv", rvi, delimiter=",") np.savetxt(filepath + "gndvi_dump.csv", gndvi, delimiter=",") np.savetxt(filepath + "msavi_dump.csv", msavi, delimiter=",") np.savetxt(filepath + "dirt_dump.csv", dirt, delimiter=",") np.savetxt(filepath + "evi_dump.csv", evi, delimiter=",") ## storage_url = gaia.store_asset_to_s3(filepath) asset = Asset.objects.create_asset(aoi, { 'type': 'ANALYTIC', 'date': date, 'storage_url': storage_url, 'planet_item_id': planet_item_id, 'usability_score': usability_score, 'note': json.dumps({ 'mean_ndvi': mean_ndvi, 'mean_ndwi': mean_ndwi, 'mean_bai': mean_bai, 'mean_rvi': mean_rvi, 'mean_gndvi': mean_gndvi, 'mean_msavi': mean_msavi, 'haze': is_hazy }) }) return asset.id