c++中使用GDAL获取图像上某个点的值

获取一系列点的值
点的信息存在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
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扎不下村村长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值