Select to view content in your preferred language

Updating metadata for image services

561
3
02-15-2023 07:18 PM
Labels (1)
Heidi_Kristenson
Occasional Contributor

When we publish image services programmatically, only a few of the metadata fields can be populated during the creation of a service definition. We would like to have a programmatic approach for updating the rest of the metadata fields (it appears that only description and tags can be populated in the CreateImageSDDraft command). 

I tried using the arcpy metadata module, but it seems to treat the item as read-only. I am able to manually edit the metadata fields in the Server Manager in the Item Description section of the service, and also to edit it in ArcGIS Pro via an ags connection in the Catalog pane without any problems. Is this just not supported in the metadata module, or am I doing something wrong? 

I tried a few different paths, and both of the URL-based options returned the expected service description (I added a few test print statements before the conditional). The path using the ags returned 'None', so that option may not have been hitting the right target. I thought this might be the best option, as the ags file contains credentials, but that doesn't help if it doesn't find the right target.

I tried setting the service to allow "Edit" functionality, but I expect that's not really referencing the metadata. Is there another setting somewhere that needs to be adjusted? 

I suspect this might have to do with credentials. I tried running the script directly from the server, and got the same result, but maybe even there I'd need to provide some sort of credential. Any ideas where I'd add that in, or if that's truly the issue? 

import arcpy
from arcpy import metadata as md

# Create a new Metadata object and add some content to it
new_md = md.Metadata()
new_md.title = 'My new title'
new_md.tags = 'tag1, tag2'
new_md.summary = 'summary language'
new_md.description = 'description language'
new_md.credits = 'credit statement'
new_md.accessConstraints = 'under construction'

# Assign the Metadata object's content to a target item
# hand_path = r'C:\Users\__\Documents\ImageServer\ImageServices\arcgis on gis-test.xxx.xxx.ags\Folder\ServiceName' # this path doesn't return expected description
# hand_path = r'https://gis-test.xxx.xxx/arcgis/rest/services/Folder/ServiceName/ImageServer/info/metadata' # this path returns expected description
hand_path = r'https://gis-test.xxx.xxx/arcgis/rest/services/Folder/ServiceName/ImageServer' # this path also returns expected description

tgt_item_md = md.Metadata(hand_path)
print('is read only: %s' % tgt_item_md.isReadOnly) # verify that the target is, indeed, read only
print(tgt_item_md.description) # verify that it's hitting the right target

if not tgt_item_md.isReadOnly:
tgt_item_md.copy(new_md)
print('copied new metadata')
tgt_item_md.save()
print('new metadata saved')
else:
print('item is read only')

This is what I see as output when running the script:

is read only: True
Expected existing description for the image service
item is read only

Process finished with exit code 0

0 Kudos
3 Replies
Heidi_Kristenson
Occasional Contributor

I looked into the credentials issue a bit more, and tried this approach prior to running the script, but got the same result: 

from arcgis.gis.server import Server
server_base_url = "https://gis-test.xxx.xxx/"
server = Server(
url="{}:6443/arcgis/admin".format(server_base_url),
tokenurl="{}:6443/arcgis/rest/generateToken".format(server_base_url),
username="<username>",
password="<password>")
0 Kudos
Heidi_Kristenson
Occasional Contributor

I wasn't convinced by the port number, and ended up amending the token script as follows: 

server_base_url = "https://gis-test.asf.alaska.edu/"
server = Server(
url="{}arcgis/admin".format(server_base_url),
tokenurl="{}arcgis/rest/generateToken".format(server_base_url),

It now actually hits the place on our server where one could request a token, but I still get the same Read Only result.

0 Kudos
Heidi_Kristenson
Occasional Contributor

In the end, we just edit the xml of the draft service definition to add in the content we want before staging the final service definition. 

It's a pretty powerful back door, if you get the xml elements right. This allowed us to add metadata, enable WMS for image services, and a number of other functions that aren't included in the programmatic publishing tools. 

0 Kudos