geotile package

Submodules

geotile.GeoTile module

class geotile.GeoTile.GeoTile(path)

Bases: object

GeoTile class

Attributes:
pathstr, python path

Path to the raster file (str)

dsrasterio.DatasetReader, object

Raster dataset

metadict

Raster metadata

heightint

Raster height

widthint

Raster width

crsstr

Raster crs (e.g. ‘EPSG:4326’). The will be generated automatically. In case of non-geographic raster, the crs will be None.

stride_xint

The stride of the x axis (int), default is 128

stride_yint

The stride of the y axis (int), default is 128

tile_xint

The size of the tile in x axis (int), default is 256

tile_yint

The size of the tile in y axis (int), default is 256

close()

Close the dataset

convert_nan_to_zero()

Convert nan values to zero

Returns:
None: Convert nan values to zero. The converted tiles will be stored in the class

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.convert_nan_to_zero()
convert_nodata_to_zero()

Convert nodata values to zero

Returns:
None: Convert nodata values to zero. The converted tiles will be stored in the class

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.convert_nodata_to_zero()
drop_nan_tiles()

Drop the tiles with nan values

Returns:
None: Drop the tiles with nan values. The dropped tiles will be lost forever

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.drop_nan_tiles()
drop_zero_value_tiles()

Drop the tiles with all zero values

Returns:
None: Drop the tiles with all zero values. The dropped tiles will lost forever

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.drop_all_zero_value_tiles()
generate_tiles(output_folder: str | None = 'tiles', suffix: str | None = None, prefix: str | None = None, save_tiles: bool | None = True, save_transform: bool | None = False, out_bands: list | None = None, image_format: str | None = None, dtype: str | None = None, tile_x: int | None = 256, tile_y: int | None = 256, stride_x: int | None = 128, stride_y: int | None = 128)

Save the tiles to the output folder

Parameters:
output_folderstr

Path to the output folder

save_tilesbool

If True, the tiles will be saved to the output folder else the tiles will be stored in the class

save_transformbool

If True, the transform will be saved to the output folder in txt file else it will only generate the tiles

suffixstr

The suffix of the tile name (eg. _img)

prefixstr

The prefix of the tile name (eg. img_)

out_bandslist

The bands to save (eg. [3, 2, 1]), if None, the output bands will be same as the input raster bands

image_formatstr

The image format (eg. tif), if None, the image format will be the same as the input raster format (eg. tif)

dtypestr, np.dtype

The output dtype (eg. uint8, float32), if None, the dtype will be the same as the input raster

tile_x: int

The size of the tile in x axis, Default value is 256

tile_y: int

The size of the tile in y axis, Default value is 256

stride_x: int

The stride of the x axis, Default value is 128 (1/2 overalapping) If you want to ignore the overlap, keep it same as tile_x

stride_y: int

The stride of the y axis, Default value is 128 (1/2 overalapping) If you want to ignore the overlap, keep it same as tile_y

Returns:
None: save the tiles to the output folder

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles('/path/to/output/folder', prefix='img_')
    # save the specific bands with other than default size
>>> gt.generate_raster_tiles('/path/to/output/folder', [3, 2, 1], tile_x=512, tile_y=512, stride_x=512, stride_y=512)
get_dtype(data_array: ndarray)

Get the appropriate dtype for the data array

Parameters:
data_array: np.ndarray

The data array for which the dtype will be calculated

Returns:
str: The appropriate dtype for the data array

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> data_array = np.array([1, 2, 3, 4])
>>> gt.get_dtype(data_array)
    'uint8'
mask(input_vector: str, out_path: str, crop=False, invert=False, **kwargs)

