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
- IO.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
- IO.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
- IO.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)
- IO.spatial_analysis.byte_file_read(file_name)
Read file from a bytes object
- IO.spatial_analysis.check_file_existence(file_name)
check whether a file exists
- IO.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. –
- IO.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
- IO.spatial_analysis.extent2shape_points(extent)
Convert extent to a two-col numpy array of shape points
- IO.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
- IO.spatial_analysis.hillshade_show(array, header, figsize=None, azdeg=315, altdeg=45, vert_exag=1)
Draw a hillshade map
- IO.spatial_analysis.map2sub(X, Y, header)
convert map coordinates to subscripts of an array
array is defined by a geo-reference header
- Parameters
X – a scalar or numpy array of coordinate values
Y – a scalar or numpy array of coordinate values
- Returns
rows and cols in the array
- Return type
numpy array
- IO.spatial_analysis.map_show(array, header, figname=None, figsize=None, dpi=300, vmin=None, vmax=None, cax=True, relocate=False, scale_ratio=1)
Display raster data
- Parameters
figname – the file name to export map, if figname is empty, then the
saved (figure will not be) –
figsize – the size of map
dpi – The resolution in dots per inch
vmax (vmin and) – the data range that the colormap covers
- IO.spatial_analysis.rank_show(array, header, figname=None, figsize=None, dpi=300, breaks=[0.2, 0.3, 0.5, 1, 2], show_colorbar=True, show_colorlegend=False, relocate=False, scale_ratio=1)
Categorize array data as ranks according to the breaks and display a ranked map
- IO.spatial_analysis.shape_extent_to_header(shape, extent, nan_value=-9999)
Create a header dict with shape and extent of an array
- IO.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
- IO.spatial_analysis.tif_read(file_name)
read tif file (only the 1st band) and return array, header, crs