= gpd.read_file('example_data/R70C21.shp')
f
f.head()'label_id'] = f.apply(lambda row: 2 if row.label == 'Standing' else 1, axis=1)
f['example_data/R70C21.shp') f.to_file(
Tiling
Tiler
Tiler (outpath, gridsize_x:int=400, gridsize_y:int=400, overlap:Tuple[int,int]=(100, 100))
Similar functions than ´solaris.tile.raster_tile’ but with more recent dependencies.
Tiler.tile_raster
Tiler.tile_raster (path_to_raster:str, allow_partial_data:bool=False)
Tiles specified raster to self.gridsize_x
times self.gridsize_y
grid, with self.overlap
pixel overlap
Tiler.tile_vector
Tiler.tile_vector (path_to_vector:str, min_area_pct:float=0.0)
Tiles a vector data file into smaller tiles. Converts all multipolygons to a regular polygons. min_area_pct
is be used to specify the minimum area for partial masks to keep. Default value 0.0 keeps all masks.
Tiler.tile_and_rasterize_vector
Tiler.tile_and_rasterize_vector (path_to_raster:str, path_to_vector:str, column:str, keep_bg_only:bool=False)
Rasterizes vectors based on tiled rasters. Requires that shapefile has numeric data in column
. By default only keeps the patches that contain polygon data, by specifying keep_bg_only=True
saves also masks for empty patches.
untile_vector
untile_vector (path_to_targets:str, outpath:str, non_max_suppression_thresh:float=0.0, nms_criterion:str='score')
Create single shapefile from a directory of predicted shapefiles
copy_sum
copy_sum (merged_data, new_data, merged_mask, new_mask, **kwargs)
Make new pixels have the sum of two overlapping pixels as their value. Useful with prediction data
untile_raster
untile_raster (path_to_targets:str, outfile:str, method:str='first')
Merge multiple patches from path_to_targets
into a single raster`
Example area looks like this
from rasterio import plot as rioplot
= rio.open('example_data/R70C21.tif')
raster rioplot.show(raster)
<Axes: >
With rasterio.plot
it is a lot easier to visualize shapefile and raster simultaneously
= plt.subplots(1,1)
fig, ax =ax)
rioplot.show(raster, ax=ax, column='label_id') f.plot(ax
<Axes: >
Let’s tile image into 240x180 sized patches using overlap of half patch size.
= Tiler(outpath='example_data/tiles', gridsize_x=240, gridsize_y=180, overlap=(120, 90)) tiler
'example_data/R70C21.tif') tiler.tile_raster(
'example_data/R70C21.shp', min_area_pct=.2) tiler.tile_vector(
Untile shapefiles and check how they look
f'example_data/tiles/vectors', outpath='example_data/untiled.geojson') untile_vector(
81 polygons before non-max suppression
81 polygons after non-max suppression
Plot with the tiled grid.
= gpd.read_file('example_data/untiled.geojson')
untiled = plt.subplots(1,1)
fig, ax =ax)
rioplot.show(raster, ax=ax)
tiler.grid.exterior.plot(ax=ax, column='label_id', facecolor='none', edgecolor='black') untiled.plot(ax
<Axes: >
If allow_partial_data=False
as is the default behaviour, tiling is done only for the area from which full sized patch can be extracted. With allow_partial_data=True
, windows can “extend” to empty areas. This is useful with inference, when predicted areas can have wonky dimensions.
'example_data/R70C21.tif', allow_partial_data=True) tiler.tile_raster(
'example_data/R70C21.shp', min_area_pct=.2) tiler.tile_vector(
Untile shapefiles and check how they look
f'example_data/tiles/vectors', outpath='example_data/untiled.geojson') untile_vector(
81 polygons before non-max suppression
81 polygons after non-max suppression
= gpd.read_file('example_data/untiled.geojson')
untiled = plt.subplots(1,1)
fig, ax =ax)
rioplot.show(raster, ax#tiler.grid.exterior.plot(ax=ax)
=ax, column='label_id', facecolor='none', edgecolor='black') untiled.plot(ax
<Axes: >
Plot with the tiled grid.
= gpd.read_file('example_data/untiled.geojson')
untiled = plt.subplots(1,1)
fig, ax =ax)
rioplot.show(raster, ax=ax)
tiler.grid.exterior.plot(ax=ax, column='label_id') untiled.plot(ax
<Axes: >
'example_data/R70C21.tif', 'example_data/R70C21.shp', column='label_id') tiler.tile_and_rasterize_vector(
'example_data/R70C21.tif', 'example_data/R70C21.shp',
tiler.tile_and_rasterize_vector(='label_id', keep_bg_only=True) column
import matplotlib.pyplot as plt
with rio.open('example_data/tiles/rasterized_vectors/R1C3.tif') as i: im = i.read()
0]) plt.imshow(im[
<matplotlib.image.AxesImage>
untile_raster
can be used to mosaic all patches into one.
'example_data/tiles/rasterized_vectors/', 'example_data/tiles/mosaic_first.tif',
untile_raster(='first') method
with rio.open('example_data/tiles/mosaic_first.tif') as mos: mosaic = mos.read()
0]) plt.imshow(mosaic[
<matplotlib.image.AxesImage>
By specifying method as sum it’s possible to collate predictions and get the most likely label for pixels
'example_data/tiles/rasterized_vectors/', 'example_data/tiles/mosaic_sum.tif',
untile_raster(='sum') method
with rio.open('example_data/tiles/mosaic_sum.tif') as mos: mosaic = mos.read()
0]) plt.imshow(mosaic[
<matplotlib.image.AxesImage>