I am trying to create multiple GDALDataset objects. All of them are supposed to be in the .tif format, so for all of them I use the same GDALDriver.
I am working in C++ (Visual Studio) and when I build the solution there are no errors. But when I run it, I get the following error:
Error 4 ‘filename.tif’ not recognised as a supported file format
The weird thing is that f I run the code consecutive times without changing anything, the GDALDataset for which I get the error change randomly. Sometimes I get the error for 5 of the 10 I am creating, sometimes for 9, etc.
I also have an additional question. When I check the tutorials in the GDAL website, when they create an object they have two different ways. For instance, creating a GDALDataset they have this two formats:
GDALDataset* PredictionRaster = (GDALDataset *) IMGdriver->Create("PredictionRaster.tif",XSize,YSize,1,TempBnd->GetRasterDataType(),NULL);
or
GDALDataset* PredictionRaster = IMGdriver->Create("PredictionRaster.tif",XSize,YSize,1,TempBnd->GetRasterDataType(),NULL);
What is the (GDALDataset *) right after the assignment operator?
I also saw that in the initialization of the driver:
GDALDriver* IMGdriver = GetGDALDriverManager()->GetDriverByName("GTiff");
or
GDALDriver* IMGdriver = (GDALDriver*) GetGDALDriverManager()->GetDriverByName("GTiff");
Here are two images showing how the error is random executing exactly the same code and the same build:
For reference, here is the code I am talking about:
#include "stdafx.h"
#include <string>
#include <vector>
#include <array>
#include <iostream>
#include <cassert>
#include <sstream>
#include "gdal_priv.h"
#include "gdal_alg.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include <ogr_spatialref.h>
#include <cmath>
#include <math.h>
class LC_Raster
{
private:
std::string m_parentProductName;
int m_year;
std::string m_rasterName;
GDALDataset* m_SourceRasterDS;
public:
LC_Raster(): m_parentProductName("NLCD"), m_year(2001)
{
std::stringstream os;
os << m_year;
std::string yearAsString;
os >> yearAsString;
m_rasterName = "Recon_" + m_parentProductName + "_" + yearAsString + ".tif";
m_SourceRasterDS = (GDALDataset*) GDALOpen(m_rasterName.c_str(),GA_ReadOnly);
}
LC_Raster(std::string parentProductName , int year ): m_parentProductName(parentProductName), m_year(year)
{
std::stringstream os;
os << m_year;
std::string yearAsString;
os >> yearAsString;
m_rasterName = "Recon_" + m_parentProductName + "_" + yearAsString + ".tif";
m_SourceRasterDS = (GDALDataset*) GDALOpen(m_rasterName.c_str(),GA_ReadOnly);
}
void setProductName (std::string name)
{
m_parentProductName = name;
}
void setRasterYear (int year)
{
m_year = year;
}
std::string getParentProductName () {return m_parentProductName; }
int getRasterYear () {return m_year; }
std::string getFileName () {return m_rasterName; }
GDALDataset* getRasterGDAL () {return m_SourceRasterDS; }
};
class LC_Product
{
private:
std::string m_productName;
std::vector<int> m_productYears;
int m_numberOfRasetrs;
std::vector<LC_Raster> m_rastersOfProduct;
public:
LC_Product(): m_productName("Default_NLCD")
{
int arrayYearsNLCD[] = {2001,2006,2011};
std::vector<int> yearsNLCD (arrayYearsNLCD, arrayYearsNLCD + sizeof(arrayYearsNLCD) / sizeof(int) );
m_productYears = yearsNLCD;
m_numberOfRasetrs = yearsNLCD.size();
m_rastersOfProduct.resize(yearsNLCD.size());
for (int iii=0; iii < yearsNLCD.size(); iii++)
{
m_rastersOfProduct[iii] = LC_Raster("NLCD",yearsNLCD[iii]);
}
}
LC_Product(std::string productName, std::vector<int> productYears): m_productName(productName), m_productYears(productYears)
{
m_numberOfRasetrs = productYears.size();
m_rastersOfProduct.resize(productYears.size());
for (int iii=0; iii < productYears.size(); iii++)
{
m_rastersOfProduct[iii] = LC_Raster(productName,productYears[iii]);
}
}
std::string getProductName () {return m_productName; }
std::vector<int> getProductYears () {return m_productYears; }
int getNumberOfRasters () {return m_numberOfRasetrs; }
LC_Raster getRaster (int year)
{
for (int iii = 0; iii < m_productYears.size(); iii++)
{
if (m_productYears[iii] == year)
{
return m_rastersOfProduct[iii];
}
}
bool yearNotFound = 0;
assert (yearNotFound);
}
std::vector<int> calculateNeighboringYears (int yearToPredict, int deltaT)
{
std::vector <int> neighborYears;
int numberOfNeighboringYears = neighborYears.size(); //This is the same as 0
for (int iii=0; iii < m_productYears.size(); iii++)
{
if ((m_productYears[iii] >= (yearToPredict-deltaT)) && (m_productYears[iii] <= (yearToPredict+deltaT)))
{
numberOfNeighboringYears += 1;
neighborYears.resize(numberOfNeighboringYears);
neighborYears[numberOfNeighboringYears-1] = m_productYears[iii];
}
}
return neighborYears;
}
};
class LC_SetOfProducts
{
private:
std::array<std::string,6> m_productNames;
std::array<LC_Product,6> m_products;
public:
LC_SetOfProducts()
{
m_productNames[0]="BC";
m_productNames[1]="Hist";
m_productNames[2]="LF";
m_productNames[3]="MODIS";
m_productNames[4]="NLCD";
m_productNames[5]="NLCD92";
//Vectors with the year of eahc product
std::vector<int> yearsBC;
for (int yearBC = 1938; yearBC <= 2005; yearBC++)
{
yearsBC.push_back(yearBC);
}
std::vector<int> yearsHist (1,1977);
int arrayYearsLF[] = {2001,2008,2010,2012};
std::vector<int> yearsLF (arrayYearsLF, arrayYearsLF + sizeof(arrayYearsLF) / sizeof(int) );
std::vector<int> yearsMODIS;
for (int yearMODIS = 2001; yearMODIS <= 2012; yearMODIS++)
{
yearsMODIS.push_back(yearMODIS);
}
int arrayYearsNLCD[] = {2001,2006,2011};
std::vector<int> yearsNLCD (arrayYearsNLCD, arrayYearsNLCD + sizeof(arrayYearsNLCD) / sizeof(int) );
std::vector<int> yearsNLCD92 (1,1992);
//initilize array of products
m_products[0] = LC_Product(m_productNames[0],yearsBC);
m_products[1] = LC_Product(m_productNames[1],yearsHist);
m_products[2] = LC_Product(m_productNames[2],yearsLF);
m_products[3] = LC_Product(m_productNames[3],yearsMODIS);
m_products[4] = LC_Product(m_productNames[4],yearsNLCD);
m_products[5] = LC_Product(m_productNames[5],yearsNLCD92);
}
std::array<LC_Product,6> getArrayOfProducts () {return m_products; }
std::array<std::string,6> getArrayOfProductNames () {return m_productNames; }
LC_Product getProductFromName (std::string name)
{
for (int iii = 0; iii < m_products.size(); iii++)
{
if (m_productNames[iii] == name)
{
return m_products[iii];
}
}
bool nameNotFound = 0;
assert (nameNotFound);
}
};
class Probability_Raster
{
private:
int m_LC_type;
GDALDataset* m_RasterDS;
int m_XSize;
int m_YSize;
std::string m_fileName;
public:
Probability_Raster( int LC_type, int XSize, int YSize, double GeoTransformProbability[6], const char *probabilityProjection, GDALDriver* IMGdriver): m_LC_type(LC_type), m_XSize(XSize), m_YSize(YSize)
{
std::stringstream os;
os << LC_type;
std::string LC_TypeAsString;
os >> LC_TypeAsString;
m_fileName = "ProbabilityRaster_" + LC_TypeAsString + ".tif";
GDALDataset* ProbabilityRasterGDAL = IMGdriver->Create(m_fileName.c_str(),XSize,YSize,1,GDALDataType::GDT_Float32,NULL);
ProbabilityRasterGDAL->SetGeoTransform(GeoTransformProbability);
ProbabilityRasterGDAL->SetProjection(probabilityProjection);
m_RasterDS = ProbabilityRasterGDAL;
}
int getLC_type () {return m_LC_type; }
std::string getFileName () {return m_fileName; }
GDALDataset* getRasterGDAL () {return m_RasterDS; }
};
int _tmain(int argc, _TCHAR* argv[])
{
//Register all the formats that the GDAL library supports, including .tif files
GDALAllRegister();
//Load the set of products used in this project: BC, Hist, NLCD, LF, MODIS, NLCD92
LC_SetOfProducts products;
//Define the size of neighboring window in meters and years
double windSizeX = 500;
double windSizeY = 500;
double windSizeT = 5;
//Create array with the LC classes (9 for this case)
const int numOf_LC_classes = 9;
std::array<int,numOf_LC_classes> LCclasses = {1,2,3,4,5,6,7,8,9};
//
/*Creation of empty raster where we would predict land cover*/
//
//In this case the raster has the same resolution and number of cells as NLCD
//Number of cells in the new raster
GDALRasterBand* TempBnd = products.getProductFromName("NLCD").getRaster(2001).getRasterGDAL()->GetRasterBand(1);
int XSize = TempBnd->GetXSize();
int YSize = TempBnd->GetYSize();
std::cout << "The size of the prediction raster is (number of X cells, number of Y cells):n";
std::cout << XSize << ", " << YSize << "n";
//First, specify the driver (format) that the new raster would have (.tif again)
//GDALDriver* IMGdriver = GetGDALDriverManager()->GetDriverByName("GTiff");
GDALDriver* IMGdriver = GetGDALDriverManager()->GetDriverByName("GTiff");
//Crate the GDAL raste robject for the prediction raster
GDALDataset* PredictionRaster = (GDALDataset *) IMGdriver->Create("PredictionRaster.tif",XSize,YSize,1,TempBnd->GetRasterDataType(),NULL);
//Create the geotransformation for the prediction raster (it is taken from NLCD product and can be modified)
//Geotransformation has the coordinates of top left corner, and the resolution of the raster
double GeoTransformPrediction[6];
products.getProductFromName("NLCD").getRaster(2001).getRasterGDAL()->GetGeoTransform(GeoTransformPrediction);
/*GeoTransformPrediction[1]=1000; //Uncomment if you want to change cell size of prediction raster*/
/*GeoTransformPrediction[5]=-1500; //Uncomment if you want to change cell size of prediction raster*/
PredictionRaster->SetGeoTransform(GeoTransformPrediction);
//Create the projection for the prediction raster (it is taken from NLCD product)
const char *predictionProjection = products.getProductFromName("NLCD").getRaster(2001).getRasterGDAL()->GetProjectionRef();
PredictionRaster->SetProjection(predictionProjection);
//
/*Creation of empty rasters where we would predict probabilities for each land cover category (one raster per category)*/
//
std::vector<Probability_Raster> vectorOfProbabilityRasters;
for (int iii = 0; iii < numOf_LC_classes; iii++)
{
vectorOfProbabilityRasters.push_back(Probability_Raster( (iii+1), XSize, YSize, GeoTransformPrediction, predictionProjection, IMGdriver ));
std::cout << vectorOfProbabilityRasters[iii].getFileName() << "n";
}
return 0;
}

