microproduct-s-sar/backScattering/BackScatteringXmlInfo.py

209 lines
9.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.

"""
@Project microproduct
@File BackScatteringXmlInfo.PY
@Function :主函数
@Author LMM
@Date 2021/10/19 14:39
@Version 1.0.0
"""
import os
from xml.etree.ElementTree import ElementTree, Element
import xml.dom.minidom
from lxml import etree
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
from PIL import Image
class CreateDict:
"""根据影像信息输出属性字典"""
def __init__(self, image_path, image_pair, 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({"imageinfo_NearincidenceAngle": "0"})
self.para_dict.update({"imageinfo_FarIncidenceAngle": "30"})
self.para_dict.update({"imageinfo_Azimuth": "1"}) # todo 结果xml新增字节
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": "1"}) # 设置图像位深度
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"})
HH, HV, VH ,VV= self.image_pair[0],self.image_pair[1],self.image_pair[2],self.image_pair[3]
if HH == 0:
HH = "delete"
else:
HH = "NULL"
self.para_dict.update({"imageinfo_QualifyValue_HH": HH})
if HV==0:
HV = "delete"
else:
HV = "NULL"
self.para_dict.update({"imageinfo_QualifyValue_HV": HV})
if VH==0:
VH = "delete"
else:
VH = "NULL"
self.para_dict.update({"imageinfo_QualifyValue_VH": VH})
if VV==0:
VV = "delete"
else:
VV = "NULL"
self.para_dict.update({"imageinfo_QualifyValue_VV": VV})
return self.para_dict
class CreateStadardXmlFile:
"""读取字典中的属性值生成一个标准的xml文件"""
def __init__(self, xml_path, par_dict, path):
"""
par_dict:字典
path:xml模板输出路径
"""
self.par_dict = par_dict
self.path = path
shutil.copy(xml_path, path)
pass
def create_standard_xml(self):
"""将字典中的信息写入到copy的xml文件中"""
tree = ElementTree()
tree.parse(self.path) # 影像头文件
root = 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",
"NearincidenceAngle", "FarIncidenceAngle", "Azimuth"]: # todo 新增xml字节
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")
if value =="delete":
element_QualifyValue = list(QualifyValue)
for i in element_QualifyValue:
if i.tag == key.split("_")[2]:
QualifyValue.remove(i)
else:
QualifyValue.find(key.split("_")[2]).text = str(value)
pass
processinfo = root.find("processinfo")
tree.write(self.path, encoding="utf-8", xml_declaration=True)