Select to view content in your preferred language

Generate Attachment Match Table - Python script issue

3299
2
11-12-2013 10:36 PM
JamesBulmer
New Contributor
Hi All,

I have a number of jpg files that I'm trying to link to records within a Feature Class, using the Attachments functionality within ArcGIS 10.1. I've done this manually with success for a few jpg files, and am now trying to automate the process.

All of the jpg files are named using the ID of the record to which they're being attached. Because there are sometimes more than one jpg per record, it is not possible to store the jps within a single folder, due to some files sharing the same name (i.e. their corresponding ID). To get around this, every jpg file is stored within an individual subfolder folder within a main folder.

The issue I'm having is when trying to use the Generate Match Table script provided within the official esri guidance here.

At the foot of this page is a code snippet that I'm following to the letter:

# Import system modules
import arcpy, os

# Set local variables.
rootFolder = 'c:/work/'

for folder in os.walk(rootFolder):
    if folder[0].find('.gdb') == -1: #exclude file geodatabases from the folder list.
        arcpy.GenerateAttachmentMatchTable_management("C:/data/parcels.gdb/parcels",folder[0],"C:/data/temp.gdb/matchtable","AttachmentKeyField", "*property*.jpg","RELATIVE")


My understanding is that this code should go through each subfolder within the main folder, pull out the required information and put this in to the Match Table. Unfortunately, this code generates an error:

ERROR 000725: Output Match Table: Dataset C:\temp\DEV\test.gdb\matchtable already exists.

It appears that this code in fact attempts to generate a Match Table PER jpg, which then crashes after the first Match Table has been created and the second Match Table cannot be created over the top of it.

It would be great to know if anyone else has experienced this issue, and/or whether a minor tweak to the python code could resolve the issue. Alternatively, does anyone have any suggestions for working around this issue - how to cycle through jpgs (some with the same names) to create the Match Table.

Thanks,

James
Tags (2)
0 Kudos
2 Replies
XanderBakker
Esri Esteemed Contributor
Hi James,

I didn't test the code, but something like this should do the trick (see code below).

The only difference is:

  • for each folder a new table is created: C:/data/temp.gdb/matchtable + unique number

  • I use the ABSOLUTE setting, since I think the RELATIVE may not work properly in this setting.

  • If the output match table was created it is added to a list

  • The list is used to merged all the tables into the output table

  • at the end the intermediate tables are removed (no checks for locks, though)


# Import system modules
import arcpy, os

# Set local variables.
rootFolder = 'c:/work/'

parcelsFc = "C:/data/parcels.gdb/parcels"
matchTbl = "C:/data/temp.gdb/matchtable"
keyField = "AttachmentKeyField"
filterFile = "*property*.jpg"
relPathSetting = "ABSOLUTE" # in this setting I would use ABSOLUTE instead of RELATIVE


lstMatch = []
cnt = 0
for folder in os.walk(rootFolder):
    if folder[0].find('.gdb') == -1:
        # create unique output with counter
        cnt+=1
        matchTblOK = matchTbl + str(cnt)

        arcpy.GenerateAttachmentMatchTable_management(parcelsFc,folder[0],matchTblOK,keyField, filterFile,relPathSetting)

        if arcpy.Exists(matchTblOK):
            lstMatch.append(matchTblOK)

# append list
arcpy.Merge_management(lstMatch, matchTbl)

# remove the intermediate tables
for tbl in lstMatch:
    arcpy.Delete_management(tbl)


Kind regards,

Xander
0 Kudos
DaveBarrett
Deactivated User
Hi,

I have had a few dramas with the mathc table tool. We have a file structure where multiple files per feature id are stored in folders that are named with the feature id. for example feature id a1 has a corresponding folder called a1 with all the relevant attachments stored within. The way around this was to create a custom match table without using the tool. I have included the function below. Hope this is of help.

import csv, os
def matchTable(inFolder, outFolder):
    """Generates a match table for use in the add attachments tool.

    Creates the match table required by the add attachments tool based on the
    folder structure. The table contains two columns, GUID and PATH. The
    GUID identifies which feature to add the attachment too and PATH holds the
    full path location to the file to attach. The inFolder is the location of
    the file store, the outFolder is the location to store the match table.
    """
    
    # Create the match table and open the csv for writing
    match = outFolder + os.sep + "match.csv"
    
    writer = csv.writer(open(match, "wb"), delimiter=",")
    # Write the header row
    writer.writerow(["GUID","PATH"])

    # Iterate through each folder in the OrientFileStore and get GUID from the folder name
    # Then list each file in that folder and write into the match table
    for dir in os.listdir(inFolder):
        GUID = "{" + (os.path.basename(dir)).upper() + "}"
        workspace = inFolder + os.sep + dir
        for file in os.listdir(workspace):
            PATH = os.path.join(workspace, file)
            writer.writerow([GUID,PATH])

    del writer
    return match


example of a folder structure this works on
Top
|
--SubFolder1 - File1,File2,File3
--SubFolder2- File1,File2,File3

Where the SubFolder name is a value within the feature class attributes that you will use to add the attachments

Hope this helps

Dave
0 Kudos