Skip to content

Commit

Permalink
rasterization bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtekson committed Sep 25, 2023
1 parent f8257cd commit c3ca843
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
20 changes: 15 additions & 5 deletions geotile/GeoTile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -679,14 +682,21 @@ 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
dataset = zip(df["geometry"], df[value_col]) if value_col else df["geometry"]

# 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]))

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion geotile/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

__author__ = "Tek Kshetri"
__email__ = "[email protected]"
__version__ = "1.0.2"
__version__ = "1.0.3"

0 comments on commit c3ca843

Please sign in to comment.