Select to view content in your preferred language

Can't add an image to a mediaLayer

160
0
3 weeks ago
Labels (2)
AurelienRobert
Occasional Contributor

Hi everyone,

I have created a web map and then uploaded a jpg file as a resource attached to that web map, using a POST request:

add_resource_url = f"https://www.arcgis.com/sharing/rest/content/users/{username}/items/{webmap_item_id}/addResources"
            
            # Get the actual JPG filename
            jpg_filename = os.path.basename(jpg_path)
            
            with open(jpg_path, 'rb') as jpg_file:
                files = {'file': jpg_file}
                resource_params = {
                    'f': 'json',
                    'token': access_token,
                    'fileName': jpg_filename  # Use the actual JPG filename
                }
                resource_response = requests.post(add_resource_url, data=resource_params, files=files)


Adding JPG as resource to web map...
Resource result: {'success': True, 'itemId': '439d96fcfcaf4cfc821b11078c7688f5', 'owner': 'mnnbl02', 'folder': 'null'} 

1st issue is that, despite 'success' always returning True, the jpg doesn't always (inexplicably inconsistent) seem to have attached to the map, judging by the file size of the map, which shows 8ko in most cases and sometimes shows 142ko (indicating the jpg has likely attached).

When the jpg seems to have attached, I try to add this resource to a mediaLayer:

media_layer = {
        "id": jpg_title,  # Use jpg_title as the layer ID
        "layerType": "MediaLayer",
        "mediaType": "image",
        "url": f"./media/{os.path.basename(jpg_path)}",  # Relative path to the JPG resource
        "visibility": True,
        "opacity": 1,
        "title": "Property data",
        "blendMode": "normal",
        "minScale": 0,
        "maxScale": 0,
        "georeference": {
            "coefficients": [],
            "controlPoints": [
                {"x": 0, "y": 90},
                {"x": 0, "y": 0},
                {"x": 250, "y": 0},
                {"x": 250, "y": 90}
            ],
            "height": 100,
            "spatialReference": {
                "wkid": 102100
            },
            "width": 259
        }
    }

I've tried both with the relative url starting with ./media and ./resources/media.
2nd issue is that the jpg file seems not to have really attached, as when I look at the map details in my Arcgis Location Platform account and click on the media layer, the image tries to load but doesn't appear and neither does it on the map (the control points are visible when opening the side-by-side view after having clicked the Edit placed item button in the layer properties in the Map Viewer).

How can I properly add the jpg as a resource to the map and then to the media layer?

Mucn appreciated,
Aurel

0 Kudos
0 Replies