microproduct-l-sar/tool/algorithm/xml/AnalysisXml.py

140 lines
6.1 KiB
Python
Raw Normal View History

2024-01-03 01:42:21 +00:00
from xml.etree.ElementTree import ElementTree
import os
class DictXml:
def __init__(self, xml_path):
self.xml_path = xml_path
self.__tree = ElementTree()
self.__root = None
self.init_xml()
def init_xml(self):
self.__root = self.__tree.parse(self.xml_path)
if self.__root is None:
raise Exception("get root failed")
def get_extend(self):
global bottomRightLon, bottomLeftLon, topRightLon, topLeftLon, topLeftLat,\
topRightLat, bottomLeftLat, bottomRightLat
productInfo = self.__root.find("productInfo")
2024-01-03 01:42:21 +00:00
if productInfo is None:
raise Exception("get productInfo failed")
sceneInfo = productInfo.find("sceneInfo")
if sceneInfo is None:
raise Exception("get sceneInfo failed")
sceneCornerCoord = sceneInfo.findall("sceneCornerCoord")
if sceneCornerCoord is None:
raise Exception("get sceneCornerCoord failed")
for value in sceneCornerCoord:
if value.attrib.get('name') == 'topRight':
topRightLon = value.find('lon').text
topRightLat = value.find('lat').text
elif value.attrib.get('name') == 'topLeft':
topLeftLon = value.find('lon').text
topLeftLat = value.find('lat').text
elif value.attrib.get('name') == 'bottomRight':
bottomRightLon = value.find('lon').text
bottomRightLat = value.find('lat').text
elif value.attrib.get('name') == 'bottomLeft':
bottomLeftLon = value.find('lon').text
bottomLeftLat = value.find('lat').text
point_upleft = [float(topLeftLon), float(topLeftLat)]
point_upright = [float(topRightLon), float(topRightLat)]
point_downleft = [float(bottomLeftLon), float(bottomLeftLat)]
point_downright = [float(bottomRightLon), float(bottomRightLat)]
2024-01-03 01:42:21 +00:00
scopes = [point_upleft, point_upright, point_downleft, point_downright]
point_upleft_buf = [float(topLeftLon) - 0.5, float(topLeftLat) + 0.5]
point_upright_buf = [float(topRightLon) + 0.5, float(topRightLat) + 0.5]
point_downleft_buf = [float(bottomLeftLon) - 0.5,
float(bottomLeftLat) - 0.5]
point_downright_buf = [float(bottomRightLon) + 0.5,
float(bottomRightLat) - 0.5]
scopes_buf = ([point_upleft_buf, point_upright_buf, point_downleft_buf, point_downright_buf],)
return scopes, scopes_buf
2024-01-03 01:42:21 +00:00
class xml_extend:
def __init__(self, xml_path):
self.xml_path = xml_path
self.__tree = ElementTree()
self.__root = None
self.init_xml()
def init_xml(self):
self.__root = self.__tree.parse(self.xml_path)
if self.__root is None:
raise Exception("get root failed")
def get_extend(self):
ProductBasicInfo = self.__root.find("ProductBasicInfo")
if ProductBasicInfo is None:
raise Exception("get ProductBasicInfo failed")
SpatialCoverageInformation = ProductBasicInfo.find("SpatialCoverageInformation")
if SpatialCoverageInformation is None:
raise Exception("get SpatialCoverageInformation failed")
TopLeftLongitude = SpatialCoverageInformation.find("TopLeftLongitude")
if TopLeftLongitude is None:
raise Exception("get TopLeftLongitude failed")
TopLeftLatitude = SpatialCoverageInformation.find("TopLeftLatitude")
if TopLeftLatitude is None:
raise Exception("get TopLeftLatitude failed")
TopRightLongitude = SpatialCoverageInformation.find("TopRightLongitude")
if TopRightLongitude is None:
raise Exception("get TopRightLongitude failed")
TopRightLatitude = SpatialCoverageInformation.find("TopRightLatitude")
if TopRightLatitude is None:
raise Exception("get TopRightLatitude failed")
BottomRightLongitude = SpatialCoverageInformation.find("BottomRightLongitude")
if BottomRightLongitude is None:
raise Exception("get BottomRightLongitude failed")
BottomRightLatitude = SpatialCoverageInformation.find("BottomRightLatitude")
if BottomRightLatitude is None:
raise Exception("get BottomRightLatitude failed")
BottomLeftLongitude = SpatialCoverageInformation.find("BottomLeftLongitude")
if BottomLeftLongitude is None:
raise Exception("get BottomLeftLongitude failed")
BottomLeftLatitude = SpatialCoverageInformation.find("BottomLeftLatitude")
if BottomLeftLatitude is None:
raise Exception("get BottomLeftLatitude failed")
point_upleft = [float(TopLeftLongitude.text), float(TopLeftLatitude.text)]
point_upright = [float(TopRightLongitude.text), float(TopRightLatitude.text)]
point_downleft = [float(BottomLeftLongitude.text), float(BottomLeftLatitude.text)]
point_downright = [float(BottomRightLongitude.text), float(BottomRightLatitude.text)]
scopes = [point_upleft, point_upright, point_downleft, point_downright]
point_upleft_buf = [float(TopLeftLongitude.text) - 0.5, float(TopLeftLatitude.text) + 0.5]
point_upright_buf = [float(TopRightLongitude.text) + 0.5, float(TopRightLatitude.text) + 0.5]
point_downleft_buf = [float(BottomLeftLongitude.text) - 0.5, float(BottomLeftLatitude.text) - 0.5]
point_downright_buf = [float(BottomRightLongitude.text) + 0.5, float(BottomRightLatitude.text) - 0.5]
scopes_buf = [point_upleft_buf, point_upright_buf, point_downleft_buf, point_downright_buf]
return scopes
if __name__ == '__main__':
xml_path = r"F:\MicroWorkspace\LT1B\LT1B_MONO_KRN_STRIP3_002875_E92.8_N39.9_20220907_GEC_HHHV_L2_0000035924\LT1B_MONO_KRN_STRIP3_002875_E92.8_N39.9_20220907_GEC_HHHV_L2_0000035924.meta.xml"
2024-01-03 01:42:21 +00:00
scopes, scopes_buf = DictXml(xml_path).get_extend()
print(scopes)
print(scopes_buf)
# path = r'D:\BaiduNetdiskDownload\GZ\lon.rdr'
# path2 = r'D:\BaiduNetdiskDownload\GZ\lat.rdr'
# path3 = r'D:\BaiduNetdiskDownload\GZ\lon_lat.tif'
# s = ImageHandler().band_merge(path, path2, path3)
# print(s)
# pass