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): productInfo = self.__root.find("imageinfo") if productInfo is None: raise Exception("get imageInfo failed") corner = productInfo.find("corner") if corner is None: raise Exception("get corner failed") topLeft = corner.find("topLeft") if topLeft is None: raise Exception("get topLeft failed") topRight = corner.find("topRight") if topRight is None: raise Exception("get topRight failed") bottomLeft = corner.find("bottomLeft") if bottomLeft is None: raise Exception("get bottomLeft failed") bottomRight = corner.find("bottomRight") if bottomRight is None: raise Exception("get bottomRight failed") point_upleft = [float(topLeft.find("longitude").text), float(topLeft.find("latitude").text)] point_upright = [float(topRight.find("longitude").text), float(topRight.find("latitude").text)] point_downleft = [float(bottomLeft.find("longitude").text), float(bottomLeft.find("latitude").text)] point_downright = [float(bottomRight.find("longitude").text), float(bottomRight.find("latitude").text)] scopes = [point_upleft, point_upright, point_downleft, point_downright] point_upleft_buf = [float(topLeft.find("longitude").text) - 0.4, float(topLeft.find("latitude").text) + 0.4] point_upright_buf = [float(topRight.find("longitude").text) + 0.4, float(topRight.find("latitude").text) + 0.4] point_downleft_buf = [float(bottomLeft.find("longitude").text) - 0.4, float(bottomLeft.find("latitude").text) - 0.4] point_downright_buf = [float(bottomRight.find("longitude").text) + 0.4, float(bottomRight.find("latitude").text) - 0.4] scopes_buf = ([point_upleft_buf, point_upright_buf, point_downleft_buf, point_downright_buf], ) return scopes, scopes_buf 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'E:\MicroWorkspace\GF3A_nanjing\GF3_SAY_QPSI_011444_E118.9_N31.4_20181012_L1A_AHV_L10003515422\GF3_SAY_QPSI_011444_E118.9_N31.4_20181012_L1A_AHV_L10003515422.meta.xml' 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