mirror of
https://github.com/Alex38Lyon/Synthese-PSM_LARRA.git
synced 2026-06-01 22:00:53 +00:00
pyThtoQgis
This commit is contained in:
@@ -0,0 +1,453 @@
|
||||
######!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2020 Xavier Robert <xavier.robert@ird.fr>
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
|
||||
"""
|
||||
#############################################################
|
||||
# #
|
||||
# Script to automatize data extraction of Therion databases #
|
||||
# #
|
||||
# By Xavier Robert #
|
||||
# Grenoble, october 2022 #
|
||||
# #
|
||||
#############################################################
|
||||
|
||||
Written by Xavier Robert, octobert 2022
|
||||
|
||||
xavier.robert@ird.fr
|
||||
|
||||
"""
|
||||
|
||||
# Do divisions with Reals, not with integers
|
||||
# Must be at the beginning of the file
|
||||
from __future__ import division
|
||||
|
||||
# Import Python modules
|
||||
#import numpy as np
|
||||
import fiona
|
||||
import shapely
|
||||
from shapely.geometry import Polygon, LineString
|
||||
import geopandas as gpd
|
||||
import pandas as pd
|
||||
import sys, os, copy, shutil
|
||||
#from functools import wraps
|
||||
from alive_progress import alive_bar # https://github.com/rsalmei/alive-progress
|
||||
|
||||
###### TO DO #####
|
||||
|
||||
# -
|
||||
|
||||
##### End TO DO #####
|
||||
|
||||
#################################################################################################
|
||||
#################################################################################################
|
||||
|
||||
#def validate(func):
|
||||
# """
|
||||
# Function to validate areas topology.
|
||||
# From https://shapely.readthedocs.io/en/latest/manual.html
|
||||
|
||||
# Args:
|
||||
# func (_type_): _description_
|
||||
|
||||
# Raises:
|
||||
# TopologicalError: Error of topology
|
||||
# - area does not close
|
||||
# - inner ring
|
||||
# - boundaries intersects
|
||||
|
||||
|
||||
# Returns:
|
||||
# _type_: _description_
|
||||
# """
|
||||
# @wraps(func)
|
||||
# def wrapper(*args, **kwargs):
|
||||
# ob = func(*args, **kwargs)
|
||||
# if not ob.is_valid:
|
||||
# raise TopologicalError(
|
||||
# "Given arguments do not determine a valid geometric object")
|
||||
# return ob
|
||||
# return wrapper
|
||||
|
||||
|
||||
def validate(inputfile, rec):
|
||||
|
||||
rec2 = rec
|
||||
#print(rec['geometry']['coordinates'][0]) # il y a visiblement un soucis avec le nombre de []
|
||||
|
||||
if not Polygon(rec['geometry']['coordinates'][0]).is_valid:
|
||||
print('Problem in %s geometry' %(inputfile))
|
||||
print('%s is not a valid geometric object' %(rec['properties']['_ID']))
|
||||
raise TopologicalError('\033[91mERROR:\033[00m Correction does not work...\n%s is not a valid geometric object\n\t The error is: %s' %(str(rec['properties']['_ID']), shapely.validation.explain_validity(rec)))
|
||||
#print('We try to correct it')
|
||||
#rec2b = shapely.validation.make_valid(Polygon(rec['geometry']['coordinates'][0]))
|
||||
# Check à améliorer, il faut que ce soit un Polygon, et non un MultiPolygon...
|
||||
#if not rec2b.is_valid:
|
||||
# raise TopologicalError('ERROR: Correction failed...\n%s is not a valid geometric object\n\t The error is: %s' %(str(rec['properties']['_ID']), shapely.validation.explain_validity(rec)))
|
||||
#else:
|
||||
# rec2['geometry']['coordinates'][0] = list(rec2b.exterior.coords)
|
||||
|
||||
# Find where there is the error if possible
|
||||
#Diagnostics
|
||||
#validation.explain_validity(ob):
|
||||
#Returns a string explaining the validity or invalidity of the object.
|
||||
#The messages may or may not have a representation of a problem point that can be parsed out.
|
||||
#coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]
|
||||
#p = Polygon(coords)
|
||||
#from shapely.validation import explain_validity
|
||||
#shapely.validation.explain_validity(p)
|
||||
#'Ring Self-intersection[1 1]'
|
||||
#shapely.validation.make_valid(ob)
|
||||
#Returns a valid representation of the geometry, if it is invalid. If it is valid, the input geometry will be returned.
|
||||
|
||||
#In many cases, in order to create a valid geometry, the input geometry must be split into multiple parts or multiple geometries. If the geometry must be split into multiple parts of the same geometry type, then a multi-part geometry (e.g. a MultiPolygon) will be returned. if the geometry must be split into multiple parts of different types, then a GeometryCollection will be returned.
|
||||
#For example, this operation on a geometry with a bow-tie structure:
|
||||
#from shapely.validation import make_valid
|
||||
#coords = [(0, 0), (0, 2), (1, 1), (2, 2), (2, 0), (1, 1), (0, 0)]
|
||||
#p = Polygon(coords)
|
||||
#make_valid(p)
|
||||
#<MULTIPOLYGON (((1 1, 0 0, 0 2, 1 1)), ((2 0, 1 1, 2 2, 2 0)))>
|
||||
#Yields a MultiPolygon with two parts, and sometimes area + line:
|
||||
|
||||
return rec2
|
||||
|
||||
#################################################################################################
|
||||
def cutareas(pathshp, outlines, outputspath):
|
||||
"""
|
||||
Function to cut shapefiles areas with the outline to only keep the lines inside the outline
|
||||
|
||||
Args:
|
||||
pathshp (str) : path where are stored output shp from Therion
|
||||
outlines (geopandas obj): the outline shapefile
|
||||
outputspath (str) : path where to copy the gpkg files
|
||||
"""
|
||||
|
||||
print('Working with areas...')
|
||||
# 2- Validate the outline and Areas shapefile
|
||||
#for rec in outlines:
|
||||
# rec2 = validate('outline2d.shp', rec)
|
||||
# # update correction --> To do ?
|
||||
# #if rec2 != rec:
|
||||
#for rec in areas:
|
||||
# rec2 = validate('areas2d.shp', rec)
|
||||
# # update correction
|
||||
# #if rec2 != rec:
|
||||
|
||||
# Read the Line Shapefile
|
||||
areas = gpd.read_file(pathshp + 'areas2d.shp', driver = 'ESRI shapefile')
|
||||
|
||||
# Extract the intersections between outlines and lines
|
||||
# be careful, for this operation, geopandas needs to work with rtree and not pygeos
|
||||
# --> uninstall pygeos and install rtree
|
||||
try:
|
||||
areasIN = areas.overlay(outlines, how = 'intersection')
|
||||
except:
|
||||
print('ERROR: 1) uninstall pygeos and install rtree\n\t2) check your polygons validity')
|
||||
import rtree
|
||||
print ('\tYou may check the validity of your polygons with the verify function in QGIS')
|
||||
areasIN = areas.overlay(outlines, how = 'intersection')
|
||||
|
||||
# Removes inner lines that have different id and scrap_id
|
||||
areasIN = areasIN[areasIN['_SCRAP_ID'] == areasIN ['_ID']]
|
||||
|
||||
# Save output
|
||||
#areasIN.to_file("areas2dMasekd.gpkg", driver = "GPKG", encoding = 'utf8')
|
||||
areasIN.to_file(outputspath + "areas2dMasekd.gpkg", driver = "GPKG")
|
||||
|
||||
return
|
||||
|
||||
#################################################################################################
|
||||
def cutLines(pathshp, outlines, outputspath):
|
||||
"""
|
||||
Function to cut shapefiles lines with the outline to only keep the lines inside the outline
|
||||
|
||||
Args:
|
||||
pathshp (str) : path where are stored output shp from Therion
|
||||
outlines (geopandas obj): the outline shapefile
|
||||
outputspath (str) : path where to copy the gpkg files
|
||||
"""
|
||||
|
||||
print('Working with lines...')
|
||||
# Read the Line Shapefile
|
||||
lines = gpd.read_file(pathshp + 'lines2d.shp', driver = 'ESRI shapefile')
|
||||
# Extract lines that are not masked by the outline
|
||||
linesOUT = pd.concat((lines[lines['_TYPE'] == 'centerline'],
|
||||
lines[lines['_TYPE'] == 'water_flow'],
|
||||
lines[lines['_TYPE'] == 'label'],
|
||||
lines[lines['_CLIP'] == 'off']),
|
||||
ignore_index=True)
|
||||
|
||||
# Extract lines will be masked by the outline
|
||||
linesIN = lines[lines['_CLIP'] != 'off']
|
||||
linesIN = linesIN[linesIN['_TYPE'] != 'centerline']
|
||||
linesIN = linesIN[linesIN['_TYPE'] != 'water_flow']
|
||||
linesIN = linesIN[linesIN['_TYPE'] != 'label']
|
||||
|
||||
# Extract the intersections between outlines and lines
|
||||
# be careful, for this operation, geopandas needs to work with rtree and not pygeos
|
||||
# --> uninstall pygeos and install rtree
|
||||
try:
|
||||
linesIN = linesIN.overlay(outlines, how = 'intersection', keep_geom_type=True)
|
||||
except:
|
||||
print('\033[91mERROR: 1\033[00m) uninstall pygeos and install rtree\n\t2) check your polygons validity')
|
||||
import rtree
|
||||
print ('\tYou may check the validity of your polygons with the verify function in QGIS')
|
||||
linesIN = linesIN.overlay(outlines, how = 'intersection', keep_geom_type=True)
|
||||
print('TEST')
|
||||
|
||||
# Removes inner lines that have different id and scrap_id
|
||||
linesIN = linesIN[linesIN['_SCRAP_ID'] == linesIN ['_ID']]
|
||||
|
||||
# Merge the IN and OUT database
|
||||
linesTOT = pd.concat((linesOUT, linesIN),
|
||||
ignore_index=True)
|
||||
|
||||
# Save output
|
||||
#linesTOT.to_file("lines2dMasekd.gpkg", driver="GPKG", encoding = 'utf8')
|
||||
linesTOT.to_file(outputspath + "lines2dMasekd.gpkg", driver="GPKG")
|
||||
|
||||
return
|
||||
|
||||
#################################################################################################
|
||||
def AddAltPoint(pathshp, outputspath):
|
||||
"""
|
||||
Function to add the altitude of the stations and entrances in the attribut table
|
||||
|
||||
Args:
|
||||
pathshp (str) : path where are stored output shp from Therion
|
||||
outputspath (str): path where to copy the gpkg files
|
||||
"""
|
||||
|
||||
|
||||
print('Working with points...')
|
||||
|
||||
# Definition des altitudes des entrées supérieures des réseaux à plusieurs entrées
|
||||
EntreeSupp = {'JB' : 2333, # Entrée C37
|
||||
'CP' : 2136, # Entrée CP16
|
||||
'LP9' : 2299, # Entrée LP9
|
||||
'CP6' : 2182, # Entrée CP53
|
||||
'CP62' : 1960, # Entrée CP62
|
||||
'A21' : 1797, # Entrée A21
|
||||
'Mirolda': 2330 # Entrée Jockers
|
||||
}
|
||||
# Définition des noms de réseau
|
||||
RNames = {'JB' : 'Gouffre Jean Bernard',
|
||||
'CP' : 'Réseau de la Combe aux Puaires',
|
||||
'LP9' : 'LP9 - CP39',
|
||||
'CP6' : 'CP6 - CP53',
|
||||
'CP62' : 'CP62 - CP63',
|
||||
'A21' : 'A21 -A24',
|
||||
'Mirolda': 'Réseau Lucien-Bouclier - Mirolda'
|
||||
}
|
||||
# Définition des noms de systèmes
|
||||
SNames = {'SynclinalJB' : 'Système du Jean-Bernard',
|
||||
'SystemeCP' : 'Système de la Combe aux Puaires',
|
||||
'SystemeAV' : 'Système des Avoudrues',
|
||||
'SystemeA21' : 'Système du A21',
|
||||
'SystemMirolda' : 'Système du Criou - Mirolda',
|
||||
'SystemeBossetan': 'Système de Bossetan',
|
||||
'sources' : 'Résurgences',
|
||||
'tuet' : 'Système du Tuet',
|
||||
'eauxfroides' : 'Système des Eaux Froides'
|
||||
}
|
||||
# Open the text file with the coordinates of the caves
|
||||
# This text file (Caves.txt) should be build with Therion compilation
|
||||
# and stored in the output's shapefiles folder
|
||||
# export cave-list -location on -o Outputs/SHP/Caves.txt
|
||||
f = open(pathshp + 'Caves.txt', 'r').readlines()
|
||||
|
||||
# Make a new shapefile instance
|
||||
with fiona.open(pathshp + 'points2d.shp', 'r') as inputshp:
|
||||
# Créer le nouveau schéma des shapefiles
|
||||
newschema = inputshp.schema
|
||||
newschema['properties']['_CAVE'] = 'str'
|
||||
newschema['properties']['_SYSTEM'] = 'str'
|
||||
newschema['properties']['_ALT'] = 'str:4'
|
||||
newschema['properties']['_DEPTH'] = 'float'
|
||||
newschema['properties']['_EASTING'] = 'float'
|
||||
newschema['properties']['_NORTHING'] = 'float'
|
||||
# Open the output shapefile
|
||||
#with fiona.open(inputfile[:-4] + 'Alt.shp', 'w', crs=inputshp.crs, driver='ESRI Shapefile', schema=newschema) as ouput:
|
||||
#with fiona.open('points2dAlt.gpkg', 'w', crs=inputshp.crs, driver='GPKG', schema=newschema, encoding = 'utf8') as ouput:
|
||||
with fiona.open(outputspath + 'points2dAlt.gpkg', 'w', crs=inputshp.crs, driver='GPKG', schema=newschema) as ouput:
|
||||
with alive_bar(len(inputshp), title = "\x1b[32;1m- Processing stations...\x1b[0m", length = 20) as bar:
|
||||
# do a loop on the stations
|
||||
for rec in inputshp:
|
||||
# Copy the schema from the input data
|
||||
g = rec
|
||||
g['properties']['_CAVE'] = ''
|
||||
g['properties']['_SYSTEM'] = ''
|
||||
g['properties']['_DEPTH'] = ''
|
||||
|
||||
# Add Alt, Easting, Northing
|
||||
g['properties']['_ALT'] = str(round(float(rec['geometry']['coordinates'][2])))
|
||||
g['properties']['_EASTING'] = float(rec['geometry']['coordinates'][0])
|
||||
g['properties']['_NORTHING'] = float(rec['geometry']['coordinates'][1])
|
||||
|
||||
if rec['properties']['_TYPE'] == 'station' and rec['properties']['_STSURVEY'] != None:
|
||||
# Find system
|
||||
system = rec['properties']['_STSURVEY'].split('.')[-2]
|
||||
g['properties']['_SYSTEM'] = SNames[system]
|
||||
|
||||
# Find Cave
|
||||
xxx = rec['properties']['_STSURVEY'].split('.')
|
||||
while len(xxx) < 4:
|
||||
xxx.append('junk')
|
||||
if 'trous' in xxx[0] or SNames[system] == 'Résurgences' or 'sources' in xxx[0]:
|
||||
g['properties']['_CAVE'] = rec['properties']['_STNAME']
|
||||
g['properties']['_DEPTH'] = 0
|
||||
|
||||
elif 'eauxfroides' in xxx[-3]:
|
||||
g['properties']['_CAVE'] = 'Résurgence des Eaux Froides'
|
||||
g['properties']['_DEPTH'] = 0
|
||||
|
||||
elif 'tuet' in xxx[-4]:
|
||||
g['properties']['_CAVE'] = 'Tuet'
|
||||
g['properties']['_DEPTH'] = 0
|
||||
|
||||
elif 'ReseauCP' in xxx[-4]:
|
||||
g['properties']['_CAVE'] = RNames['CP']
|
||||
g['properties']['_DEPTH'] = EntreeSupp['CP'] - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
elif 'LP9' in xxx[-4]:
|
||||
g['properties']['_CAVE'] = RNames['LP9']
|
||||
g['properties']['_DEPTH'] = EntreeSupp['LP9'] - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
elif 'CP6' in xxx[-4]:
|
||||
g['properties']['_CAVE'] = RNames['CP6']
|
||||
g['properties']['_DEPTH'] = EntreeSupp['CP6'] - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
elif 'CP62' in xxx[-4]:
|
||||
g['properties']['_CAVE'] = RNames['CP62']
|
||||
g['properties']['_DEPTH'] = EntreeSupp['CP62'] - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
elif xxx[-3] == 'Jean-Bernard':
|
||||
#g['properties']['_CAVE'] = rec['properties']['_STSURVEY'].split('.')[-3]
|
||||
g['properties']['_CAVE'] = RNames['JB']
|
||||
g['properties']['_DEPTH'] = EntreeSupp['JB'] - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
elif 'A21' in xxx[-4]:
|
||||
g['properties']['_CAVE'] = RNames['A21']
|
||||
g['properties']['_DEPTH'] = EntreeSupp['A21'] - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
elif 'Mirolda' in xxx[-3]:
|
||||
g['properties']['_CAVE'] = RNames['Mirolda']
|
||||
g['properties']['_DEPTH'] = EntreeSupp['Mirolda'] - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
else:
|
||||
g['properties']['_CAVE'] = xxx[-4]
|
||||
if g['properties']['_CAVE'] == 'A22':
|
||||
g['properties']['_CAVE'] = 'A(V)22'
|
||||
#g['properties']['_DEPTH'] = 0
|
||||
# Trouver l'altitude de l'entrée !!!!
|
||||
for line in f:
|
||||
if g['properties']['_CAVE'] in line and line.split('\t')[6] != '\n':
|
||||
altmax = float(line.split('\t')[6])
|
||||
g['properties']['_DEPTH'] = altmax - float(rec['geometry']['coordinates'][2])
|
||||
|
||||
# Write record
|
||||
ouput.write (g)
|
||||
# Update progress bar
|
||||
bar()
|
||||
return
|
||||
|
||||
#################################################################################################
|
||||
def shp2gpkg(pathshp, outputspath):
|
||||
"""
|
||||
function to convert shp files into gpkg files
|
||||
|
||||
Args:
|
||||
pathshp (str) : path where are stored output shp from Therion
|
||||
outputspath (str): path where to copy the gpkg files
|
||||
"""
|
||||
|
||||
# files to be converted
|
||||
files = ['outline2d', 'points2d']
|
||||
|
||||
print('shp2gpkg : ', files)
|
||||
with alive_bar(len(files), title = "\x1b[32;1m- Processing shp2pkg...\x1b[0m", length = 20) as bar:
|
||||
for fname in files :
|
||||
if fname == 'walls3d':
|
||||
print('shp2gpkg does not support walls3d files...\n\t I am only copying the shp file into the right folder')
|
||||
for ftype in ['.shp', '.dbf', '.prj', '.shx']:
|
||||
shutil.copy2(pathshp + fname + ftype, outputspath + fname + ftype)
|
||||
#pass
|
||||
#input = gpd.read_file(fname + '.shp', layer = 'walls3d', driver = 'ESRI shapefile')
|
||||
#input.to_file(fname + ".gpkg", driver="GPKG", encoding = 'utf8')
|
||||
#with fiona.open(fname + '.shp', 'r') as inputshp:
|
||||
# with fiona.open(fname + '.gpkg', 'w', crs=inputshp.crs, driver='GPKG', schema=inputshp.schema, encoding = 'utf8') as ouput:
|
||||
# for rec in inputshp:
|
||||
# # Write record
|
||||
# ouput.write (g)
|
||||
else:
|
||||
input = gpd.read_file(pathshp + fname + '.shp', driver = 'ESRI shapefile')
|
||||
#input.to_file(fname + ".gpkg", driver="GPKG", encoding = 'utf8')
|
||||
input.to_file(outputspath + fname + ".gpkg", driver="GPKG")
|
||||
#input.to_file(fname + ".gpkg", driver="GPKG")
|
||||
#update bar
|
||||
bar()
|
||||
|
||||
return
|
||||
|
||||
#################################################################################################
|
||||
def ThCutAreas(pathshp, outputspath):
|
||||
|
||||
print(' ')
|
||||
print('****************************************************************')
|
||||
print('Program to cut areas and lines that are intersecting the outline')
|
||||
print(' Written by X. Robert, ISTerre')
|
||||
print(' October 2022 ')
|
||||
print('****************************************************************')
|
||||
print(' ')
|
||||
|
||||
# Check if areas, lines and outline shapefiles exists...
|
||||
areaOK = True
|
||||
for fname in ['outline2d', 'lines2d', 'areas2d', 'points2d']:
|
||||
if not os.path.isfile(pathshp + fname + '.shp'):
|
||||
if fname == 'areas2d':
|
||||
areaOK = False
|
||||
else:
|
||||
print(f'\033[91mERROR:\033[00m File {(str(pathshp + fname + '.shp'))} does not exist')
|
||||
return
|
||||
# Check if Outputs path exists
|
||||
if not os.path.exists(outputspath):
|
||||
print ('\033[91mWARNING:\033[00m ' + outputspath + ' does not exist, I am creating it...')
|
||||
os.mkdir(outputspath)
|
||||
|
||||
#1- Read the outline shapefile
|
||||
outlines = gpd.read_file(pathshp + 'outline2d.shp', driver = 'ESRI shapefile')
|
||||
print('Check')
|
||||
## Change SHP to gpkg
|
||||
shp2gpkg(pathshp, outputspath)
|
||||
## Work with points
|
||||
#AddAltPoint(pathshp, outputspath)
|
||||
## Work with lines
|
||||
cutLines(pathshp, outlines, outputspath)
|
||||
## Work with Areas
|
||||
if areaOK:
|
||||
print ('Cuting areas...')
|
||||
cutareas(pathshp, outlines, outputspath)
|
||||
else:
|
||||
print ("No areas to process...")
|
||||
|
||||
#5- End ?
|
||||
|
||||
print('')
|
||||
print('Update point, areas and lines done.')
|
||||
print('')
|
||||
|
||||
######################################################################################################
|
||||
if __name__ == u'__main__':
|
||||
###################################################
|
||||
# initiate variables
|
||||
#inputfile = 'stations3d.shp'
|
||||
pathshp = './Inputs/'
|
||||
outputspath = './Outputs/'
|
||||
###################################################
|
||||
# Run the transformation
|
||||
ThCutAreas(pathshp, outputspath)
|
||||
# End...
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,239 @@
|
||||
"""
|
||||
!#############################################################################################
|
||||
# #
|
||||
# general_fonctions.py for pythStat.py #
|
||||
# #
|
||||
!#############################################################################################
|
||||
|
||||
Alex 2026 01 09
|
||||
|
||||
"""
|
||||
import os, logging, sys, re, unicodedata
|
||||
from pathlib import Path
|
||||
|
||||
log = logging.getLogger("Logger")
|
||||
|
||||
#################################################################################################
|
||||
# Couleurs ANSI par niveau de log
|
||||
#################################################################################################
|
||||
COLOR_CODES = {
|
||||
logging.DEBUG: "\033[94m", # Bleu
|
||||
logging.INFO: "\033[92m", # Vert
|
||||
logging.WARNING: "\033[95m", # MAGENTA
|
||||
logging.ERROR: "\033[91m", # Rouge
|
||||
logging.CRITICAL: "\033[1;91m", # Rouge vif
|
||||
}
|
||||
RESET = "\033[0m"
|
||||
|
||||
#################################################################################################
|
||||
# Codes de couleur ANSI
|
||||
class Colors:
|
||||
BLACK = '\033[90m'
|
||||
RED = '\033[91m'
|
||||
GREEN = '\033[92m'
|
||||
YELLOW = '\033[93m'
|
||||
BLUE = '\033[94m'
|
||||
MAGENTA = '\033[95m'
|
||||
CYAN = '\033[96m'
|
||||
WHITE = '\033[97m'
|
||||
|
||||
ERROR = '\033[91m'
|
||||
WARNING = '\033[95m'
|
||||
HEADER = '\033[1;94m'
|
||||
DEBUG = '\033[94m' # Bleu
|
||||
INFO = '\033[92m' # Vert
|
||||
CRITICAL = '\033[1;91m', # Rouge vif
|
||||
|
||||
ENDC = '\033[0m'
|
||||
BOLD = '\033[1m'
|
||||
UNDERLINE = '\033[4m'
|
||||
|
||||
#################################################################################################
|
||||
# Coloration des messages d'aide d'arg #
|
||||
#################################################################################################
|
||||
def colored_help(parser):
|
||||
"""
|
||||
Affiche l'aide colorée pour les arguments de la ligne de commande.
|
||||
|
||||
Args:
|
||||
parser (argparse.ArgumentParser): Le parseur d'arguments.
|
||||
Returns:
|
||||
None
|
||||
|
||||
"""
|
||||
# Captures the help output
|
||||
help_text = parser.format_help()
|
||||
|
||||
# Coloration des différentes parties
|
||||
colored_help_text = help_text.replace(
|
||||
'usage:', f'{Colors.ERROR}usage:{Colors.ENDC}'
|
||||
).replace(
|
||||
'options:', f'{Colors.GREEN}options:{Colors.ENDC}'
|
||||
).replace('positional arguments:', f'{Colors.BLUE}positional arguments:{Colors.ENDC}'
|
||||
).replace(', --help', f'{Colors.BLUE}, --help:{Colors.ENDC}'
|
||||
).replace('elp:', f'{Colors.BLUE}elp{Colors.ENDC}')
|
||||
|
||||
# Surligner les arguments
|
||||
# for action in parser._actions:
|
||||
# if action.option_strings:
|
||||
# # Colorer les options (--xyz)
|
||||
# for opt in action.option_strings:
|
||||
# colored_help_text = colored_help_text.replace(opt, f'{Colors.BLUE}{opt}{Colors.ENDC}').replace('--help', f'{Colors.BLUE}--help:{Colors.ENDC}')
|
||||
|
||||
# Imprimer le texte coloré
|
||||
print(colored_help_text)
|
||||
sys.exit(1)
|
||||
|
||||
#################################################################################################
|
||||
# Mise au format des noms #
|
||||
#################################################################################################
|
||||
def sanitize_filename(thName):
|
||||
"""
|
||||
Cleans a string to make it compatible with filenames on Windows, Linux, and macOS.
|
||||
Replaces special and accented characters with compatible characters.
|
||||
Replaces parentheses with underscores and enforces proper casing.
|
||||
|
||||
Args:
|
||||
thName (str): The filename to clean.
|
||||
|
||||
Returns:
|
||||
str: The cleaned and compatible string.
|
||||
|
||||
"""
|
||||
# Unicode normalization to replace accented characters with their non-accented equivalents
|
||||
thName = unicodedata.normalize('NFKD', thName).encode('ASCII', 'ignore').decode('ASCII')
|
||||
|
||||
# Replace parentheses with underscores
|
||||
thName = thName.replace('(', '_').replace(')', '_')
|
||||
|
||||
# Replace illegal characters with an underscore
|
||||
thName = re.sub(r'[<>:"/\\|?*\']', '_', thName) # Illegal on Windows
|
||||
thName = re.sub(r'\s+', '_', thName) # Spaces to underscores
|
||||
thName = re.sub(r'[^a-zA-Z0-9._-]', '_', thName) # Keep only allowed chars
|
||||
|
||||
# Convert to lowercase, then capitalize the first letter
|
||||
# thName = thName.lower().capitalize()
|
||||
# thName = thName.capitalize()
|
||||
|
||||
# Suppression des underscores en début et fin
|
||||
thName = thName.strip('_')
|
||||
|
||||
return thName or "default_filename" # Avoid empty result
|
||||
|
||||
#################################################################################################
|
||||
# Supprime les codes ANSI (pour l'écriture dans les fichiers)
|
||||
#################################################################################################
|
||||
def strip_ansi_codes(text):
|
||||
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
|
||||
return ansi_escape.sub('', text)
|
||||
|
||||
#################################################################################################
|
||||
# Formatter pour la console avec couleurs
|
||||
#################################################################################################
|
||||
class ConsoleFormatter(logging.Formatter):
|
||||
def format(self, record):
|
||||
color = COLOR_CODES.get(record.levelno, "")
|
||||
message = super().format(record)
|
||||
return f"{color}{message}{RESET}"
|
||||
|
||||
#################################################################################################
|
||||
# Formatter pour le fichier avec "!!!" sur les erreurs
|
||||
#################################################################################################
|
||||
class FileFormatter(logging.Formatter):
|
||||
def format(self, record):
|
||||
clean_msg = strip_ansi_codes(record.getMessage())
|
||||
prefix = "!!! " if record.levelno >= logging.ERROR else ""
|
||||
record_copy = logging.LogRecord(
|
||||
name=record.name,
|
||||
level=record.levelno,
|
||||
pathname=record.pathname,
|
||||
lineno=record.lineno,
|
||||
msg=f"{prefix}{clean_msg}",
|
||||
args=(),
|
||||
exc_info=record.exc_info,
|
||||
func=record.funcName,
|
||||
sinfo=record.stack_info
|
||||
)
|
||||
return super().format(record_copy)
|
||||
|
||||
#################################################################################################
|
||||
# Fonction de configuration du logger
|
||||
#################################################################################################
|
||||
def setup_logger(logfile="app.log", debug_log=False):
|
||||
logger = logging.getLogger("Logger")
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.handlers.clear()
|
||||
|
||||
min_level = logging.DEBUG if debug_log else logging.INFO
|
||||
|
||||
# Console stderr handler — affichage à l'écran avec couleurs
|
||||
stderr_handler = logging.StreamHandler(sys.stderr)
|
||||
stderr_handler.setLevel(min_level)
|
||||
stderr_formatter = ConsoleFormatter("%(levelname)s: %(message)s") # <-- Ta classe personnalisée
|
||||
stderr_handler.setFormatter(stderr_formatter)
|
||||
logger.addHandler(stderr_handler)
|
||||
|
||||
# File handler — fichier de log
|
||||
file_handler = logging.FileHandler(logfile, encoding="utf-8")
|
||||
file_handler.setLevel(min_level)
|
||||
file_formatter = FileFormatter("%(asctime)s - %(levelname)s - %(message)s") # <-- Ta classe personnalisée
|
||||
file_handler.setFormatter(file_formatter)
|
||||
logger.addHandler(file_handler)
|
||||
|
||||
return logger
|
||||
|
||||
#################################################################################################
|
||||
# fonction pour réduire l'affichage des chemins long #
|
||||
#################################################################################################
|
||||
def safe_relpath(path, base_dir=None, max_depth=3, max_name_len=50, prefix="~"):
|
||||
"""
|
||||
Retourne un chemin lisible et sûr pour affichage (logs / UI).
|
||||
|
||||
- Compatible Windows / Linux / macOS
|
||||
- Tronque la profondeur du chemin
|
||||
- Tronque le nom de fichier si trop long
|
||||
- Ne lève jamais d'exception
|
||||
"""
|
||||
|
||||
try:
|
||||
path = Path(path).expanduser().resolve()
|
||||
except Exception:
|
||||
return str(path)
|
||||
|
||||
try:
|
||||
base = Path(base_dir).expanduser().resolve() if base_dir else Path.cwd().resolve()
|
||||
except Exception:
|
||||
base = None
|
||||
|
||||
name = path.name or str(path)
|
||||
if len(name) > max_name_len:
|
||||
stem = path.stem[: max(1, max_name_len - 6)]
|
||||
name = f"{stem}...{path.suffix}"
|
||||
|
||||
try:
|
||||
if base:
|
||||
rel = path.relative_to(base)
|
||||
parts = list(rel.parts)
|
||||
else:
|
||||
raise ValueError
|
||||
except Exception:
|
||||
parts = list(path.parts)
|
||||
|
||||
if not parts:
|
||||
parts = ["."]
|
||||
|
||||
if isinstance(max_depth, int) and max_depth > 0 and len(parts) > max_depth:
|
||||
parts = parts[-max_depth:]
|
||||
parts.insert(0, prefix)
|
||||
|
||||
if parts and parts[-1] not in (".", os.sep):
|
||||
parts[-1] = name
|
||||
|
||||
try:
|
||||
return os.path.join(*parts)
|
||||
except Exception:
|
||||
return name
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
"""
|
||||
!#############################################################################################!
|
||||
|
||||
global_data.py for pyThtoQgis.py
|
||||
|
||||
!#############################################################################################!
|
||||
"""
|
||||
|
||||
Version = "2026.01.12"
|
||||
|
||||
#################################################################################################
|
||||
|
||||
pathshp = ".\\Inputs\\"
|
||||
outputspath = ".\\Outputs\\"
|
||||
|
||||
# error_count = 0 # Compteur d'erreurs
|
||||
debug_log = False
|
||||
Reference in New Issue
Block a user