Python_scripts/createMNT_intersects.py

84 lines
2.2 KiB
Python

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from zipfile import ZipFile
import geopandas as gpd
from os import system, chdir
from sqlalchemy.engine import URL
from sqlalchemy import create_engine
###########
# Parametres
# path to mnt
path = '/home/colas/Craig/CEN_38/Altimétrie/IGN - RGE Alti 1M/'
dalle = '_dallage_rgealti_1m.gpkg'
# path output
path_tmp = '/home/colas/Documents/tmp/'
out = path_tmp + 'etandLong_MNT1m.tif'
# param geometrie
read = 'file' # 'file' or 'bdd'
# if read == 'file'
# file_geom = 'cheminversmageometrie/monfichier'
file_geom = "/media/colas/SRV/FICHIERS/TRANSFERTS-EQUIPE/CG/Etang des longs - zone d'étude/Zone d'étude.shp"
# if read == 'bdd'
sql_select = "SELECT code_site, geom FROM sites.c_sites_zonages WHERE code_site='GOUR' AND type_zonage = 'ZO'"
user = 'cen_admin'
pwd = '#CEN38@venir'
adr = '192.168.0.189'
base = 'bd-cen-38'
schema = None
table = None
url = URL.create('postgresql+psycopg2',
username=user,
password=pwd,
host=adr,
database=base,
)
con = create_engine(url)
if __name__ == "__main__":
data = gpd.read_file(path+dalle)
data = data.set_geometry('geometry',crs=2154)
if read == 'file' :
df = gpd.read_file(file_geom)
if read == 'bdd' :
df = gpd.read_postgis(sql=sql_select, con=con)
data.intersects(df.unary_union)
data = data[data.intersects(df.unary_union)]
if all(data.location.str.contains('/')):
data['loc_zip'] = [ x[1] for x in data.location.str.split('/') ]
else :
data['loc_zip'] = data.location
# Copie des zip en local
chdir(path)
print('INIT files copy : %s'%data.shape[0])
for file in data.location:
op = 'cp %s %s' % (file,path_tmp)
system(op)
# Identification des dalles MNT dans les zip
chdir(path_tmp)
data['mnt'] = None
for file in data.loc_zip:
zip = ZipFile(file).namelist()
data.loc[data.loc_zip==file,'mnt'] = [z for z in zip if 'MNT' in z][0]
data['loc_mnt'] = '/vsizip/' + data.loc_zip + '/' + data['mnt']
# Merge des dalles
op = 'gdal_merge.py -o {out_mnt} -of GTIFF -n -99999 -a_nodata 0 {in_mnt}'.format(out_mnt=out,in_mnt=' '.join(data['loc_mnt']))
system(op)
# Suppression des zip
op = 'rm -R *.zip'
system(op)