2

I'm trying to call GDALOpen from a function to open a tiff raster and then use the dataset further in my main or other functions. This is the code I have so far:

int main( int argc, char* argv[] ){
GDALAllRegister();
GDALDataset *poRasterDataset;
openRaster(poRasterDataset, argv[1]);
...}

void openRaster(GDALDataset * poDataset, char* rasterName){
poDataset = (GDALDataset *) GDALOpen(rasterName, GA_ReadOnly);
...
}

My problem is if I do it this way my program crashes because it finds the poRasterDataset to be empty in main after calling openRaster, but if I call GDALOpen from main I have no problem using the dataset. I'm not very experienced in C++ but I suppose it has to do with how I'm passing poDataset in main after calling openRaster.

1 Answer 1

2

This might help you, it's from working code:

char* Raster = new char[FullPathMax];
wcstombs(Raster,argv[2],FullPathMax);
GDALAllRegister();
GDALDataset* SourceRasterDS = (GDALDataset*) GDALOpen(Raster,GA_ReadOnly);

So there's nothing wrong with your implementation per se. The problem with your code is that in your code block:

GDALDataset *poRasterDataset;
openRaster(poRasterDataset, argv[1]);

C++ wont let you assign pointers inside a function, you don't actually pass the pointer but pass the pointers' value, have a read of https://stackoverflow.com/questions/416162/assignment-inside-function-that-is-passed-as-pointer for more info. Consider:

int main( int argc, char* argv[] ){
GDALAllRegister();
GDALDataset *poRasterDataset = openRaster(argv[1]);
...}

GDALDataset* openRaster(char* rasterName){
GDALDataset poDataset = (GDALDataset *) GDALOpen(rasterName, GA_ReadOnly);
return poDataset;
}

As an alternative. Also, be aware that argv[1] isn't necessarily a char*, it could be a _TCHAR* which doesn't work with GDALOpen, hence I've used wcstombs (safer version wcstombs_s) to convert the _TCHAR pointer to char(byte) pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.