因为对于globalmap不熟悉,不怎么怎么修改高程,好像也没有这功能。

干脆自己手动修改了高程图tiff了

由于自身一直使用osg的 自己使用了osgDB直接读取tiff,修改后保存的。

同事小周一直研究gdal,她使用了gdal库直接改的,事实证明在专业gis处理上还是gdal更合适,现在把两种方式都总结一下:

第一种:通过osgDB修改tiff

  1. #include <osg/Image>
  2. #include <osgViewer/Viewer>
  3. #include <osgEarth/Registry>
  4. #include <osgEarth/ImageToHeightFieldConverter>
  5. #include <osgEarth/ImageUtils>
  6. #include <osgEarth/FileUtils>
  7. #include <osgEarth/MapFrame>
  8. #include <osgDB/FileUtils>
  9. #include <osgDB/FileNameUtils>
  10. #include <osgDB/ReadFile>
  11. #include <osgDB/WriteFile>
  12. #include <math.h>
  13. using namespace osgEarth;
  14.  
  15. void floating(osg::HeightField* &hf)
  16. {
  17. float floatingData = 179.0;
  18. float fBegin = 140.0;
  19. for (unsigned col = ; col < hf->getNumColumns(); ++col)
  20. {
  21. for (unsigned row = ; row < hf->getNumRows(); ++row)
  22. {
  23. float height = hf->getHeight(col, row);
  24. if (height < fBegin)
  25. {
  26. hf->setHeight(col, row, floatingData);
  27. }
  28. }
  29. }
  30. }
  31.  
  32. void seaboard(osg::HeightField* &hf)
  33. {
  34.  
  35. float fBegin = 0.1;
  36. //float fEnd = 0.000001;
  37. float fLowestValue = 1000.0;
  38. int fWide = 100.0;
  39. int *fFlag = new int[hf->getNumColumns()*hf->getNumRows()];
  40.  
  41. for (unsigned col = ; col < hf->getNumColumns(); ++col)
  42. {
  43. for (unsigned row = ; row < hf->getNumRows(); ++row)
  44. {
  45. fFlag[col*hf->getNumRows() + row] = ;
  46. float height = hf->getHeight(col, row);
  47. if (height < fBegin)
  48. {
  49. fFlag[col*hf->getNumRows() + row] = ;
  50. hf->setHeight(col, row, - fLowestValue);
  51.  
  52. /*简单的计算
  53. float newh = -1000.0;
  54. if(height > 0.00001)
  55. newh = 0.1 - (0.1 - height)/ (0.1-0.00001)*1000.0;
  56. hf->setHeight(col, row, newh);*/
  57. }
  58. }
  59. }
  60.  
  61. for (int i = ; i < hf->getNumColumns()*hf->getNumRows(); i++)
  62. {
  63. if (fFlag[i] == )//如果这值在海面以下
  64. {
  65. bool isNearSide = false;
  66. int nowX = i / hf->getNumRows();
  67. int nowY = i%hf->getNumRows();
  68. for (int j = ; j <= fWide; j++)
  69. {
  70. //从离此值最近的值开始找附近的岸边,往外延伸
  71. //向东南西北四个方向找,没层都遍历一圈
  72. for (int x = ; x <= j; x++)
  73. {
  74. //如果找到有岸边
  75. int fDifValueX = x;
  76. int fDifValueY = j - x;
  77. int realX = nowX - fDifValueX;
  78. if (realX > )
  79. {
  80. int realY = nowY - fDifValueY;
  81. if (realY > )
  82. {
  83. if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
  84. isNearSide = true;
  85. }
  86. realY = nowY + fDifValueY;
  87. if (realY < hf->getNumRows())
  88. {
  89. if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
  90. isNearSide = true;
  91. }
  92. }
  93.  
  94. realX = nowX + fDifValueX;
  95. if (realX < hf->getNumColumns())
  96. {
  97. int realY = nowY - fDifValueY;
  98. if (realY > )
  99. {
  100. if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
  101. isNearSide = true;
  102. }
  103. realY = nowY + fDifValueY;
  104. if (realY < hf->getNumRows())
  105. {
  106. if (fFlag[realX*hf->getNumRows() + realY] == )//如果是岸边
  107. isNearSide = true;
  108. }
  109. }
  110. }
  111.  
  112. //查找这个范围内是否有值,如果有值则用此值
  113. if (isNearSide)
  114. {
  115. float fRealHeight = fBegin - j * fLowestValue / fWide;
  116. hf->setHeight((i / hf->getNumRows()), (i % hf->getNumRows()), fRealHeight);
  117. break;//退出当前寻找的延伸
  118. }
  119. }
  120. }
  121. }
  122.  
  123. delete[]fFlag;
  124. }
  125.  
  126. int main(int argc, char** argv)
  127. {
  128.  
  129. ReadResult result;
  130. osg::ref_ptr<osgDB::ReaderWriter> reader = osgDB::Registry::instance()->getReaderWriterForExtension("tif");
  131. std::string name("D:\\srtm_19_032.tif");
  132. osgDB::ReaderWriter::Options* opt= NULL;
  133. osgDB::ReaderWriter::ReadResult rr = reader->readImage(name, opt);
  134.  
  135. if (rr.validImage())
  136. {
  137. result = ReadResult(rr.takeImage());
  138. result.getImage()->setName("srtm_19_03.tif");
  139. }
  140.  
  141. if (result.succeeded())
  142. {
  143. result.getObject();
  144. result.metadata();
  145. osg::ref_ptr<osg::Image> image = result.getImage();
  146.  
  147. osgEarth::ImageToHeightFieldConverter conv;
  148. osg::HeightField* hf = conv.convert(image.get());
  149.  
  150. //seaboard(hf);
  151. floating(hf);
  152.  
  153. osg::Image* newimage = conv.convert(hf);
  154. std::string nameofnew("D:\\srtm_19_03.tif");
  155. reader->writeImage(*newimage, nameofnew);
  156.  
  157. }
  158.  
  159. return ;
  160. }

