1 /*
2 =========================================================================================
3 以下是一些测试GDAL用法的函数
4 =========================================================================================
5 */
6
7 //描述:测试某类型是否支持CreateCopy和Create
8 //参数:pszFormat-驱动格式,比如“GTIFF”
9 //输出:结果打印到命令行
10 void ISCanCreate(const char *pszFormat)
11 {
12 //determine if a particular format supports Create or CreateCopy it is possible to check the
13 //DCAP_CREATE and DCAP_CREATECOPY metadata on the format driver object.
14
15 GDALDriver *poDriver;
16 char **papszMetadata;
17 GDALAllRegister();
18 poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
19
20 if( poDriver == NULL )
21 exit( 1 );
22
23 char message[100];
24 papszMetadata = poDriver->GetMetadata();
25 if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )
26 sprintf(message, "Driver %s supports Create() method.\n", pszFormat );
27 if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )
28 sprintf(message, "Driver %s supports CreateCopy() method.\n", pszFormat );
29
30 AfxMessageBox(message);
31 }
32
33 //描述:测试读取到DataSet并输出图像的基本信息
34 //参数:pszFilename-文件名称
35 //输出:信息打印到命令行
36 bool OpenFileToDataSet(char* pszFilename)
37 {
38 GDALDataset *poDataset;
39
40 poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
41 if( poDataset != NULL )
42 {
43 double adfGeoTransform[6];
44 char message[100];
45
46 sprintf(message, "Driver: %s/%s\n",
47 poDataset->GetDriver()->GetDescription(),
48 poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
49
50 sprintf(message, "Size is %dx%dx%d\n",
51 poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
52 poDataset->GetRasterCount() );
53
54 if( poDataset->GetProjectionRef() != NULL )
55 sprintf(message, "Projection is `%s'\n", poDataset->GetProjectionRef());
56
57 if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
58 {
59 sprintf(message, "Origin = (%.6f,%.6f)\n",
60 adfGeoTransform[0], adfGeoTransform[3] );
61
62 sprintf(message, "Pixel Size = (%.6f,%.6f)\n",
63 adfGeoTransform[1], adfGeoTransform[5] );
64 }
65
66 AfxMessageBox(message);
67 return true;
68 }else
69 return false;
70 }
71
72 //描述:测试读取一个BAND并输出基本信息
73 //参数:poDataset-数据集
74 //输出:信息打印到命令行
75 void ReadBandInf(GDALDataset* poDataset)
76 {
77 GDALRasterBand *poBand;
78 int nBlockXSize, nBlockYSize;
79 int bGotMin, bGotMax;
80 double adfMinMax[2];
81 char message[100];
82
83 poBand = poDataset->GetRasterBand( 1 );
84 poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
85 sprintf(message, "Block=%dx%d Type=%s, ColorInterp=%s\n",
86 nBlockXSize, nBlockYSize,
87 GDALGetDataTypeName(poBand->GetRasterDataType()),
88 GDALGetColorInterpretationName(
89 poBand->GetColorInterpretation()) );
90
91 adfMinMax[0] = poBand->GetMinimum( &bGotMin );
92 adfMinMax[1] = poBand->GetMaximum( &bGotMax );
93 if( ! (bGotMin && bGotMax) )
94 GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
95
96 sprintf(message, "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );
97
98 if( poBand->GetOverviewCount() > 0 )
99 sprintf(message, "Band has %d overviews.\n", poBand->GetOverviewCount() );
100
101 if( poBand->GetColorTable() != NULL )
102 sprintf(message, "Band has a color table with %d entries.\n",
103 poBand->GetColorTable()->GetColorEntryCount() );
104
105 AfxMessageBox(message);
106 }
107
108 //描述:测试读取一个BAND中的数据块并输出
109 //参数:poBand-数据集的一个波段
110 //输出:信息打印到命令行
111 void PrintBandData(GDALRasterBand *poBand)
112 {
113 double *pafScanline;
114 int nXSize = poBand->GetXSize();
115 int nYSize = poBand->GetYSize();
116 char message[100];
117
118 //从总行数的中间开始读取,边缘部分数据往往相同
119 for (int j=nYSize/2;j<nYSize;j++)
120 {
121 pafScanline = (double *) CPLMalloc(sizeof(double)*nXSize);
122 poBand->RasterIO( GF_Read, 0, j, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0 );
123 for (int i=0;i<nXSize;i++)
124 {
125 sprintf(message,"[%.6f]",*(pafScanline+i));
126 AfxMessageBox(message);
127 }
128 CPLFree(pafScanline);
129 }
130 }
2 =========================================================================================
3 以下是一些测试GDAL用法的函数
4 =========================================================================================
5 */
6
7 //描述:测试某类型是否支持CreateCopy和Create
8 //参数:pszFormat-驱动格式,比如“GTIFF”
9 //输出:结果打印到命令行
10 void ISCanCreate(const char *pszFormat)
11 {
12 //determine if a particular format supports Create or CreateCopy it is possible to check the
13 //DCAP_CREATE and DCAP_CREATECOPY metadata on the format driver object.
14
15 GDALDriver *poDriver;
16 char **papszMetadata;
17 GDALAllRegister();
18 poDriver = GetGDALDriverManager()->GetDriverByName(pszFormat);
19
20 if( poDriver == NULL )
21 exit( 1 );
22
23 char message[100];
24 papszMetadata = poDriver->GetMetadata();
25 if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATE, FALSE ) )
26 sprintf(message, "Driver %s supports Create() method.\n", pszFormat );
27 if( CSLFetchBoolean( papszMetadata, GDAL_DCAP_CREATECOPY, FALSE ) )
28 sprintf(message, "Driver %s supports CreateCopy() method.\n", pszFormat );
29
30 AfxMessageBox(message);
31 }
32
33 //描述:测试读取到DataSet并输出图像的基本信息
34 //参数:pszFilename-文件名称
35 //输出:信息打印到命令行
36 bool OpenFileToDataSet(char* pszFilename)
37 {
38 GDALDataset *poDataset;
39
40 poDataset = (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
41 if( poDataset != NULL )
42 {
43 double adfGeoTransform[6];
44 char message[100];
45
46 sprintf(message, "Driver: %s/%s\n",
47 poDataset->GetDriver()->GetDescription(),
48 poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );
49
50 sprintf(message, "Size is %dx%dx%d\n",
51 poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
52 poDataset->GetRasterCount() );
53
54 if( poDataset->GetProjectionRef() != NULL )
55 sprintf(message, "Projection is `%s'\n", poDataset->GetProjectionRef());
56
57 if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
58 {
59 sprintf(message, "Origin = (%.6f,%.6f)\n",
60 adfGeoTransform[0], adfGeoTransform[3] );
61
62 sprintf(message, "Pixel Size = (%.6f,%.6f)\n",
63 adfGeoTransform[1], adfGeoTransform[5] );
64 }
65
66 AfxMessageBox(message);
67 return true;
68 }else
69 return false;
70 }
71
72 //描述:测试读取一个BAND并输出基本信息
73 //参数:poDataset-数据集
74 //输出:信息打印到命令行
75 void ReadBandInf(GDALDataset* poDataset)
76 {
77 GDALRasterBand *poBand;
78 int nBlockXSize, nBlockYSize;
79 int bGotMin, bGotMax;
80 double adfMinMax[2];
81 char message[100];
82
83 poBand = poDataset->GetRasterBand( 1 );
84 poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
85 sprintf(message, "Block=%dx%d Type=%s, ColorInterp=%s\n",
86 nBlockXSize, nBlockYSize,
87 GDALGetDataTypeName(poBand->GetRasterDataType()),
88 GDALGetColorInterpretationName(
89 poBand->GetColorInterpretation()) );
90
91 adfMinMax[0] = poBand->GetMinimum( &bGotMin );
92 adfMinMax[1] = poBand->GetMaximum( &bGotMax );
93 if( ! (bGotMin && bGotMax) )
94 GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);
95
96 sprintf(message, "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );
97
98 if( poBand->GetOverviewCount() > 0 )
99 sprintf(message, "Band has %d overviews.\n", poBand->GetOverviewCount() );
100
101 if( poBand->GetColorTable() != NULL )
102 sprintf(message, "Band has a color table with %d entries.\n",
103 poBand->GetColorTable()->GetColorEntryCount() );
104
105 AfxMessageBox(message);
106 }
107
108 //描述:测试读取一个BAND中的数据块并输出
109 //参数:poBand-数据集的一个波段
110 //输出:信息打印到命令行
111 void PrintBandData(GDALRasterBand *poBand)
112 {
113 double *pafScanline;
114 int nXSize = poBand->GetXSize();
115 int nYSize = poBand->GetYSize();
116 char message[100];
117
118 //从总行数的中间开始读取,边缘部分数据往往相同
119 for (int j=nYSize/2;j<nYSize;j++)
120 {
121 pafScanline = (double *) CPLMalloc(sizeof(double)*nXSize);
122 poBand->RasterIO( GF_Read, 0, j, nXSize, 1, pafScanline, nXSize, 1, GDT_Float32, 0, 0 );
123 for (int i=0;i<nXSize;i++)
124 {
125 sprintf(message,"[%.6f]",*(pafScanline+i));
126 AfxMessageBox(message);
127 }
128 CPLFree(pafScanline);
129 }
130 }