|
sgsPy
structurally guided sampling
|
This class represents a spatial raster, and is used as an input to many sgs functions. More...
Public Member Functions | |
| __init__ (self, str|GDALRasterWrapper image) | |
| __del__ (self) | |
| info (self) | |
| get_band_index (self, str|int band) | |
| load_arr (self, int band_index) | |
| band (self, str|int band) | |
| plot (self, Optional[matplotlib.axes.Axes] ax=None, int target_width=1000, int target_height=1000, Optional[int|str] band=None, **kwargs) | |
| from_rasterio (cls, ds, arr=None) | |
| to_rasterio (self, with_arr=False) | |
| from_gdal (cls, ds, arr=None) | |
| to_gdal (self, with_arr=False) | |
| srast_info (self, Optional[int|str] band=None) | |
Public Attributes | |
| cpp_raster = GDALRasterWrapper(image, PROJDB_PATH) | |
| driver = self.cpp_raster.get_driver() | |
| width = self.cpp_raster.get_width() | |
| height = self.cpp_raster.get_height() | |
| band_count = self.cpp_raster.get_band_count() | |
| crs = self.cpp_raster.get_crs() | |
| projection = self.cpp_raster.get_projection().encode('ascii', 'ignore').decode('unicode_escape') | |
| xmin = self.cpp_raster.get_xmin() | |
| xmax = self.cpp_raster.get_xmax() | |
| ymin = self.cpp_raster.get_ymin() | |
| ymax = self.cpp_raster.get_ymax() | |
| pixel_width = self.cpp_raster.get_pixel_width() | |
| pixel_height = self.cpp_raster.get_pixel_height() | |
| dict | band_name_dict = {} |
| dict | band_data_dict = {} |
| bands = self.cpp_raster.get_bands() | |
| srast_metadata_info = None | |
| bool | is_strat_rast = False |
| temp_dir | |
| cpp_arr = np.ascontiguousarray(arr) | |
Static Public Attributes | |
| bool | have_temp_dir = False |
| bool | temp_dataset = False |
| str | filename = "" |
| bool | closed = False |
This class represents a spatial raster, and is used as an input to many sgs functions.
It has a number of additional uses, including accessing the raster data within as a numpy array, plotting with matplotlib, as well as converting to a GDAL or Rasterio dataset object. This class also has various attributes representing metadata of the raster which may be useful and can be seen in the 'Public Attributes' section.
raster data can be accessed in the form of a NumPy array per band. This can be done using the 'band' function. The band function takes a single parameter, which must be either an integer or a string. If it is an integer, it must refer to a valid zero-indexed band number. If it is a string, it must refer to a valid band name within the raster. This function may fail if the band is too large to fit in memory.
rast = sgspy.SpatialRaster('test.tif') raster with three layers
b0 = rast.band(band=0)
b1 = rast.band(band=1)
b2 = rast.band(band=2)
zq90 = rast.band(band='zq90')
pzabove2 = rast.band(band='pzabove2')
zstd = rast.band(band='zstd')
raster metadata can be displayed using the info() function. Info inclues: raster driver, band names, dimensions, pixel size, and bounds.
rast = sgspy.SpatialRaster('test.tif')
rast.info()
the plot() function provides a wrapper around matplotlibs imshow functionality (matplotlib.pyplot.imshow). Only a single band can be plotted, and for multi-band rasters an indication must be given for which band to plot.
Target width and heights can be given in the parameters target_width and target_height. Default parameters are 1000 pixels for both. Information on the actual downsampling can be found here: https://gdal.org/en/stable/api/gdaldataset_cpp.html#classGDALDataset_1ae66e21b09000133a0f4d99baabf7a0ec
If no 'band' argument is given, the function will throw an error if the image does not contain a single band.
The 'band' argument allows the end-user to specify either the band index or the band name. 'band' may be an int or str.
Optionally, any of the arguments which may be passed to the matplotlib imshow function may also be passed to plot_image(), such as cmap for a specific color mapping.
#plots the single band
rast = sgspy.SpatialRaster('test_single_band_raster.tif')
rast.plot_image()
#plots the second band
rast = sgspy.SpatialRaster('test_multi_band_raster.tif')
rast.plot(band=1)
#plots the 'zq90' band
rast = sgspy.SpatialRaster('test_multi_band_raster.tif')
rast.plot(band='zq90')
driver : str
gdal dataset driver, for info/display purposes
width : int
the pixel width of the raster image
height : int
the pixel height of the raster image
band_count : int
the number of bands in the raster image
bands : list[str]
the raster band names
crs : str
coordinate reference system
projection : str
full projection string as wkt
xmin : double
minimum x value as defined by the gdal geotransform
xmax : double
maximum x value as defined by the gdal geotransform
ymin : double
minimum y value as defined by the gdal geotransform
ymax : double
maximum y value as defined by the gdal geotransform
pixel_height : double
pixel height as defined by the gdal geotransform
pixel_width : double
pixel width as defined by the gdal geotransform
info()
takes no arguments, prints raster information to the console
plot()
takes one optional 'band' argument of type int, or str
band()
returns the band data as a numpy array, may throw an error if the raster band is too large
Optionally, any of the arguments that can be passed to matplotlib.pyplot.imshow can also be passed to plot_image().
| sgspy.utils.raster.SpatialRaster.__init__ | ( | self, | |
| str | GDALRasterWrapper | image ) |
Constructing method for the SpatialRaster class.
Has one required parameter to specify a raster path. The following
attributes are populated:
self.cpp_raster
self.driver
self.width
self.height
self.band_count
self.crs
self.projection
self.xmin
self.xmax
self.ymin
self.ymax
self.pixel_height
self.pixel_width
self.bands
Parameters
--------------------
image : str
specifies a raster file path
| sgspy.utils.raster.SpatialRaster.__del__ | ( | self | ) |
| sgspy.utils.raster.SpatialRaster.band | ( | self, | |
| str | int | band ) |
gets a numpy array with the specified bands data.
Parameters:
band : int | str
string or int representing band
| sgspy.utils.raster.SpatialRaster.from_gdal | ( | cls, | |
| ds, | |||
| arr = None ) |
This function is used to convert from a gdal.Dataset object representing a raster into an sgspy.SpatialRaster
object. A np.ndarray may be passed as the 'arr' parameter, if so, the following must be true:
arr.shape == (ds.RasterCount, ds.RasterYSize, ds.RasterXSize)
Examples:
ds = gdal.Open("rast.tif")
rast = sgspy.SpatialRaster.from_gdal(ds)
ds = gdal.Open("rast.tif")
bands = []
for i in range(1, ds.RasterCount + 1):
bands.append(ds.GetRasterBand(1).ReadAsArray())
arr = np.stack(bands, axis=0)
arr[arr < 2] = np.nan
rast = sgspy.SpatialRaster.from_gdal(ds, arr)
| sgspy.utils.raster.SpatialRaster.from_rasterio | ( | cls, | |
| ds, | |||
| arr = None ) |
This function is used to convert from a rasterio dataset object representing a raster into an sgspy.SpatialRaster
object. A np.ndarray may be passed as the 'arr' parameter, if so, the following must be true:
arr.shape == (ds.count, ds.height, ds.width)
Examples:
ds = rasterio.open("rast.tif")
rast = sgspy.SpatialRaster.from_rasterio(ds)
ds = rasterio.open("rast.tif")
arr = ds.read()
arr[arr < 2] = np.nan
rast = sgspy.SpatialRaster.from_rasterio(ds, arr)
| sgspy.utils.raster.SpatialRaster.get_band_index | ( | self, | |
| str | int | band ) |
Utilizes the band_name_dict to convert a band name to an index if requried.
Parameters:
band : str or int
string representing a band or int representing a band
| sgspy.utils.raster.SpatialRaster.info | ( | self | ) |
Displays driver, band, size, pixel size, and bound information of the raster.
| sgspy.utils.raster.SpatialRaster.load_arr | ( | self, | |
| int | band_index ) |
Loads the rasters gdal dataset into a numpy array.
Parameters:
band : int
integer representing band index
| sgspy.utils.raster.SpatialRaster.plot | ( | self, | |
| Optional[matplotlib.axes.Axes] | ax = None, | ||
| int | target_width = 1000, | ||
| int | target_height = 1000, | ||
| Optional[int | str] | band = None, | ||
| ** | kwargs ) |
Calls plot_raster() on self.
Parameters
--------------------
ax : matplotlib.axes.Axes
axes to plot the raster on
target_width : int
maximum width in pixels for the image (after downsampling)
target_height : int
maximum height in pixels for the image (after downsampling)
band : int or str
specification of which bands to plot
**kwargs
any parameters which may be passed to matplotlib.pyplot.imshow
| sgspy.utils.raster.SpatialRaster.srast_info | ( | self, | |
| Optional[int|str] | band = None ) |
| sgspy.utils.raster.SpatialRaster.to_gdal | ( | self, | |
| with_arr = False ) |
This function is used to convert an sgspy.SpatialRaster into a GDAL dataset. If with_arr is set to True,
the function will return a numpy.ndarray as a tuple with the GDAL dataset object.
Examples:
rast = sgspy.SpatialRaster('rast.tif')
ds = rast.to_gdal()
rast = sgspy.SpatialRaster('mraster.tif')
ds, arr = sgs.to_gdal(with_arr=True)
| sgspy.utils.raster.SpatialRaster.to_rasterio | ( | self, | |
| with_arr = False ) |
This function is used to convert an sgspy.SpatialRaster into a rasterio dataset. If with_arr is set to True,
the function will return a numpy.ndarray as a tuple with the rasterio dataset object.
Examples:
rast = sgspy.SpatialRaster('rast.tif')
ds = rast.to_rasterio()
rast = sgspy.SpatialRaster('mraster.tif')
ds, arr = sgs.to_rasterio(with_arr=True)
| sgspy.utils.raster.SpatialRaster.band_count = self.cpp_raster.get_band_count() |
| dict sgspy.utils.raster.SpatialRaster.band_data_dict = {} |
| dict sgspy.utils.raster.SpatialRaster.band_name_dict = {} |
| sgspy.utils.raster.SpatialRaster.bands = self.cpp_raster.get_bands() |
|
static |
| sgspy.utils.raster.SpatialRaster.cpp_arr = np.ascontiguousarray(arr) |
| sgspy.utils.raster.SpatialRaster.cpp_raster = GDALRasterWrapper(image, PROJDB_PATH) |
| sgspy.utils.raster.SpatialRaster.crs = self.cpp_raster.get_crs() |
| sgspy.utils.raster.SpatialRaster.driver = self.cpp_raster.get_driver() |
|
static |
|
static |
| sgspy.utils.raster.SpatialRaster.height = self.cpp_raster.get_height() |
| bool sgspy.utils.raster.SpatialRaster.is_strat_rast = False |
| sgspy.utils.raster.SpatialRaster.pixel_height = self.cpp_raster.get_pixel_height() |
| sgspy.utils.raster.SpatialRaster.pixel_width = self.cpp_raster.get_pixel_width() |
| sgspy.utils.raster.SpatialRaster.projection = self.cpp_raster.get_projection().encode('ascii', 'ignore').decode('unicode_escape') |
| sgspy.utils.raster.SpatialRaster.srast_metadata_info = None |
|
static |
| sgspy.utils.raster.SpatialRaster.temp_dir |
| sgspy.utils.raster.SpatialRaster.width = self.cpp_raster.get_width() |
| sgspy.utils.raster.SpatialRaster.xmax = self.cpp_raster.get_xmax() |
| sgspy.utils.raster.SpatialRaster.xmin = self.cpp_raster.get_xmin() |
| sgspy.utils.raster.SpatialRaster.ymax = self.cpp_raster.get_ymax() |
| sgspy.utils.raster.SpatialRaster.ymin = self.cpp_raster.get_ymin() |