示例#1
0
def z_adjustData(tile, source_data,target_data,filetype,outputfolder):

    tilename = tile.name

    source_tile = os.path.join(source_data, '{0}.{1}'.format(tilename,filetype)).replace('\\','/')
    target_tile = os.path.join(target_data, '{0}.{1}'.format(tilename,filetype)).replace('\\','/')
    temp_outputfile = os.path.join(outputfolder, '{0}.{1}'.format(tilename,'las')).replace('\\','/')
    outputfile = os.path.join(outputfolder, '{0}.{1}'.format(tilename,filetype)).replace('\\','/')
    
    #read the obfuscated tile
    sourcefile = File(source_tile, mode='r')
    targetfile = File(target_tile, mode='r')


    #get z values from source
    z_values=sourcefile.get_z_scaled()
    outfile = File(temp_outputfile, mode="w", header=targetfile.header)
    outfile.points=targetfile.points

    
    #Replace the z values from source file
    outfile.set_z_scaled(z_values)

    outfile.close()
    sourcefile.close()
    targetfile.close()


    #lascopy all x,y,z,classification based on the corrected gps time
    subprocessargs=['C:/LAStools/bin/las2las.exe', '-i', temp_outputfile, '-olaz', '-o', outputfile]
    subprocessargs=list(map(str,subprocessargs))    
    p = subprocess.run(subprocessargs,stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False, check=True, universal_newlines=True)

    if os.path.isfile(outputfile):
        #read the obfuscated tile
        log = f'Z replaced from {source_tile} -> {target_tile}'
        os.remove(temp_outputfile)
        print(log)

   
        return(True,outputfile,log)


    else:

        log = f'Failed replacing Z from {source_tile} -> {target_tile}'
        #os.remove(temp_outputfile)
        print(log)

        return(False,None,log)
def obfuscate_data(tile, inputfolder, originx, originy, rotation, xoffset,
                   yoffset, zoffset, toffset, outputfolder, filetype,
                   outtilename, buffer):
    """Rotate a point around a given point.
    x and y are numpy lists
    """
    tilename = tile.name
    input_file = os.path.join(outputfolder,
                              '{0}.{1}'.format(tilename,
                                               filetype)).replace('\\', '/')
    buffered_file = os.path.join(outputfolder,
                                 '{0}_buff.laz'.format(tilename)).replace(
                                     '\\', '/')
    temp_outputfile = os.path.join(outputfolder,
                                   '{0}.{1}'.format(outtilename,
                                                    'las')).replace('\\', '/')
    outputfile = os.path.join(outputfolder,
                              '{0}.{1}'.format(outtilename,
                                               filetype)).replace('\\', '/')
    log = ''
    try:
        neighbours = tile.getneighbours(buffer)
        neighbourfiles = []
        for fi in neighbours:
            nfi = os.path.join(inputfolder,
                               '{0}.{1}'.format(fi,
                                                filetype)).replace('\\', '/')
            if os.path.isfile(nfi):
                neighbourfiles.append(nfi)

        keep = '-keep_xy {0} {1} {2} {3}'.format(str(tile.xmin - buffer),
                                                 str(tile.ymin - buffer),
                                                 str(tile.xmax + buffer),
                                                 str(tile.ymax + buffer))
        keep = keep.split()

        subprocessargs = [
            'C:/LAStools/bin/las2las.exe', '-i'
        ] + neighbourfiles + ['-olaz', '-o', buffered_file, '-merged'] + keep
        subprocessargs = list(map(str, subprocessargs))
        p = subprocess.run(subprocessargs,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT,
                           shell=False,
                           check=True,
                           universal_newlines=True)

        infile = File(buffered_file, mode='r')

        xvalues = infile.get_x_scaled()
        yvalues = infile.get_y_scaled()
        zvalues = infile.get_z_scaled()
        tvalues = infile.get_gps_time()

        print(
            f"\ntilename={tilename:s} xoffset={xoffset:0.4f} yoffset={yoffset:0.4f} zoffset={zoffset:0.4f} rotation={rotation:0.4f}  toffset={toffset:0.4f}"
        )

        #rotate points
        xvalues, yvalues = rotate_about_origin(xvalues, yvalues,
                                               math.radians(rotation), originx,
                                               originy)
        #print(tilename,xvalues[0],yvalues[0],zvalues[0])
        #offset points
        newxvals = xvalues + xoffset
        newyvals = yvalues + yoffset
        newzvals = zvalues + zoffset
        newtvals = tvalues + toffset

        #print(tilename,newxvals[0],newyvals[0],newzvals[0])
        outfile = File(temp_outputfile, mode="w", header=infile.header)
        outfile.points = infile.points
        outfile.set_x_scaled(newxvals)
        outfile.set_y_scaled(newyvals)
        outfile.set_z_scaled(newzvals)
        outfile.set_gps_time(newtvals)

        outfile.close()
        infile.close()

        subprocessargs = [
            'C:/LAStools/bin/las2las.exe', '-i', temp_outputfile, '-olaz',
            '-o', outputfile
        ]
        subprocessargs = list(map(str, subprocessargs))
        p = subprocess.run(subprocessargs,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT,
                           shell=False,
                           check=True,
                           universal_newlines=True)

        if os.path.isfile(outputfile):
            log = f'Succesfully Obfuscated {tilename}'
            os.remove(temp_outputfile)
            os.remove(buffered_file)

            return (True, outputfile, log)

    except:

        log = f"{tilename} : Obfuscation Failed"
        print(log)
        #outfile.close()
        #infile.close()
        return (False, tilename, log)