Buggy behaviour when executing the script under Windows
The following example is run under Windows 10 Home. This is a hard to trace "bug" when linking to an external raster map of type Float32, from inside a GRASS GIS data base, rather than importing the map in GRASS GIS' native raster file format.
First the basics:
C:\Windows\System32>g.version -b
GRASS 7.6.1 (2019)
./configure --host=x86_64-w64-mingw32 '--with-libs=/c/msys64/usr/src/grass761/mswindows/osgeo4w/lib ' --with-includes=/c/OSGeo4W64/include --libexecdir=/c/OSGeo4W64/bin --prefix=/c/OSGeo4W64/apps/grass --bindir=/c/OSGeo4W64/bin --includedir=/c/OSGeo4W64/include --without-x --with-cxx --enable-shared --enable-largefile --with-fftw --with-freetype --with-proj-share=/c/OSGeo4W64/share/proj --with-gdal=/c/msys64/usr/src/grass761/mswindows/osgeo4w/gdal-config --with-geos=/c/msys64/usr/src/grass761/mswindows/osgeo4w/geos-config --with-sqlite --with-regex --with-nls --with-freetype-includes=/c/OSGeo4W64/include/freetype2 --with-zstd --with-odbc --with-cairo --with-postgres --with-opengl=windows --with-bzlib --with-liblas=/c/msys64/usr/src/grass761/mswindows/osgeo4w/liblas-config
C:\Windows\System32>g.proj -fj
+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +no_defs +a=6378137 +rf=298.257222101 +towgs84=0.000,0.000,0.000 +to_meter=1
C:\Windows\System32>g.region -gf
projection=99
zone=0
n=2879700 s=2748850 w=4735600 e=4854650 nsres=50 ewres=50 rows=2617 cols=2381 cells=6231077
The raster map according to GDAL
C:\Windows\System32>gdalinfo C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif -nomd -noct -nogcp -stats
Driver: GTiff/GeoTIFF
Files: C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif
C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif.aux.xml
Size is 2381, 2617
Coordinate System is:
PROJCS["ETRS89 / LAEA Europe",
GEOGCS["ETRS89",
DATUM["European_Terrestrial_Reference_System_1989",
SPHEROID["GRS 1980",6378137,298.257222101,
AUTHORITY["EPSG","7019"]],
TOWGS84[0,0,0,0,0,0,0],
AUTHORITY["EPSG","6258"]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]],
AUTHORITY["EPSG","4258"]],
PROJECTION["Lambert_Azimuthal_Equal_Area"],
PARAMETER["latitude_of_center",52],
PARAMETER["longitude_of_center",10],
PARAMETER["false_easting",4321000],
PARAMETER["false_northing",3210000],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","3035"]]
Origin = (4735600.000000000000000,2879700.000000000000000)
Pixel Size = (50.000000000000000,-50.000000000000000)
Corner Coordinates:
Upper Left ( 4735600.000, 2879700.000) ( 15d39'28.78"E, 48d53'13.80"N)
Lower Left ( 4735600.000, 2748850.000) ( 15d31'39.85"E, 47d42'49.53"N)
Upper Right ( 4854650.000, 2879700.000) ( 17d16'27.34"E, 48d47'35.80"N)
Lower Right ( 4854650.000, 2748850.000) ( 17d 6'26.37"E, 47d37'20.45"N)
Center ( 4795125.000, 2814275.000) ( 16d23'29.04"E, 48d15'25.69"N)
Band 1 Block=2381x1 Type=Float32, ColorInterp=Gray
Min=0.000 Max=1.000
Minimum=0.000, Maximum=1.000, Mean=0.037, StdDev=0.103
NoData Value=nan
Trying to link the map inside GRASS GIS' data base
C:\Windows\System32>r.external input=C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif output=water_resources
Reading band 1 of 1...
Link to raster map <water_resources> created.
C:\Windows\System32>r.info water_resources -g
north=2879700
south=2748850
east=4854650
west=4735600
nsres=50
ewres=50
rows=2617
cols=2381
cells=6231077
datatype=FCELL
ncats=0
C:\Windows\System32>r.univar water_resources -g
n=1073298
null_cells=5157779
cells=6231077
min=0.000101275327324402
max=1.00000011920929
range=0.999898843881965
mean=0.124543353658236
mean_of_abs=0.124543353658236
stddev=0.157836355473214
variance=0.0249123151090666
coeff_var=126.732058224752
sum=133672.132394677
Comparing the statistics between gdalinfo
and r.univar
shows obviously an important difference. For the matter of fact, the descriptive statistics shown for the linked raster map, from inside GRASS GIS, are halve the values of the 'real' map.
If instead of linking we import the map, then:
C:\Windows\System32>g.remove raster name=water_resources -f
Removing raster <water_resources>
C:\Windows\System32>r.in.gdal input=C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif output=water_resources
Importing raster map <water_resources>...
100%
C:\Windows\System32>r.info water_resources -g
north=2879700
south=2748850
east=4854650
west=4735600
nsres=50
ewres=50
rows=2617
cols=2381
cells=6231077
datatype=FCELL
ncats=0
C:\Windows\System32>r.univar water_resources -g
n=3604276
null_cells=2626801
cells=6231077
min=0
max=1.00000011920929
range=1.00000011920929
mean=0.0370870966581574
mean_of_abs=0.0370870966581574
stddev=0.103256976655045
variance=0.0106620032279405
coeff_var=278.417525121458
sum=133672.132394677
Again, to confirm the bug (?)
C:\Windows\System32>g.remove raster name=water_resources -f
Removing raster <water_resources>
C:\Windows\System32>r.external input=C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif output=water_resources
Reading band 1 of 1...
Link to raster map <water_resources> created.
C:\Windows\System32>r.univar water_resources -g
n=1073298
null_cells=5157779
cells=6231077
min=0.000101275327324402
max=1.00000011920929
range=0.999898843881965
mean=0.124543353658236
mean_of_abs=0.124543353658236
stddev=0.157836355473214
variance=0.0249123151090666
coeff_var=126.732058224752
sum=133672.132394677
Compare the output of r.univar
after r.external
and after r.in.gdal
!
Further, --overwrite
does not work!
r.in.gdal input=C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif output=water_resources --o
WARNING: Raster map <water_resources> already exists and will be
overwritten
Importing raster map <water_resources>...
100%
C:\Windows\System32>r.univar water_resources -g
n=1073298
null_cells=5157779
cells=6231077
min=0.000101275327324402
max=1.00000011920929
range=0.999898843881965
mean=0.124543353658236
mean_of_abs=0.124543353658236
stddev=0.157836355473214
variance=0.0249123151090666
coeff_var=126.732058224752
sum=133672.132394677
Only after removing the map, and re-importing via r.in.gdal
it works
C:\Windows\System32>g.remove raster name=water_resources -f
Removing raster <water_resources>
C:\Windows\System32>r.in.gdal input=C:\Users\nikos\Desktop\example_data_epsg_3035\raster_x\water_resources.tif output=water_resources --o
Importing raster map <water_resources>...
100%
C:\Windows\System32>r.univar water_resources -g
n=3604276
null_cells=2626801
cells=6231077
min=0
max=1.00000011920929
range=1.00000011920929
mean=0.0370870966581574
mean_of_abs=0.0370870966581574
stddev=0.103256976655045
variance=0.0106620032279405
coeff_var=278.417525121458
sum=133672.132394677