spatial_analysis
functions to analyse data in raster and/or feature datasets to replace ArcGridDataProcessing
- Assumptions:
map unit is meter
its cellsize is the same in both x and y direction
its reference position is on the lower left corner of the southwest cell
- To do:
read and write arc
- hydro_raster.spatial_analysis.arc_header_read(file_name, header_rows=6)
read the header of a asc file as a dictionary
- Parameters:
file_name – (string) file name
header_rows – (int) number of header rows
- Returns:
header: a dictionary with keys:
ncols: (int) number of columns
nrows: (int) number of rows
xllcorner: (int/float) x-coordinate of the lower left corner of the lower left cell of the grid
yllcorner: (int/float) y-coordinate of the lower left corner of the bottom left cell of the grid
cellsize: (int/float) the length of one square cell
NODATA_value: (int/float)|-9999 the value representing nodata cell
- Return type:
dict
- hydro_raster.spatial_analysis.arcgridread(file_name, header_rows=6, return_nan=True)
Read ArcGrid format raster file
- Parameters:
file_name – (str) the file name to read data
header_rows – (int) the number of head rows of the asc file
- Returns:
the data content dict: head of the raster to provide reference information of the grid tuple: outline extent of the grid (left, right, bottom, top)
- Return type:
numpy array
Note
this function can also read compressed gz files
- hydro_raster.spatial_analysis.arcgridwrite(file_name, array, header, compression=False)
write gird data into a ascii file
- Parameters:
file_name – (str) the file name to write grid data. A compressed file will automatically add a suffix ‘.gz’
array – (int/float numpy array)
header – (dict) to provide reference information of the grid
compression – (logic) to inidcate whether compress the ascii file
Example:
gird = np.zeros((5,10)) grid[0,:] = -9999 grid[-1,:] = -9999 header = {'ncols':10, 'nrows':5, 'xllcorner':0, 'yllcorner':0, 'cellsize':2, 'NODATA_value':-9999} file_name = 'example_file.asc' arcgridwrite(file_name, array, header, compression=False) arcgridwrite(file_name, array, header, compression=True)
- hydro_raster.spatial_analysis.byte_file_read(file_name)
Read file from a bytes object
- hydro_raster.spatial_analysis.check_file_existence(file_name)
check whether a file exists
- hydro_raster.spatial_analysis.combine_raster(asc_files, num_header_rows=6)
Combine a list of asc files to a DEM Raster
- Parameters:
asc_files – a list of asc file names. All raster files have the same
cellsize. –
- hydro_raster.spatial_analysis.compare_extent(extent0, extent1)
Compare and show the difference between two Raster extents
- Parameters:
extent0 – objects or extent dicts to be compared
extent1 – objects or extent dicts to be compared
displaye – whether to show the extent in figures
- Returns:
0 extent0>=extent1; 1 extent0<extent1; 2 extent0 and extent1 have intersections
- Return type:
int
- hydro_raster.spatial_analysis.extent2shape_points(extent)
Convert extent to a two-col numpy array of shape points
- hydro_raster.spatial_analysis.header2extent(header)
convert a header dict to a 4-element tuple (left, right, bottom, top) all four elements shows the coordinates at the edge of a cell, not center
- hydro_raster.spatial_analysis.map2sub(X, Y, geo_ref)
convert map coordinates to subscripts of the raster array according to geo_ref which is either a header or a geo_transform object from raster
- Parameters:
X (scalar or numpy.ndarray) – Input X coordinate
Y (scalar or numpy.ndarray) – Input Y coordinate
geo_ref (_type_) – _description_
- Returns:
rows and cols in the array
- Return type:
tuple (int, int)
- hydro_raster.spatial_analysis.meta2header(ras_meta)
Transfer rasterio meta object to a header dict
- hydro_raster.spatial_analysis.shape_extent_to_header(shape, extent, nan_value=-9999)
Create a header dict with shape and extent of an array
- hydro_raster.spatial_analysis.sub2map(rows, cols, header)
convert subscripts of a matrix to map coordinates rows, cols: subscripts of the data matrix, starting from 0
- Parameters:
rows – rows in the array
cols – cols in the array
- Returns:
X and Y coordinate values
- hydro_raster.spatial_analysis.tif_read(file_name, read_extent=None)
read tif file (only the 1st band) and return array, header, crs and meta
- Parameters:
file_name (str) – tif file name
read_extent (dict, optional) – extent window (left, right, bottom, top in map coordinates) to read part of the raster. Defaults to None.
- Returns:
_description_
- Return type:
_type_