pygeoc.raster module

Raster Utility Class. 用于创建栅格数据对象并进行简单操作,如另存为ASCII格式栅格、栅格重分类等。

author: Liangjun Zhu

changlog:

  • 12-04-12 jz - origin version.
  • 16-07-01 lj - reorganized for pygeoc.
  • 17-06-25 lj - check by pylint and reformat by Google style.
  • 17-07-20 lj - add GDALDataType dict, and WhiteBox GAT D8 code.
  • 17-11-21 yw - add raster_binarization, raster_erosion, raster_dilation, openning, closing.
pygeoc.raster.GDALDataType = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11}

dict – GDAL DataType

Type GDAL Datatype Description
0 GDT_Unknown Unknown or unspecified type
1 GDT_Byte Eight bit unsigned integer
2 GDT_UInt16 Sixteen bit unsigned integer
3 GDT_Int16 Sixteen bit signed integer
4 GDT_UInt32 Thirty two bit unsigned integer
5 GDT_Int32 Thirty two bit signed integer
6 GDT_Float32 Thirty two bit floating point
7 GDT_Float64 Sixty four bit floating point
8 GDT_CInt16 Complex Int16
9 GDT_CInt32 Complex Int32
10 GDT_CFloat32 Complex Float32
11 GDT_CFloat64 Complex Float64
class pygeoc.raster.Raster(n_rows, n_cols, data, nodata_value=None, geotransform=None, srs=None, datatype=6)

Bases: object

Basic Raster Class.

参数:
  • n_rows – row count.
  • n_cols – col count.
  • data – 2D array data.
  • nodata_value – NODATA value, None as default.
  • geotransform – geographic transformation, None as default.
  • srs – coordinate system, None as default.
  • datatype (pygeoc.raster.GDALDataType) – Raster datatype.
nRows

int – Row number.

nCols

int – Column number.

data

numpy.array – 2D array raster data.

noDataValue

float – NoData value.

geotrans

list – geographic transformation list.

srs

osgeo.osr.SpatialReference – Spatial reference.

dataType

pygeoc.raster.GDALDataType – Raster datatype.

dx

float – cell size.

xMin

float – left X coordinate.

xMax

float – right X coordinate.

yMin

float – lower Y coordinate.

yMax

float – upper Y coordinate.

validZone

numpy.array – 2D boolean array that NoDataValue is False.

validValues

numpy.array – 2D raster array with None in NoDataValue.

Examples

The common usage is read raster data from a raster file (e.g., geotiff) and get the Raster instance.

>>> from pygeoc.raster import RasterUtilClass
>>> rst_file = r'tests/data/Jamaica_dem.tif'
>>> rst_obj = RasterUtilClass.read_raster(rst_file)
>>> print(rst_obj)
<pygeoc.raster.Raster object at 0x...>
get_average()

Get average exclude NODATA.

get_central_coors(row, col)

Get the coordinates of central grid.

参数:
  • row – row number, range from 0 to (nRows - 1).
  • col – col number, range from 0 to (nCols - 1).
返回:

XY coordinates. If the row or col are invalid, raise ValueError.

get_max()

Get maximum exclude NODATA.

get_min()

Get minimum exclude NODATA.

get_std()

Get Standard Deviation exclude NODATA.

get_sum()

Get sum exclude NODATA.

get_type()

get datatype as GDALDataType.

返回:dataType
get_value_by_row_col(row, col)

Get raster value by (row, col).

参数:
  • row – row number.
  • col – col number.
返回:

raster value, None if the input are invalid.

get_value_by_xy(x, y)

Get raster value by xy coordinates.

参数:
  • x – X Coordinate.
  • y – Y Coordinate.
返回:

raster value, None if the input are invalid.

class pygeoc.raster.RasterUtilClass

Bases: object

Utility function to handle raster data.

参见

pygeoc.raster.raster.Raster.

static closing(input_rasterfilename, times)

Do closing.

Closing: Dilate firstly, then Erode.

参数:
  • input_rasterfilename – input original raster image filename.
  • times – Erode and Dilate times.
返回:

raster image after close.

返回类型:

closing_raster