第二种:通过gdal库修改tiff

getImgInfo.h

  1. #include "gdal.h"
  2. #include "gdal_priv.h"
  3. #include <string>
  4. using namespace std;
  5.  
  6. int getImgInfo(string szInFile, GDALDataset **poDataset, int *nbands, double **geoTrans, int *width, int *height, GDALDataType *gdt, const char** projRef, GDALRasterBand *** poBand)
  7. {
  8. GDALAllRegister();
  9.  
  10. GDALDataset *poDatasetTmp = *poDataset;
  11. poDatasetTmp = (GDALDataset*)GDALOpen(szInFile.c_str(), GA_ReadOnly);
  12.  
  13. int widthTmp = *width, heightTmp = *height, nbandsTmp = *nbands;
  14. widthTmp = poDatasetTmp->GetRasterXSize();
  15. heightTmp = poDatasetTmp->GetRasterYSize();
  16. nbandsTmp = poDatasetTmp->GetRasterCount();
  17.  
  18. GDALDataType gdtTmp = *gdt;
  19. gdtTmp = poDatasetTmp->GetRasterBand()->GetRasterDataType();
  20.  
  21. double *geoTransTmp = *geoTrans;
  22. geoTransTmp = new double[];
  23. poDatasetTmp->GetGeoTransform(geoTransTmp);//获取地理坐标信息,地理坐标信息是一个含6个double型数据的数组,
  24.  
  25. const char* projRefTmp = *projRef;
  26. projRefTmp = poDatasetTmp->GetProjectionRef(); //获取投影信息
  27.  
  28. GDALRasterBand ** poBandTmp = *poBand;
  29. poBandTmp = new GDALRasterBand *[nbandsTmp];
  30. if (poBand == NULL)
  31. {
  32. cout << "GDALRasterBand ** poBand = new GDALRasterBand *[nBands]; failed!" << endl;
  33. }
  34. for (int i = ; i < nbandsTmp; i++)
  35. {
  36. poBandTmp[i] = poDatasetTmp->GetRasterBand(i + );
  37. }
  38.  
  39. *poDataset = poDatasetTmp;
  40. *nbands = nbandsTmp;
  41. *geoTrans = geoTransTmp;
  42. *width = widthTmp;
  43. *height = heightTmp;
  44. *gdt = gdtTmp;
  45. *projRef = projRefTmp;
  46. *poBand = poBandTmp;
  47.  
  48. return ;
  49. }

