def clip(indir, chip_csv, outdir, image_pattern, chip_pattern, shape, driver): """ Output image chips listed in a CSV file \b CSV file expects the following columns: * idx (int): index of the chip * name (str): name of chip land cover * x (float): upper left X coordinate of chip * y (float): upper left Y coordinate of chip """ # Handle 1 or 2 inputs if not len(shape): shape = None else: shape = (shape[0], shape[0]) if len(shape) == 1 else shape indir, chip_csv, outdir = Path(indir), Path(chip_csv), Path(outdir) outdir.mkdir(parents=True, exist_ok=True) # Chip info chips = pd.read_csv(chip_csv) # Input images images = list(indir.glob(image_pattern)) for chip in chips.itertuples(): _chip = dict(zip(chip._fields, chip)) _chip['Index'] += 1 # index on 1 for image in images: # Format output filename _chip['input'] = image.name out_image = outdir.joinpath(chip_pattern.format(**_chip)) # Make sure output directory exists out_image.parent.mkdir(parents=True, exist_ok=True) with rasterio.open(str(image)) as src: # Formulate chip bounds col, row = map(int, ~src.transform * (chip.x, chip.y)) window = Window.from_offlen(col, row, shape[0], shape[1]) # Form output kwargs out_kwargs = src.meta.copy() out_kwargs['driver'] = driver out_kwargs['width'] = shape[0] out_kwargs['height'] = shape[1] out_kwargs['transform'] = src.window_transform(window) click.echo('Writing output for image: {}' .format(out_image.name)) with rasterio.open(str(out_image), 'w', **out_kwargs) as dst: dst.write(src.read(window=window))
def clip(indir, chip_csv, outdir, image_pattern, chip_pattern, shape, driver): """ Output image chips listed in a CSV file \b CSV file expects the following columns: * idx (int): index of the chip * name (str): name of chip land cover * x (float): upper left X coordinate of chip * y (float): upper left Y coordinate of chip """ # Handle 1 or 2 inputs if not len(shape): shape = None else: shape = (shape[0], shape[0]) if len(shape) == 1 else shape indir, chip_csv, outdir = Path(indir), Path(chip_csv), Path(outdir) outdir.mkdir(parents=True, exist_ok=True) # Chip info chips = pd.read_csv(chip_csv) # Input images images = list(indir.glob(image_pattern)) for chip in chips.itertuples(): _chip = dict(zip(chip._fields, chip)) _chip['Index'] += 1 # index on 1 for image in images: # Format output filename _chip['input'] = image.name out_image = outdir.joinpath(chip_pattern.format(**_chip)) # Make sure output directory exists out_image.parent.mkdir(parents=True, exist_ok=True) with rasterio.open(str(image)) as src: # Formulate chip bounds col, row = map(int, ~src.transform * (chip.x, chip.y)) window = Window.from_offlen(col, row, shape[0], shape[1]) # Form output kwargs out_kwargs = src.meta.copy() out_kwargs['driver'] = driver out_kwargs['width'] = shape[0] out_kwargs['height'] = shape[1] out_kwargs['transform'] = src.window_transform(window) click.echo('Writing output for image: {}'.format( out_image.name)) with rasterio.open(str(out_image), 'w', **out_kwargs) as dst: dst.write(src.read(window=window))
def test_window_from_offlen(): """from_offlen classmethod works.""" assert Window.from_offlen(2, 0, 1, 1) == ((0, 1), (2, 3))
def test_window_from_offlen(): """from_offlen classmethod works.""" with pytest.warns(RasterioDeprecationWarning): assert Window.from_offlen(2, 0, 1, 1) == Window.from_slices((0, 1), (2, 3))