API Reference

io

Reading and writing functions.

gkit.io.read(filepath, band=None, **kwargs)[source]

Read rasters from file.

Parameters:
  • filepath (str) – Raster file path.
  • band (int or list) –
    Band number (read all bands by default)
    Should be a int or list to read one or more bands.
    Bands are numbered starting from 1.
Returns:

Raster or a list of Raster.

gkit.io.read_gdal(ds, band=None, **kwargs)[source]

Read raster from gdal.Dataset.

Parameters:
  • ds (gdal.Dataset) – gdal.Dataset.
  • band (int or list) –
    Band number (read all bands by default)
    Should be a int or list to read one or more bands.
    Bands are numbered starting from 1.
Returns:

Raster or a list of Raster.

gkit.io.save(raster, out_path=None, driver_name='GTiff', compress=False, options=None)[source]

save Raster to GeoTIFF file or gdal.Dataset.

Parameters:
  • raster (Raster or a list of Rasters) – Save rasters to file. When it’s a list or tuple of Raster, save them all as multi bands in one file.
  • out_path (str) – The output path. If it is None, return a gdal.Dataset.(use MEM driver)
  • driver_name (str) – Use which driver to save.(default=”GTiff”)
  • compress (int) –
    Could be following options:
    compress=True Use LZW to compress
    compress=False (default) Do not compress
    compress='DEFAULT'
    compress='PACKBITS'
    … other algorithms gdal supported
Returns:

None or gdal.Dataset

core

class gkit.core.raster.Raster[source]
clip_by_extent(extent)[source]

Clip raster by extent.

Parameters:extent (tuple or list) – [left, right, bottom, top]
Returns:None or gdal.Dataset
clip_by_feature(feature)[source]

Clip raster by on or more features.

clip_by_layer(layer)[source]

Clip raster by layer.

clip_by_shp(shp_path)[source]

Clip raster by shapefile.

coord(x, y)[source]

Get point value by coordinate.

Parameters:
  • x (float) – The X coordinate of the point.
  • y (float) – The Y coordinate of the point.
extent

The extent of raster in current coordinates. [left, right, bottom, top]

plot(*args, ax=None, cmap_name='seismic', if_show=False, **kwargs)[source]

Use matplotlib to plot preview picture

Parameters:
reproject(x_count=None, y_count=None, transform=None, projection=None, a_srs=None, method=1)[source]

Reproject/Resample

Parameters:
  • x_count (int) – Row count. (RasterXSize)
  • y_count (int) – Column count. (RasterYSize)
  • transform (list) – Use current transform in default.
  • projection – Use current projection in default.
  • method (int) –
    Could be following options:
    gdal.GRA_Bilinear (default)
    gdal.GRA_Average
    gdal.GRA_Cubic
    gdal.GRA_CubicSpline
    gdal.GRA_Lanczos
    gdal.GRA_NearestNeighbour
Returns:

Raster

resample(x_count=None, y_count=None, transform=None, method=None)[source]

Alias of self.reproject(). The only difference is that resample() cannot change the projection of raster.

rolling(function, size=None, footprint=None, mode='reflect', cval=0.0)[source]

Calculate a 2D filter using the given function.

At each element the provided function is called. The input values within the filter footprint at that element are passed to the function as a 1D array of double values.

Parameters:
  • function (callable, str) – Function to apply at each element.
  • size (scalar, tuple) – See foorprint, below. Ignored if footprint is given.
  • footprint (array, str) –
    Either size or footprint must be defined. size gives the shape that is taken from the input array, at every element position, to define the input to the filter function.
    footprint is a boolean array that specifies (implicitly) a shape, but also which of the elements within this shape will get passed to the filter function.
    When footprint is given, size is ignored.
  • mode (str) –
    The mode parameter determines how the input array is extended when the filter overlaps a border. By passing a sequence of modes with length equal to the number of dimensions of the input array, different modes can be specified along each axis. Default value is ‘reflect’. The valid values and their behavior is as follows:
    ’reflect’ (d c b a | a b c d | d c b a) The input is extended by reflecting about the edge of the last pixel.
    ’constant’ (k k k k | a b c d | k k k k) The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.
    ’nearest’ (a a a a | a b c d | d d d d) The input is extended by replicating the last pixel.
    ’mirror’ (d c b | a b c d | c b a) The input is extended by reflecting about the center of the last pixel.
    ’wrap’ (a b c d | a b c d | a b c d) The input is extended by wrapping around to the opposite edge.
  • cval (scalar) – Value to fill past edges of input if mode is ‘constant’. Default is 0.0.
Returns:

Raster

save(out_path=None, driver_name='GTiff', compress=True, options=None)[source]

save Raster to GeoTIFF file or gdal.Dataset.

Parameters:
  • raster (Raster or a list of Rasters) – Save rasters to file. When it’s a list or tuple of Raster, save them all as multi bands in one file.
  • out_path (str) – The output path. If it is None, return a gdal.Dataset.(use MEM driver)
  • driver_name (str) – Use which driver to save.(default=”GTiff”)
  • compress (int) –
    Could be following options:
    compress=True Use LZW to compress
    compress=False (default) Do not compress
    compress='DEFAULT'
    compress='PACKBITS'
    … other algorithms gdal supported
Returns:

None or gdal.Dataset

set_fill_value(value=None)[source]

Set fill value.

Parameters:value (int or float) – Fill value. The max or min value of current dtype will be used when this argument is greater or lower than current dtype’s range.
show(*args, **kwargs)[source]

A shortcut of self.plot(). Just set if_show=True.

functions

gkit.core.functions.split_by_shp(raster, shp_path, by=None, overall=False)[source]

Split rasters into several parties by polygons from shapefile.

Parameters:
  • raster (Raster or list of Raster) – One or more rasters.
  • shp_path (str) – Shapefile path.
  • by (str) – Field name, used to group polygons.(default=FID)
  • overall (bool) – Use whole area(combine all polygons) as a result. (default=False)
Returns:

dict

gkit.core.functions.zonal_apply(raster, shp, func, by=None, overall=False, args=(), kwargs={})[source]

Apply a function to each zone.

Parameters:
  • raster (Raster or list of Raster) – One or more rasters.
  • shp_path (str) – Shapefile path.
  • func (function) – Function to apply to each zone.
  • by (str) – Field name, used to group polygons.(default=FID)
  • overall (bool) – Use whole area(combine all polygons) as a result. (default=False)
Returns:

dict

math

Wrapping some math functions from numpy, make it work fine with Raster.

gkit.math.abs(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.agg_func(f)[source]

Aggregation functions wrapper.

gkit.math.cos(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.cosh(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.count(rasters, axis=0, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.exp(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.log(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.log10(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.log2(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.max(rasters, axis=0, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.mean(rasters, axis=0, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.median(rasters, axis=0, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.min(rasters, axis=0, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.radians(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.sin(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.sinh(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.std(rasters, axis=0, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.sum(rasters, axis=0, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.tan(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.tanh(rasters, *args, **kwargs)

Wrapped numpy functions. Get more information by seeing the corresponding item in numpy.ma.

gkit.math.ufunc(f)[source]

Universal functions wrapper.