質問

NOTE: I added both C and C++ tags since I tried with the C++ version and the C version of the API.

I have to load a geotiff file in order to show it along a car trajectory. First, I open the file using gdal C++ API to extract some information from it:

double  geo_transform[6];
GDALDataset* data = (GDALDataset*) GDALOpenShared( "MyRaster.tif", GA_ReadOnly );
if(data) {
  size[0] = data->GetRasterXSize();
  size[1] = data->GetRasterYSize();
  qDebug() << "RASTER TOTAL BANDS:" << data->GetRasterCount();
  if( CE_None == data->GetGeoTransform( geo_transform ) ) {
    qDebug() << "RASTER TL:" << geo_transform[0] << geo_transform[3];
    qDebug() << "RASTER SIZE:" << size[0] << size[1];
    qDebug() << "RASTER ROTATIONS:" << geo_transform[2] << geo_transform[4];
    qDebug() << "PIXEL SIZE:" << geo_transform[1] << geo_transform[5];
    toret = true;
  }

  /*
  *  Retrieve the pixel size and top left corner and store it in a proper variable.
  */
  GDALClose(data);

Then, in a different thread (launched via QThread) I "load" the image into a QPixmap (which is a variable of the class where I load the raster. In the constructor, it is initialized to an empty pixmap):

temp_pixmap.load( "MyRaster.tif" );

This seems to work, as the raster is shown and I can zoom it, pan it, etc.

The problem is that I can only open the same file once. If I want to have the raster loaded in two differents pixmaps, so the first one can be attached to the trajectory and the second one can be used to make annotations, the second one is loaded in blank.

I have tried these variations, but with no success:

Variation 1: Having the GDALDataSource pointer as a variable of the class and calling GDALClose in the destructor.

Variation 2: Instead of opening the raster with GDALOpen, I tried it with GDALOpenShared and close it in the destructor.

Variation 3: Using the C version:

  GDALDatasetH  hDataset;
  hDataset = GDALOpen( Element::getFilePath().c_str(), GA_ReadOnly );

  if( hDataset != NULL ) {
    GDALDriverH   hDriver;

    hDriver = GDALGetDatasetDriver( hDataset );

    if( GDALGetGeoTransform( hDataset, geo_transform ) == CE_None ) {
     // Get the size and top left corner...
    }
  }
  GDALClose(hDataset);

In the Closing the Dataset section of the API tutorial there's this:

Forgetting to call GDALClose on a dataset opened in update mode in a popular format like GTiff will likely result in being unable to open it afterwards

I have opened the dataset if READ_ONLY mode and made sure to call always to Close. So might the problem be in the Qt side when it loads the raster into the pixmap?

NEW:

After talking to a co-worker that had the same problem, we think that the problem is in the side of gdal, as it seems that the raster is not closed properly and this forces the behaviour explaining in the API Tutorial. I'll keep on investigating...

役に立ちましたか?

解決

NOTE: This is temporary and not satisfactory. I'm still searching for a better solution. So if anyone knows a way, post an answer and I will gratefully accept and upvote it.

The solution I'm using now is closing the raster whenever I need to inspect the other one. Our app has a tree widget that shows how many items we have open (and their type). So when we select the raster that is linked to the trajectory, we simply close it if it's loaded (like in this case, that we need to have it open twice) and re-open it.

Then, when we need to work on the raster alone, we have to close it in the linked node and reopen it in the I'm all alone here node.

A real pain.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top