2

我创建了一个脚本来使用基于 arcpy 的编码自动生成基本 ANZLIC 标准元数据。谁能帮我把它转换成 OGR/GDAL python?这样它就可以在 QGIS 中运行并处理所有类型的矢量和栅格数据。

在此处输入图像描述

下面是对重要部分进行一些注释的工作代码。

import os, xml, arcpy, shutil, datetime
from xml.etree import ElementTree as et

添加/替换为以下内容

import osgeo, os
from osgeo import ogr

...

Count=0
DECLARATION = """<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='ANZMeta.xsl'?>\n"""

RootDirectory=arcpy.GetParameterAsText(0)
LogDirectory=arcpy.GetParameterAsText(1)
Metadata_Template=arcpy.GetParameterAsText(2)
ANZMeta=arcpy.GetParameterAsText(3)

#RootDirectory=os.getcwd()
#LogDirectory=os.getcwd()

currentPath=RootDirectory
arcpy.env.workspace = RootDirectory
root = Tk()
InputStrings = StringVar()
Entry(root, textvariable=InputStrings).pack()

Generated_XMLs=LogDirectory+'\GeneratedXML_LOG.txt'
f = open(Generated_XMLs, 'a')
f.write("Log of Metadata Creation Process - Update: "+str(datetime.datetime.now())+"\n")
f.close()

for root, dirs, files in os.walk(RootDirectory, topdown=False):
    #print root, dirs
    for directory in dirs:
        try:
            currentPath=os.path.join(root,directory)
        except:
            pass
        os.chdir(currentPath)
        arcpy.env.workspace = currentPath
        print currentPath
#def Create_xml(currentPath):

在这里,我们将需要一个类似的调用来列出 ogr 可访问的数据集(ogrinfo 似乎只列出文件的属性 - 有没有类似于 arcpy.List 的东西......)

        FileList = arcpy.ListFeatureClasses()
        zone="_Zone"

        for File in FileList:
            Count+=1
            FileDesc_obj = arcpy.Describe(File)
            FileNm=FileDesc_obj.file

上面的代码需要调用 osgeo.ogr.Open(File) 对吧?

            check_meta=os.listdir(currentPath)
            existingXML=FileNm[:FileNm.find('.')]
            existingExtension=FileNm[FileNm.find('.'):]
            print "XML: "+existingXML
            #print check_meta
            #if  existingXML+'.xml' in check_meta:
            #newMetaFile='new'
            for f in check_meta:
                if f.startswith(existingXML) and f.endswith('.xml'):
                    print "exists, file name:", f
                    newMetaFile=FileNm+"_2012Metadata.xml"
                    try:
                        shutil.copy2(f, newMetaFile)
                    except:
                        pass
                    break
                else:
                    #print "Does not exist"
                    newMetaFile=FileNm+"_BaseMetadata.xml"

            print "New meta file: "+newMetaFile+ " for: "+File
            if newMetaFile.endswith('_BaseMetadata.xml'):
                    metafile=Metadata_Template
                    shutil.copy2(metafile,newMetaFile)
                    print "copied: "+metafile

            else:
                    shutil.copy2(Metadata_Template, newMetaFile)
                    shutil.copy2(ANZMeta, 'ANZMeta.xsl')


            print "Parsing meta file: "+newMetaFile
            tree=et.parse(newMetaFile)
            print "Processing: "+str(File)


            # Update following info only if no old metadata was found

            # Update all XML files with following data
            for node in tree.findall('.//procstep/srcused'):
                node.text = str(currentPath+"\\"+existingXML+".xml")

            dt=str(datetime.datetime.now())
            for node in tree.findall('.//procstep/date'):
                node.text = str(dt[:10])
            for node in tree.findall('.//procstep/time'):
                node.text = str(dt[11:13]+dt[16:19])
            for node in tree.findall('.//metd/date'):
                node.text = str(dt[:10])

以下需要为osgeo重写...

            for node in tree.findall('.//northbc'):
                node.text = str(FileDesc_obj.extent.YMax)
            for node in tree.findall('.//southbc'):
                node.text = str(FileDesc_obj.extent.YMin)
            for node in tree.findall('.//westbc'):
                node.text = str(FileDesc_obj.extent.XMin)
            for node in tree.findall('.//eastbc'):
                node.text = str(FileDesc_obj.extent.XMax)

            for node in tree.findall('.//nondig/formname'):
                node.text = "File: "+str(os.getcwd()+"\\"+File)

            for node in tree.findall('.//native/digform/formname'):
                node.text = "Type: "+str(FileDesc_obj.featureType)
            for node in tree.findall('.//avlform/nondig/formname'):
                node.text = "Ext: "+str(FileDesc_obj.extension)
            for node in tree.findall('.//avlform/digform/formname'):
                size= str(int(os.path.getsize(currentPath+"\\"+File))/int(1024))+" KB"
                node.text = "Size: "+size
                print size
            for node in tree.findall('.//theme'):
                node.text = str(FileDesc_obj.spatialReference.name +" ; EPSG: "+str(FileDesc_obj.spatialReference.factoryCode))
                print str(FileDesc_obj.spatialReference.name +" ; EPSG: "+str(FileDesc_obj.spatialReference.factoryCode))
            #print node.text
            projection_info=[]
            Zone=FileDesc_obj.spatialReference.name

            if "GCS" in str(FileDesc_obj.spatialReference.name):
                projection_info=[FileDesc_obj.spatialReference.GCSName, FileDesc_obj.spatialReference.angularUnitName, FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName]
                #print "Geographic Coordinate system"
            else:
                projection_info=[FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName, FileDesc_obj.spatialReference.angularUnitName, Zone[Zone.rfind(zone)-3:]]
                #print "Projected Coordinate system"
            x=0
            for node in tree.findall('.//spdom'):
                for node2 in node.findall('.//keyword'):
                    #print node2.text
                    node2.text = str(projection_info[x])
                    #print node2.text
                    x=x+1


            tree.write(newMetaFile)

            # CODE to add declaration back into xml (sometimes required)
##            s = open(currentPath+"\\"+newMetaFile).read()
##            s = s.replace('<anzmeta>', DECLARATION+'<anzmeta>')
##            #s = s.replace('P.O. Box 1616', 'P.O. Box 573')
##            f = open(currentPath+"\\"+newMetaFile, 'w')
##            f.write(s)
##            f.close()

            f = open(Generated_XMLs, 'a')
            f.write(str(Count)+": "+File+"; "+newMetaFile+"; "+currentPath+";"+existingXML+"\n")
            f.close()

我看到我可以使用以下内容

dataSource=osgeo.ogr.Open(fn)
layer = dataSource.GetLayer(0)
projection = layer.GetSpatialRef() #Get spatial reference
feature = layer.GetFeature(0)
extent=layer.GetExtent()
numFeatures=layer.GetFeatureCount()
4

0 回答 0