gdal找到dem中距离待求点最近的已知高程点c++

时间: 2024-05-16 09:15:24 浏览: 5
可以使用GDAL库中的GDALRasterBand类来实现这个功能。以下是一个简单的示例代码: ```c++ #include <iostream> #include "gdal_priv.h" #include "cpl_conv.h" // for CPLMalloc() int main() { // Open the DEM file GDALAllRegister(); GDALDataset *pDataset = (GDALDataset *) GDALOpen("dem.tif", GA_ReadOnly); if (pDataset == NULL) { std::cerr << "Failed to open DEM file!" << std::endl; return 1; } // Get the raster band GDALRasterBand *pBand = pDataset->GetRasterBand(1); // Get the geo-transform double adfGeoTransform[6]; pDataset->GetGeoTransform(adfGeoTransform); // Define the point to query double x = 123.45; double y = 67.89; // Convert the point to pixel coordinates int nPixel, nLine; pBand->GetGeoTransform(adfGeoTransform, x, y, &nPixel, &nLine); // Read the pixel value float fValue; pBand->RasterIO(GF_Read, nPixel, nLine, 1, 1, &fValue, 1, 1, GDT_Float32, 0, 0); // Print the pixel value std::cout << "Pixel value at (" << x << ", " << y << ") is " << fValue << std::endl; // Close the dataset GDALClose(pDataset); return 0; } ``` 该示例代码首先打开了DEM文件,然后获取了第一个波段(假设DEM只有一个波段)。接下来,它获取了DEM的地理变换,用于将点坐标转换为像素坐标。然后,它读取了像素值,并将其输出到控制台。 如果要找到离查询点最近的已知高程点,您可以在DEM中查询一系列点,并找到其中距离查询点最近的点。以下是一个示例代码: ```c++ #include <iostream> #include "gdal_priv.h" #include "cpl_conv.h" // for CPLMalloc() #include <vector> #include <cmath> struct Point { double x; double y; float z; }; int main() { // Open the DEM file GDALAllRegister(); GDALDataset *pDataset = (GDALDataset *) GDALOpen("dem.tif", GA_ReadOnly); if (pDataset == NULL) { std::cerr << "Failed to open DEM file!" << std::endl; return 1; } // Get the raster band GDALRasterBand *pBand = pDataset->GetRasterBand(1); // Get the geo-transform double adfGeoTransform[6]; pDataset->GetGeoTransform(adfGeoTransform); // Define the query point double x = 123.45; double y = 67.89; // Convert the query point to pixel coordinates int nPixel, nLine; pBand->GetGeoTransform(adfGeoTransform, x, y, &nPixel, &nLine); // Define the search radius in pixels int nRadius = 10; // Define the search area in pixels int nLeft = nPixel - nRadius; int nTop = nLine - nRadius; int nWidth = 2 * nRadius + 1; int nHeight = 2 * nRadius + 1; // Read the pixel values in the search area std::vector<float> vecValues(nWidth * nHeight); pBand->RasterIO(GF_Read, nLeft, nTop, nWidth, nHeight, &vecValues[0], nWidth, nHeight, GDT_Float32, 0, 0); // Convert the search area to geographic coordinates std::vector<Point> vecPoints(nWidth * nHeight); for (int i = 0; i < nWidth; ++i) { for (int j = 0; j < nHeight; ++j) { int nIndex = i * nHeight + j; double x = adfGeoTransform[0] + (nLeft + i) * adfGeoTransform[1] + (nTop + j) * adfGeoTransform[2]; double y = adfGeoTransform[3] + (nLeft + i) * adfGeoTransform[4] + (nTop + j) * adfGeoTransform[5]; vecPoints[nIndex].x = x; vecPoints[nIndex].y = y; vecPoints[nIndex].z = vecValues[nIndex]; } } // Find the point closest to the query point Point closestPoint; closestPoint.z = std::numeric_limits<float>::max(); for (const auto &point : vecPoints) { double dx = point.x - x; double dy = point.y - y; double distance = std::sqrt(dx*dx + dy*dy); if (distance < nRadius && point.z < closestPoint.z) { closestPoint = point; } } // Print the closest point std::cout << "Closest point to (" << x << ", " << y << ") is (" << closestPoint.x << ", " << closestPoint.y << ", " << closestPoint.z << ")" << std::endl; // Close the dataset GDALClose(pDataset); return 0; } ``` 该示例代码首先定义了要查询的点,并将其转换为像素坐标。然后,它定义了一个搜索半径,并使用该半径在DEM中查询像素值。接下来,它将搜索区域中的每个像素转换为地理坐标,并将其保存在一个点向量中。然后,它找到最靠近查询点的点,并将其输出到控制台。 请注意,此示例代码仅在搜索区域内搜索点。如果您需要在整个DEM中搜索点,则需要更改搜索区域并读取整个DEM,这可能会非常耗时。

相关推荐

最新推荐

recommend-type

在python中利用GDAL对tif文件进行读写的方法

今天小编就为大家分享一篇在python中利用GDAL对tif文件进行读写的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

C++实现分水岭算法(Watershed Algorithm)

主要为大家详细介绍了C++实现分水岭算法Watershed Algorithm,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

Java用GDAL读写shapefile的方法示例

Shapefile文件是描述空间数据的几何和属性特征的非拓扑实体矢量数据结构的一种格式,由ESRI公司开发。这篇文章给大家介绍了Java如何用GDAL读写shapefile的方法示例,有需要的朋友们可以参考借鉴,下面来一起看看吧。
recommend-type

python gdal安装与简单使用

主要介绍了python gdal安装与简单使用,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

python+gdal+遥感图像拼接(mosaic)的实例

主要介绍了python+gdal+遥感图像拼接(mosaic)的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。