Check existence of linked file for each record in table

1004
6
Jump to solution
07-31-2012 10:41 AM
johnodonnell
New Contributor
We have a feature class consisting of manholes. Each manhole has a corresponding photo. All the photos reside on the server. The photo file names equate to the manholes' unique ID number. There is a field in the feature class that is calculated to the file path to each manhole's photo. I need a bit of code that I can use in Field Calculator to check if there is indeed a photo at the end of each path. Any ideas?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Not sure about field calculator, but cursors will work well for this.
photo_field = "your_path_field" curs = arcpy.SearchCursor(fc,"","",photo_field) for row in curs:     photo_path = row.getValue(photo_field)     if os.path.exists(photo_path):         print "%s exists" % os.path.basename(photo_path)      else:         print "%s not found" % os.path.basename(photo_path) 


Alternatively you can use an update cursor and fill a field with Exists Y/N during the check.

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
Not sure about field calculator, but cursors will work well for this.
photo_field = "your_path_field" curs = arcpy.SearchCursor(fc,"","",photo_field) for row in curs:     photo_path = row.getValue(photo_field)     if os.path.exists(photo_path):         print "%s exists" % os.path.basename(photo_path)      else:         print "%s not found" % os.path.basename(photo_path) 


Alternatively you can use an update cursor and fill a field with Exists Y/N during the check.
0 Kudos
ChristopherBlinn1
Occasional Contributor III
This can be done a different way and still be pretty easy.  If your images are stored in the same folder or directory then this won't be that bad.

You can use the command prompt to list all files in a directory using the DIR command and export that to a text file.

Ex.

DIR c:\ /B > c:\test.txt

Now, test.txt has a list of all files in the directory c:\.

Open test.txt in Excel, remove your file types at the end (can be done using a simple find/replace).  Give the column a title like MANHOLE_ID and save the table.

Create a short integer field in your Manhole table called something like IMG_EXIST.

You can then open the table you created in Excel in ArcGIS, export it to have an OID, then join it to your Manhole's table.  Base the join off of the MANHOLE_ID field and keep all records.

Once joined, only matching values with have a MANHOLE_ID value in both the Manhole and Imported table will not have NULL values in the MANHOLE_ID field.  Use the field calculator to populate the IMG_EXIST field to denote if an image exists.

Something like:

dim x
if IsNull( [MANHOLE_ID] ) then
x = 0
else
x = 1
end if

IMG_EXIST = x

All values of 0 will show manholes without an image, and manholes with 1 do have an image.

You can save the scripts and use them to update the field as you get more images in the future.

Not the python solution but hope it helps.  We do this same approach to populate fields for our websites which let a user know if attachments or additional documents exist for properties, etc.

Best,
Chris B.
0 Kudos
johnodonnell
New Contributor
Thank you for the suggestions. I've built upon one, creating a standalone script that I've added to a custom toolbox. The body of the script is included below. This script will run and complete with no errors, but it won't produce the result I'm going for. The field "PhotoExists" still only contain null values. Any thoughts? And thanks again!

import arcpy
import os

featureClass = arcpy.GetParameterAsText(0)

photo_field = "PhotoHotlink"
results_field = "PhotoExists"
curs = arcpy.UpdateCursor(featureClass, results_field)
row = curs.next()

for row in curs:
    photo_path = row.getValue(photo_field)
    if os.path.exists(photo_path):
        row.setValue(results_field, "Y")
    else:
        row.setValue(results_field, "N")

    curs.updateRow(row)
    row = curs.next()


del curs, row
0 Kudos
MathewCoyle
Frequent Contributor
your syntax for the cursor is a little off. The first parameter is the input which is correct, but the second parameter is the where clause which you have populated with your field variable. Also, you don't need to add the next row commands. You want it to look like this.

import arcpy
import os

featureClass = arcpy.GetParameterAsText(0)

photo_field = "PhotoHotlink"
results_field = "PhotoExists"
curs = arcpy.UpdateCursor(featureClass, "", "", [photo_field, results_field])
#row = curs.next()

for row in curs:
    photo_path = row.getValue(photo_field)
    if os.path.exists(photo_path):
        row.setValue(results_field, "Y")
    else:
        row.setValue(results_field, "N")

    curs.updateRow(row)
    #row = curs.next()


del curs, row


Actually, you need to include both your reading and writing fields.
0 Kudos
johnodonnell
New Contributor
Thanks for the fast response! I made the corrections you suggested and got the following error/message:

Executing: Script P:\xxxxxxxxx
Start Time: Wed Aug 01 13:28:06 2012
Running script Script...

Traceback (most recent call last):
  File "P:\2012_Projects\12004_Chatt_ST_DBCC\Toolboxes\PhotoExists2.py", line 13, in <module>
    if os.path.exists(photo_path):
  File "C:\Python27\ArcGIS10.1\Lib\genericpath.py", line 18, in exists
    os.stat(path)
TypeError: coercing to Unicode: need string or buffer, NoneType found

Failed to execute (Script).
Failed at Wed Aug 01 13:28:31 2012 (Elapsed Time: 25.00 seconds)


Any idea what's wrong?
0 Kudos
johnodonnell
New Contributor
Never mind! It works great! Thanks so much for your help!
0 Kudos