Select to view content in your preferred language

Move Raster from field to attachment

656
3
Jump to solution
07-25-2025 09:32 AM
Syvertson
Frequent Contributor

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:

Screenshot 2025-07-25 113121.png

 

I need guidance/advice!

Matt Syvertson

0 Kudos
2 Solutions

Accepted Solutions
sjones_esriau
Esri Contributor

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.

View solution in original post

0 Kudos
Syvertson
Frequent Contributor

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:

import arcpy
 
with arcpy.da.SearchCursor("GEO.Section_Control_Points", ["Picture", "GLOBALID@"]) as cursor:
for row in cursor:
raster_object = row[0]
global_id = row[1]
output_image = arcpy.os.path.join(r"C:\\tempoutput\\", f"raster_{global_id}.tif")
if raster_object is not None:
raster_object.save(output_image)
arcpy.AddMessage(f"Exported raster from GLOBALID {global_id} to {output_image}")
else:
arcpy.AddMessage(f"No raster found for GLOBALID {global_id}")
 
The code allowed me to create a folder with all the attachments, but I could not get the code to add the rasters as attachments to work, but I was able to manually use the geoprocessing tool "Generate Attachment Table" like this:
Syvertson_0-1753973981676.png

and then the "Add Attachments" tool, like this:

Syvertson_1-1753974041677.png

It all worked!  Thanks.

View solution in original post

0 Kudos
3 Replies
sjones_esriau
Esri Contributor

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.

0 Kudos
Syvertson
Frequent Contributor

Thanks.  I will try this and report back.

0 Kudos
Syvertson
Frequent Contributor

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:

import arcpy
 
with arcpy.da.SearchCursor("GEO.Section_Control_Points", ["Picture", "GLOBALID@"]) as cursor:
for row in cursor:
raster_object = row[0]
global_id = row[1]
output_image = arcpy.os.path.join(r"C:\\tempoutput\\", f"raster_{global_id}.tif")
if raster_object is not None:
raster_object.save(output_image)
arcpy.AddMessage(f"Exported raster from GLOBALID {global_id} to {output_image}")
else:
arcpy.AddMessage(f"No raster found for GLOBALID {global_id}")
 
The code allowed me to create a folder with all the attachments, but I could not get the code to add the rasters as attachments to work, but I was able to manually use the geoprocessing tool "Generate Attachment Table" like this:
Syvertson_0-1753973981676.png

and then the "Add Attachments" tool, like this:

Syvertson_1-1753974041677.png

It all worked!  Thanks.

0 Kudos