Select to view content in your preferred language

Coding Help!

4799
6
Jump to solution
03-04-2015 03:34 AM
SamPlant
Deactivated User

Hi,

 

I have a FGDB for a project at work.

We have plotted points for road signs and collected various attributes for each point.

We also have a picture attachment for each point.

The information was collected on the Collector app and has now been downloaded.

 

I now need to do a few things to the data.

 

I need to populate a field named 'USSR' with an automatic incremental number followed by the letter 'A', starting at a set number i.e. 7000A, 7001A, 7002A etc.

 

I then need the attachments to be saved and named as '7000A.jpg', '7001A.jpg', '7002A.jpg' etc.

 

Then I need the 'Photo' field to be filled with the photo name i.e. '7000A.jpg'

 

I've been looking for some Python script I can run from ArcMap to batch rename and update for me, but I'm struggling to find anything I can use.

 

If anyone could help me I'd be very grateful.

I have a very limited knowledge of coding and have no idea where to start.

 

Thanks

Sam Plant

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Try the code below.

Note:  In this example, Global IDs exist, so the relationship between the feature class and attachments table is based off of the GLOBALID and REL_GLOBALID fields.  If your data does not contain GlobalIDs, the relationship will exist between the OBJECTID and REL_OBJECTID field.  So, you will need to update line 10 to "REL_OBJECTID" and line 20 to row.OBJECTID.

import arcpy, os  
from arcpy import env  
env.workspace = r"C:\temp\python\test.gdb"  
  
#Update USSR and Photo field  
fc = "Airports"
tbl = "Airports__ATTACH"
field1 = "USSR"  
field2 = "Photo"
relateField = "REL_GLOBALID"
fldBLOB = 'DATA'  
fldAttName = 'ATT_NAME'  
outFolder = r"C:\Temp\Python\Attachments" 
  
x = 7000  
y = 0  
  
rows = arcpy.UpdateCursor(fc)  
for row in rows:
    globalID = row.GlobalID
    with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName,relateField]) as cursor:  
       for row2 in cursor:
           if row2[2] == globalID:
               row.setValue(field1, str(x + y) + "A")  
               row.setValue(field2, str(x + y) + "A.JPG") 
               binaryRep = row2[0]  
               open(outFolder + os.sep + str(x + y) + "A.JPG", 'wb').write(binaryRep.tobytes())             
    y += 1  
    rows.updateRow(row)  
  
del row, rows, row2, cursor
  
   
print 'Finished' 

View solution in original post

0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Sam,

Here is an example on how to do this:

import arcpy, os
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

#Update USSR and Photo field
fc = "Airports"
field1 = "USSR"
field2 = "Photo"

x = 7000
y = 0

rows = arcpy.UpdateCursor(fc)
for row in rows:
    row.setValue(field1, str(x + y) + "A")
    row.setValue(field2, str(x + y) + "A.JPG")
    y += 1
    rows.updateRow(row)

del row, rows


#Save attachments
tbl = r"C:\Temp\Python\Test.gdb\Airports__ATTACH"
fldBLOB = 'DATA'
fldAttName = 'ATT_NAME'
outFolder = r"C:\Temp\Python\Attachments"

x = 7000
y = 0

with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName]) as cursor:
   for row in cursor:
      binaryRep = row[0]
      open(outFolder + os.sep + str(x + y) + "A.JPG", 'wb').write(binaryRep.tobytes())
      y += 1

del cursor
print 'Finished'
SamPlant
Deactivated User

Thanks that's brilliant.

I'll have a go and see if I can get it working.

I have manually had to rename 4000 signs from a different program, and it takes a long time...

0 Kudos
SamPlant
Deactivated User

I've tried the code and it works well, but I have a slight problem. A few of the points don't have attachments, but I'm not sure where. So the pictures coming out don't match up with the points with the same name.

Is there a way to identify these points?

Or skip these for the renaming?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Try the code below.

Note:  In this example, Global IDs exist, so the relationship between the feature class and attachments table is based off of the GLOBALID and REL_GLOBALID fields.  If your data does not contain GlobalIDs, the relationship will exist between the OBJECTID and REL_OBJECTID field.  So, you will need to update line 10 to "REL_OBJECTID" and line 20 to row.OBJECTID.

import arcpy, os  
from arcpy import env  
env.workspace = r"C:\temp\python\test.gdb"  
  
#Update USSR and Photo field  
fc = "Airports"
tbl = "Airports__ATTACH"
field1 = "USSR"  
field2 = "Photo"
relateField = "REL_GLOBALID"
fldBLOB = 'DATA'  
fldAttName = 'ATT_NAME'  
outFolder = r"C:\Temp\Python\Attachments" 
  
x = 7000  
y = 0  
  
rows = arcpy.UpdateCursor(fc)  
for row in rows:
    globalID = row.GlobalID
    with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName,relateField]) as cursor:  
       for row2 in cursor:
           if row2[2] == globalID:
               row.setValue(field1, str(x + y) + "A")  
               row.setValue(field2, str(x + y) + "A.JPG") 
               binaryRep = row2[0]  
               open(outFolder + os.sep + str(x + y) + "A.JPG", 'wb').write(binaryRep.tobytes())             
    y += 1  
    rows.updateRow(row)  
  
del row, rows, row2, cursor
  
   
print 'Finished' 
0 Kudos
SamPlant
Deactivated User

Brilliant. I managed to figure out a way around it, by splitting the table in two and saving separately, but this will help me the next few times I need to do it, and save me time.

Thankyou

0 Kudos
LarryJahn
Frequent Contributor

Can this code be adapted to bulk rename picture attachments collected with Collector for ArcGIS/ArcGIS Online?  The picture attachments have been downloaded locally but need to be renamed based on feature class ID attribute (e.g. Stop_ID in a point feature class).

0 Kudos