import fnmatch import os import traceback import laspy # conda install -c conda-forge laspy | pip install laspy | pip install laspy[lazrs,laszip] import numpy as np import subprocess import time import datetime import csv import sys import re import json import pdal from osgeo import gdal start_time = time.time() def get_bounds(raster): #point_cloud = lp.read(entrada+raster) #print('\n segun laspy: \n') #xmax = round(point_cloud.header.x_max) #print('\nAltitud maxima x:', laspyxmax) #ymax = round(point_cloud.header.y_max) #print('\nAlitud maxima y:', laspyymax) #xmin = round(point_cloud.header.x_min) #print('\nAltitud minima x:', laspyxmin) #ymin = round(point_cloud.header.y_min) #print('\nAltitud minima y:', laspyymin, '\n') coordenadas = re.search('[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]',raster).group(0) xmin = int(coordenadas.split('-')[0]) * 1000 xmax = xmin + 2000 ymax = int(coordenadas.split('-')[1]) * 1000 ymin = ymax - 2000 return (xmin,ymin,xmax,ymax) def colorize(inLAZ,raster): jsonColorize=[] readers = { "type": "readers.las", "filename":inLAZ } jsonColorize.append(readers) colorizer = { "type": "filters.colorization", "raster": raster } jsonColorize.append(colorizer) writers = { "type": "writers.las", "filename":inLAZ } jsonColorize.append(writers) peticion=json.dumps(jsonColorize) pipeline=pdal.Pipeline(peticion) count=pipeline.execute() arrays=pipeline.arrays metadata=pipeline.metadata log=pipeline.log def procesalasz(path_lasz,out,raster,logs,id): nombrearchivo_sinRGB=path_lasz.rsplit('/',1)[1].rsplit('.',1)[0] # Cargar el archivo LAS de origen inFile = laspy.read(path_lasz) try: xmin= inFile.header.x_min ymin= inFile.header.y_min xmax= inFile.header.x_max ymax= inFile.header.y_max inFile.write(out+nombrearchivo_sinRGB+".laz") bounds=(xmin-1000,ymin-1000,xmax+1000,ymax+1000) #input_vrt = gdal.Open(raster) try: os.mkdir('./temporal'+nombrearchivo_sinRGB) except: subprocess.run('rm -r {}'.format('./temporal'+nombrearchivo_sinRGB),shell=True) os.mkdir('./temporal'+nombrearchivo_sinRGB) ds=gdal.Warp('./temporal'+nombrearchivo_sinRGB+'/'+nombrearchivo_sinRGB+'.tif',raster,format='GTiff',cropToCutline=True,outputBounds=bounds) colorize(out+nombrearchivo_sinRGB+".laz",'./temporal'+nombrearchivo_sinRGB+'/'+nombrearchivo_sinRGB+'.tif') subprocess.run('rm -r {}'.format('./temporal'+nombrearchivo_sinRGB),shell=True) #inFile.intensity = rgbFile.intensity # Actualizar intensidad except Exception: file=open(logs+"log_error_give_color_"+id+".txt","a") log='\nError al convertir '+path_lasz+"\n" print(log) file.write(log) file.write(traceback.format_exc()) file.write("\n\n") traceback.print_exc() return def give_color(entrada,salida,raster,logs,id): try: filogs=logs+"log_give_color_"+id+".txt" file = open(filogs,"w") for archivo in os.listdir(entrada): if fnmatch.fnmatch(archivo,'*.las') or fnmatch.fnmatch(archivo,'*.laz') or fnmatch.fnmatch(archivo,'*.LAS') or fnmatch.fnmatch(archivo,'*.LAZ'): file.write(archivo+"\n") try: procesalasz(entrada+'/'+archivo,salida,raster,logs,id) except: continue log="\n--- "+str(time.time() - start_time)+" seconds ---\n" file.write(log) file.close() except Exception: file = open(logs+"log_error_give_color_"+id+".txt","a") traceback.print_exc() file.write(traceback.format_exc()) log="\n--- "+str(time.time() - start_time)+" seconds ---\n" file.write(log) file.close() give_color('./in/','./out/',sys.argv[1],'./logs/',sys.argv[2])