""" @Project :microproduct @File :BackScatteringXmlInfo.PY @Function :主函数 @Author :LMM @Date :2021/10/19 14:39 @Version :1.0.0 """ from xml.etree.ElementTree import ElementTree import shutil from tool.algorithm.image.ImageHandle import ImageHandler from tool.algorithm.algtools.PreProcess import PreProcess as pp from osgeo import gdal import numpy as np import datetime class CreateDict: """根据影像信息输出属性字典""" def __init__(self, image_path, image_pair, out_path1, out_path2): """ image_path:影像路径 image_pair:输入的压缩包中的极化对 例:hh,hv,vh,vv=【1,1,1,1】 out_path1:地理转平面的输出路径 out_path2:平面转地理的输出路径 """ self.ImageHandler = ImageHandler() self.para_dict = {} self.image_path = image_path self.out_path = out_path1 self.out_path2 = out_path2 self.image_pair = image_pair pass def calu_nature(self, start): """存入字典""" imageinfo_width=self.ImageHandler.get_img_width(self.image_path) self.para_dict.update({"imageinfo_width":imageinfo_width}) imageinfo_height=self.ImageHandler.get_img_height(self.image_path) self.para_dict.update({"imageinfo_height":imageinfo_height}) # imageinfo_EarthModel=self.ImageHandler.get_projection(self.image_path).split("SPHEROID[", 2)[1].split(",", 2)[0] # self.para_dict.update({"imageinfo_EarthModel":imageinfo_EarthModel.replace('"','')}) self.para_dict.update({"imageinfo_EarthModel": "WGS84"}) # imageinfo_ProjectModel = self.ImageHandler.get_projection(self.image_path).split("DATUM[", 2)[1].split(",", 2)[0] # self.para_dict.update({"imageinfo_ProjectModel":imageinfo_ProjectModel.replace('"','')}) self.para_dict.update({"imageinfo_ProjectModel": "UTM"}) proj = self.ImageHandler.get_projection(self.image_path) # 输出的影像若是投影坐标系则先转成地理坐标系 keyword = proj.split("[", 2)[0] # 若是地理坐标系则pass if keyword == "GEOGCS": pass elif keyword == "PROJCS": pp.trans_projcs2geogcs(self.out_path2, self.image_path) self.image_path = self.out_path2 elif len(keyword) == 0 or keyword.strip() == "" or keyword.isspace() is True: raise Exception('image projection is missing!') pp.trans_geogcs2projcs(self.out_path, self.image_path) # 坐标投影, 地理转平面投影坐标 imageinfo_widthspace = self.ImageHandler.get_geotransform(self.out_path)[1] # 投影后的分辨率 imageinfo_heightspace = -self.ImageHandler.get_geotransform(self.out_path)[5] # 投影后的分辨率 self.para_dict.update({"imageinfo_widthspace":imageinfo_widthspace}) self.para_dict.update({"imageinfo_heightspace":imageinfo_heightspace}) self.para_dict.update({"NominalResolution":imageinfo_widthspace}) WidthInMeters = imageinfo_width*imageinfo_widthspace # 投影后的分辨率×宽度 self.para_dict.update({"WidthInMeters":WidthInMeters}) # image_array = self.ImageHandler.get_band_array(self.image_path) # a2 = np.where(np.isnan(image_array), 999999, image_array) # MinValue = np.min(a2) # a3 = np.where(np.isnan(image_array), -999999, image_array) # MaxValue = np.max(a3) # # self.para_dict.update({"MaxValue":MaxValue}) # self.para_dict.update({"MinValue":MinValue}) get_scope = self.ImageHandler.get_scope(self.image_path) point_upleft, point_upright, point_downleft, point_downright=get_scope[0], get_scope[1], get_scope[2], get_scope[3] self.para_dict.update({"imageinfo_corner_topLeft_latitude": point_upleft[1]}) self.para_dict.update({"imageinfo_corner_topLeft_longitude": point_upleft[0]}) self.para_dict.update({"imageinfo_corner_topRight_latitude": point_upright[1]}) self.para_dict.update({"imageinfo_corner_topRight_longitude": point_upright[0]}) self.para_dict.update({"imageinfo_corner_bottomLeft_latitude": point_downleft[1]}) self.para_dict.update({"imageinfo_corner_bottomLeft_longitude": point_downleft[0]}) self.para_dict.update({"imageinfo_corner_bottomRight_latitude": point_downright[1]}) self.para_dict.update({"imageinfo_corner_bottomRight_longitude": point_downright[0]}) longitude_max=np.array([point_upleft[0], point_upright[0], point_downleft[0], point_downright[0]]).max() longitude_min=np.array([point_upleft[0], point_upright[0], point_downleft[0], point_downright[0]]).min() latitude_max=np.array([point_upleft[1], point_upright[1], point_downleft[1], point_downright[1]]).max() latitude_min=np.array([point_upleft[1], point_upright[1], point_downleft[1], point_downright[1]]).min() imageinfo_center_latitude=(latitude_max+latitude_min)/2 imageinfo_center_longitude=(longitude_max+longitude_min)/2 self.para_dict.update({"imageinfo_center_latitude": imageinfo_center_latitude}) self.para_dict.update({"imageinfo_center_longitude": imageinfo_center_longitude}) self.para_dict.update({"productType": "GTC"}) # 设置产品类型 self.para_dict.update({"productFormat": "TIF"}) productGentime = datetime.datetime.now() self.para_dict.update({"productGentime": productGentime}) msg=productGentime-start self.para_dict.update({"productConsumeTime": msg}) self.para_dict.update({"unit": "none"}) # 设置单位 self.para_dict.update({"NoDataValue": "nan"}) self.para_dict.update({"productLevel": "5"}) # noinspection PyBroadException # try: # 设置图像位深度 # imagebit="0bit" # # im=Image.open(self.image_path) # im = Image.open(r"E:\0test\testworkspace\DEM\ASTGTM2_N24E110_dem.tif") # if "1" in im.getbands(): # imagebit="1bit" # elif "L" in im.getbands() or "P" in im.getbands(): # imagebit = "8bit" # elif "R" in im.getbands() and "G" in im.getbands() and "B" in im.getbands(): # imagebit = "24bit" # elif "R" in im.getbands() and "G" in im.getbands() and "B" in im.getbands() and "A" in im.getbands(): # imagebit = "32bit" # elif "C" in im.getbands() and "M" in im.getbands() and "Y" in im.getbands() and "K" in im.getbands(): # imagebit = "32bit" # elif "Y" in im.getbands() and "C" in im.getbands() and "b" in im.getbands() and "C" in im.getbands(): # imagebit = "32bit" # elif "I" in im.getbands() or "F" in im.getbands(): # imagebit = "32bit" image_array = self.ImageHandler.get_band_array(self.image_path) try: gdal_dtypes = { 'int8': gdal.GDT_Byte, 'unit16': gdal.GDT_UInt16, 'int16': gdal.GDT_Int16, 'unit32': gdal.GDT_UInt32, 'int32': gdal.GDT_Int32, 'float32': gdal.GDT_Float32, 'float64': gdal.GDT_Float64, } bit_dtypes = { 'int8': 8, 'unit16': 16, 'int16': 16, 'unit32': 32, 'int32': 32, 'float32': 32, 'float64': 64, } if not gdal_dtypes.get(image_array.dtype.name, None) is None: bit_num = str(bit_dtypes[image_array.dtype.name]) datatype=bit_num+"bit" else: datatype = str(32) + "bit" # datatype = str(gdal.GDT_Float32)+"bit" self.para_dict.update({"imagebit": datatype}) except Exception: self.para_dict.update({"imagebit": "None"}) self.para_dict.update({"imageinfo_QualifyValue_Product": "NULL"}) return self.para_dict class CreateStadardXmlFile: """读取字典中的属性值,生成一个标准的xml文件""" def __init__(self, xml_path, para_xml_path, par_dict, path): """ xml_path:模板路径 para_xml_path:算法配置文件的路径 par_dict:字典 path:xml模板输出路径 """ self.par_dict = par_dict self.path = path self.para_xml_path= para_xml_path shutil.copy(xml_path, path) pass def create_standard_xml(self): """将字典中的信息写入到copy的xml文件中""" tree = ElementTree() tree.parse(self.path) # 影像头文件 root = tree.getroot() para_tree = ElementTree() para_tree.parse(self.para_xml_path) # 影像头文件 para_root = para_tree.getroot() productinfo = root.find("productinfo") for key, value in self.par_dict.items(): if key.split("_")[0] != "imageinfo": productinfo.find(key).text = str(value) elif key.split("_")[0] == "imageinfo": imageinfo = productinfo.find("imageinfo") if key.split("_")[1] in ["EarthModel", "ProjectModel", "width", "height", "widthspace", "heightspace"]: imageinfo.find(key.split("_")[1]).text = str(value) elif key.split("_")[1] == "center": center = imageinfo.find("center") center.find(key.split("_")[2]).text = str(value) elif key.split("_")[1] == "corner": corner = imageinfo.find("corner") corner.find(key.split("_")[2]).find(key.split("_")[3]).text = str(value) elif key.split("_")[1] == "QualifyValue": QualifyValue = imageinfo.find("QualifyValue") QualifyValue.find(key.split("_")[2]).text = str(value) pass Parameter = root.find("processinfo").find("Parameter") Parameter.find("NdviThresholdMin").text = str(0) Parameter.find("NdviThresholdMax").text = str(0.8) Parameter.find("CoverThresholdMin").text = str(0) Parameter.find("CoverThresholdMax").text = str(185) tree.write(self.path, encoding="utf-8", xml_declaration=True)