// gdal_geotiff.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "gdal_priv.h"
#include <iostream>
#include <iomanip>
#pragma comment(lib, "gdal_i.lib")
#define BYTE short //方便数据类型的修改
using namespace std;
int main(int argc, char* argv[])
{
int num_iamge_size=0;
BYTE *pafScanblock1; //开辟缓存区
char *file_path_name="C:/Users/afa/Desktop/srtm_59_04/srtm_59_04.tif";
GDALDataset *poDataset; //GDAL数据集
GDALAllRegister(); //注册所有的驱动
poDataset = (GDALDataset *) GDALOpen(file_path_name, GA_ReadOnly );
if( poDataset == NULL )
{
cout<<"fail in open files!!!"<<endl;
return 0;
}
//获取图像波段
GDALRasterBand *poBand1;
poBand1=poDataset->GetRasterBand(1);
//获取图像的尺寸
int nImgSizeX=poDataset->GetRasterXSize();
int nImgSizeY=poDataset->GetRasterYSize();
//获取坐标变换系数
double trans[6];
CPLErr aaa=poDataset->GetGeoTransform(trans);
//读取图像高程数据
double Xgeo,Ygeo;
pafScanblock1 = (BYTE *) CPLMalloc(sizeof(BYTE)*(nImgSizeX)*(nImgSizeY));
poBand1->RasterIO( GF_Read, 0, 0,nImgSizeX,nImgSizeY,pafScanblock1,nImgSizeX,nImgSizeY,GDALDataType(poBand1->GetRasterDataType()),0, 0 );
for(int i=0;i<(nImgSizeX-5990);i++)
{
for(int j=0;j<(nImgSizeY-5990);j++)
{
BYTE elevation=*pafScanblock1;
Xgeo=trans[0]+i*trans[1]+j*trans[2];
Ygeo=trans[3]+i*trans[4]+j*trans[5];
num_iamge_size++;
pafScanblock1++;
cout<<setprecision(15)<<Xgeo<<" "<<Ygeo<<" "<<elevation<<endl;
}
}
cout<<"总计:"<<num_iamge_size<<endl;
delete poDataset;
return 0;
}