File Geodatabase에 모든 파일 이름 (및 메타 데이터)이 포함 된 테이블을 만드시겠습니까?


12

File Geodatabase의 모든 파일에 대한 메타 데이터 테이블을 만들어야합니다.

새로운 파일 이름 및 메타 데이터 (ArcGIS 메타 데이터 편집기에서 생성 된)로 자동 업데이트 할 수 있습니까?

답변:


23

다음은 출력이 지오 데이터베이스 테이블이 아니라 CSV 파일이라는 점을 제외하고 기본적으로 원하는 것을 수행하는 ArcGIS 10 용 Python 스크립트입니다. 원하는대로 수정하고 사용하십시오. 잘 테스트되지 않았으며 지원되지 않으므로 위험에 따라 사용하십시오.

"""
This script looks through the specified geodatabase and reports the
names of all data elements, their schema owners and their feature
dataset (if applicable). Certain metadata elements such as abstract,
purpose and search keys are also reported.

The output is a CSV file that can be read by Excel, ArcGIS, etc.

Only geodatabases are supported, not folder workspaces.

Note: If run from outside of ArcToolbox, you will need to add
the metadata tool assemblies to the Global Assembly Cache.
See: http://forums.arcgis.com/threads/74468-Python-Errors-in-IDLE-when-processing-metadata

Parameters:
    0 - Input workspace (file geodatabase, personal geodatabase,
            or SDE connection file)
    1 - Output CSV file

Date updated: 2/11/2013
"""

import arcpy
import os
import csv
import tempfile
import codecs
import cStringIO
from xml.etree.ElementTree import ElementTree

def ListWorkspaceContentsAndMetadata(workspace):
    """Generator function that lists the contents of the geodatabase including those within feature datasets.
       Certain metadata elements are also listed. Only geodatabases are supported, not folder workspaces."""

    if not arcpy.Exists(workspace):
        raise ValueError("Workspace %s does not exist!" % workspace)

    desc = arcpy.Describe(workspace)

    if not desc.dataType in ['Workspace', 'FeatureDataset']:
        if not hasattr(desc, "workspaceType") or not desc.workspaceType in ["LocalDatabase", "RemoteDatabase"]:
            raise ValueError("Workspace %s is not a geodatabase!" % workspace)

    children = desc.children
    if desc.dataType == 'FeatureDataset':
        validationWorkspace = os.path.dirname(workspace)
        fdsName = arcpy.ParseTableName(desc.name, validationWorkspace).split(",")[2].strip() # Get the short name of the feature dataset (sans database/owner name)
    else:
        validationWorkspace = workspace
        fdsName = ""

    for child in children:
        # Parse the full table name into database, owner, table name
        database, owner, tableName = [i.strip() if i.strip() != "(null)" else "" for i in arcpy.ParseTableName(child.name, validationWorkspace).split(",")]
        datasetType = child.datasetType if hasattr(child, "datasetType") else ""
        alias = child.aliasName if hasattr(child, "aliasName") else ""
        outrow = [owner, tableName, alias, fdsName, datasetType]
        try:
            outrow.extend(GetMetadataItems(child.catalogPath))
        except:
            pass
        print ",".join(outrow)
        yield outrow

        # Recurse to get the contents of feature datasets
        if datasetType == 'FeatureDataset':
            for outrow in ListWorkspaceContentsAndMetadata(child.catalogPath):
                yield outrow

def WriteCSVFile(csvfile, rows, header=None):
    """Creates a CSV file from the input header and row sequences"""
    with open(csvfile, 'wb') as f:
        f.write(codecs.BOM_UTF8) # Write Byte Order Mark character so Excel knows this is a UTF-8 file
        w = UnicodeWriter(f, dialect='excel', encoding='utf-8')
        if header:
            w.writerow(header)
        w.writerows(rows)

def CreateHeaderRow():
    """Specifies the column names (header row) for the CSV file"""
    return ("OWNER", "TABLE_NAME", "ALIAS", "FEATURE_DATASET", "DATASET_TYPE", "ORIGINATOR", "CONTACT_ORG", "ABSTRACT", "PURPOSE", "SEARCH_KEYS", "THEME_KEYS")

