|
#!/usr/bin/env python3 |
|
# |
|
# script: EffMeshSize |
|
|
|
import os |
|
import grass.script as gscript |
|
|
|
TMP_MAPS = [] |
|
|
|
def cleanup(): |
|
|
|
# effacer couches temporaires |
|
gscript.run_command('g.remove', flags='f', type='raster', name=TMP_MAPS) |
|
|
|
def main(): |
|
|
|
# donnees en entree : vecteur grille + raster OCS + colonne id grille |
|
v_grid = 'grid_L93@PERMANENT' |
|
r_ocs = 'OCS_2018_CESBIO@PERMANENT' |
|
nom_colonne_id = 'id_int' |
|
|
|
# codes zone naturelle |
|
zones_nat = [13,18,19] |
|
|
|
# optionnel : couche vecteur masque (delimite zone a traiter) |
|
v_mask = 'emprise_5km@PERMANENT' |
|
|
|
# donnees en sortie (temporaire) |
|
tmp_rast_grid = 'grid_rast_' |
|
tmp_rast_mask = 'emprise_rast_' |
|
tmp_rast_grid_mask = 'grid_rast_mask_' |
|
tmp_rast_zonesnat = 'ocs_znat_' |
|
tmp_rast_patches = 'ocs_znat_patches_' |
|
# tmp_rast_grid = 'grid_rast_' + str(os.getpid()) |
|
# tmp_rast_mask = 'emprise_rast_' + str(os.getpid()) |
|
# tmp_rast_grid_mask = 'grid_rast_mask_' + str(os.getpid()) |
|
# tmp_rast_zonesnat = 'ocs_znat_' + str(os.getpid()) |
|
# tmp_rast_patches = 'ocs_znat_patches_' + str(os.getpid()) |
|
TMP_MAPS = [tmp_rast_grid, tmp_rast_mask, tmp_rast_zonesnat] |
|
|
|
# definir region temporaire |
|
gscript.run_command('g.region', vector=v_mask, align=r_ocs, flags='ps') |
|
|
|
# rasterisation grille |
|
gscript.run_command( |
|
'v.to.rast', input=v_grid, type='area', output=tmp_rast_grid, |
|
use='attr', attribute_column=nom_colonne_id) |
|
|
|
# rasterisation emprise |
|
if (v_mask != None): |
|
gscript.run_command( |
|
'v.to.rast', input=v_mask, type='area', output=tmp_rast_mask, use='val', value=1) |
|
# application du masque sur le raster grille |
|
formule_calc = tmp_rast_grid_mask + ' = if(' + tmp_rast_mask + '==1, ' + tmp_rast_grid + ', null())' |
|
gscript.mapcalc(formule_calc) |
|
# critere additionnel masque pour masquer OCS |
|
crit_mask = ' && ' + tmp_rast_mask + '==1' |
|
else: |
|
gscript.run_command('g.copy', raster=[tmp_rast_grid, tmp_rast_grid_mask]) |
|
crit_mask = '' |
|
|
|
# creer raster zones naturelles avec mapcalc |
|
# exemple de formule : |
|
# "ocs_znat = if((OCS_2018_CESBIO == 13 || OCS_2018_CESBIO == 18 || OCS_2018_CESBIO == 19) && emprise_rast==1, 1, null())" |
|
liste_crit = [r_ocs + ' == ' + str(x) for x in zones_nat] |
|
formule_calc = tmp_rast_zonesnat + ' = if((' + (' || '.join(liste_crit)) + ')' + crit_mask + ', 1, null())' |
|
gscript.mapcalc(formule_calc) |
|
gscript.run_command('r.clump', input=tmp_rast_zonesnat, output=tmp_rast_patches, flags='d') |
|
cleanup() |
|
|
|
if __name__ == '__main__': |
|
gscript.use_temp_region() |
|
main() |