def worker(tile): x, y, z = list(map(str, tile)) if args.masks and args.labels: label = np.array(Image.open(os.path.join(args.labels, z, x, "{}.png".format(y)))) mask = np.array(Image.open(os.path.join(args.masks, z, x, "{}.png".format(y)))) assert label.shape == mask.shape, "Inconsistent tiles (size or dimensions)" try: dist, fg_ratio, qod = compare(torch.as_tensor(label, device="cpu"), torch.as_tensor(mask, device="cpu")) except: progress.update() return False, tile if not args.minimum_fg <= fg_ratio <= args.maximum_fg or not args.minimum_qod <= qod <= args.maximum_qod: progress.update() return True, tile tiles_compare.append(tile) if args.mode == "side": for i, root in enumerate(args.images): img = tile_image_from_file(tile_from_xyz(root, x, y, z)[1]) if i == 0: side = np.zeros((img.shape[0], img.shape[1] * len(args.images), 3)) side = np.swapaxes(side, 0, 1) if args.vertical else side image_shape = img.shape else: assert image_shape[0:2] == img.shape[0:2], "Unconsistent image size to compare" if args.vertical: side[i * image_shape[0] : (i + 1) * image_shape[0], :, :] = img else: side[:, i * image_shape[0] : (i + 1) * image_shape[0], :] = img tile_image_to_file(args.out, tile, np.uint8(side)) elif args.mode == "stack": for i, root in enumerate(args.images): tile_image = tile_image_from_file(tile_from_xyz(root, x, y, z)[1]) if i == 0: image_shape = tile_image.shape[0:2] stack = tile_image / len(args.images) else: assert image_shape == tile_image.shape[0:2], "Unconsistent image size to compare" stack = stack + (tile_image / len(args.images)) tile_image_to_file(args.out, tile, np.uint8(stack)) elif args.mode == "list": tiles_list.append([tile, fg_ratio, qod]) progress.update() return True, tile
def worker(tile): if isinstance(tile, mercantile.Tile): _, src = tile_from_xyz(args.dir, tile.x, tile.y, tile.z) dst_dir = os.path.join(args.out, str(tile.z), str(tile.x)) else: src = tile dst_dir = os.path.join(args.out, os.path.dirname(tile)) assert os.path.isfile(src) dst = os.path.join(dst_dir, os.path.basename(src)) if not os.path.isdir(dst_dir): os.makedirs(dst_dir, exist_ok=True) if args.move: shutil.move(src, dst) elif args.delete: os.remove(src) else: shutil.copyfile(src, dst) return os.path.splitext(src)[1][1:] # ext
def worker(tile_key): if len(tiles_map[tile_key]) == 1: return image = np.zeros((width, height, bands), np.uint8) x, y, z = map(int, tile_key) for i in range(len(tiles_map[tile_key])): root = os.path.join(splits_path, str(i)) _, path = tile_from_xyz(root, x, y, z) if not args.label: split = tile_image_from_file(path) if args.label: split = tile_label_from_file(path) split = split.reshape((width, height, 1)) # H,W -> H,W,C assert image.shape == split.shape image[np.where(image == 0)] += split[np.where(image == 0)] if not args.label and is_nodata(image, args.nodata, args.nodata_threshold, args.keep_borders): progress.update() return tile = mercantile.Tile(x=x, y=y, z=z) if not args.label: tile_image_to_file(args.out, tile, image) if args.label: tile_label_to_file(args.out, tile, palette, image) progress.update() return tile
def main(args): assert args.out or args.delete, "out parameter is required" args.out = os.path.expanduser(args.out) print("RoboSat.pink - subset {} with cover {}, on CPU".format( args.dir, args.cover), file=sys.stderr, flush=True) ext = set() tiles = set(tiles_from_csv(os.path.expanduser(args.cover))) for tile in tqdm(tiles, ascii=True, unit="tiles"): if isinstance(tile, mercantile.Tile): src_tile = tile_from_xyz(args.dir, tile.x, tile.y, tile.z) if not src_tile: print("WARNING: skipping tile {}".format(tile), file=sys.stderr, flush=True) continue _, src = src_tile dst_dir = os.path.join(args.out, str(tile.z), str(tile.x)) else: src = tile dst_dir = os.path.join(args.out, os.path.dirname(tile)) assert os.path.isfile(src) dst = os.path.join(dst_dir, os.path.basename(src)) ext.add(os.path.splitext(src)[1][1:]) if not os.path.isdir(dst_dir): os.makedirs(dst_dir, exist_ok=True) if args.delete: os.remove(src) assert not os.path.lexists(src) elif args.copy: shutil.copyfile(src, dst) assert os.path.exists(dst) else: if os.path.islink(dst): os.remove(dst) os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst) assert os.path.islink(dst) if tiles and not args.no_web_ui and not args.delete: assert len(ext) == 1, "ERROR: Mixed extensions, can't generate Web UI" template = "leaflet.html" if not args.web_ui_template else args.web_ui_template base_url = args.web_ui_base_url if args.web_ui_base_url else "." web_ui(args.out, base_url, tiles, tiles, list(ext)[0], template)
def worker(tile_key, nodata): if len(tiles_map[tile_key]) == 1: return image = np.zeros((args.ts, args.ts, bands), np.uint8) x, y, z = map(int, tile_key) for i in range(len(tiles_map[tile_key])): root = os.path.join(splits_path, str(i)) _, path = tile_from_xyz(root, x, y, z) if not args.label: split = tile_image_from_file(path) if args.label: split = tile_label_from_file(path) split = split.reshape( (args.ts, args.ts, 1)) # H,W -> H,W,C assert image.shape == split.shape image[:, :, :] += split[:, :, :] if not args.label and skip_nodata(image, nodata["border"], nodata["value"], nodata["threshold"]): progress.update() return tile = mercantile.Tile(x=x, y=y, z=z) if not args.label: ret = tile_image_to_file(args.out, tile, image) if args.label: ret = tile_label_to_file(args.out, tile, palette, image) if not ret: sys.exit("Error: Unable to write tile {}.".format( str(tile_key))) progress.update() return tile
def worker(tile_key): if len(tiles_map[tile_key]) == 1: return image = np.zeros((args.ts, args.ts, bands), np.uint8) x, y, z = map(int, tile_key) for i in range(len(tiles_map[tile_key])): root = os.path.join(splits_path, str(i)) _, path = tile_from_xyz(root, x, y, z) if not args.label: split = tile_image_from_file(path) if args.label: split = tile_label_from_file(path) split = split.reshape( (args.ts, args.ts, 1)) # H,W -> H,W,C assert image.shape == split.shape image[np.where(image == 0)] += split[np.where(image == 0)] if not args.label and is_nodata(image, threshold=args.nodata_threshold): progress.update() return tile = mercantile.Tile(x=x, y=y, z=z) if not args.label: ret = tile_image_to_file(args.out, tile, image) if args.label: ret = tile_label_to_file(args.out, tile, palette, image) assert ret, "Unable to write tile {} from raster {}.".format( str(tile_key)) progress.update() return tile