def CreateDummyXMLFile():
    """Creates an XML file with the required root element 'metadata' in
    the user's temporary files directory. Returns the path to the file.
    The calling code is responsible for deleting the temporary file."""
    tempdir = tempfile.gettempdir()
    fd, filepath = tempfile.mkstemp(".xml", text=True)
    with os.fdopen(fd, "w") as f:
        f.write("<metadata />")
        f.close()
    return filepath

def GetMetadataElementTree(dataset):
    """Creates and returns an ElementTree object from the specified
    dataset's metadata"""
    xmlfile = CreateDummyXMLFile()
    arcpy.MetadataImporter_conversion(dataset, xmlfile)
    tree = ElementTree()
    tree.parse(xmlfile)
    os.remove(xmlfile)
    return tree

def GetElementText(tree, elementPath):
    """Returns the specified element's text if it exists or an empty
    string if not."""
    element = tree.find(elementPath)
    return element.text if element != None else ""

def GetFirstElementText(tree, elementPaths):
    """Returns the first found element matching one of the specified
    element paths"""
    result = ""
    for elementPath in elementPaths:
        element = tree.find(elementPath)
        if element != None:
            result = element.text
            break
    return result

def ListElementsText(tree, elementPath):
    """Returns a comma+space-separated list of the text values of all
    instances of the specified element, or an empty string if none are
    found."""
    elements = tree.findall(elementPath)
    if elements:
        return ", ".join([element.text for element in elements])
    else:
        return ""

def GetMetadataItems(dataset):
    """Retrieves certain metadata text elements from the specified dataset"""
    tree = GetMetadataElementTree(dataset)
    originator = GetElementText(tree, "idinfo/citation/citeinfo/origin") # Originator
    pocorg = GetFirstElementText(tree, ("idinfo/ptcontac/cntinfo/cntperp/cntorg", # Point of contact organization (person primary contact)
                                        "idinfo/ptcontac/cntinfo/cntorgp/cntorg")) # Point of contact organization (organization primary contact)
    abstract = GetElementText(tree, "idinfo/descript/abstract") # Abstract
    purpose = GetElementText(tree, "idinfo/descript/purpose") # Purpose
    searchkeys = ListElementsText(tree, "dataIdInfo/searchKeys/keyword") # Search keywords
    themekeys = ListElementsText(tree, "idinfo/keywords/theme/themekey") # Theme keywords
    del tree
    metadataItems = (originator, pocorg, abstract, purpose, searchkeys, themekeys)
    return metadataItems

class UnicodeWriter:
    """
    A CSV writer which will write rows to CSV file "f",
    which is encoded in the given encoding.
    """

    def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
        # Redirect output to a queue
        self.queue = cStringIO.StringIO()
        self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
        self.stream = f
        self.encoder = codecs.getincrementalencoder(encoding)()

    def writerow(self, row):
        self.writer.writerow([s.encode("utf-8") for s in row])
        # Fetch UTF-8 output from the queue ...
        data = self.queue.getvalue()
        data = data.decode("utf-8")
        # ... and reencode it into the target encoding
        data = self.encoder.encode(data)
        # write to the target stream
        self.stream.write(data)
        # empty queue
        self.queue.truncate(0)

    def writerows(self, rows):
        for row in rows:
            self.writerow(row)

if __name__ == '__main__':
    workspace = arcpy.GetParameterAsText(0)
    csvFile = arcpy.GetParameterAsText(1)
    headerRow = CreateHeaderRow()
    print headerRow
    datasetRows = ListWorkspaceContentsAndMetadata(workspace)
    WriteCSVFile(csvFile, datasetRows, headerRow)

@ blah238 대본! 스크립트를 실행할 때 많은 열이 관련 메타 데이터로 채워지지 않습니다. 채워진 열에는 몇 년 전에 다른 사람들이 메타 데이터를 만들었습니다. 스크립트가 모든 메타 데이터를 캡처하려면 메타 데이터가 특정 형식이어야합니까? 아니면이 메타 데이터가 없을 수도있는 다른 것이 있습니까? ArcMap 10.3을 실행하고 대부분 SDE를 사용합니다.
reevesii

@reevesii 다양한 메타 데이터 형식이 있으므로 원하는 특정 메타 데이터 항목에 액세스하고 출력하도록 스크립트를 조정해야합니다 ( GetMetadataItemsCreateHeaderRow기능 참조 ).
blah238
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.