pihmLIBS/pickGridValue.cpp

Go to the documentation of this file.
00001 #include "pickGridValue.h"
00002 //#include <qgis.h>
00003 
00004 using namespace std;
00005 
00006 double readValue ( void *data, GDALDataType type, int index )
00007 {
00008   double val;
00009 
00010   switch ( type )
00011   {
00012   case GDT_Byte:
00013     return (double) ((GByte *)data)[index];
00014     break;
00015   case GDT_UInt16:
00016     return (double) ((GUInt16 *)data)[index];
00017     break;
00018   case GDT_Int16:
00019     return (double) ((GInt16 *)data)[index];
00020     break;
00021   case GDT_UInt32:
00022     return (double) ((GUInt32 *)data)[index];
00023     break;
00024   case GDT_Int32:
00025     return (double) ((GInt32 *)data)[index];
00026     break;
00027   case GDT_Float32:
00028     return (double) ((float *)data)[index];
00029     break;
00030   case GDT_Float64:
00031     val = ((double *)data)[index];
00032     return (double) ((double *)data)[index];
00033     break;
00034   default:
00035     cout<<"Data type "<<type<<" is not supported";
00036   }
00037   return 0.0;
00038 }
00039 
00040 static double Xmax, Xmin, Ymax, Ymin;
00041 static double Xres, Yres;
00042 
00043 void getExtent(GDALDataset *temp, double *ranges){
00044         //cout<<"IN : getExtent\n";
00045         double adfGeoTransform[6];
00046         temp->GetGeoTransform(adfGeoTransform);
00047         double myXMaxDouble = adfGeoTransform[0] + temp->GetRasterXSize() * adfGeoTransform[1] + temp->GetRasterYSize() * adfGeoTransform[2];
00048         double myYMinDouble = adfGeoTransform[3] + temp->GetRasterXSize() * adfGeoTransform[4] + temp->GetRasterYSize() * adfGeoTransform[5];
00049 
00050         Xmax = myXMaxDouble;
00051         // The affine transform reduces to these values at the
00052         // top-left corner of the raster
00053         Xmin = adfGeoTransform[0];
00054         Ymax = adfGeoTransform[3];
00055         Ymin = myYMinDouble; 
00056 
00057         int rasterXDimInt = temp->GetRasterXSize();
00058         int rasterYDimInt = temp->GetRasterYSize(); 
00059 
00060         Xres = (Xmax - Xmin) / rasterXDimInt;
00061         //cout<<"Xres= "<<Xres<<"\n";
00062         Yres = (Ymax - Ymin) / rasterYDimInt;
00063         //cout<<"Yres= "<<Yres<<"\n";
00064         ranges[0]=Xmin;
00065         ranges[1]=Xmax;
00066         ranges[2]=Ymin;
00067         ranges[3]=Ymax;
00068         ranges[4]=Xres;
00069         ranges[5]=Yres;
00070         return; 
00071         //cout<<"OUT: getExtant\n";
00072 }
00073 
00074 
00075 double getRasterValue(GDALDataset* layer, int bandNumber, double x, double y, double *ranges){
00076         //cout<<"IN : getRasterValue\n";
00077         //cout<<"Xres= "<<Xres<<"\n";
00078         //cout<<"Yres= "<<Yres<<"\n";
00079         
00080         //GDALDataset* layer;
00081         //GDALAllRegister();
00082         //layer = (GDALDataset*)GDALOpen(layerName, GA_ReadOnly);
00083         GDALRasterBand *band = layer->GetRasterBand(bandNumber); //starts with 1
00084         GDALDataType type = band->GetRasterDataType();
00085         int size = GDALGetDataTypeSize(type) / 8;
00086         void *data = CPLMalloc(size);
00087         int col = (int) floor( (x-ranges[0])/ranges[4] );
00088         int row = (int) floor( (ranges[3]-y)/ranges[5] );
00089         //cout<<"col = "<<col<<"\n";
00090         //cout<<"row = "<<row<<"\n";
00091         //band->ReadBlock(col, row, data);
00092         CPLErr err = band->RasterIO(GF_Read, col, row, 1, 1, data, 1, 1, type, 0, 0);
00093         //cout<<"OUT: getRasterValue\n";
00094         return readValue(data, type, 0);
00095 } 
00096 
00097 /* 
00098 main(){
00099         GDALDataset* temp;
00100         GDALAllRegister();
00101         temp = (GDALDataset*)GDALOpen("test/w001001.adf", GA_ReadOnly); */
00102         /*
00103         cout<<"X Size= "<<temp->GetRasterXSize()<<"\n";
00104         cout<<"Y Size= "<<temp->GetRasterYSize()<<"\n";
00105         cout<<"Count = "<<temp->GetRasterCount()<<"\n";
00106         //cout<<"Count = "<<temp->GetRasterCount()<<"\n";
00107         //GDALDataType data[3];
00108         GDALRasterBand *band = temp->GetRasterBand(1);
00109         cout<<"X     = "<<band->GetXSize()<<"\n";
00110         GDALDataType type = band->GetRasterDataType();
00111         int size = GDALGetDataTypeSize(type) / 8;
00112         void *data = CPLMalloc(size);
00113         //temp->RasterIO(GF_Read, 1, 1, 1, 1, data, 1, 1, type, 1, NULL, 0, 0, 0);
00114         band->ReadBlock(0, 1, data);
00115         cout<<"data= "<<readValue ( data, type, 0 );
00116         //for(int i=0; i<3; i++)
00117         //      cout<<"data= "<<data[i]<<"\n";
00118         */
00119         /*
00120         cout<<"\n*********************\n";
00121         getExtent(temp);
00122         cout<<getRasterValue("test", 1, -113.042284, 42.617891);
00123         cout<<"\n*********************\n";                  
00124                        
00125         //cout<<"\n"<<myXMaxDouble<<" "<<myYMinDouble<<"\n";
00126 }*/

Generated on Sun Aug 5 17:33:59 2007 for PIHMgis by  doxygen 1.5.2