Skip to content

[Bug]: Zonal Statistics returns 0 cells for every zone when the polygon layer is in a projected CRS #887

Closed

Description

Before you start

  • I searched the existing issues and did not find a duplicate.
  • This is a bug report, not a usage question.

What happened?

Zonal Statistics returns count = 0 and empty statistics for every polygon when the zone layer is in a projected CRS. There is no notice. The same polygons in EPSG:4326 give the right numbers.

16 Italian municipalities (ISTAT) over a 25 m slope raster (EPSG:25832, 1865 × 1373 cells), Glorenza row:

Zone layer count mean
GeoPackage, EPSG:25832 0 null
GeoPackage, EPSG:32632 0 null
GeoPackage, EPSG:3003 0 null
Shapefile, EPSG:25832 0 null
GeoPackage, EPSG:4326 21,056 38.236290340270045
QGIS 4.2 native:zonalstatisticsfb, EPSG:25832 21,056 38.236290340270045

The raster does not matter: the same happens with the raster written by GDAL (gdal_translate) and with a copy warped to EPSG:4326. With the EPSG:4326 zones, all 16 rows match QGIS exactly.

Steps to reproduce

rustgis-cli run --data slope.tif --data comuni_25832.gpkg \
  --cmd 'gp.run={"tool":"zonalStats","params":{"raster":"slope","polygons":"comuni_vv","output":"zs"}}' \
  --cmd 'share.exportLayer={"layer":"zs","path":"zs.gpkg"}'

Every slope_count is 0. Run it again with the zones reprojected to EPSG:4326 (ogr2ogr -t_srs EPSG:4326) and the counts are right.

Expected behavior

The same statistics whatever the CRS of the zone layer, as in QGIS.

Where it may come from

In crates/analysis/src/tools.rs, zonal_stats builds poly_tr from poly_ds.crs when the layer is not lon/lat. It then calls transform_feature_polygons_to_lonlat(polys, poly_tr.as_ref()). The features of a loaded layer already hold lon/lat (xy_table_to_point in the same file writes to.to_lonlat(...)), so the polygons seem to be transformed a second time. They end up near lon 4.51, lat 0.0004 (the inverse UTM of coordinates read as metres), outside the raster; the comment below confirms the spot with a trap raster. The code is the same on main today.

How are you running RustGIS?

Desktop app (Windows), portable build, through rustgis-cli run. The projects with the results open in the app with every label at 0.

Version

0.4.0

Operating system, browser and graphics

Windows 11

Sample data

Copernicus DEM GLO-30 slope (25 m, EPSG:25832) and ISTAT municipal boundaries 2025 (CC BY 3.0) for Val Venosta. I can share the two files.

Activity

  1. GISspecialistAlejandraDuque commented on Oct 25, 2026

    @GISspecialistAlejandraDuque
    ContributorAuthor

    I confirmed the double transformation with an experiment, so the cause is no longer a guess.

    If the polygons, already in lon/lat, go through the EPSG:25832 → lon/lat transform a second time, the whole layer (lon 10.13..11.26, lat 46.38..46.9) must land in a speck about 1.3e-5 degrees wide near lon 4.5113510, lat 0.0004206 (the inverse UTM of coordinates that are tiny when read as metres). Two trap rasters in EPSG:4326, filled with the value 7:

    Trap raster Cell size Result
    Covering that spot, 1e-4° cells larger than the collapsed polygons 0 cells (polygons fall between cell centres)
    Centred on lon 4.5113509512, lat 0.0004205629, 3.3e-8° cells smaller than the collapsed polygons 17,125 cells, mean 7.0, and the largest municipality (Malles) gets the most cells

    So with the projected zone layer the polygons really are transformed twice and land near (4.51°, 0.0004°), outside any real raster, with no part flagged as failed because every coordinate stays finite.

    Two cross-checks with the same build:

    • xyTableToPoint with crs: EPSG:25832 places the real UTM coordinates (622855, 5163163) correctly at (10.6044, 46.6108), so the transformer itself is fine. Fed (10.6, 46.6) it answers (4.5113511, 0.0004203), the same speck.
    • The run prints no skipped-feature or failed-part message, which matches finite but wrong coordinates.
  2. choupeanut commented on Oct 25, 2026

    @choupeanut
    Contributor

    I'll take this. I'll add a regression for loaded projected polygon layers, remove the duplicate CRS transformation in zonal statistics, and check the command path, undo, and the WASM build. No new dependencies or data-model changes planned.

  3. added
    bug
    on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions