当前位置:   article > 正文

CIA和LGC数据处理成tif并显示的python程序_cia lgc

cia lgc

CIA和LGC数据是做数据融合的实验数据,一个是modis,一个是landsat,以下是根据不同数据做的python程序,我也是经常在网上参考大家的代码,包括以下的代码,也是参考了网上的信息,希望能给大家带来一点帮助,祝大家学习快乐!
以下是LGC_int转tif的程序

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Sep 28 14:47:13 2023
  4. @author: Administrator
  5. """
  6. # -*- coding: utf-8 -*-
  7. import numpy as np
  8. from osgeo import gdal
  9. import os
  10. # 依据BSQ存储规则,按照存储完单波段整幅图像后再存储下一波段的存储方法进行提取并存入数组。
  11. def read_as_bsq(dataarr,bands, rows, col):
  12. imgarr = np.zeros((bands,rows,col))
  13. for b in range(bands): #取出每个波段
  14. start = b * rows * col
  15. end = start + rows * col
  16. arrtem = dataarr[start:end]
  17. for r in range(rows): #一维数组按行取出,存入对应三维数组。
  18. start2 = r * col
  19. end2 = start2 + col
  20. imgarr[b,r,:] = arrtem[start2:end2]
  21. return imgarr
  22. def test_writetotif(dir):
  23. #读取二进制数据并转换成int16类型的数组
  24. #dir = r"J:\G\workk\2023\zy\LGC\MODIS\MOD09GA_A2004107.sur_refl.int"
  25. f = open(dir, 'rb')
  26. fint = np.fromfile(f,dtype = np.int16)
  27. #数据提取
  28. bands, rows, col =6, 2720,3200
  29. #imgarr = read_as_bil(fint, bands, rows, col)
  30. imgarr = read_as_bsq(fint, bands, rows, col)
  31. #imgarr = read_as_bip(fint, bands, rows, col)
  32. #将提取的数组存储为tif格式图像.
  33. #注意这里未设置地理坐标和仿射变换信息,所以不能用ENVI等软件打开。
  34. savedir=dir.split(".")[0]+"_sur_refl_new.tif"
  35. #savedir = r"E:\datasets\stfdatasets\Coleambally_Irrigation_Area\CIA\MODIS\2001_281_08oct\MOD09GA_A2001281.sur_refl.tif"
  36. datatype = gdal.GDT_UInt16
  37. bands, high, width = imgarr.shape
  38. driver = gdal.GetDriverByName("GTiff")
  39. datas = driver.Create(savedir, col, rows, bands, datatype)
  40. #设置地理坐标和仿射变换信息
  41. """
  42. image_geotrans=[378000,25,0,6170000,0,25]
  43. image_projection="AMG55"
  44. datas.SetGeoTransform(image_geotrans)
  45. datas.SetProjection(image_projection)"""
  46. for i in range(bands):
  47. datas.GetRasterBand(i + 1).WriteArray(imgarr[i])
  48. del datas
  49. if __name__ =="__main__":
  50. in_dir = r'J:\G\workk\2023\zy\LGC\MODIS' # input dir
  51. file_list = os.listdir(in_dir)
  52. for file in file_list:
  53. if file.endswith('.int'):
  54. test_writetotif(in_dir+"\\"+file)
  55. print("save succfully")

以下是CIA_bil格式转tif

  1. """
  2. Created on Thu Sep 28 14:47:13 2023
  3. @author: Administrator
  4. """
  5. # -*- coding: utf-8 -*-
  6. import numpy as np
  7. from osgeo import gdal
  8. import os
  9. from pyproj import Proj,transform
  10. from osgeo import osr
  11. # 依据BSQ存储规则,按照存储完单波段整幅图像后再存储下一波段的存储方法进行提取并存入数组。
  12. def read_as_bsq(dataarr,bands, rows, col):
  13. imgarr = np.zeros((bands,rows,col))
  14. for b in range(bands): #取出每个波段
  15. start = b * rows * col
  16. end = start + rows * col
  17. arrtem = dataarr[start:end]
  18. for r in range(rows): #一维数组按行取出,存入对应三维数组。
  19. start2 = r * col
  20. end2 = start2 + col
  21. imgarr[b,r,:] = arrtem[start2:end2]
  22. return imgarr
  23. def ReadBilFile(bil):
  24. gdal.GetDriverByName('EHdr').Register()
  25. img = gdal.Open(bil)
  26. band = img.GetRasterBand(1)
  27. data = band.ReadAsArray()
  28. return data
  29. # 依据BIP存储规则,按照一个像素所有波段进行存储完,再存储下一个像素所有波段的存储方法进行提取并存入数组。
  30. def read_as_bip(dataarr,bands, rows, col):
  31. imgarr = np.zeros((bands,rows,col))
  32. for r in range(rows): #按行列遍历每个像元
  33. for c in range(col):
  34. if r == 0 :
  35. pix = c
  36. else:
  37. pix = r * col + c
  38. start = pix * bands
  39. end = start + bands
  40. arrtem = dataarr[start:end] #从一维数组中取出每个像元的全波段元素(6个)
  41. for b in range(bands):
  42. imgarr[b,r,c] = arrtem[b] # 赋值给对应数组
  43. return imgarr
  44. # 依据BIL存储规则,按照存储完一行的所有波段再存储下一行,进行提取并存入数组。
  45. def read_as_bil(dataarr,bands, rows, col):
  46. imgarr = np.zeros((bands,rows,col))
  47. for r in range(rows): #取出一行的所有波段
  48. start = r * col * bands
  49. end = start + col * bands
  50. arrtem = dataarr[start:end]
  51. for b in range(bands): #取出每个波段
  52. start2 = b * col
  53. end2 = start2 + col
  54. imgarr[b,r,:] = arrtem[start2:end2] #存入数组对应位置
  55. return imgarr
  56. def test_writetotif(dir):
  57. #读取二进制数据并转换成int16类型的数组
  58. #dir = r"J:\G\workk\2023\zy\LGC\MODIS\MOD09GA_A2004107.sur_refl.int"
  59. f = open(dir, 'rb')
  60. fint = np.fromfile(f,dtype = np.int16)
  61. #数据提取
  62. bands, rows, col =6, 2040, 1720
  63. imgarr = read_as_bil(fint, bands, rows, col)
  64. #imgarr = read_as_bsq(fint, bands, rows, col)
  65. #imgarr = read_as_bip(fint, bands, rows, col)
  66. #将提取的数组存储为tif格式图像.
  67. #注意这里未设置地理坐标和仿射变换信息,所以不能用ENVI等软件打开。
  68. savedir=dir.split(".")[0]+"_sur_refl.tif"
  69. #savedir = r"E:\datasets\stfdatasets\Coleambally_Irrigation_Area\CIA\MODIS\2001_281_08oct\MOD09GA_A2001281.sur_refl.tif"
  70. datatype = gdal.GDT_UInt16
  71. bands, high, width = imgarr.shape
  72. driver = gdal.GetDriverByName("GTiff")
  73. datas = driver.Create(savedir, col, rows, bands, datatype)
  74. #设置地理坐标和仿射变换信息
  75. image_geotrans=[378000,25,0,6170000,0,25]
  76. #image_projection="20255"
  77. #image_projection="agd66"
  78. #agd66 = Proj(init='EPSG:20255')
  79. #p = Proj(init="EPSG:32650")
  80. datas.SetGeoTransform(image_geotrans)
  81. #datas.SetProjection(agd66)
  82. outRasterSRS = osr.SpatialReference()
  83. outRasterSRS.ImportFromEPSG(20255)
  84. datas.SetProjection(outRasterSRS.ExportToWkt())
  85. for i in range(bands):
  86. datas.GetRasterBand(i + 1).WriteArray(imgarr[i])
  87. del datas
  88. if __name__ =="__main__":
  89. in_dir = r'J:\G\workk\2023\zy\CIA\Landsat-init' # input dir
  90. file_list = os.listdir(in_dir)
  91. for file in file_list:
  92. if file.endswith('.bil'):
  93. test_writetotif(in_dir+"\\"+file)
  94. print("save succfully")

以下是CIA int格式转tif

  1. """
  2. Created on Thu Sep 28 14:47:13 2023
  3. @author: Administrator
  4. """
  5. # -*- coding: utf-8 -*-
  6. import numpy as np
  7. from osgeo import gdal
  8. import os
  9. from osgeo import osr
  10. # 依据BSQ存储规则,按照存储完单波段整幅图像后再存储下一波段的存储方法进行提取并存入数组。
  11. def read_as_bsq(dataarr,bands, rows, col):
  12. imgarr = np.zeros((bands,rows,col))
  13. for b in range(bands): #取出每个波段
  14. start = b * rows * col
  15. end = start + rows * col
  16. arrtem = dataarr[start:end]
  17. for r in range(rows): #一维数组按行取出,存入对应三维数组。
  18. start2 = r * col
  19. end2 = start2 + col
  20. imgarr[b,r,:] = arrtem[start2:end2]
  21. return imgarr
  22. def test_writetotif(dir):
  23. #读取二进制数据并转换成int16类型的数组
  24. #dir = r"J:\G\workk\2023\zy\LGC\MODIS\MOD09GA_A2004107.sur_refl.int"
  25. f = open(dir, 'rb')
  26. fint = np.fromfile(f,dtype = np.int16)
  27. #数据提取
  28. bands, rows, col =6, 2040, 1720
  29. #imgarr = read_as_bil(fint, bands, rows, col)
  30. imgarr = read_as_bsq(fint, bands, rows, col)
  31. #imgarr = read_as_bip(fint, bands, rows, col)
  32. #将提取的数组存储为tif格式图像.
  33. #注意这里未设置地理坐标和仿射变换信息,所以不能用ENVI等软件打开。
  34. savedir=dir.split(".")[0]+"_sur_refl.tif"
  35. #savedir = r"E:\datasets\stfdatasets\Coleambally_Irrigation_Area\CIA\MODIS\2001_281_08oct\MOD09GA_A2001281.sur_refl.tif"
  36. datatype = gdal.GDT_UInt16
  37. bands, high, width = imgarr.shape
  38. driver = gdal.GetDriverByName("GTiff")
  39. datas = driver.Create(savedir, col, rows, bands, datatype)
  40. #设置地理坐标和仿射变换信息
  41. image_geotrans=[378000,25,0,6170000,0,25]
  42. image_projection="AMG55"
  43. datas.SetGeoTransform(image_geotrans)
  44. #datas.SetProjection(agd66)
  45. outRasterSRS = osr.SpatialReference()
  46. outRasterSRS.ImportFromEPSG(20255)
  47. datas.SetProjection(outRasterSRS.ExportToWkt())
  48. for i in range(bands):
  49. datas.GetRasterBand(i + 1).WriteArray(imgarr[i])
  50. del datas
  51. if __name__ =="__main__":
  52. in_dir = r'J:\G\workk\2023\zy\CIA\MODIS-init' # input dir
  53. file_list = os.listdir(in_dir)
  54. for file in file_list:
  55. if file.endswith('.int'):
  56. test_writetotif(in_dir+"\\"+file)
  57. print("save succfully")

以下是多波段tif数据选择rgb波段然后显示的程序

  1. """
  2. Created on Sun Oct 8 15:09:42 2023
  3. @author: Administrator
  4. """
  5. import numpy as np
  6. import tifffile as tf #tifffile是tiff文件的读取库
  7. from PIL import Image
  8. import cv2 as cv
  9. from osgeo import gdal
  10. import matplotlib.pyplot as plt
  11. #以下为三种拉伸方式,如果不拉伸,图像太黑,拉伸完显示的图像更好看
  12. def optimized_linear(arr):
  13. a, b = np.percentile(arr, (2.5, 99))
  14. c = a - 0.1 * (b - a)
  15. d = b + 0.5 * (b - a)
  16. arr = (arr - c) / (d - c) * 255
  17. arr = np.clip(arr, 0, 255)
  18. return np.uint8(arr)
  19. def percent_linear(arr, percent=2):
  20. arr_min, arr_max = np.percentile(arr, (percent, 100-percent))
  21. arr = (arr - arr_min) / (arr_max - arr_min) * 255
  22. arr = np.clip(arr, 0, 255)
  23. return np.uint8(arr)
  24. def linear(arr):
  25. arr_min, arr_max = arr.min(), arr.max()
  26. arr = (arr - arr_min) / (arr_max - arr_min) * 255
  27. arr = np.clip(arr, 0, 255)
  28. return np.uint8(arr)
  29. path=r"J:\G\workk\2023\zy\LGC\MODIS\MOD09GA_A2004235_sur_refl.tif"
  30. data = gdal.Open(path) # 读取tif文件
  31. num_bands = data.RasterCount # 获取波段数
  32. print(num_bands)
  33. tmp_img = data.ReadAsArray() #将数据转为数组
  34. print(tmp_img.shape)
  35. img_rgb = tmp_img.transpose(1, 2, 0) #由波段、行、列——>行、列、波段
  36. img_rgb = np.array(img_rgb, dtype=np.uint16) #设置数据类型,np.unit8可修改
  37. #img_rgb = np.array(img_rgb)
  38. r = img_rgb[:, :,3]
  39. g = img_rgb[:, :,2]
  40. b =img_rgb[:, :,1]
  41. img_rgb = np.dstack((percent_linear(r), percent_linear(g),percent_linear(b))) # 波段组合
  42. #img_rgb = np.array(img_rgb, dtype=np.uint8)
  43. # plt.imshow(img_rgb)
  44. # plt.show()
  45. # 通过调用plt.axis(“ off”),可以删除编号的轴
  46. plt.figure(dpi=800)
  47. plt.axis("off")
  48. plt.imshow(img_rgb)
  49. plt.show()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/322843
推荐阅读
相关标签
  

闽ICP备14008679号