Skip to content

Commit

Permalink
join attributes columns after performing zonal stats as to avoid NULL…
Browse files Browse the repository at this point in the history
… attributes in the input vector attr table (#110)

Co-authored-by: Ioan Ferencik <[email protected]>
  • Loading branch information
iferencik and Ioan Ferencik authored Jan 10, 2025
1 parent bccb1c9 commit 8d3d254
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cbsurge/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import shutil
from cbsurge.session import Session
from cbsurge.admin import silence_httpx_az
from cbsurge.util import silence_httpx_az

logger = logging.getLogger(__name__)

Expand Down
10 changes: 4 additions & 6 deletions cbsurge/stats/ZonalStats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from exactextract import exact_extract
from cbsurge.stats.GDALRasterSource import GDALRasterSource
from cbsurge.stats.OGRDataSource import OGRDataSource

import geopandas

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,17 +76,14 @@ def compute(self, rasters: list, operations: list = None, operation_cols: list =
with GDALRasterSource(raster) as raster_ds:
reprojected_rast = raster_ds.reproject(self.target_crs)
logger.debug(f"raster ({raster}) was reprojected to {self.target_crs} as {reprojected_rast}")
# only first raster keep all fields
include_cols = vector_fields if raster_index == 0 else None


gdf = exact_extract(
reprojected_rast,
reprojected_vector_path,
ops=operations,
include_cols=include_cols,
include_geom=True,
output="pandas"
output="pandas",
progress=True
)
raster_name_with_ext = os.path.basename(raster)
raster_file_name = os.path.splitext(raster_name_with_ext)[0]
Expand All @@ -107,6 +104,7 @@ def compute(self, rasters: list, operations: list = None, operation_cols: list =

os.remove(reprojected_rast)
logger.debug(f"deleted {reprojected_rast}")
combined_results = combined_results.merge(geopandas.read_file(reprojected_vector_path), on='geometry', how='inner')
os.remove(reprojected_vector_path)
logger.debug(f"deleted {reprojected_vector_path}")
logger.info(f"end computing zonal statistics for {rasters}")
Expand Down

0 comments on commit 8d3d254

Please sign in to comment.