main.cpp

  1. #include "gdal.h"
  2. #include "gdal_priv.h"
  3. #include <iostream>
  4. #include <string>
  5.  
  6. #include "getImgInfo.h"
  7. using namespace std;
  8.  
  9. typedef unsigned short UINT; //(0,65535)
  10.  
  11. int _tmain(int argc, _TCHAR* argv[])
  12. {
  13. int ret=;
  14. GDALAllRegister();
  15.  
  16. string szInFile="F:\\IMG_PROCESS\\srtm_19_03.tif";
  17.  
  18. double *adfGeoTransform;
  19. GDALDataType gdt;
  20. GDALDataset *poDataset;
  21. int Width, Height, nBands;
  22. GDALRasterBand ** poBand;
  23. const char* projRef;
  24. ret=getImgInfo(szInFile,&poDataset,&nBands,&adfGeoTransform,&Width,&Height ,&gdt,&projRef, &poBand);
  25.  
  26. UINT **poBandBlock = new UINT*[nBands];
  27. if (poBandBlock==NULL)
  28. {
  29. cout<<"UINT **poBandBlock = new UINT *[outBand]; failed!"<<endl;
  30. system("pause");
  31. return -;
  32. }
  33. UINT **poBandBlock_out = new UINT *[nBands];
  34. if (poBandBlock_out==NULL)
  35. {
  36. cout<<"UINT **poBandBlock_out = new byte *[outBand]; failed!"<<endl;
  37. system("pause");
  38. return -;
  39. }
  40.  
  41. int xHei=; //一次处理xHei=400行数据 分块处理
  42. int timePerxH=Height/xHei+;
  43.  
  44. GDALDriver *poDriver=NULL;
  45. poDriver = GetGDALDriverManager()->GetDriverByName("GTiff");
  46.  
  47. string output_file="F:\\IMG_PROCESS\\srtm_19_03_tmp.tif";
  48.  
  49. GDALDataset *BiomassDataset;
  50. BiomassDataset=poDriver->Create(output_file.c_str(),Width,Height,nBands,gdt, NULL);
  51.  
  52. int tmp=;//防止无符号数越界
  53.  
  54. //开始分块处理////
  55. for (int i=;i<timePerxH - ;i++) //i表示分块处理需处理次数计数
  56. {
  57. for (int j=;j<nBands;j++)
  58. {
  59. poBandBlock[j] = new UINT[Width*xHei];
  60. if (poBandBlock[j]==NULL)
  61. {
  62. cout<<"poBandBlock[j] = new UINT[Width*xHei]; failed!"<<endl;
  63. system("pause");
  64. return -;
  65. }
  66. poBandBlock_out[j] = new UINT[Width*xHei];
  67. if (poBandBlock_out[j]==NULL)
  68. {
  69. cout<<"poBandBlock_out[j] = new byte[Width*xHei]; failed!"<<endl;
  70. system("pause");
  71. return -;
  72. }
  73. }
  74. for (int k=; k<nBands; k++ )
  75. {
  76. poBand[k]->RasterIO(GF_Read, ,i*xHei,Width,xHei, poBandBlock[k], Width,xHei, gdt, , );
  77. }
  78.  
  79. //poBandBlock像素操作////
  80. for (int i=;i<nBands;i++)
  81. {
  82. for (int j=;j<xHei;j++)
  83. {
  84. for (int k=;k<Width;k++)
  85. {
  86. if (poBandBlock[i][j*Width+k]==)
  87. {
  88. poBandBlock_out[i][j*Width+k]=;
  89. }
  90. else if (poBandBlock[i][j*Width+k]>=)
  91. {
  92. poBandBlock_out[i][j*Width+k]=;
  93. }
  94. else
  95. {
  96. poBandBlock_out[i][j*Width+k] = poBandBlock[i][j*Width+k];
  97. }
  98. }
  99. }
  100. }
  101. for(int k = ; k < nBands; k++)
  102. {
  103. BiomassDataset->GetRasterBand (k+)->RasterIO(GF_Write,,xHei*i,Width,xHei,poBandBlock_out[k],Width,xHei,gdt,,);
  104. }
  105.  
  106. for (int j=;j<nBands;j++)
  107. {
  108. delete []poBandBlock[j];
  109. poBandBlock[j]=NULL;
  110.  
  111. delete poBandBlock_out[j];
  112. poBandBlock_out[j]=NULL;
  113. }
  114. }
  115.  
  116. //////剩余行数处理////
  117. int foreH=(timePerxH-)*xHei;
  118. int leftH=Height-foreH;
  119. for (int j=;j<nBands;j++)
  120. {
  121. poBandBlock[j] = new UINT[Width*leftH];
  122. if (poBandBlock[j]==NULL)
  123. {
  124. cout<<"poBandBlock[j] = new UINT[Width*leftH]; failed!"<<endl;
  125. system("pause");
  126. return -;
  127. }
  128. poBandBlock_out[j] = new UINT[Width*leftH];
  129. if (poBandBlock_out[j]==NULL)
  130. {
  131. cout<<"poBandBlock_out[j] = new byte[Width*leftH]; failed!"<<endl;
  132. system("pause");
  133. return -;
  134. }
  135. }
  136. for (int k=; k<nBands; k++ )
  137. {
  138. poBand[k]->RasterIO(GF_Read, ,foreH,Width,leftH, poBandBlock[k], Width,leftH, gdt, , );
  139. }
  140. //poBandBlock像素操作////
  141. for (int i=;i<nBands;i++)
  142. {
  143. for (int j=;j<leftH;j++)
  144. {
  145. for (int k=;k<Width;k++)
  146. {
  147. if (poBandBlock[i][j*Width+k]==)
  148. {
  149. poBandBlock_out[i][j*Width+k]=;
  150. }
  151. else if (poBandBlock[i][j*Width+k]>=)
  152. {
  153. poBandBlock_out[i][j*Width+k]=;
  154. }
  155. else
  156. {
  157. poBandBlock_out[i][j*Width+k]=poBandBlock[i][j*Width+k];
  158. }
  159. }
  160. }
  161. }
  162. for(int k = ; k < nBands; k++)
  163. {
  164. BiomassDataset->GetRasterBand(k+)->RasterIO(GF_Write,,foreH,Width,leftH,poBandBlock_out[k],Width,leftH,gdt,,);
  165. }
  166. for (int j=;j<nBands;j++)
  167. {
  168. delete poBandBlock[j];
  169. poBandBlock[j]=NULL;
  170.  
  171. delete poBandBlock_out[j];
  172. poBandBlock_out[j]=NULL;
  173. }
  174.  
  175. ////////对指定的某个区域数据重新赋值//////////////////////////////////////////////////////////////////
  176. for (int j = ; j < nBands; j++)
  177. {
  178. poBandBlock[j] = new UINT[ * ];
  179. if (poBandBlock[j] == NULL)
  180. {
  181. cout << "poBandBlock[j] = new UINT[Width*leftH]; failed!" << endl;
  182. system("pause");
  183. return -;
  184. }
  185. poBandBlock_out[j] = new UINT[ * ];
  186. if (poBandBlock_out[j] == NULL)
  187. {
  188. cout << "poBandBlock_out[j] = new byte[Width*leftH]; failed!" << endl;
  189. system("pause");
  190. return -;
  191. }
  192. }
  193. for (int k = ; k < nBands; k++)
  194. {
  195. poBand[k]->RasterIO(GF_Read, , , , , poBandBlock[k], , , gdt, , );
  196. }
  197. //poBandBlock像素操作//
  198. for (int i = ; i < nBands; i++)
  199. {
  200. for (int j = ; j < ; j++)
  201. {
  202. for (int k = ; k < ; k++)
  203. {
  204. poBandBlock_out[i][j* + k] = ;
  205. }
  206. }
  207. }
  208. for (int k = ; k < nBands; k++)
  209. {
  210. BiomassDataset->GetRasterBand(k + )->RasterIO(GF_Write, , , , , poBandBlock_out[k], , , gdt, , );
  211. }
  212. for (int j = ; j < nBands; j++)
  213. {
  214. delete poBandBlock[j];
  215. poBandBlock[j] = NULL;
  216.  
  217. delete poBandBlock_out[j];
  218. poBandBlock_out[j] = NULL;
  219. }
  220. //////////////////////////////////////////////////////////////////////////
  221.  
  222. delete poBandBlock;
  223. poBandBlock=NULL;
  224. delete poBandBlock_out;
  225. poBandBlock_out=NULL;
  226.  
  227. BiomassDataset->SetGeoTransform(adfGeoTransform);
  228. BiomassDataset->SetProjection(projRef);
  229. system("pause");
  230.  
  231. }

