Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use panSrcBands when accessing the source data #295

Closed
wants to merge 1 commit into from

Conversation

tomhughes
Copy link

Commit 65fd09c introduced an optimisation when there is only a single band being read, but when reading from the source it is using panDstBands to find the number of that band which will be wrong if the band number is different in the source and destination file.

This is a simple test case, extracted from one of the node-gdal tests, which demonstrates the problem:

#include <stdlib.h>

#include <gdal_priv.h>
#include <gdalwarper.h>
#include <ogrsf_frmts.h>

int main(int argc, char **argv)
{
  GDALAllRegister();

  GDALDriver *mem = GetGDALDriverManager()->GetDriverByName("MEM");
  GDALDataset *src = (GDALDataset *)GDALOpenEx("./rpm/BUILD/node-gdal-0.9.6-fedora/test/data/sample.tif", GDAL_OF_READONLY, NULL, NULL, NULL);
  GDALDataset *dst = mem->Create("temp", 208, 127, 2, GDT_Int16, nullptr);
  GDALWarpOptions *options = GDALCreateWarpOptions();
  int srcBands[1] = { 1 };
  int dstBands[1] = { 2 };
  double transform[6] = { -110.3765574645805, 0.0005, 0, 44.59676651822825, 0, -0.0005 };

  options->hSrcDS = src;
  options->hDstDS = dst;
  options->nDstAlphaBand = 1;
  options->nBandCount = 1;
  options->panSrcBands = srcBands;
  options->panDstBands = dstBands;

  const char *s_srs_wkt = src->GetProjectionRef();
  char *t_srs_wkt;

  OGRSpatialReference::GetWGS84SRS()->exportToWkt(&t_srs_wkt);

  dst->SetProjection(t_srs_wkt);
  dst->SetGeoTransform(transform);

  GDALReprojectImage(options->hSrcDS, s_srs_wkt,
                     options->hDstDS, t_srs_wkt,
                     options->eResampleAlg, 0.0, 0,
                     nullptr, nullptr, options);

  exit(0);
}

without this change that will error as it tries to read band 2 from a source that only has one band:

ERROR 5: ./rpm/BUILD/node-gdal-0.9.6-fedora/test/data/sample.tif: GDALDataset::GetRasterBand(2) - Illegal band #

@rouault
Copy link
Member

rouault commented Jan 24, 2018

Committed in trunk r41327 and branches/2.2 r41328. Thanks !

@rouault rouault closed this Jan 24, 2018
rouault added a commit that referenced this pull request Jan 24, 2018
rouault added a commit that referenced this pull request Jan 24, 2018
…ixes #295, patch by tomhuges)

git-svn-id: https://svn.osgeo.org/gdal/trunk@41327 f0d54148-0727-0410-94bb-9a71ac55c965
kwrobot pushed a commit to aashish24/gdal-svn that referenced this pull request Jan 24, 2018
@tomhughes tomhughes deleted the pansrcbands branch January 25, 2018 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants