from xml.etree.ElementTree import ElementTree import os import numpy as np 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") 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)] scopes = [point_upleft, point_upright, point_downright, point_downleft] lon = [float(topLeftLon), float(topRightLon), float(bottomLeftLon), float(bottomRightLon)] lat = [float(topLeftLat), float(topRightLat), float(bottomLeftLat), float(bottomRightLat)] min_lon = np.min(lon) max_lon = np.max(lon) min_lat = np.min(lat) max_lat = np.max(lat) box = [str(min_lat), str(max_lat), str(min_lon), str(max_lon)] box_str = ';'.join(box) 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, box_str 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"D:\micro\LWork\VegetationHeight\Temporary\origin\SARS\20230328\LT1A_MONO_KSC_STRIP4_006353_E109.3_N19.0_20230328_SLC_AHV_L1A_0000087468.meta.xml" xml_path1 = r"D:\micro\LWork\VegetationHeight\Temporary\origin\SARS\20230405\LT1A_MONO_KSC_STRIP4_006472_E109.3_N19.0_20230405_SLC_AHV_L1A_0000091812.meta.xml" scopes, scopes_buf, box_str = DictXml(xml_path).get_extend() scopes1, scopes_buf1, box_str1 = DictXml(xml_path1).get_extend() print(scopes) print(scopes1) # 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