Skip to content

Commit e073dd8

Browse files
committed
Refs #37055 -- Raised NotImplementedError for unsupported GDAL raster float16 pixel type.
Python does not currently have a ctype for float16. This is being tracked in python/cpython#153740. Updated GDAL_PIXEL_TYPES to allow a more specific error message to be raised until an appropriate datatype is available in the ctypes module.
1 parent 21b3a4c commit e073dd8

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎django/contrib/gis/gdal/raster/band.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,12 @@ def data(self, data=None, offset=None, size=None, shape=None, as_memoryview=Fals
204204
raise ValueError("Size is larger than raster.")
205205

206206
# Create ctypes type array generator
207-
ctypes_array = GDAL_TO_CTYPES[self.datatype()] * (shape[0] * shape[1])
207+
datatype = self.datatype()
208+
if datatype == 15:
209+
raise NotImplementedError(
210+
"16 bit floating point (GDT_Float16) is not yet supported."
211+
)
212+
ctypes_array = GDAL_TO_CTYPES[datatype] * (shape[0] * shape[1])
208213

209214
if data is None:
210215
# Set read mode

‎django/contrib/gis/gdal/raster/const.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
12: "GDT_UInt64", # 64 bit unsigned integer (GDAL 3.5+).
3333
13: "GDT_Int64", # 64 bit signed integer (GDAL 3.5+).
3434
14: "GDT_Int8", # 8 bit signed integer (GDAL 3.7+).
35+
15: "GDT_Float16", # 16 bit floating point (GDAL 3.11+).
36+
16: "GDT_CFloat16", # Complex Float16 (GDAL 3.11+).
3537
}
3638

3739
# A list of gdal datatypes that are integers.
@@ -57,6 +59,8 @@
5759
c_uint64,
5860
c_int64,
5961
c_int8,
62+
None, # _Float16 ticket https://github.com/python/cpython/issues/153740
63+
None,
6064
]
6165

6266
# List of resampling algorithms that can be used to warp a GDALRaster.

‎tests/gis_tests/gdal_tests/test_raster.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,3 +1006,26 @@ def test_band_data_replication(self):
10061006
)
10071007
else:
10081008
self.assertEqual(band.data(), list(combo[2]))
1009+
1010+
def test_16bit_float(self):
1011+
rstfile = NamedTemporaryFile(suffix=".tif")
1012+
self.addCleanup(rstfile.close)
1013+
rst = GDALRaster(
1014+
{
1015+
"driver": "GTiff",
1016+
"name": rstfile.name,
1017+
"srid": 4326,
1018+
"width": 255,
1019+
"height": 255,
1020+
"nr_of_bands": 1,
1021+
"papsz_options": dict(NBITS=16),
1022+
}
1023+
)
1024+
if GDAL_VERSION < (3, 11):
1025+
self.assertEqual(rst.bands[0].datatype(as_string=True), "GDT_Float32")
1026+
else:
1027+
with self.assertRaisesMessage(
1028+
NotImplementedError,
1029+
"16 bit floating point (GDT_Float16) is not yet supported.",
1030+
):
1031+
rst.bands[0].data()

0 commit comments

Comments
 (0)