def split_runid(in_dir_update, out_dir_update, start_index): ## # Kaffeezimmer IKG ## x_offset = 548495 ## y_offset = 5804458 ## r = 25 list_runids = os.listdir(in_dir_update) for fn_runid in list_runids[start_index:]: runid = fn_runid[:9] data = read_bin_double(in_dir_update + fn_runid, 7) data = np.array(data) - [x_offset, y_offset, 0] d = rasterize(data, r, dim=2) for cell_idx in d.keys(): x, y = [int(idx) for idx in cell_idx.split('+')] ply_name, cell_name = coord_fn_from_cell_index(x, y, runid) subdata = data[d[cell_idx]] - [x * r, y * r, 0] check_and_create(out_dir_update + cell_name) write_points(subdata, out_dir_update + cell_name + '\\' + ply_name + '.ply') del subdata del data, d print fn_runid
def calc_mms(path_mms, out_dir_mms, method): list_cells = os.listdir(out_dir_mms) for fn_cell in list_cells: list_ply = os.listdir(out_dir_mms + fn_cell) print fn_cell, len(list_ply) if len(list_ply) == 1: fn_ply = list_ply[0] fn = out_dir_mms + fn_cell + '\\' + fn_ply data = read_bin(fn, 7) d = rasterize(data, 0.1, dim=2) list_keys = d.keys() new_points = [] data = np.array(data) for key in list_keys: list_xyz = data[d[key]] new_points.append(list(method(list_xyz, axis=0))) write_points(new_points, path_mms + fn_cell + '.ply') del new_points else: list_ply = os.listdir(out_dir_mms + fn_cell) list_data = [] list_d = [] list_keys = [] for fn_ply in list_ply: fn = out_dir_mms + fn_cell + '\\' + fn_ply data = read_bin(fn, 7) d = rasterize(data, 0.1, dim=2) list_data.append(np.array(data)) list_d.append(d) list_keys.extend(d.keys()) del data, d list_keys = list(set(list_keys)) new_points = [] for key in list_keys: list_xyz = np.array([0, 0, 0]) for i in xrange(len(list_d)): data = list_data[i] d = list_d[i] if d.has_key(key): list_xyz = mergecloud(list_xyz, data[d[key]]) list_xyz = list_xyz[1:] new_points.append(list(method(list_xyz, axis=0))) write_points(new_points, path_mms + fn_cell + '.ply') del new_points
def split_ref_to_tiles(ref_path, ref_out_dir, r, x_offset, y_offset, res_ref): check_and_create(ref_out_dir) list_ref_ply = os.listdir(ref_path) print("Loading") num_point_per_kacheln = int(pow(r / res_ref, 2)) imcomplete = [] for fn_ref in list_ref_ply: data = read_bin_double(ref_path + fn_ref, 9) data = np.array(data) - [x_offset, y_offset, 0] d = rasterize(data, r, dim=2) check_and_create(ref_out_dir + fn_ref) for cell_idx in d.keys(): x, y = [int(idx) for idx in cell_idx.split('+')] ply_name, cell_name = coord_fn_from_cell_index(x, y, '') subdata = data[d[cell_idx]] - [x * r, y * r, 0] output_fn = ref_out_dir + fn_ref + '\\' + cell_name + '.ply' write_points(subdata, output_fn) if len(d[cell_idx]) < num_point_per_kacheln: imcomplete.append(cell_name + '.ply') del subdata del data, d print("Load finished") list_ref_tiles = os.listdir(ref_out_dir) removed = [] for i in xrange(len(list_ref_tiles) - 1): list_left = ref_out_dir + list_ref_tiles[i] list_right = ref_out_dir + list_ref_tiles[i + 1] left = os.listdir(list_left) right = os.listdir(list_right) intersect = set(left).intersection(right) for fn in intersect: data_left = read_bin(list_left + '\\' + fn, 7) data_right = read_bin(list_right + '\\' + fn, 7) data_new = mergecloud(data_left, data_right) if len(data_new) == num_point_per_kacheln: os.remove(list_left + '\\' + fn) os.remove(list_right + '\\' + fn) write_points(data_new, ref_out_dir + fn) removed.append(fn) intersection = list(set(imcomplete) - set(removed)) check_and_create(ref_out_dir + 'reduced\\') for fn_list in list_ref_tiles: path = ref_out_dir + fn_list + '\\' list_path = os.listdir(path) for fn_file in list_path: if fn_file in intersection: shutil.move(path + fn_file, ref_out_dir + 'reduced\\' + fn_file) else: shutil.move(path + fn_file, ref_out_dir + fn_file) [os.rmdir(ref_out_dir + fn_list) for fn_list in list_ref_tiles] print 'Finish spliting'
def cal_ref_duplicate(full_list, out_dir_update, path): list_cells = os.listdir(out_dir_update) for fn_cell in list_cells: list_ply = os.listdir(out_dir_update + fn_cell) if len(list_ply) == 1: copytoDst(out_dir_update + fn_cell, list_ply[0], path) else: list_count = [] for fn_ply in list_ply: fn = out_dir_update + fn_cell + '\\' + fn_ply data = read_bin(fn, 7) list_count.append(len(data)) if np.mean(list_count) == 2500: copytoDst(out_dir_update + fn_cell, list_ply[0], path) else: list_count = np.array(list_count) new_count = list_count[list_count != 2500] new_list = np.array(list_ply)[list_count != 2500] if len(new_count) == 1: copytoDst(out_dir_update + fn_cell, new_list[0], path) else: union = [] list_data = [] list_d = [] for fn_ply in new_list: fn = out_dir_update + fn_cell + '\\' + fn_ply data = read_bin(fn, 7) d = rasterize(data, 0.5, dim=2) list_data.append(data) list_d.append(d) del_list = set(full_list) - set(d.keys()) union.extend(del_list) res = list(set(full_list) - set(union)) new_points = [] for key in res: list_z = [] x = 0 y = 0 for i in xrange(len(list_d)): data = list_data[i] d = list_d[i] list_z.append(data[d[key][0]][2]) x, y = data[d[key][0]][0:2] new_points.append([x, y, np.median(list_z)]) if len(res) > 0: write_points(new_points, path + fn_cell + '.ply') if np.std(list_z) > 0.5: print fn_cell, len(res), np.mean(list_z), np.median( list_z), np.std(list_z)
def update_dtm(list_shift_img, raster_size, radius, ref_cut_dir, ref_update_dir, shift, res_ref, list_pointcloud_ref, ref_out_dir): # nonvalue nonvalue = -999.0 check_and_create(ref_cut_dir) check_and_create(ref_update_dir) for fn in tqdm(list_shift_img.keys()): img = list_shift_img[fn] - shift single_len = img.shape[0] new_size = (2 * radius + 1) * img.shape[0] neighbour = np.zeros((new_size, new_size)) m, n = fn[:17].split('_') int_m = Hexa2Decimal(m) int_n = Hexa2Decimal(n) combi = np.array( list((product(range(-radius, radius + 1), range(-radius, radius + 1))))) combi_global = combi + [int_m, int_n] neigh_list = [ coord_fn_from_cell_index(m, n, '')[1] + '.ply' for m, n in combi_global ] not_in_list = [] for neigh, loc in zip(neigh_list, combi): if neigh in list_shift_img.keys(): a, b = (loc + radius) * single_len neighbour[a:a + single_len, b:b + single_len] = list_shift_img[neigh] - shift else: if neigh in list_pointcloud_ref: a, b = (loc + radius) * single_len not_in_list.append([neigh, (a, b)]) print fn, not_in_list img = neighbour img = np.nan_to_num(img) filtered, boundbuffer, mask = apply_gaussian(img, 0, 0, nonvalue, 'linear') boundbuffer = np.nan_to_num(boundbuffer) a, b = (np.array([0, 0]) + radius) * single_len update = boundbuffer[a:a + single_len, b:b + single_len] upmask = mask[a:a + single_len, b:b + single_len] data_ref = read_bin(ref_out_dir + fn, 7) d_ref = rasterize(data_ref, res_ref, dim=2) data_ref = np.array(data_ref) raster_size = single_len data_output = [] for i in xrange(0, raster_size): for j in xrange(0, raster_size): string = str.join('+', [str(i), str(j)]) index = d_ref[string] if upmask[i, j] == 0: data_output.append(data_ref[index][0] + [0, 0, update[i, j]]) write_points(data_output, ref_cut_dir + fn) for fn_not, (a, b) in not_in_list: update = boundbuffer[a:a + single_len, b:b + single_len] print np.sum(update) if abs(np.sum(update)) > 0.01: data_ref = read_bin(ref_out_dir + fn_not, 7) d_ref = rasterize(data_ref, res_ref, dim=2) data_ref = np.array(data_ref) data_output = [] for i in xrange(0, raster_size): for j in xrange(0, raster_size): string = str.join('+', [str(i), str(j)]) index = d_ref[string] data_output.append(data_ref[index][0] + [0, 0, update[i, j]]) check_and_create(ref_update_dir + fn_not) write_points( data_output, ref_update_dir + fn_not + '//' + fn_not + '_from_' + fn)