Geodata
- class geodataset.geodata.Raster(path, output_name_suffix=None, ground_resolution=None, scale_factor=None, temp_dir='./tmp')[source]
Bases:
object
Class responsible for loading a .tif or .png raster with or without CRS/transform data, while resampling it to match a given ground_resolution or scale_factor. It contains methods to align geometries in GeoDataFrame objects in a given CRS to the resampled raster’s CRS or pixel coordinate system.
- Parameters:
path (str or pathlib.Path) – The path to the raster file.
output_name_suffix (str, optional) – A suffix to add to the output name of the tiles generated from this raster.
ground_resolution (float) – The ground resolution in meter per pixel desired when loading the raster. Only one of ground_resolution and scale_factor can be set at the same time.
scale_factor (float) – Scale factor for rescaling the data (change pixel resolution). Only one of ground_resolution and scale_factor can be set at the same time.
temp_dir (str or pathlib.Path) – Temporary directory to store the resampled Raster, if it is too big to fit in memory.
- adjust_geometries_to_raster_crs_if_necessary(gdf)[source]
Adjusts the geometries in a GeoDataFrame to the CRS of the Raster.
- Parameters:
gdf (gpd.GeoDataFrame) – The GeoDataFrame containing the geometries to adjust.
- Return type:
gpd.GeoDataFrame
- adjust_geometries_to_raster_pixel_coordinates(gdf)[source]
Adjusts the geometries in a GeoDataFrame to the pixel coordinates of the Raster. This should be called after calling the method adjust_geometries_to_raster_crs_if_necessary.
- Parameters:
gdf (gpd.GeoDataFrame) – The GeoDataFrame containing the geometries to adjust.
- Return type:
gpd.GeoDataFrame
- create_tile_metadata(window, tile_id)[source]
Generates and returns a Tile from a window of the Raster.
- Parameters:
window (Window) – The window to generate the Tile from.
tile_id (int) – The id of the tile.
- Return type:
RasterTileMetadata or None
- get_polygon_tile(polygon, polygon_id, polygon_aoi, tile_size, use_variable_tile_size, variable_tile_size_pixel_buffer)[source]
Generates and returns a PolygonTile from a Polygon geometry, along with the possibly cropped original Polygon. The PolygonTile (object representing an image containing the pixels of the Raster where the Polygon is) will be centered on the Polygon’s centroid. The pixels of the generated PolygonTile that are outside the Polygon will be blacked-out (masked).
- Parameters:
polygon (Polygon) – The Polygon geometry to generate the PolygonTile from.
polygon_id (int) – The id of the polygon. Used in the tile name, and should be unique across a dataset.
polygon_aoi (str) – The Area of Interest (AOI) the polygon belongs to. Used in the tile name.
tile_size (int) – The size of the tile in pixels. If the polygon extent is larger than this value, then the returned Polygon will be cropped to the tile extent. If not, the returned Polygon will be identical to the source Polygon.
use_variable_tile_size (bool) – If True, the tile size will be automatically adjusted to the Polygon’s extent, up to the tile_size value, which in this case acts as a ‘maximum size’. This avoids having too many blacked-out pixels around the Polygon of interest. It should be combined with the variable_tile_size_pixel_buffer parameter to add a fixed pixel buffer around the Polygon. This parameter is recommended if saving disk space is a concern.
variable_tile_size_pixel_buffer (int) – The number of blacked-out pixels to add around the Polygon’s extent as a buffer. Only used when use_variable_tile_size is set to ‘True’.
- Return type:
Tuple[RasterPolygonTileMetadata, Polygon]