Generate a mask raster from a vector This tool is similar to QGIS clip raster by mask layer (https://docs.qgis.org/2.8/en/docs/user_manual/processing_algs/gdalogr/gdal_extraction/cliprasterbymasklayer.html)

Parameters:
input_vector: str, python path

Path to the input vector (supports: shp, geojson, zip) All the vector formats supported by geopandas are supported

out_path: Str, python Path

Path to the output location of the mask raster

crop: bool

If True, the mask will be cropped to the extent of the vector If False, the mask will be the same size as the raster

invert: bool

If True, the mask will be inverted, pixels outside the mask will be filled with 1 and pixels inside the mask will be filled with 0

kwargs: dict

# rasterio.mask.mask (e.g. bounds, res, nodataetc.) The kwargs from rasterio.mask.mask can be used here: https://rasterio.readthedocs.io/en/latest/api/rasterio.mask.html

Returns:
out_path

Save the mask as a out_path

Examples:
>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_mask('/path/to/vector.shp', '/path/to/output/file.tif')
merge_tiles(output_path: str, image_format: str | None = None, dtype: str | None = None)

Merge the tiles and save the merged raster. Make sure the tiles are generated before merging and all the tiles having similar properties (eg. dtype, crs, transform)

Parameters:
output_pathstr

Path to the output raster

image_formatstr

The image format (eg. tif), if None, the image format will be the same as the input raster format (eg. tif)

dtypestr, np.dtype

The output dtype (eg. uint8, float32), if None, the dtype will be the same as the input raster

meta: dict

The metadata of the output raster. If provided, the output raster will be created with the provided metadata else the output raster will be created with the metadata of the input raster or the other given parameters

Returns:
None: save the merged raster to the output folder

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.merge_tiles('/path/to/output/file.tif')
normalize_tiles()

Normalize the tiles between 0 and 1 (MinMaxScaler)

Returns:
None: Normalize the tiles. The normalized tiles will be stored in the class

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.normalize_tiles()
rasterization(input_vector: str, out_path: str, value_col=None, no_data: int | None = None, **kwargs)

Convert vector shapes into raster The metadata of the raster will be the same as the raster from GeoTile class. The raster will be filled with the value of the value_col of the vector.

Parameters:
input_vector: str, python path

Path to the input vector (supports: shp, geojson, zip) All the vector formats supported by geopandas are supported

out_path: str, python path

Path to the output location of the rasterized vector

value_col: str

The column name of the vector to be rasterized If None, the rasterization will be binary otherwise the rasterization will be the based on value of the column

no_data: int

The no data value of the raster. Default value is None.

kwargs: dict

# rasterio.rasterize.rasterize (e.g. fill, transform etc.) The kwargs from rasterio.rasterize can be used here: https://rasterio.readthedocs.io/en/latest/api/rasterio.rasterize.html

Returns:
None: save the rasterized vector as a out_path
Examples:
>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.rasterize_vector('/path/to/vector.shp', '/path/to/output/file.tif', fill=0)
reprojection(out_path: str, out_crs: str, resampling_method: str = 'nearest')

Reproject a raster to a new coordinate system

Parameters:
out_path: str, python path

Path to the output location of the reprojected raster

out_crs: str

The coordinate system of the output raster (e.g. ‘EPSG:4326’)

resampling_method: str

The resampling method to use (e.g. ‘bilinear’) It should be one of following, “nearest”, “bilinear”, “cubic”, “cubic_spline”, “lanczos”, “average”, “mode”, “gauss”, “max”, “min”, “median”, “q1”, “q3”, “std”, “sum”, “rms”

Returns:
out_path: str

Path to the output location of the reprojected raster

Examples:
>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.reprojection('/path/to/output/file.tif', 'EPSG:4326')
resample(out_path: str, upscale_factor: int, resampling_method: str = 'bilinear')

Resample a raster to a new resolution

Parameters:
out_path: str, python path

Path to the output location of the resampled raster

upscale_factor: int

The upscale factor of the output raster (e.g. 2, i.e. 10x10 cell size to 5x5 cell size) If you want to downscale by 2, that mean upscale_factor = 0.5

resampling_method: str

The resampling method to use (e.g. ‘bilinear’) It should be one of following, “nearest”, “bilinear”, “cubic”, “cubic_spline”, “lanczos”, “average”, “mode”, “gauss”, “max”, “min”, “median”, “q1”, “q3”, “std”, “sum”, “rms”

Returns:
out_path: str

Path to the output location of the resampled raster

Examples:
>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.resample('/path/to/output/file.tif', 2)
save_numpy(file_name: str, dtype: str | None = None)

Save the tiles to the output folder

Parameters:
file_namestr

Path or name of the output numpy file

dtypestr, np.dtype

The output dtype (eg. uint8, float32), if None, the dtype will be the same as the input raster

Returns:
None: save the tiles to the output folder, the shape of the numpy file will be (n, tile_x, tile_y, band)

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.save_numpys('/folder/to/output/file.npy')
save_tiles(output_folder: str, prefix: str | None = None, suffix: str | None = None, image_format: str | None = None, nodata: int | None = None, dtype: str | None = None)

Save the tiles to the output folder

Parameters:
output_folderstr

Path to the output folder

prefixstr

The prefix of the tile name (eg. img_)

suffixstr

The suffix of the tile name (eg. _img)

image_formatstr

The image format (eg. tif), if None, the image format will be the same as the input raster format (eg. tif)

nodataint, float

The nodata value of the raster, if None, the nodata value will be assigned based on the dtype (either max or min value)

dtypestr, np.dtype

The output dtype (eg. uint8, float32), if None, the dtype will be the same as the input raster

Returns:
None: save the tiles to the output folder

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.save_tiles('/path/to/output/folder', prefix='img_')
shuffle_tiles(random_state: int | None = None)

Shuffle the tiles

Parameters:
random_state: int

Random state for shuffling the tiles

Returns:
None: Shuffle the tiles. The offsets will be shuffled in place

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.shuffle_tiles()
standardize_tiles()

Normalize the tiles using z-score (StandardScaler)

Returns:
None: Normalize the tiles. The normalized tiles will be stored in the class

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.generate_raster_tiles(save_tiles=False)
>>> gt.standardize_tiles()
tile_info()

Get the information of the tiles

Returns:
dict: (tile_x, tile_y, stride_x, stride_y)

Examples

>>> from geotile import GeoTile
>>> gt = GeoTile('/path/to/raster/file.tif')
>>> gt.tile_info()
    {'tile_x': 256, 'tile_y': 256, 'stride_x': 128, 'stride_y': 128}

geotile.utils module

geotile.utils.mosaic(input_folder: str, output_file: str, image_format: str | None = 'tif', **kwargs)

Mosaic the rasters inside the input folder

This method is used to merge the tiles into single file

Parameters:
input_folder: str, python path

Path to the input folder

output_file: str, python path

Path to the output file

image_format: str

The image format (eg. tif), if None, the image format will be the same as the input raster format.

kwargs: dict

The kwargs from rasterio.merge.merge can be used here: https://rasterio.readthedocs.io/en/latest/api/rasterio.merge.html#rasterio.merge.merge (e.g. bounds, res, nodata etc.)

Returns:
output_file

Save the mosaic as a output_file. Returns the output_file path

Examples

>>> from geotile import mosaic
>>> mosaic('/path/to/input/folder', '/path/to/output/file.tif')
geotile.utils.vectorize(input_raster: str, output_file: str, band: int | None = 1, raster_values: str | list | None = 'all', mask: str | None = None)

Vectorize the raster

This method is used to vectorize the raster

Parameters:
input_raster: str, python path

Path to the input raster

output_file: str, python path

Path to the output file

band: int

The band to be vectorized

raster_values: str, list

The values to be vectorized. Default is ‘all’

Returns:
output_file

Save the vectorized raster as a output_file. Returns the output_file path

Examples

>>> from geotile import vectorize
>>> vectorize('/path/to/input/raster.tif', '/path/to/output/file.shp')
>>> vectorize('/path/to/input/raster.tif', '/path/to/output/file.shp', raster_values=[1])

Module contents