Skip to content

rasterio.features.shapes returns incorrect value labels for large integers #3370

@larrybradley

Description

@larrybradley

Expected behavior and actual behavior.

I would like to extract connected shapes from an int64 array. Unfortunately, rasterio.features.shapes does not support int64 dtypes, but it claims to support float32 and float64. However, if I cast my array to either of those types, the returned label values are incorrect.

Steps to reproduce the problem.

import numpy as np
import rasterio.features

image_int64 = np.zeros((5, 5), dtype=np.int64)
label1 = np.iinfo(np.int32).max + 100
label2 = np.iinfo(np.int32).max + 1000

image_int64[:2, :2] = label1
image_int64[3:, 3:] = label2
print(image_int64)

polygons = list(rasterio.features.shapes(image_int64.astype(float)))
for polygon in polygons:
    print(polygon[1])

polygons = list(rasterio.features.shapes(image_int64.astype(np.float32)))
for polygon in polygons:
    print(polygon[1])

The output is:

[[2147483747 2147483747          0          0          0]
 [2147483747 2147483747          0          0          0]
 [         0          0          0          0          0]
 [         0          0          0 2147484647 2147484647]
 [         0          0          0 2147484647 2147484647]]

2147483648.0 0.0 2147484672.0
2147483648.0 0.0 2147484672.0

The returned labels do not match the values in the input image.

Is there any chance that support for np.int64 dtypes could be added to rasterio.features.shapes?

Environment Information

rasterio info:
rasterio: 1.4.3
GDAL: 3.9.3
PROJ: 9.4.1
GEOS: 0.0.0
PROJ DATA: /Users/lbradley/.venvs/photutils-dev/lib/python3.13/site-packages/rasterio/proj_data
GDAL DATA: /Users/lbradley/.venvs/photutils-dev/lib/python3.13/site-packages/rasterio/gdal_data

System:
python: 3.13.4 (main, Jun 5 2025, 13:09:21) [Clang 17.0.0 (clang-1700.0.13.5)]
executable: /Users/lbradley/.venvs/photutils-dev/bin/python
machine: macOS-15.5-arm64-arm-64bit-Mach-O

Python deps:
affine: 2.4.0
attrs: 25.3.0
certifi: 2025.06.15
click: 8.2.1
cligj: 0.7.2
cython: None
numpy: 2.3.1
click-plugins: None
setuptools: 80.9.0

Installation Method

Installed from PyPI using pip 25.1.1.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions