Skip to content

Commit

Permalink
open byte raster
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomei committed Oct 20, 2023
1 parent 730f732 commit 87d9750
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
4 changes: 4 additions & 0 deletions agrolib/gis/gis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ namespace gis
{
nrRows = 0;
nrCols = 0;
nrBytes = 4;
cellSize = NODATA;
flag = NODATA;
}
Expand All @@ -168,6 +169,7 @@ namespace gis
{
nrRows = 0;
nrCols = 0;
nrBytes = 4;
dx = NODATA;
dy = NODATA;
flag = NODATA;
Expand Down Expand Up @@ -358,8 +360,10 @@ namespace gis
mapTime.setNullTime();
minimum = NODATA;
maximum = NODATA;

header->nrRows = 0;
header->nrCols = 0;
header->nrBytes = 4;
header->cellSize = NODATA;
header->llCorner.initialize();

Expand Down
2 changes: 2 additions & 0 deletions agrolib/gis/gis.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
public:
int nrRows;
int nrCols;
int nrBytes;
double dx, dy;
float flag;
Crit3DGeoPoint llCorner;
Expand All @@ -116,6 +117,7 @@
public:
int nrRows;
int nrCols;
int nrBytes;
double cellSize;
float flag;
Crit3DUtmPoint llCorner;
Expand Down
62 changes: 52 additions & 10 deletions agrolib/gis/gisIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <math.h>
#include <algorithm>

#include "commonConstants.h"
#include "gis.h"

using namespace std;
Expand Down Expand Up @@ -113,7 +112,7 @@ namespace gis
* \param error string
* \return true on success, false otherwise
*/
bool readEsriGridHeader(string fileName, gis::Crit3DRasterHeader *header, string &error)
bool readEsriGridHeader(string fileName, gis::Crit3DRasterHeader *header, string &errorStr)
{
string myLine, myKey, upKey, valueStr;
int nrKeys = 0;
Expand All @@ -123,7 +122,7 @@ namespace gis

if (!myFile.is_open())
{
error = "Missing file: " + fileName;
errorStr = "Missing file: " + fileName;
return false;
}

Expand All @@ -145,7 +144,18 @@ namespace gis
else if (upKey == "NROWS")
header->nrRows = stoi(valueStr);

// LLCORNER is the lower-left corner
else if (upKey == "DATATYPE")
{
// non standard key - derived from envi
header->nrBytes = stoi(valueStr);
if (header->nrBytes > 4)
{
errorStr = "Wrong data type:" + valueStr + " The maximum allowed is 4 (float).";
return false;
}
}

// LLCORNER = Lower Left corner
else if (upKey == "XLLCORNER")
header->llCorner.x = stod(valueStr);

Expand All @@ -163,7 +173,7 @@ namespace gis

if (nrKeys < 6)
{
error = "Missing keys in header file.";
errorStr = "Missing keys in header file.";
return false;
}

Expand Down Expand Up @@ -214,10 +224,10 @@ namespace gis

else if (upKey == "DATATYPE")
{
int type = stoi(valueStr);
if (type != 4)
header->nrBytes = stoi(valueStr);
if (header->nrBytes > 4)
{
errorStr = "Only data type 4 (float) is allowed.";
errorStr = "Wrong data type:" + valueStr + " The maximum allowed is 4 (float).";
return false;
}
}
Expand Down Expand Up @@ -315,9 +325,41 @@ namespace gis
return false;
}

for (int row = 0; row < rasterGrid->header->nrRows; row++)
if (rasterGrid->header->nrBytes == 4)
{
fread (rasterGrid->value[row], sizeof(float), unsigned(rasterGrid->header->nrCols), filePointer);
// float
for (int row = 0; row < rasterGrid->header->nrRows; row++)
{
fread (rasterGrid->value[row], sizeof(float), unsigned(rasterGrid->header->nrCols), filePointer);
}
}
else if (rasterGrid->header->nrBytes == 1)
{
// byte
byte *rowValues = new byte[unsigned(rasterGrid->header->nrCols)];
for (int row = 0; row < rasterGrid->header->nrRows; row++)
{
fread (rowValues, sizeof(byte), unsigned(rasterGrid->header->nrCols), filePointer);
for(int col = 0; col < rasterGrid->header->nrCols; col++)
{
rasterGrid->value[row][col] = float(rowValues[col]);
}
}
delete[] rowValues;
}
else if (rasterGrid->header->nrBytes == 2)
{
// short
short int *rowValues = new short int[unsigned(rasterGrid->header->nrCols)];
for (int row = 0; row < rasterGrid->header->nrRows; row++)
{
fread (rowValues, sizeof(short int), unsigned(rasterGrid->header->nrCols), filePointer);
for(int col = 0; col < rasterGrid->header->nrCols; col++)
{
rasterGrid->value[row][col] = float(rowValues[col]);
}
}
delete[] rowValues;
}

fclose (filePointer);
Expand Down
2 changes: 1 addition & 1 deletion agrolib/project/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ bool Project::loadDEM(QString myFileName)
logInfo("Digital Elevation Model = " + myFileName);

// check nodata
if (DEM.header->flag != NODATA)
if (! isEqual(DEM.header->flag, NODATA))
{
QString infoStr = "WARNING: " + QString::number(DEM.header->flag) + " is not a valid NODATA value for DEM!";
infoStr += " It will be converted in: " + QString::number(NODATA);
Expand Down

0 comments on commit 87d9750

Please sign in to comment.