def toSphericalMercator(inputFile,outputFile): # Open the LAS file and reproject to Pseudo-Mercator # WGS 84 / Pseudo-Mercator -- Spherical Mercator, Google Maps, OpenStreetMap, Bing, ArcGIS, ESRI targetProjection = srs.SRS() targetProjection.set_userinput('epsg:3857') # Ending projection ( http://epsg.io/3857 ) inf = file.File(inputFile, header=None, mode='r', in_srs=None , out_srs=targetProjection) inh = inf.header # If the height is in US Feets scale them into meters if (inh.srs.proj4.find('+units=us-ft')): scaleZ = 0.3048006096012192 # Create the out put file. outh = header.Header() outh.dataformat_id = 1 outh.scale = [0.01,0.01,0.01] outh.offset = [0.0,0.0,-0.0] outsrs = srs.SRS() outsrs.set_userinput('epsg:3857') outh.srs = outsrs outh.schema = inh.schema # Open file outf = file.File(outputFile, mode='w', header=outh) # filter the outside the bBox (projected) for p in inf: p.z = p.z * scaleZ outf.write(p) outf.close();
def rewrite_header(self): self.output.close() self.output = lasfile.File(self.options.output) h = self.output.header self.output.close() h.min = [self.minx, self.miny, self.minz] h.max = [self.maxx, self.maxy, self.maxz] rc = h.point_return_count rc[0] = self.count h.point_return_count = rc self.output = lasfile.File(self.options.output, mode='w+', header=h) self.output.close()
def __init__(self, LasFile): print "Initalizing Filter ......." self.f = file.File(LasFile, mode='r') self.h = self.f.header self.lidar_list = [pnt for pnt in self.f] self.window_size = 10 self.window_step = self.window_size # no-overlapping
def initialize_las(path): h = header.Header() h.dataformat_id = 3 h.major_version = 1 h.minor_version = 3 h.scale = [0.00001, 0.00001, 0.00001] f = file.File('pointcloud_classified.las', mode="w", header=h) # Classification by name of pcl in input for filename in glob.glob(os.path.join(path, '*.ply')): print(filename) name = filename.split("/")[-1] name = name.split(".")[0] pathfile = filename if name == "rgb": setClassification(f, pathfile, 1) if name == "ground": setClassification(f, pathfile, 2) if name == "vegetation": setClassification(f, pathfile, 3) if name == "road": setClassification(f, pathfile, 4) if name == "cables": setClassification(f, pathfile, 5) if name == "building": setClassification(f, pathfile, 6) if name == "poles": setClassification(f, pathfile, 7) if name == "anomalies": setClassification(f, pathfile, 8)
def read_las(las_filepath): points = [] p = [] f = file.File(las_filepath) for point in f: p = [point.x, point.y, point.z] points.append(p) return points
def cropTile(inputFile, x, y, z): # Get the edges of a tile south, west, north, east = tileEdges(x, y, z) scaleZ = 1.0 # Open the LAS file and reproject to Pseudo-Mercator # WGS 84 / Pseudo-Mercator -- Spherical Mercator, Google Maps, OpenStreetMap, Bing, ArcGIS, ESRI targetProjection = srs.SRS() targetProjection.set_userinput( 'epsg:3857') # Ending projection ( http://epsg.io/3857 ) inf = file.File(inputFile, header=None, mode='r', in_srs=None, out_srs=targetProjection) inh = inf.header # If the height is in US Feets scale them into meters if (inh.srs.proj4.find('+units=us-ft')): scaleZ = 0.3048006096012192 # Create the out put file. outh = header.Header() outh.dataformat_id = 1 outh.scale = [0.01, 0.01, 0.01] outh.offset = [0.0, 0.0, -0.0] outsrs = srs.SRS() outsrs.set_userinput('epsg:3857') outh.srs = outsrs outh.schema = inh.schema # Open file outf = file.File(str(int(x)) + '-' + str(int(y)) + '-' + str(int(z)) + '.las', mode='w', header=outh) # filter the outside the bBox (projected) for p in inf: if west <= p.x <= east and south <= p.y <= north: p.z = p.z * scaleZ outf.write(p) print '%.2f,%.2f,%.2f' % (p.x, p.y, p.z) outf.close()
def removevegetation(self, newlasfile, oldlasfile, NdviImage): h = header.Header() h.dataformat_id = 3 h.minor_version = 1 ############################################### '''get bounding box''' ################################################## outputtxt = './data/output.txt' fout = open(outputtxt, 'a') cmd = 'lasinfo ' + oldlasfile + '>' + outputtxt subprocess.check_output(cmd, shell=True) fout.close() rows = [i.split()[2:] for i in open(outputtxt, 'r') if "Bounding" in i] box = [i.strip(',') for i in rows[0]] minx = float(box[0]) miny = float(box[1]) maxx = float(box[2]) maxy = float(box[3]) ###################################################### g = gdal.Open(NdviImage) lasfile = file.File(oldlasfile, mode='r') newlas = file.File(newlasfile, mode='w', header=h) ####################################################### ndviband = g.ReadAsArray() ndviband = np.array(ndviband, dtype=float) m = len(ndviband) # y axe len n = len(ndviband[0]) # x axe len ####################################################### # print lastotif(2035311.686,525276.565,minx,miny,maxx,maxy,m,n) count = 0 for p in lasfile: xy = self.lastotif(p.x, p.y, minx, miny, maxx, maxy, m, n) yval = xy[1] xval = xy[0] if yval <= m and xval <= n and ndviband[yval - 1][xval - 1] < 0.25: newlas.write(p) count += 1 print count newlas.close()
def create_test_file(lidar_fileName): f = file.File(lidar_fileName, mode='r') test_file = open('final_test_file.txt', 'w') for p in f: test_file.write(str(p.color.red) + ',') test_file.write(str(p.color.green) + ',') test_file.write(str(p.color.blue) + '\n') #test_file.write(str(p.z)+'\n') test_file.close()
def extract_classifications(infile, outfile, classifications): inp = file.File(infile, mode='r') header = inp.header output = file.File(outfile, mode='w', header=header) cnt = 0 for p in inp: cls = p.classification if int(cls) in classifications: output.write(p) cnt += 1 # keep a new count inp.close() output.close() del inp del output # overwrite our header with our point count header.point_records_count = cnt output = file.File(outfile, mode='w+', header=header) output.close() del output print 'wrote %d points' % cnt
def MinFilter(self, MinOut): print "Starting Min Filter..." fw = file.File(MinOut, mode="w", header=self.h) for i in range(0, len(self.lidar_list) - self.window_size + 1, self.window_step): moving_window = [ self.lidar_list[i + j].z for j in range(self.window_size) ] critical_val = min(moving_window) critical_index = moving_window.index(critical_val) fw.write(self.lidar_list[i + critical_index]) fw.close()
def MeanFilter(self, MeanOut): print "Starting Mean Filter..." fw = file.File(MeanOut, mode="w", header=self.h) for i in range(0, len(self.lidar_list) - self.window_size + 1, self.window_step): moving_window = [ self.lidar_list[i + j].z for j in range(self.window_size) ] critical_val = sum(moving_window) / self.window_size for j in range(self.window_size): if moving_window[j] >= critical_val: fw.write(self.lidar_list[i + j]) break fw.close()
def read_retrieval_las(las_filepath, pointnum): points = [] p = [] f = file.File(las_filepath) for point in f: p = [point.x, point.y, point.z] points.append(p) points_one = [] if len(points) > point_n: points = np.array(points) idx = np.arange(len(points)) np.random.shuffle(idx) temp_data = points[idx, ...] points_one = temp_data[0:point_n] return points_one
def create_output(self): # Use the same header as before, but just add support for color # and adjust our version to 1.2 h = self.options.las.header s = h.schema s.color = True h.schema = s h.version = '1.2' if self.options.target_srs: h.srs = self.options.target_srs self.options.output = lasfile.File(self.options.output, header=h, mode='w') if self.options.target_srs: self.options.output.set_srs(self.options.target_srs)
def __init__(self, onlybuildings, PlaneImage): only_building = file.File(onlybuildings, mode='r') # Get the value of bounding box height = width = 0 oldlasnm = onlybuildings outputtxt = 'output.txt' fout = open(outputtxt, 'w') cmd = 'lasinfo ' + oldlasnm + '>' + outputtxt subprocess.check_output(cmd, shell=True) fout.close() rows = [i.split()[2:] for i in open(outputtxt, 'r') if "Bounding" in i] box = [i.strip(',') for i in rows[0]] minx = float(box[0]) miny = float(box[1]) maxx = float(box[2]) maxy = float(box[3]) height = int(ceil(maxy - miny)) width = int(ceil(maxx - minx)) print(height, width) h = header.Header() h.dataformat_id = 3 # h = only_building.header print(h.min, h.max) lidar_list = [pnt for pnt in only_building] im = Image.new('RGB', (height, width)) for i in range(0, len(lidar_list)): temp = lidar_list[i] temp.z = 0 im.putpixel( [int(temp.x - minx), int(temp.y - miny)], (temp.color.red, temp.color.green, temp.color.blue)) # im.append((temp.color.red, temp.color.green, temp.color.blue)) # print(temp.color.red, temp.color.green, temp.color.blue) # fw.write(temp) # im.putdata([(i.color.red,i.color.green, i.color.blue) # for i in lidar_list]) im.save(PlaneImage) print(len(lidar_list)) # fw.close() only_building.close()
def open_output(self): h = header.Header() prec = 10**-(self.options.precision - 1) h.scale = [prec, prec, prec] if self.options.offset: h.offset = [self.min.x, self.min.y, self.min.z] if self.options.verbose: print 'using minimum offsets', h.offset if self.srs: h.srs = self.srs output = lasfile.File(self.options.output, mode='w', header=h) return output
def write_file(version, format): h = header.Header() h.guid = g h.date = datetime.datetime.now() h.dataformat_id = format h.major_version = 1 h.minor_version = version h.min = [p.x, p.y, p.z] h.max = [p.x, p.y, p.z] h.point_return_count = [0L, number_of_points, 0L, 0L, 0L, 0L, 0L, 0L] h.srs = s h.date = p.time f = file.File('1.%d_%d.las' % (version, format), mode='w', header=h) for i in xrange(number_of_points): f.write(p) f.close()
def open_output(self): self.header = header.Header() prec = 10**-(self.options.precision-1) self.header.scale = [prec, prec, prec] if self.options.offset: h.offset = [self.minx, self.miny, self.minz] if self.options.verbose: print 'using minimum offsets', h.offset self.header.compressed = self.options.compressed if self.srs: self.header.srs = self.srs self.header.data_format_id = 1 output = lasfile.File(self.options.output,mode='w',header=self.header) return output
def __init__(self, LasFile, PcdFile): newcloud = pcl.PointCloud() _file = file.File(LasFile, mode='r') h = _file.header _val = [] for pnt, i in zip(_file, range(30000)): c = pnt.color _val.append([pnt.x, pnt.y, pnt.z]) print "Importing into pcl----------------------------" newcloud.from_list(_val) ''' fil = newcloud.make_statistical_outlier_filter() fil.set_mean_k (50) fil.set_std_dev_mul_thresh (1.0) fil.filter().to_file("test.pcd") ''' newcloud.to_file(PcdFile) print "Done importing into pcl-----------------------"
def draw_image(fileName): lidar_file = file.File(fileName, mode='r') maxmincoordinates = lidar_proocessing.getMaxMinCoordinatesforlidarfile( lidar_file) i = Image.new(mode='RGB', size=(maxmincoordinates[1] - maxmincoordinates[0] + 1, maxmincoordinates[3] - maxmincoordinates[2] + 1), color=None) result_file = open('final_test_file.txt', 'r') all_results = result_file.readlines() no = 0 matched_points = 0 for p in lidar_file: if all_results[no] == '1': i.putpixel((int(p.x) - maxmincoordinates[0], int(p.y) - maxmincoordinates[2]), (p.color.red, p.color.green, p.color.blue)) matched_points = matched_points + 1 no = no + 1 print(no) print('matched points:', matched_points) i.save('final_road_image', format="JPEG")
def las2ply(inputFile, outputFile): # Open the LAS file and reproject to Pseudo-Mercator # WGS 84 / Pseudo-Mercator -- Spherical Mercator, Google Maps, OpenStreetMap, Bing, ArcGIS, ESRI targetProjection = srs.SRS() targetProjection.set_userinput( 'epsg:3857') # Ending projection ( http://epsg.io/3857 ) inFile = file.File(inputFile, header=None, mode='r', in_srs=None, out_srs=targetProjection) # Clear file open(outputFile, 'w').close() plyHeader = '''ply format ascii 1.0 element vertex ''' + str(inHeader.count) + ''' property float x property float y property float z property uchar red property uchar green property uchar blue end_header ''' newLine = '' newFile = open(outputFile, 'w') for p in inFile: newline = '%.2f %.2f %.2f %i %i %i' % (p.x, p.y, p.z, p.intensity, p.intensity, p.intensity) newFile.write(newline + "\n") newFile.close() line_prepend(outputFile, plyHeader)
def loadLAS(fpath, length_scale, eligible_classes): def convertToLASPoint(p): lp = LASPoint() lp.x = p.x * length_scale lp.y = p.y * length_scale lp.z = p.z * length_scale lp.intensity = p.intensity lp.return_number = p.return_number lp.number_of_returns = p.number_of_returns lp.scan_direction = p.scan_direction lp.flightline_edge = p.flightline_edge lp.classification = p.classification lp.scan_angle = p.scan_angle lp.user_data = p.user_data lp.point_source_id = p.point_source_id return lp fin = lasfile.File(fpath, mode='r') cloud = [] for p in fin: if p.classification not in eligible_classes: continue cloud.append(convertToLASPoint(p)) fin.close() return cloud
import os import numpy as np inputLocation = './' outputfile = open('outputfile.txt', 'w') def listFiles(dir): rootdir = dir for root, subFolders, files in os.walk(rootdir): for file in files: yield os.path.join(root, file) return for foundFile in listFiles(inputLocation): if (foundFile.split('.')[-1] == 'las'): print foundFile inputfile = file.File(foundFile, mode='r') xAverage = [] yAverage = [] zAverage = [] for row in range(0, 10): xAverage.append(inputfile[row].x) yAverage.append(inputfile[row].y) zAverage.append(inputfile[row].z) outputfile.write('%s\t\t\t%s, %s, %s\n' % (foundFile, np.mean(xAverage), np.mean(yAverage), np.mean(zAverage))) outputfile.close()
def readInDataSet(filename): dataX, dataY, dataZ = [], [], [] # Handle NetCDF files if filename.endswith('nc'): from scipy.io import netcdf try: f = netcdf.netcdf_file(filename, 'r') dataX = f.variables['x'][:] dataY = f.variables['y'][:] dataZ = f.variables['z'][:] except IOError: print '1 - cannot open', filename, 'it may not exist. Please check path' # Handle LAZ files elif filename.endswith('.laz'): print('is a LAZ file, this is not compatible now') # import laszip # Handle LAS files elif filename.endswith('.las'): from liblas import file try: f = file.File(filename, mode='r') print 'Reading LAS' for p in f: if p.classification == 2: dataX.append(p.x) dataY.append(p.y) dataZ.append(p.z) except IOError: print('2 - cannot open', filename, 'it may not exist. Please check path') # Handle Ascii files elif filename.endswith('.txt'): # Modified from... http://stackoverflow.com/questions/16155494/python-parse-lines-of-input-file try: with open(filename, 'r') as f: for line in f: # Parse the columnated data on each line if line.find( " " ): # Each data value on each line is seperated by a space info = line.split( ) # Split the data into variables based on seperation criteria: the space #print info[0], info[1], info[2] dataX.append(float(info[0])) dataY.append(float(info[1])) dataZ.append(float(info[2])) except IOError: print '3 - cannot open', filename, 'it may not exist. Please check path' # Handle Mat files elif filename.endswith('.mat'): try: matFile = sio.loadmat(filename) #dataX = matFile['lidar']['E'][0][0] #dataY = matFile['lidar']['N'][0][0] #dataZ = matFile['lidar']['Z'][0][0] dataX = matFile['data'][:, 0] dataY = matFile['data'][:, 1] dataZ = matFile['data'][:, 2] except IOError: print '4 - cannot open', filename, 'it may not exist. Please check path' else: print 'The file extension of,', filename, ', is not supported. Please try again' print 'Supported file extension: \n.nc\n.laz\n.las\n.txt\n.mat' # Reshape the data... for compatibility dataX = np.reshape(dataX, (len(dataX), )) dataY = np.reshape(dataY, (len(dataY), )) dataZ = np.reshape(dataZ, (len(dataZ), )) # Need to handle any NaNs? return dataX, dataY, dataZ
from liblas import file import os os.add_dll_directory(r'C:/OSGeo4W64/bin') f = file.File(sys.argv[1], mode='r') for p in f: print('X,Y,Z: ', p.x, p.y, p.z)
def las_to_np_array(folder_name, file_name, min_x=0, max_x=0, min_y=0, max_y=0, ratio=1, visualize=False, rasterize=False, tile_size=512): las_file = file.File(os.path.join(folder_name, file_name), mode='r') las_maxs = np.array(las_file.header.max) las_mins = np.array(las_file.header.min) las_ranges = las_maxs - las_mins + [1, 1, 1] point_count = las_file.header.count if max_x == 0: max_x = las_ranges[0] if max_y == 0: max_y = las_ranges[1] range_x = max_x - min_x range_y = max_y - min_y min_array = np.zeros([range_y * ratio, range_x * ratio]) max_array = np.zeros([range_y * ratio, range_x * ratio]) mean_array = np.zeros([range_y * ratio, range_x * ratio]) count_array = np.zeros([range_y * ratio, range_x * ratio]) min_distance_array = np.zeros([range_y * ratio, range_x * ratio]) percent = 0 for i, point in enumerate(las_file): x = point.x - las_mins[0] y = point.y - las_mins[1] z = point.z - las_mins[2] if min_x < x < max_x and min_y < y < max_y: x = floor((x - min_x) * ratio) y = floor((y - min_y) * ratio) count_array[y][x] += 1 if min_array[y][x] == 0 or z < min_array[y][x]: min_array[y][x] = z if max_array[y][x] == 0 or z > max_array[y][x]: max_array[y][x] = z mean_array[y][x] = z * (1 / count_array[y][x]) + mean_array[y][x] * (1 - 1 / count_array[y][x]) if round((i * 100.0) / point_count, 2) != percent: percent = round((i * 100.0) / point_count, 2) print('\r{} %'.format(percent), end='') difference_array = max_array - min_array # # Threshold adjust # difference_array_adjusted = np.array(difference_array) # count_high_cutoff = 2.5 # count_low_cutoff = .5 # for i, value in ndenumerate(difference_array): # if value < count_low_cutoff or value > count_high_cutoff: # difference_array_adjusted[i] = 0 # np.save('output/sample_diff_adjusted', difference_array_adjusted) # np.save(os.path.join(output_dir, os.path.splitext(file_name)[0] + '_min'), min_array) # np.save(os.path.join(output_dir, os.path.splitext(file_name)[0] + '_max'), max_array) # np.save(os.path.join(output_dir, os.path.splitext(file_name)[0] + '_mean'), mean_array) # # np.save(os.path.join(output_dir, os.path.splitext(file_name)[0] + '_variance'), variance_array) # np.save(os.path.join(output_dir, os.path.splitext(file_name)[0] + '_count'), count_array) # np.save(os.path.join(output_dir, os.path.splitext(file_name)[0] + '_min_distance'), min_distance_array) np.save(os.path.join(output_dir, os.path.splitext(file_name)[0] + '_difference'), difference_array) if rasterize: las_x_scale = las_ranges[1] / difference_array.shape[0] las_y_scale = las_ranges[0] / difference_array.shape[1] np_array_to_geoimage( difference_array, las_mins[0], las_mins[1], las_y_scale, las_x_scale, 26949, os.path.join(output_dir, os.path.splitext(file_name)[0] + '_difference.tiff')) if visualize: visualize(difference_array, "Data Written to GeoTiff")
from liblas import file from TDA import * import numpy as np import matplotlib.pyplot as plt f = file.File( '/home/nikita/Projects/topology/NEON_AOP_sample_data_v2/LiDAR/Discrete_LiDAR/Point_Cloud/2013_SJER_AOP_point_cloud_classified.las', mode='r') array = [] i = 0 for point in f: #print 'X,Y,Z: ', point.x, point.y, point.z if i % 1000 == 0: array.append([float(point.x), float(point.y), float(point.z)]) i += 1 f.close() del (f) print(len(array)) array = np.asarray(array) PDs = doRipsFiltration(array, 1) PD = PDs[1] plotDGM(PD) plt.show()
def __init__(self, filename): self.las = lasfile.File(filename, mode='r') self.s = PointSummary() self.summarize_points()
def write_las_file(filePath,header,point): f = file.File('junk.las',mode='w', header= header) pt = point.Point() f.write(pt) f.close()
def cross(a, b): return [ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] ] las_file = 'data/20597825pai4.las' # for p in las_file: # print(p.x,p.y,p.z,p.classification) # '''Read LAS file and create an array to hold X, Y, Z values''' # # Get file # las_file = r"E:\Testing\ground_filtered.las" # # Read file f = file.File(las_file, mode='r') # Get number of points from header num_points = int(f.__len__()) las_header = f.header # with file.File('fliter.las', mode='w', header=las_header) as outfile: # for point in f: # if point.classification is 13: # outfile.write(point) # Create empty numpy array PointsXYZIC = np.empty(shape=(num_points, 5)) # Load all LAS points into numpy array counter = 0 for p in f: newrow = [p.x, p.y, p.z, p.intensity, p.classification] PointsXYZIC[counter] = newrow
('BLK_ID', ogr.OFTInteger), ]) p = index.Property() p.filename = sys.argv[1] p.overwrite = False p.storage = index.RT_Disk idx = index.Index(sys.argv[1]) leaves = idx.leaves() # leaves[0] == (0L, [2L, 92L, 51L, 55L, 26L], [-132.41727847799999, # -96.717721818399994, -132.41727847799999, -96.717721818399994]) from liblas import file f = file.File(sys.argv[1]) def area(minx, miny, maxx, maxy): width = abs(maxx - minx) height = abs(maxy - miny) return width * height def get_bounds(leaf_ids, lasfile, block_id): # read the first point and set the bounds to that p = lasfile.read(leaf_ids[0]) minx, maxx = p.x, p.x miny, maxy = p.y, p.y