Select to view content in your preferred language

ArcGIS Pro Python Notebook - Download Images From Feature Service Layer & Related Tables

289
0
01-10-2024 01:13 PM
Labels (3)
JohnVan_Hoesen1
New Contributor

I am running ArcGIS Pro 3.1 and have a working Python notebook that bulk downloads images from a Survey 123 feature layer hosted in an Enterprise Portal. The Survey includes repeats and I want the bulk download to include images associated with these repeats. I have tried everything:

  • ListRelatedTables and GetRelatedTables have thrown a number of errors that those modules don't exist
  • My current, closest to success result is identifying a relationship class, but the name is not one I can make sense of it and it too is throwing error messages. 

For more context, the feature layer is named WHEG_Old_Field, the related table is displayed as oldfield_repeat_f1:
notebook_table_name.JPG

When I look at the table added from S123 I see it named as:
notebook_table_name_s123.JPG

I have almost had success with the following, but I'm still getting an error and the output name doesn't make sense to me or where the extra characters are coming from. 

# Define Download Attachments From Related Table
def download_attachments_from_related_table(related_table_name, global_id, local_directory):
    rel_attach_table = f"{related_table_name}__ATTACH"
    rel_pic_fields = ["ATTACHMENTID", "GLOBALID", "ATT_NAME", "DATA"]

    with arcpy.da.SearchCursor(rel_attach_table, rel_pic_fields, f"GLOBALID = '{global_id}'") as cursor:
        for row in cursor:
            pic_path = os.path.join(local_directory, f"{str(row[0])} {str(row[2])}")
            pic_blob = row[3]
            open(pic_path, 'wb').write(pic_blob.tobytes())
            if exportCSV:
                if not global_id in picDict:
                    picDict[global_id] = [pic_path]
                else:
                    picDict[global_id].append(pic_path)

# Get Related Tables From Selected Feature Layer
def get_related_tables(feature_layer):
    related_tables = []
    for relClass in arcpy.Describe(feature_layer).relationshipClassNames:
        print(f"Found relationship class: {relClass}")  # Print for debugging
        try:
            relClassObj = arcpy.Describe(relClass)
            if relClassObj.isAttachmentRelationship:
                continue  # Skip attachment relationships
            if feature_layer in [relClassObj.originClassNames, relClassObj.destinationClassNames]:
                related_table = relClassObj.originClassNames if feature_layer != relClassObj.originClassNames else relClassObj.destinationClassNames
                related_tables.extend(related_table)
        except Exception as e:
            print(f"Error describing {relClass}: {e}")
    return related_tables

 

This is the output from my debugging, it make no sense. It found a relationship class but it doesn't exist?!

notebook_table_errors.JPG

I can post the whole code if that will help debug, but I'm mostly curious if anyone has any success downloaded image from a primary feature layer AND from all related tables? Any suggestions or examples to share? I have looked extensively and haven't had any luck.

Thanks in advance!

0 Kudos
0 Replies