Retrieve Attachments From Features in a Vector

Learn how to access attachments from existing features and make use of the data within custom analytics.

1. Description

This article explains how to retrieve attachments from features in a vector, especially in order to use the attachments in custom analytics.

Examples of attachments that can be retrieved:

  • Images (ex: .jpg) attached to a feature with the Infield Mobile App
  • Images (ex: .jpg), files (ex: .pdf) attached to a feature directly in the platform in the interface

A feature here corresponds to a polygon, line, or point of a vector file.  Example: one microplot of a microplots file.

2. Workflow

Step 1 - Connect to the SDK:

import alteia
sdk = alteia.SDK(config_path=MY_CONFIG_PATH)

Step 2 - Initialize variables:

PROJECT_ID = 'MY_PROJECT_ID'
PATH_TO_STORE_ATTACHMENTS = 'MY_PATH'
VECTOR_NAME = 'NAME_OF_MY_VECTOR'

Find the collection of the features to retrieve attachments from features. The collection is linked to the map-service vector.

Step 3 - Search the vector map-service:

vector = sdk.datasets.search(
   filter={
       'project': {'$eq': PROJECT_ID},
       'source.name': {'$eq': 'map-service'},
       'name': {'$eq': VECTOR_NAME}
       }
)[0] # return a list so take only the first element

Step 4 - Extract the collection ID and then the features IDs to iterate on them to extract attachments:

# Extract collection ID
collection_id = vector.components[0]['collection']['id']

# Describe collection to find features
collection = sdk.collections.describe(collection_id) 
# Return a list of features ID
features = collection.features 

# Iterate on features
for feat in features:
   f = sdk.features.describe(feat)
   if hasattr(f, 'attachments'): # Keep only features with attachments
       attachments_ids = f.attachments # returns a list of attachments ID
       for a in attachments_ids:
           d = sdk.datasets.describe(a) # attachment is considered as a dataset
           sdk.datasets.download_component(
               d._id,
               component=d.components[0]['name'],
               target_path=PATH_TO_STORE_ATTACHMENTS,
               target_name=d.components[0]['filename'],
               overwrite=True) # false by default but the download fail if there is the same attachment name

3. Results

Once retrieved, the attachments can be used by your code in custom analytics for example. 


Knowledge Base Software powered by Helpjuice