获取一系列点的值
点的信息存在txt中,使用read_point 类读取
txt分为三列 点名 经度 纬度
n1 3.1 49.991
n2 3.2 49.992
n3 3.3 49.993
n4 3.4 49.994
n5 3.6 49.996
n2 3.5 49.992
n2 3.33 49.9923
#include "read_point.h"
#include<fstream>
#include<iostream>
#include<sstream>
void read_point::read_point_(string file_path)
{
fstream file(file_path);
if(!file)
{
cout << "fail to open the file" <<endl;
//return ;
exit(0);
}
string line;
int number=0;
while (getline(file, line))
{
number++;
}
this->number=number;
file.close();
this->name=new string[number];
this->lat=new double[number];
this->lon=new double[number];
fstream file1(file_path);
for(int i=0;i<number;i++)
{
getline(file1, line);
stringstream line1(line);
line1>>this->name[i];
line1>>this->lon[i];
line1>>this->lat[i];
}
}
void image_process::get_point_value(string image_file, string point_file, string result_file)
{
read_point point;
point.read_point_(point_file);
GDALAllRegister();
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
GDALDataset* pInDataset1 = (GDALDataset*)GDALOpen(image_file.c_str(), GA_ReadOnly);
if (pInDataset1 == NULL)
{
cout << "无法打开图像!" << endl;
GDALDestroyDriverManager();
}
int nImgWidth = pInDataset1->GetRasterXSize();
int nImgHeight = pInDataset1->GetRasterYSize();
double adfGeoTransform[6];
pInDataset1->GetGeoTransform(adfGeoTransform);
double lon = 0.0, lat = 0.0;//经度 纬度
int dx, dy;
GDALRasterBand* poBand = pInDataset1->GetRasterBand(1);
float* pData = new float[point.number];
for (int i = 0; i < point.number; i++)
{
lon = point.lon[i];
lat = point.lat[i];
dx = (lon - adfGeoTransform[0]) / adfGeoTransform[1];
dy = (lat - adfGeoTransform[3]) / adfGeoTransform[5];
if (dx > nImgWidth || dy > nImgHeight)
{
cout << "beyond the max" << endl;
return;
}
cout << dx << " " << dy << endl;
poBand->RasterIO(GF_Read, dx, dy, 1, 1, (pData + i), 1, 1, GDT_Float32, 0, 0, 0);
}
for (int i = 0; i < point.number; i++)
{
cout << pData[i] << endl;
}
ofstream file(result_file, fstream::out);
for (int i = 0; i < point.number; i++)
{
file << point.name[i] << " " << point.lon[i] << " " << point.lat[i] << " " << pData[i] << endl;
}
file.close();
GDALClose(pInDataset1);
}
最后获取的值会追加到点信息的后面
n1 3.1 49.991 2.42302
n2 3.2 49.992 2.4186
n3 3.3 49.993 2.41691
n4 3.4 49.994 2.41694
n5 3.6 49.996 2.40511
n2 3.5 49.992 2.40511
n2 3.33 49.9923 2.42717