microproduct-l-sar/surfaceRoughness-L-SAR/SurfaceRoughnessTool.py

140 lines
5.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import glob
import struct
import numpy as np
from tool.algorithm.algtools.MetaDataHandler import MetaDataHandler
from tool.algorithm.polsarpro.AHVToPolsarpro import AHVToPolsarpro
from tool.algorithm.polsarpro.pspLeeRefinedFilterT3 import LeeRefinedFilterT3
from tool.algorithm.image.ImageHandle import ImageHandler
from tool.algorithm.polsarpro.pspSurfaceInversion import SurfaceInversionHisto
from tool.algorithm.polsarpro.pspSurfaceInversion import SurfaceInversionDubois
from tool.algorithm.polsarpro.pspSurfaceInversion import SurfaceInversionOh
from tool.algorithm.polsarpro.pspSurfaceInversion import SurfaceInversionOh2004
from tool.algorithm.polsarpro.bin2tif import write_bin_to_tif_soil
from tool.algorithm.algtools.oh2004 import oh2004
from tool.algorithm.algtools.MetaDataHandler import Calibration
import os
from osgeo import osr
import logging
import xml.etree.ElementTree as ET
logger = logging.getLogger("mylog")
FILTER_SIZE = 3
class SoilMoistureTool:
def __init__(self, tif_path, out_path, cols, rows, incidence_file, xml_file):
self.__workspace_processing_path = tif_path
self.__workspace_preprocessed_path = out_path
self.imageHandler = ImageHandler()
# self.ref_img = hh_path
self.xml_path = xml_file
self.__cols = cols # self.imageHandler.get_img_width(self.ref_img)
self.__rows = rows # self.imageHandler.get_img_height(self.ref_img)
self.incidence_file = incidence_file # self.create_incidence(incidence_xml)
def ahv_to_t3(self):
# 全极化tif转bin格式T3数据
atp = AHVToPolsarpro()
t3_path = os.path.join(self.__workspace_preprocessed_path,
'psp_t3\\')
polarization = ['HH', 'HV', 'VH', 'VV']
calibration = Calibration.get_Calibration_coefficient(self.xml_path, polarization)
tif_path = atp.calibration(calibration, in_ahv_dir=self.__workspace_processing_path)
atp.ahv_to_polsarpro_t3_soil(t3_path, tif_path)
# Lee滤波
leeFilter = LeeRefinedFilterT3()
lee_filter_path = os.path.join(self.__workspace_processing_path,
'lee_filter\\')
leeFilter.api_lee_refined_filter_T3('', t3_path, lee_filter_path, 0, 0, atp.rows(), atp.cols())
logger.info("refine_lee filter success!")
# logging.info("refine_lee filter success!")
return lee_filter_path
# def create_incidence(self, incidence_xml):
# tree = ET.parse(incidence_xml)
# root = tree.getroot()
# import numpy as np
# arr = np.zeros((self.__rows, self.__cols), dtype=float)
# i = 0
# for child in root:
# if child.tag == 'incidenceValue':
# arr[0, i] = float(child.text)
# i += 1
#
# arr[1:, :] = arr[0, :]
# geo_trans = self.imageHandler.get_geotransform(self.ref_img)
# wgs84 = osr.SpatialReference()
# wgs84.SetWellKnownGeogCS("WGS84")
# proj = wgs84.ExportToWkt()
# incidence_file = os.path.join(self.__workspace_preprocessed_path, 'incidence.tif')
# self.imageHandler.write_img(incidence_file, proj, geo_trans, arr)
# return incidence_file
def __write_img_bin(self, im, file_path):
"""
写入影像到bin文件中保存为float32类型
:param im : 影像矩阵数据,暂支持单通道影像数据
:param file_path: bin文件的完整路径
"""
with open(file_path, 'wb') as f:
self._rows = im.shape[0]
self._cols = im.shape[1]
for row in range(self._rows):
im_bin = struct.pack("f" * self._cols, *np.reshape(im[row, :], (self._cols, 1), order='F'))
f.write(im_bin)
f.close()
def soil_oh2004(self):
lee_file = self.ahv_to_t3()
radarCenterFrequency = MetaDataHandler.get_RadarCenterFrequency(self.xml_path)
# lamda = MetaDataHandler.get_lamda(self.xml_path)
lamda = float(299792458 / radarCenterFrequency)
f = oh2004.lamda2freq(lamda) / 1e9
if not os.path.exists(self.__workspace_preprocessed_path):
os.mkdir(self.__workspace_preprocessed_path)
incidence_dat = os.path.join(self.__workspace_preprocessed_path, 'incidence.dat')
im_proj, im_geotrans, im_arr = ImageHandler().read_img(self.incidence_file)
ImageHandler().write_img_envi(incidence_dat, im_proj, im_geotrans, im_arr)
# self.__write_img_bin(im_arr, incidence_dat)
# incidence_bin = r'D:\micro\WorkSpace\soil_test\processing\in_angle.dat'
oh2004Filter = SurfaceInversionOh2004()
oh2004_path = os.path.join(self.__workspace_preprocessed_path, '0h2004\\')
if not os.path.exists(oh2004_path):
os.mkdir(oh2004_path)
oh2004Filter.api_surface_inversion_oh2004('', lee_file, oh2004_path, incidence_dat, 0, 0, self.__rows, self.__cols, f, 0)
oh2004_tifpath = os.path.join(self.__workspace_preprocessed_path, 'oh2004tif\\')
write_bin_to_tif_soil(oh2004_tifpath, oh2004_path)
logger.info("bin to tif successful! ")
return oh2004_tifpath
if __name__ == '__main__':
tif_path = r'F:\20230605\ortho\tif'
put_path = r'F:\20230605\back\oh2004'
cols = 7670
rows = 7443
incidence_file = r'F:\20230605\ortho\RD_localincidentAngle.tiff'
xml_file = r'F:\20230605\ortho\GF3C_MYC_QPSI_005390_E121.7_N41.1_20230415_L1A_AHV_L10000135644.meta.xml'
soil = SoilMoistureTool(tif_path, put_path, cols, rows, incidence_file, xml_file)
soil.soil_oh2004()
# fn = r'F:\MicroWorkspace\mico_datas\GF3A_nanjing\PIESAR\test\GF3_SAY_QPSI_011444_E118.9_N31.4_20181012_L1A_HH_L10003515422.img'
# fn1 = r'F:\MicroWorkspace\mico_datas\GF3A_nanjing\PIESAR\test11.bin'
# data = ImageHandler().get_data(fn)
# data.tofile(fn1)