From c3ca84358054e7dea913404e8b3c58d57f4d543c Mon Sep 17 00:00:00 2001 From: iamtekson Date: Mon, 25 Sep 2023 13:43:50 -0600 Subject: [PATCH] rasterization bug fix --- geotile/GeoTile.py | 20 +++++++++++++++----- geotile/__version__.py | 2 +- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/geotile/GeoTile.py b/geotile/GeoTile.py index f2dc395..5d15ab4 100644 --- a/geotile/GeoTile.py +++ b/geotile/GeoTile.py @@ -638,7 +638,8 @@ def rasterization( input_vector: str, out_path: str, value_col=None, - no_data_val: Optional[int] = None, + no_data: Optional[int] = None, + fill: Optional[int] = 0, **kwargs, ): """Convert vector shapes into raster @@ -655,9 +656,11 @@ def rasterization( value_col: str The column name of the vector to be rasterized If None, the rasterization will be binary otherwise the rasterization will be the based on value of the column - no_data_val: int + no_data: int The no data value of the raster. If None, the no data value of the raster will be the same as the input raster + fill: int + The fill value of the raster (e.g. 0) kwargs: dict # rasterio.rasterize.rasterize (e.g. fill, transform etc.) The kwargs from rasterio.rasterize can be used here: https://rasterio.readthedocs.io/en/latest/api/rasterio.rasterize.html @@ -670,7 +673,7 @@ def rasterization( Examples: >>> from geotile import GeoTile >>> gt = GeoTile('/path/to/raster/file.tif') - >>> gt.rasterize_vector('/path/to/vector.shp', '/path/to/output/file.tif') + >>> gt.rasterize_vector('/path/to/vector.shp', '/path/to/output/file.tif', fill=0) """ # open the input vector @@ -679,6 +682,9 @@ def rasterization( # check the coordinate system for both raster and vector and reproject vector if necessary raster_crs = self.meta["crs"] if raster_crs != df.crs: + print( + f"CRS of raster doesn't match with vector. Reprojecting the vector ({df.crs}) to the raster coordinate system ({raster_crs})" + ) df = df.to_crs(raster_crs) # if value column is specified, rasterize the vector based on value column else bianary classification @@ -686,7 +692,11 @@ def rasterization( # rasterize the vector based on raster metadata mask = rasterize( - dataset, self.ds.shape, transform=self.meta["transform"], **kwargs + dataset, + self.ds.shape, + transform=self.meta["transform"], + fill=fill, + **kwargs, ) mask = np.reshape(mask, (1, mask.shape[0], mask.shape[1])) @@ -705,7 +715,7 @@ def rasterization( # update the metadata meta = self.meta.copy() - meta.update({"count": 1, "dtype": self.get_dtype(mask), "nodata": no_data_val}) + meta.update({"count": 1, "dtype": self.get_dtype(mask), "nodata": no_data}) # write the output raster with rio.open(out_path, "w", **meta) as outds: diff --git a/geotile/__version__.py b/geotile/__version__.py index 86396a1..d4b382a 100644 --- a/geotile/__version__.py +++ b/geotile/__version__.py @@ -6,4 +6,4 @@ __author__ = "Tek Kshetri" __email__ = "iamtekson@gmail.com" -__version__ = "1.0.2" +__version__ = "1.0.3"