static get_mask_from_raster(rasterfile, outmaskfile, keep_nodata=False)

Generate mask data from a given raster data.

参数:
  • rasterfile – raster file path.
  • outmaskfile – output mask file path.
返回:

Raster object of mask data.

static get_negative_dem(raw_dem, neg_dem)

Get negative DEM data.

static mask_raster(in_raster, mask, out_raster)

Mask raster data. :param in_raster: list or one raster :param mask: Mask raster data :param out_raster: list or one raster

static openning(input_rasterfilename, times)

Do openning.

Openning: Erode firstly, then Dilate.

参数:
  • input_rasterfilename – input original raster image filename.
  • times – Erode and Dilate times.
返回:

raster image after open.

返回类型:

openning_raster

static raster_binarization(given_value, rasterfilename)

Make the raster into binarization.

The opening and closing are based on binary image. Therefore we need to make the raster into binarization.

参数:
  • given_value – The given value’s pixels will be value in 1,
  • pixels will be value in 0. (other) –
  • rasterfilename – The initial rasterfilena,e.
返回:

Raster after binarization.

返回类型:

binary_raster

static raster_dilation(rasterfile)

Dilate the raster image.

Find the max pixel’s value in 8-neighborhood. Then change the compute pixel’s value into the max pixel’s value.
参数:
  • rasterfile – input original raster image, type can be filename(string,
  • "test1.tif"), rasterfile (like) –
返回:

raster image after dilation, type is numpy.ndarray.

返回类型:

dilation_raster

static raster_erosion(rasterfile)

Erode the raster image.

Find the min pixel’s value in 8-neighborhood. Then change the compute pixel’s value into the min pixel’s value.
参数:
  • rasterfile – input original raster image, type can be filename(string,
  • "test1.tif"), rasterfile (like) –
返回:

raster image after erosion, type is numpy.ndarray.

返回类型:

erosion_raster

static raster_reclassify(srcfile, v_dict, dstfile, gdaltype=6)

Reclassify raster by given classifier dict.

参数:
  • srcfile – source raster file.
  • v_dict – classifier dict.
  • dstfile – destination file path.
  • gdaltype (pygeoc.raster.GDALDataType) – GDT_Float32 as default.
static raster_statistics(raster_file)

Get basic statistics of raster data.

参数:raster_file – raster file path.
返回:min, max, mean, std.
static raster_to_asc(raster_f, asc_f)

Converting Raster format to ASCII raster.

参数:
  • raster_f – raster file.
  • asc_f – output ASCII file.
static raster_to_gtiff(tif, geotif, change_nodata=False, change_gdal_type=False)

Converting Raster format to GeoTIFF.

参数:
  • tif – source raster file path.
  • geotif – output raster file path.
  • change_nodata – change NoDataValue to -9999 or not.
  • gdal_type (pygeoc.raster.GDALDataType) – GDT_Float32 as default.
  • change_gdal_type – If True, output the Float32 data type.
static read_raster(raster_file)

Read raster by GDAL.

参数:raster_file – raster file path.
返回:Raster object.
static split_raster(rs, split_shp, field_name, temp_dir)

Split raster by given shapefile and field name.

参数:
  • rs – origin raster file.
  • split_shp – boundary (ESRI Shapefile) used to spilt raster.
  • field_name – field name identify the spilt value.
  • temp_dir – directory to store the spilt rasters.
static write_asc_file(filename, data, xsize, ysize, geotransform, nodata_value)

Output Raster to ASCII file.

参数:
  • filename – output ASCII filename.
  • data – 2D array data.
  • xsize – Col count.
  • ysize – Row count.
  • geotransform – geographic transformation.
  • nodata_value – nodata_flow value.
static write_gtiff_file(f_name, n_rows, n_cols, data, geotransform, srs, nodata_value, gdal_type=6)

Output Raster to GeoTiff format file.

参数:
  • f_name – output gtiff file name.
  • n_rows – Row count.
  • n_cols – Col count.
  • data – 2D array data.
  • geotransform – geographic transformation.
  • srs – coordinate system.
  • nodata_value – nodata value.
  • gdal_type (pygeoc.raster.GDALDataType) – output raster data type, GDT_Float32 as default.