I have a geodatabase feauture class that we have been using for years. We used to collect an image into a raster field for each feature in the Enterprise Geodatabase (SQL Server). With our new field maps collecton process I cannot figure out how to collect into the raster field, so we have enabled attachments. I would like to now move any existing pictures from the raster field into an attachment.
Here is an example:
I need guidance/advice!
Matt Syvertson
Solved! Go to Solution.
This should be easy to do with ArcPy. Save the raster out and add it back to the related attachments table.
My python is rusty so I asked CoPilot.
---
Script Explanation:
Extract the raster data from the raster field.
Save the raster data as an image file.
Add the saved image as an attachment to the corresponding feature.
import arcpy
import os
# Input feature class
feature_class = r"path\to\your\feature_class"
raster_field = "RasterFieldName" # Replace with your raster field name
attachment_table = r"path\to\your\attachment_table" # Attachment table path
output_folder = r"path\to\output\folder" # Temporary folder to save images
# Ensure output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Add attachments if not already enabled
if not arcpy.Describe(feature_class).hasAttachments:
arcpy.EnableAttachments_management(feature_class)
# Iterate through features
with arcpy.da.SearchCursor(feature_class, ["OID@", raster_field]) as cursor:
for oid, raster in cursor:
if raster:
# Save raster to a temporary file
output_image = os.path.join(output_folder, f"feature_{oid}.jpg")
raster.save(output_image)
# Add the image as an attachment
arcpy.AddAttachments_management(
feature_class,
"OBJECTID",
attachment_table,
"REL_OBJECTID",
"ATTACHMENTID",
output_image
)
# Optionally, delete the raster field value (if needed)
with arcpy.da.UpdateCursor(feature_class, [raster_field], f"OBJECTID = {oid}") as update_cursor:
for row in update_cursor:
row[0] = None
update_cursor.updateRow(row)
print("Images moved to attachments successfully!")
Notes:
Replace placeholders like path\to\your\feature_class, RasterFieldName, and path\to\output\folder with your actual paths and field names.
Ensure the feature class has attachments enabled. If not, the script will enable it.
The script saves raster images temporarily in the specified folder. You can clean up this folder after execution.
Thanks for the code. It did not all work, but it got me on the right track and I was able to complete the task. Here is the code as I modified it:
and then the "Add Attachments" tool, like this:
It all worked! Thanks.
This should be easy to do with ArcPy. Save the raster out and add it back to the related attachments table.
My python is rusty so I asked CoPilot.
---
Script Explanation:
Extract the raster data from the raster field.
Save the raster data as an image file.
Add the saved image as an attachment to the corresponding feature.
import arcpy
import os
# Input feature class
feature_class = r"path\to\your\feature_class"
raster_field = "RasterFieldName" # Replace with your raster field name
attachment_table = r"path\to\your\attachment_table" # Attachment table path
output_folder = r"path\to\output\folder" # Temporary folder to save images
# Ensure output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Add attachments if not already enabled
if not arcpy.Describe(feature_class).hasAttachments:
arcpy.EnableAttachments_management(feature_class)
# Iterate through features
with arcpy.da.SearchCursor(feature_class, ["OID@", raster_field]) as cursor:
for oid, raster in cursor:
if raster:
# Save raster to a temporary file
output_image = os.path.join(output_folder, f"feature_{oid}.jpg")
raster.save(output_image)
# Add the image as an attachment
arcpy.AddAttachments_management(
feature_class,
"OBJECTID",
attachment_table,
"REL_OBJECTID",
"ATTACHMENTID",
output_image
)
# Optionally, delete the raster field value (if needed)
with arcpy.da.UpdateCursor(feature_class, [raster_field], f"OBJECTID = {oid}") as update_cursor:
for row in update_cursor:
row[0] = None
update_cursor.updateRow(row)
print("Images moved to attachments successfully!")
Notes:
Replace placeholders like path\to\your\feature_class, RasterFieldName, and path\to\output\folder with your actual paths and field names.
Ensure the feature class has attachments enabled. If not, the script will enable it.
The script saves raster images temporarily in the specified folder. You can clean up this folder after execution.
Thanks. I will try this and report back.
Thanks for the code. It did not all work, but it got me on the right track and I was able to complete the task. Here is the code as I modified it:
and then the "Add Attachments" tool, like this:
It all worked! Thanks.