利⽤影像的元数据⽂件⽣成落图的程序(xmltfw)有时候需要获取遥感影像的范围,即落图⽂件。利⽤⾼分影像的元数据⽂件⽣成则⼗分简便快捷。根据实际需求,编写了⽣成落图的程序,主要针对⾼分的影像。不同的传感器的数据存储的形式也不⼀定⼀样,针对相同的存储形式写了相应的函数。基于Python实现。考虑到不同的影像有不同的投影,最后每个xml⽂件对应⼀个shp。
1 导⼊需要的库
import os
as ogr
import osgeo.osr as osr
import ElementTree
import tkFileDialog
2 设置输⼊输出路径 ,⼿动设置或⽤导航式选择
#inputpath="E:\\wcg\\luotu"
#outputpath="E:\\wcg\\luotu13"
inputpath=tkFileDialog.askdirectory(title="Select inputs XML folder")
outputpath=tkFileDialog.askdirectory(title="Select outputs SHPFILE folder")
3 获取输⼊路径内的所有⽂件
def getListFiles(path):
assert os.path.isdir(path),'%s not exist,'%path
ret=[]
for root,dirs,files in os.walk(path):
for filespath in files:
ret.append(os.path.join(root,filespath))
return ret
4 针对ZY3、02C、GF2、GF1、SV1 的函数
def read_ZY3_02C_GF2_GF1_SV1(text):
corner={}
corner["A"]=[]text函数什么意思
corner["B"]=[]
corner["C"]=[]
corner["D"]=[]
others={}
others["SolarAzimuth"]=""
others["SolarZenith"]=""
others["SatelliteAzimuth"]=""
others["SatelliteZenith"]=""
others["StartTime"]=""
others["EndTime"]=""
others["CloudPercent"]=""
root = ElementTree.parse(text)
lst_node = iterator("TopLeftLatitude")
for node in lst_node:
corner["A"].)
lst_node = iterator("TopLeftLongitude")
for node in lst_node:
corner["A"].)
lst_node = iterator("TopRightLatitude")
for node in lst_node:
corner["B"].)
lst_node = iterator("TopRightLongitude")
for node in lst_node:
corner["B"].)
lst_node = iterator("BottomRightLatitude")      for node in lst_node:
corner["C"].)
lst_node = iterator("BottomRightLongitude")      for node in lst_node:
corner["C"].)
lst_node = iterator("BottomLeftLatitude")
for node in lst_node:
corner["D"].)
lst_node = iterator("BottomLeftLongitude")      for node in lst_node:
corner["D"].)
lst_node = iterator("SolarAzimuth")
for node in lst_node:
others["SolarAzimuth"]=
lst_node = iterator("SolarZenith")
for node in lst_node:
others["SolarZenith"]=
lst_node = iterator("SatelliteAzimuth")
for node in lst_node:
others["SatelliteAzimuth"]=
lst_node = iterator("SatelliteZenith")
for node in lst_node:
others["SatelliteZenith"]=
lst_node = iterator("StartTime")
for node in lst_node:
others["StartTime"]=
lst_node = iterator("EndTime")
for node in lst_node:
others["EndTime"]=
lst_node = iterator("CloudPercent")
for node in lst_node:
others["CloudPercent"]=
return corner,others
5 针对YG24的函数
def read_YG24(text):
per=ElementTree.parse(text)
corners=per.findall('Corners')
four={}
four["A"]=[]
four["B"]=[]
four["C"]=[]
four["D"]=[]
others={}
others["SolarAzimuth"]=""
others["SolarZenith"]=""
others["SatelliteAzimuth"]=""
others["SatelliteZenith"]=""
others["StartTime"]=""
others["EndTime"]=""
others["CloudPercent"]=""
for corner in corners:
for child hildren():
if child.tag=="UpperLeft":
for chi in child:
four["A"].)
if child.tag=="UpperRight":
for chi in child:
four["B"].)
if child.tag=="LowerRight":
for chi in child:
four["C"].)
if child.tag=="LowerLeft":
for chi in child:
four["D"].)
metas=per.findall('ProductMeta')                        for meta in metas:
for child hildren():
if child.tag=="StartTime":
for chi in child:
others["StartTime"]=
if child.tag=="EndTime":
for chi in child:
others["EndTime"]=
return four,others
6 针对SPOT6的函数
def read_SPOT6(text):
per=ElementTree.parse(text)
content=per.findall('Dataset_Content')
extent=content[0].findall("Dataset_Extent")
fourlist=[]
for corner in extent:
for child hildren():
if child.tag=="Vertex":
for chi in child:
if chi.tag=="LON":
fourlist.)
if chi.tag=="LAT":
fourlist.)
four={}
four["A"]=[fourlist[1],fourlist[0]]
four["B"]=[fourlist[3],fourlist[2]]
four["C"]=[fourlist[5],fourlist[4]]
four["D"]=[fourlist[7],fourlist[6]]
others={}
others["SolarAzimuth"]=""
others["SolarZenith"]=""
others["SatelliteAzimuth"]=""
others["SatelliteZenith"]=""
others["StartTime"]=""
others["EndTime"]=""
others["CloudPercent"]=""
cloud=(content[0].findall("CLOUD_COVERAGE"))[0].text
others["CloudPercent"]=cloud
sources=per.findall('Dataset_Sources')
times=sources[0].findall("Source_Identification")
for time in times:
for child hildren():
if child.tag=="Strip_Source":
for chi in child:
if chi.tag=="IMAGING_DATE":
others["StartTime"]=+" "
if chi.tag=="IMAGING_TIME":
others["StartTime"]+=
others["EndTime"]= others["StartTime"]
return four,others
7 转出到⽮量的函数
def toshp(ABCD1,otherss,outShapefile):
ring = ogr.Geometry(ogr.wkbLinearRing)
for jw in ABCD1:
ring.AddPoint(jw[0],jw[1])
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS('WGS84')
poly = ogr.Geometry(ogr.wkbPolygon)
poly.AddGeometry(ring)
outDriver = ogr.GetDriverByName("ESRI Shapefile")
if ists(outShapefile):
outDriver.DeleteDataSource(outShapefile)
outDataSource = outDriver.CreateDataSource(outShapefile)
outLayer = outDataSource.CreateLayer("states_extent",srs,geom_type=ogr.wkbPolygon)
outLayer = outDataSource.CreateLayer("states_extent",srs,geom_type=ogr.wkbPolygon)    yxmc=ogr.FieldDefn("YXMC", ogr.OFTString)
yxmc.SetWidth(255)
outLayer.CreateField(ogr.FieldDefn("ID", ogr.OFTInteger))
outLayer.CreateField(yxmc)
SolarAzimuth=ogr.FieldDefn("SolAzimuth", ogr.OFTString)
SolarAzimuth.SetWidth(10)
outLayer.CreateField(SolarAzimuth)
SolarZenith=ogr.FieldDefn("SolZenith", ogr.OFTString)
SolarZenith.SetWidth(10)
outLayer.CreateField(SolarZenith)
SatelliteAzimuth=ogr.FieldDefn("SatAzimuth", ogr.OFTString)
SatelliteAzimuth.SetWidth(10)
outLayer.CreateField(SatelliteAzimuth)
SatelliteZenith=ogr.FieldDefn("SatZenith", ogr.OFTString)
SatelliteZenith.SetWidth(10)
outLayer.CreateField(SatelliteZenith)
StartTime=ogr.FieldDefn("StartTime", ogr.OFTString)
StartTime.SetWidth(30)
outLayer.CreateField(StartTime)
EndTime=ogr.FieldDefn("EndTime", ogr.OFTString)
EndTime.SetWidth(30)
outLayer.CreateField(EndTime)
CloudPercent=ogr.FieldDefn("CloudPer", ogr.OFTString)
CloudPercent.SetWidth(10)
outLayer.CreateField(CloudPercent)
featureDefn = outLayer.GetLayerDefn()
feature = ogr.Feature(featureDefn)
feature.SetGeometry(poly)
feature.SetField("id", 1)
index1=outShapefile.rfind("/")
feature.SetField("YXMC", outShapefile[index1+1:-4])
feature.SetField("SolAzimuth",otherss["SolarAzimuth"])
feature.SetField("SolZenith",otherss["SolarZenith"])
feature.SetField("SatAzimuth",otherss["SatelliteAzimuth"])
feature.SetField("SatZenith",otherss["SatelliteZenith"])
feature.SetField("StartTime",otherss["StartTime"])
feature.SetField("EndTime",otherss["EndTime"])
feature.SetField("CloudPer",otherss["CloudPercent"])
outLayer.CreateFeature(feature)
feature = None
outDataSource = None
8 最后的调⽤

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。