[原][osg][gdal]两种方式修改tiff高程的更多相关文章

  1. Win10秘笈:两种方式修改网卡物理地址(MAC)

    每台能够上网的电脑都有网卡,不管是有线还是无线,网卡本身都得有物理地址,也就是MAC(Media Access Control 或 Medium Access Control)地址.这个地址理论上是固 ...

  2. 云服务器 ECS Linux 服务器修改时区的两种方式

    在云服务器 ECS Linux 系统中,以 Centos6.5 为例,可以通过如下两种方式,修改系统时区: 可以使用命令 tzselect,修改时区.操作示例: [root@localhost ~]# ...

  3. linux中添加一个用户到指定用户组的两种方式,修改一个用户到指定用户组的一种方式

    添加一个用户到指定用户组: gpasswd –a 用户名 组名usermod –G 组名 用户名 //第一种:gpasswd –a 用户名 组名 [root@localhost ~]# id user ...

  4. mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样

    Mybatis批量更新数据 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批量更新两种方式:1.修改值全部一样 2.修改每条记录值不一样 mybatis批 ...

  5. TreeMap集合根据指定元素,进行删除和修改的两种方式实现及bug梳理

    TreeMap根据key中的指定元素进行删除修改的两种方式实现及注意事项: 方式一:利用增强for进行删除和修改 总结:逻辑简单,但是局限性太强,如果排序规则是从小到大进行排序的,则该方法不能进行删除 ...

  6. 传递引用类型参数的两种方式(转自 MSDN)

    引用类型的变量不直接包含其数据:它包含的是对其数据的引用.当通过值传递引用类型的参数时,有可能更改引用所指向的数据,如某类成员的值(更改属性的值),但是无法更改引用本身的值:也就是说,不能使用相同的引 ...

  7. 将html页改成jsp的两种方式

    将html页改成jsp的两种方式 作者: 字体:[增加 减小] 类型:转载 时间:2013-08-13 将html页改成jsp有两种方法,第一种是直接修改html文件,另一种是新建jsp文件.下面为大 ...

  8. java 的对象拷贝(有深浅拷贝两种方式,深拷贝实现的两种方式(逐层实现cloneable接口,序列化的方式来实现))

    Java提高篇--对象克隆(复制)(转自:http://www.cnblogs.com/Qian123/p/5710533.html#_label0)   阅读目录 为什么要克隆? 如何实现克隆 浅克 ...

  9. 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式

    一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...

随机推荐

  1. js之数组排序

    数组,大家都不陌生,只要是学编程的人都知道这个入门的数据结构,在js中也是有数组这个概念的,跟普通的数组一样只是定义的形式不同罢了.下面是一个数组的排序代码: <html> <hea ...

  2. win10安装后耳机有声音而外放无声音

    安装win10后耳机声音正常,而外放没声音.检查了小喇叭.播放器和设备管理器一切正常,驱动也重装了好些次.喇叭音量设置.视频音量设置均正常.花费很多时间,结果发现是键盘上有个小喇叭+的键的问题.按fn ...

  3. PKU2503_map应用

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  4. uva1330 在一个大的矩阵中寻找面积最大的子矩阵

    大白书 P50页 #include <algorithm> #include <cstdio> using namespace std; ; int ma[maxn][maxn ...

  5. 《Hadoop权威指南》(Hadoop:The Definitive Guide) 气象数据集下载脚本

    已过时,无法使用 从网上找到一个脚本,修改了一下 #!/bin/bash CURRENT_DIR=$(cd `dirname $0`; pwd) [ -e $CURRENT_DIR/ncdc ] || ...

  6. vm #set、日期截取、#foreach&#if

    <div class="form_row"> <label>状态:</label> #if ($!cp.runState =='t') #set ...

  7. 串口WIF简单I调试

    串口WIF简单I调试 /*********************************************************************** Title:Wifi串口调试 H ...

  8. C语言: 两个int变量相除,结果保留两位小数

    #include<stdio.h> void main() { ,j=; float h; h=(*/)/; printf("%.2f",h); } 注:%f:不指定宽 ...

  9. SpringCloud请求响应数据转换(一)

    异常现象 近期做Spring Cloud项目,工程中对Controller添加ResponseBodyAdvice切面,在切片中将返回的结果封装到ResultMessage(自定义结构),但在Cont ...

  10. 常用模块之hashlib,subprocess,logging,re,collections

    hashlib 什么是hashlib 什么叫hash:hash是一种算法(3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,M ...