Skip to content
Snippets Groups Projects
Commit 95c93b05 authored by pascal.mouquet_ird.fr's avatar pascal.mouquet_ird.fr
Browse files

code optimization

parent 3bcff6e9
No related branches found
No related tags found
No related merge requests found
Pipeline #19 canceled
......@@ -159,22 +159,26 @@ def get_processed_indices_vect(
#~ mndwi_index = Library().mndwi
#~ indices_index = set().union(ndvi_index, ndwigao_index, ndwimcf_index, mndwi_index)
indices_index = {t for c in (getattr(Library(), toto) for toto in ['l2a', 'ndvi', 'ndwigao', 'ndwimcf', 'mndwi']) for t in c}
total_index = {t for c in (getattr(Library(), toto) for toto in ['l1c', 'l2a', 'ndvi', 'ndwigao', 'ndwimcf', 'mndwi']) for t in c}
lib = Library()
indices_index = {t for c in (getattr(lib, toto) for toto in ['l2a', 'ndvi', 'ndwigao', 'ndwimcf', 'mndwi']) for t in c}
total_index = {t for c in (getattr(lib, toto) for toto in ['l1c', 'l2a', 'ndvi', 'ndwigao', 'ndwimcf', 'mndwi']) for t in c}
complement_index = total_index - indices_index
tuile_count = {}
tile_count = {}
for key in total_index:
tuile_count[key]={"lc1": 0, "l2a": 0, 'ndvi': 0, 'ndwigao': 0, 'ndwimcf': 0, 'mndwi': 0}
for p in ['l1c', 'l2a']:
for key in getattr(Library(), p):
tuile_count[key][p] = len(getattr(Tile(key), p))
for p in ['ndvi', 'ndwigao', 'ndwimcf', 'mndwi']:
for key in getattr(Library(), p):
tuile_count[key][p] = len(getattr(Tile(key), p).masks)
logger.info(key)
tile_count[key]={"lc1": 0, "l2a": 0, 'ndvi': 0, 'ndwigao': 0, 'ndwimcf': 0, 'mndwi': 0}
tile = Tile(key)
for p in ['l1c', 'l2a']:
tile_count[key][p] = len(getattr(tile, p))
for p in ['ndvi', 'ndwigao', 'ndwimcf', 'mndwi']:
try:
tile_count[key][p] = len(getattr(tile, p).masks)
except:
pass
# pour supprimer les tuiles ayant tous les indices à 0
indices_index_nonull = {k: v for k, v in tuile_count.items() if sum(v[indice] for indice in [z for z in v.keys() if z in ['ndvi', 'ndwigao', 'ndwimcf', 'mndwi']])}
indices_index_nonull = {k: v for k, v in tile_count.items() if sum(v[indice] for indice in [z for z in v.keys() if z in ['ndvi', 'ndwigao', 'ndwimcf', 'mndwi']])}
# Couche des indices
query_str = 'or '.join(['"{}" = "{}"'.format("Name", idx) for idx in indices_index_nonull.keys()])
......@@ -191,55 +195,56 @@ def get_processed_indices_vect(
field_name = ogr.FieldDefn(name, ogr.OFTString)
field_name.SetWidth(10)
out_layer.CreateField(field_name)
def fill_fields(layer):
for feat in layer:
tile = feat.GetField("Name")
tile_name = feat.GetField("Name")
tile = Tile(tile_name)
feat.SetField("L1C", tuile_count[tile]['l1c'])
feat.SetField("L1C", tile_count[tile_name]['l1c'])
try:
feat.SetField("L1C_F", Tile(tile).l1c.first.date.strftime("%d/%m/%Y"))
feat.SetField("L1C_L", Tile(tile).l1c.last.date.strftime("%d/%m/%Y"))
feat.SetField("L1C_F", tile.l1c.first.date.strftime("%d/%m/%Y"))
feat.SetField("L1C_L", tile.l1c.last.date.strftime("%d/%m/%Y"))
except:
feat.SetField("L1C_F", 0)
feat.SetField("L1C_L", 0)
feat.SetField("L2A", tuile_count[tile]['l2a'])
feat.SetField("L2A", tile_count[tile_name]['l2a'])
try:
feat.SetField("L2A_F", Tile(tile).l2a.first.date.strftime("%d/%m/%Y"))
feat.SetField("L2A_L", Tile(tile).l2a.last.date.strftime("%d/%m/%Y"))
feat.SetField("L2A_F", tile.l2a.first.date.strftime("%d/%m/%Y"))
feat.SetField("L2A_L", tile.l2a.last.date.strftime("%d/%m/%Y"))
except:
feat.SetField("L2A_F", 0)
feat.SetField("L2A_L", 0)
feat.SetField("NDVI", tuile_count[tile]['ndvi'])
feat.SetField("NDVI", tile_count[tile_name]['ndvi'])
try:
feat.SetField("NDVI_F", Tile(tile).ndvi.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("NDVI_L", Tile(tile).ndvi.masks.last.date.strftime("%d/%m/%Y"))
feat.SetField("NDVI_F", tile.ndvi.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("NDVI_L", tile.ndvi.masks.last.date.strftime("%d/%m/%Y"))
except:
feat.SetField("NDVI_F", 0)
feat.SetField("NDVI_L", 0)
feat.SetField("NDWIGAO", tuile_count[tile]['ndwigao'])
feat.SetField("NDWIGAO", tile_count[tile_name]['ndwigao'])
try:
feat.SetField("NDWIGAO_F", Tile(tile).ndwigao.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("NDWIGAO_L", Tile(tile).ndwigao.masks.last.date.strftime("%d/%m/%Y"))
feat.SetField("NDWIGAO_F", tile.ndwigao.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("NDWIGAO_L", tile.ndwigao.masks.last.date.strftime("%d/%m/%Y"))
except:
feat.SetField("NDWIGAO_F", 0)
feat.SetField("NDWIGAO_L", 0)
feat.SetField("NDWIMCF", tuile_count[tile]['ndwimcf'])
feat.SetField("NDWIMCF", tile_count[tile_name]['ndwimcf'])
try:
feat.SetField("NDWIMCF_F", Tile(tile).ndwimcf.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("NDWIMCF_L", Tile(tile).ndwimcf.masks.last.date.strftime("%d/%m/%Y"))
feat.SetField("NDWIMCF_F", tile.ndwimcf.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("NDWIMCF_L", tile.ndwimcf.masks.last.date.strftime("%d/%m/%Y"))
except:
feat.SetField("NDWIMCF_F", 0)
feat.SetField("NDWIMCF_L", 0)
feat.SetField("MNDWI", tuile_count[tile]['mndwi'])
feat.SetField("MNDWI", tile_count[tile_name]['mndwi'])
try:
feat.SetField("MNDWI_F", Tile(tile).mndwi.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("MNDWI_L", Tile(tile).mndwi.masks.last.date.strftime("%d/%m/%Y"))
feat.SetField("MNDWI_F", tile.mndwi.masks.first.date.strftime("%d/%m/%Y"))
feat.SetField("MNDWI_L", tile.mndwi.masks.last.date.strftime("%d/%m/%Y"))
except:
feat.SetField("MNDWI_F", 0)
feat.SetField("MNDWI_L", 0)
......@@ -250,7 +255,7 @@ def get_processed_indices_vect(
del out_layer, out_ds
# Couche complète
query_str = 'or '.join(['"{}" = "{}"'.format("Name", idx) for idx in tuile_count.keys()])
query_str = 'or '.join(['"{}" = "{}"'.format("Name", idx) for idx in tile_count.keys()])
input_layer_lyr.SetAttributeFilter(query_str)
driver = ogr.GetDriverByName('ESRI Shapefile')
out_ds = driver.CreateDataSource(out_shapefile_total)
......@@ -269,7 +274,7 @@ def get_processed_indices_vect(
del out_layer, out_ds
# Couche complémentaire
query_str = 'or '.join(['"{}" = "{}"'.format("Name", idx) for idx in (tuile_count.keys()-indices_index_nonull.keys())])
query_str = 'or '.join(['"{}" = "{}"'.format("Name", idx) for idx in (tile_count.keys()-indices_index_nonull.keys())])
input_layer_lyr.SetAttributeFilter(query_str)
driver = ogr.GetDriverByName('ESRI Shapefile')
out_ds = driver.CreateDataSource(out_shapefile_complement)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment