@@ -59,7 +59,7 @@ typedef struct {
5959 double * qc_y ;
6060 double * qc_x_reproj ;
6161 double * qc_y_reproj ;
62- float * qc_values ;
62+ double * qc_values ;
6363 int * qc_class ;
6464 int * qc_red ;
6565 int * qc_green ;
@@ -76,7 +76,7 @@ typedef struct {
7676 pointObj target_point ;
7777
7878 GDALColorTableH hCT ;
79-
79+ GDALDataType eDataType ;
8080 double shape_tolerance ;
8181
8282} rasterLayerInfo ;
@@ -211,7 +211,7 @@ static void msRasterLayerInfoInitialize(layerObj *layer)
211211/************************************************************************/
212212
213213static void msRasterQueryAddPixel (layerObj * layer , pointObj * location ,
214- pointObj * reprojectedLocation , float * values )
214+ pointObj * reprojectedLocation , double * values )
215215
216216{
217217 rasterLayerInfo * rlinfo = (rasterLayerInfo * )layer -> layerinfo ;
@@ -238,8 +238,8 @@ static void msRasterQueryAddPixel(layerObj *layer, pointObj *location,
238238 (double * )msSmallCalloc (sizeof (double ), rlinfo -> query_alloc_max );
239239 rlinfo -> qc_y_reproj =
240240 (double * )msSmallCalloc (sizeof (double ), rlinfo -> query_alloc_max );
241- rlinfo -> qc_values = (float * )msSmallCalloc (
242- sizeof (float ),
241+ rlinfo -> qc_values = (double * )msSmallCalloc (
242+ sizeof (double ),
243243 ((size_t )rlinfo -> query_alloc_max ) * rlinfo -> band_count );
244244 rlinfo -> qc_red =
245245 (int * )msSmallCalloc (sizeof (int ), rlinfo -> query_alloc_max );
@@ -285,7 +285,7 @@ static void msRasterQueryAddPixel(layerObj *layer, pointObj *location,
285285 if (rlinfo -> qc_values != NULL )
286286 rlinfo -> qc_values = msSmallRealloc (
287287 rlinfo -> qc_values ,
288- sizeof (float ) * rlinfo -> query_alloc_max * rlinfo -> band_count );
288+ sizeof (double ) * rlinfo -> query_alloc_max * rlinfo -> band_count );
289289 if (rlinfo -> qc_class != NULL )
290290 rlinfo -> qc_class = msSmallRealloc (rlinfo -> qc_class ,
291291 sizeof (int ) * rlinfo -> query_alloc_max );
@@ -385,7 +385,7 @@ static void msRasterQueryAddPixel(layerObj *layer, pointObj *location,
385385 /* -------------------------------------------------------------------- */
386386 if (rlinfo -> qc_values != NULL )
387387 memcpy (rlinfo -> qc_values + rlinfo -> query_results * rlinfo -> band_count ,
388- values , sizeof (float ) * rlinfo -> band_count );
388+ values , sizeof (double ) * rlinfo -> band_count );
389389
390390 /* -------------------------------------------------------------------- */
391391 /* Add to the results cache. */
@@ -408,7 +408,7 @@ static int msRasterQueryByRectLow(mapObj *map, layerObj *layer,
408408 double dfXMin , dfYMin , dfXMax , dfYMax , dfX , dfY , dfAdjustedRange ;
409409 int nWinXOff , nWinYOff , nWinXSize , nWinYSize ;
410410 int nRXSize , nRYSize ;
411- float * pafRaster ;
411+ double * pafRaster ;
412412 int nBandCount , * panBandMap , iPixel , iLine ;
413413 CPLErr eErr ;
414414 rasterLayerInfo * rlinfo ;
@@ -504,15 +504,20 @@ static int msRasterQueryByRectLow(mapObj *map, layerObj *layer,
504504 /* band in the file. Later we will deal with the various band */
505505 /* selection criteria. */
506506 /* -------------------------------------------------------------------- */
507- pafRaster = (float * )calloc (((size_t )nWinXSize ) * nWinYSize * nBandCount ,
508- sizeof (float ));
509- MS_CHECK_ALLOC (pafRaster , sizeof (float ) * nWinXSize * nWinYSize * nBandCount ,
510- -1 );
511507
512- eErr = GDALDatasetRasterIO (hDS , GF_Read , nWinXOff , nWinYOff , nWinXSize ,
513- nWinYSize , pafRaster , nWinXSize , nWinYSize ,
514- GDT_Float32 , nBandCount , panBandMap ,
515- 4 * nBandCount , 4 * nBandCount * nWinXSize , 4 );
508+ size_t nPixels = (size_t )nWinXSize * nWinYSize * nBandCount ;
509+ pafRaster = (double * )calloc (nPixels , sizeof (double ));
510+ MS_CHECK_ALLOC (pafRaster , sizeof (double ) * nPixels , -1 );
511+
512+ // read raster block as GDT_Float64
513+ eErr = GDALDatasetRasterIO (
514+ hDS , GF_Read , nWinXOff , nWinYOff , nWinXSize , nWinYSize , pafRaster ,
515+ nWinXSize , nWinYSize , GDT_Float64 , nBandCount , panBandMap ,
516+ sizeof (double ) * nBandCount , sizeof (double ) * nBandCount * nWinXSize ,
517+ sizeof (double ));
518+
519+ // store the datatype of the original raster to use for output formatting
520+ rlinfo -> eDataType = GDALGetRasterDataType (GDALGetRasterBand (hDS , 1 ));
516521
517522 if (eErr != CE_None ) {
518523 msSetError (MS_IOERR , "GDALDatasetRasterIO() failed: %s" ,
@@ -1179,13 +1184,36 @@ int msRASTERLayerGetShape(layerObj *layer, shapeObj *shape, resultObj *record) {
11791184 if (iValue != 0 )
11801185 strlcat (szWork , "," , bufferSize );
11811186
1182- snprintf (szWork + strlen (szWork ), bufferSize - strlen (szWork ), "%.8g" ,
1187+ snprintf (szWork + strlen (szWork ), bufferSize - strlen (szWork ),
1188+ "%.10g" ,
11831189 rlinfo -> qc_values [shapeindex * rlinfo -> band_count + iValue ]);
11841190 }
11851191 } else if (EQUALN (layer -> items [i ], "value_" , 6 ) && rlinfo -> qc_values ) {
11861192 int iValue = atoi (layer -> items [i ] + 6 );
1187- snprintf (szWork , bufferSize , "%.8g" ,
1188- rlinfo -> qc_values [shapeindex * rlinfo -> band_count + iValue ]);
1193+
1194+ double pixelValue =
1195+ rlinfo -> qc_values [shapeindex * rlinfo -> band_count + iValue ];
1196+
1197+ switch (rlinfo -> eDataType ) {
1198+ case GDT_Byte :
1199+ case GDT_Int8 :
1200+ case GDT_UInt16 :
1201+ case GDT_Int16 :
1202+ case GDT_Int32 :
1203+ snprintf (szWork , bufferSize , "%d" , (int )pixelValue );
1204+ break ;
1205+ case GDT_UInt32 :
1206+ snprintf (szWork , bufferSize , "%u" , (unsigned int )pixelValue );
1207+ break ;
1208+ case GDT_Float64 :
1209+ snprintf (szWork , bufferSize , "%.12g" , pixelValue );
1210+ break ;
1211+ default :
1212+ // GDT_Float32
1213+ snprintf (szWork , bufferSize , "%.8g" , pixelValue );
1214+ break ;
1215+ }
1216+
11891217 } else if (EQUAL (layer -> items [i ], "class" ) && rlinfo -> qc_class ) {
11901218 int p_class = rlinfo -> qc_class [shapeindex ];
11911219 if (layer -> class [p_class ] -> name != NULL )
0 commit comments