microproduct/Ortho/tool/algorithm/xml/CreatMetafile.py

320 lines
11 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 OnePlantHeight.PY
@Function :主函数
@Author LMM
@Date 2021/10/19 14:39
@Version 1.0.0
"""
from xml.dom import minidom
from xml.etree.ElementTree import ElementTree, Element
import xml.dom.minidom
from lxml import etree
import codecs
import xml.etree.cElementTree as ET
class CreateMetafile:
"""
生成元文件案例
"""
def __init__(self, input_image_path, input_para_file, an_li_path, path):
"""
input_image_path: 影像头文件
input_para_file: 配置文件
an_li_path案例路径
path保存路径
"""
self.input_image_path = input_image_path
self.input_para_file = input_para_file
self.an_li_path= an_li_path
self.path = path
pass
def create_xml(self):
"""
读取元文件只保留从头文件到sensor节点的部分
输出sensor的节点位置
"""
tree = ElementTree()
tree.parse(self.input_image_path) # 影像头文件
root = tree.getroot()
# 1、只保留从头文件到sensor节点的部分
element_trees = list(root)
count = 0
count_01=1
for element in element_trees:
count = count+1
if element.tag == "sensor":
element.tail = "\n\n\t"
count_01 = count-1
for i in range(0, len(element_trees)):
if i > count_01:
root.remove(element_trees[i])
# 2、只保留"satellite", "orbitType", "attiType", "Direction", "ReceiveTime", "sensor"的部分
element_trees2 = list(root)
for i in element_trees2:
if i.tag not in ["satellite", "orbitType", "attiType", "Direction", "ReceiveTime", "sensor"]:
root.remove(i)
# 3、获取"sensor"节点的位置,并输出
count2 = 0
count2_01=1
element_trees3 = list(root)
for element in element_trees3:
count2 = count2+1
if element.tag == "sensor":
element.tail = "\n\n\t"
count2_01 = count2-1
tree.write(self.path, encoding="utf-8", xml_declaration=True)
return count2_01
@staticmethod
def create_node(tag, property_map, content):
"""
fun: 新造一个节点
para: tag:节点标签
para: property_map:属性及属性值map
para: content: 节点闭合标签里的文本内容
para: return 新节点
"""
element = Element(tag, property_map)
element.text = content
element.tail = "\n\t"
return element
def add_standard_xml(self, num):
"""
模板直接写入到元文件中
"""
tree = ElementTree()
tree.parse(self.path) # 影像头文件
root = tree.getroot()
tree2 = ElementTree()
tree2.parse(self.an_li_path) # 影像头文件
root2 = tree2.getroot()
productinfo = root2.find("productinfo")
root.insert(num + 1, productinfo)
processinfo = root2.find("processinfo")
root.insert(num + 2, processinfo)
tree.write(self.path, encoding="utf-8", xml_declaration=True)
def add_img_xml(self, num,SrcImageName):
"""添加影像信息"""
tree = ElementTree()
tree.parse(self.path)
root = tree.getroot()
a = self.create_node("SrcImageName", {"desc": "原始影像名称"}, SrcImageName)
root.insert(num+1, a)
# root.append(a)
b = self.create_node("AlgCompt", {"desc": "算法信息"}, "\n\t\t")
b.tail = "\n\n\t"
# root.append(b)
root.insert(num+2, b)
tree.write(self.path, encoding="utf-8", xml_declaration=True)
def add_info_xml(self):
"""
向元文件中添加配置文件的部分节("AlgorithmName", "ChsName", "AlgorithmDesc", "Version",
"AlgorithmClass", "AlgorithmLevel", "AlgoirthmID", "Author"
"""
tree = ElementTree()
tree.parse(self.input_para_file) # 配置文件
root = tree.getroot()
tree2 = ElementTree()
tree2.parse(self.path)
root2 = tree2.getroot()
AlgCompt = root2.find("AlgCompt")
a = root.find("AlgCompt")
element_trees = list(a)
for element in element_trees:
if element.tag in ["AlgorithmName", "ChsName", "AlgorithmDesc", "Version",
"AlgorithmClass", "AlgorithmLevel", "AlgoirthmID", "Author"]:
element.tail = "\n\t\t"
AlgCompt.append(element)
if element.tag == "Author":
element.tail = "\n\t"
tree2.write(self.path, encoding="utf-8", xml_declaration=True)
def add_class_info(self, type_id_name, type_id_parent=None):
"""
向元文件中:
1.删除productinfo-productType信息
2.加入地物类别信息;
输出:
<Class1>
<parent_id>1</parent_id>
<id>101</id>
<covernm>耕地</covernm>
</Class1>
<Class2>
<parent_id>5</parent_id>
<id>502</id>
<covernm>草地</covernm>
</Class2>
"""
tree = ElementTree()
tree.parse(self.path) # 配置文件
root = tree.getroot()
productinfo = root.find("productinfo")
# element_trees = list(productinfo)
# for element in element_trees:
# if element.tag == "productType":
# productinfo.remove(element) # 移除 "productType"
productinfo.find("productConsumeTime").tail = "\n\t\t" # 定位到productConsumeTime设置好位置
b = self.create_node("LandCoverClass", {}, "\n\t\t\t")
b.tail = "\n\t\t"
productinfo_count=0
for i in list(productinfo):
productinfo_count=productinfo_count+1
if i.tag=="productConsumeTime":
break
productinfo.insert(productinfo_count, b)
# productinfo.insert(num, b) # 插入LandCoverClass
class_num = 1
for key, value in type_id_name.items():
LandCoverClass = productinfo.find("LandCoverClass")
name="Class"+str(class_num)
# name = "Class"
c = self.create_node(name, {}, "\n\t\t\t\t")
if class_num!=(len(type_id_name.keys())):
c.tail = "\n\t\t\t"
else:
c.tail = "\n\t\t"
LandCoverClass.append(c) # 插入LandCoverClass
# LandCoverClass.find("Class")[num].tail = "\n\t\t\t"
aaa=LandCoverClass.find(name)
if type_id_parent is not None:
parent_id = self.create_node("parent_id", {}, type_id_parent[key])
parent_id.tail="\n\t\t\t\t"
LandCoverClass.find(name).append(parent_id)
id = self.create_node("id", {}, str(key))
id.tail = "\n\t\t\t\t"
LandCoverClass.find(name).append(id)
covernm = self.create_node("covernm", {}, value)
covernm.tail = "\n\t\t\t"
LandCoverClass.find(name).append(covernm)
class_num=class_num+1
tree.write(self.path, encoding="utf-8", xml_declaration=True)
def rewrite_name(self):
"""
修改class的名字:
class1->class
class2->class
"""
tree = ElementTree()
tree.parse(self.path) # 配置文件
root = tree.getroot()
productinfo = root.find("productinfo")
LandCoverClass=productinfo.find("LandCoverClass")
element_trees = list(LandCoverClass)
for element in element_trees:
element.tag="Class"
tree.write(self.path, encoding="utf-8", xml_declaration=True)
def OrthoInsertNode(self):
"""正射算法专用,插入节点<l1aInfo>"""
tree = ElementTree()
tree.parse(self.path) # 影像头文件
root = tree.getroot()
# 插入节点<l1aInfo>
count2 = 0
count2_01=1
element_trees3 = list(root)
for element in element_trees3:
count2 = count2+1
if element.tag == "sensor":
element.tail = "\n\n\t"
count2_01 = count2-1
b = self.create_node("l1aInfo", {}, "\n\t\t")
b.tail = "\n\n\t"
root.insert(count2_01+1, b)
# 查询节点位置<l1aInfo>
node_l1aInfo=root.find("l1aInfo")
img_tree = ElementTree()
img_tree.parse(self.input_image_path) # 影像头文件
img_root = img_tree.getroot()
node_imageinfo = img_root.find("imageinfo")
node_processinfo=img_root.find("processinfo")
ele_node_imageinfo = list(node_imageinfo)
ele_node_processinfo= list(node_processinfo)
for i in ele_node_imageinfo:
if i.tag == "QualifyValue":
i.tail = "\n\t\t"
node_l1aInfo.append(i)
for j in ele_node_processinfo:
if j.tag == "CalibrationConst":
j.tail = "\n\t" #后一个节点的位置
node_l1aInfo.append(j)
tree.write(self.path, encoding="utf-8", xml_declaration=True)
def process(self,SrcImageName):
"""
不涉及到地表覆盖调用此函数
"""
if self.input_image_path is None:
import xml.etree.cElementTree as ET
product = ET.Element("product") # 根节点tag= "product"
product.text = "\n\t"
tree = ET.ElementTree(product)
tree.write(self.path)
count = 0
count_2 = -1
else:
count = self.create_xml()
count_2 = count
self.add_standard_xml(count)
self.add_img_xml(count_2, SrcImageName)
self.add_info_xml()
def process2(self, type_id_name, type_id_parent,SrcImageName):
"""
涉及到地表覆盖的调用此函数
type_id_name={"101":"耕地","502":"草地"}
type_id_parent={"101":"1""502":"5"}
"""
count = self.create_xml()
self.add_standard_xml(count)
self.add_img_xml(count,SrcImageName)
self.add_info_xml()
self.add_class_info(type_id_name, type_id_parent)
self.rewrite_name()
def process3(self,SrcImageName):
"""
正射调用此函数
"""
if self.input_image_path is None:
import xml.etree.cElementTree as ET
product = ET.Element("product") # 根节点tag= "product"
product.text = "\n\t"
tree = ET.ElementTree(product)
tree.write(self.path)
count = 0
else:
count = self.create_xml()
self.add_standard_xml(count)
self.add_img_xml(count, SrcImageName)
self.add_info_xml()
self.OrthoInsertNode()