Instrução
É possível substituir um serviço da feição hospedado usando arcgis.gis.module como uma solução alternativa. O módulo é usado para superar a limitação conhecida do módulo ArcPy, com credenciais do ArcGIS Online em um script de Python. O script de Python permite que o processo seja automatizado. No entanto, o Python 3, que está incluído em uma instalação do ArcGIS Pro, é necessário.
Este artigo fornece instruções para especificar os módulos e credenciais, criar o arquivo da definição de serviço (SD) e publicar.
Nota: Este procedimento funciona apenas em serviços da feição hospedados com arquivos da definição de serviço existentes.
import arcpy import os, sys from arcgis.gis import GIS
prjPath = r"C:\Users\SampleFolder\Project_Name.aprx"
sd_fs_name = "hosted feature service name" portal = "http://www.arcgis.com" user = "USERNAME" password = "PASSWORD"
shrOrg = True shrEveryone = False shrGroups = ""
relPath = r'C:\Users\SampleFolder_1' sddraft = os.path.join(relPath, "temporary service name.sddraft") sd = os.path.join(relPath, "temporary service name.sd")
Nota: Na seção sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name), mantenha o parâmetro "HOSTING_SERVER" e "FEATURE" como o parâmetro padrão. Edite apenas o parâmetro definido, sd_fs_name, com o mesmo nome de variável definido pelo usuário.
print("Creating SD file") arcpy.env.overwriteOutput = True prj = arcpy.mp.ArcGISProject(prjPath) mp = prj.listMaps()[0] #define um novo objeto para utilizar a função getWebLayerSharingDraft com HOSTING_SERVER and FEATURE como os parâmetros padrão sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name) sharing_draft.summary = "My Summary" sharing_draft.tags = "My Tags" sharing_draft.description = "My Description" sharing_draft.credits = "My Credits" sharing_draft.useLimitations = "My Use Limitations" sharing_draft.exportToSDDraft("temporary service name.sddraft") arcpy.StageService_server(sddraft, sd)
print("Connecting to {}".format(portal)) gis = GIS(portal, user, password)
print("Search for original SD on portal…") print(f"Query: {sd_fs_name}") sdItem = gis.content.search(query=sd_fs_name, item_type="Service Definition") i = 0 while sdItem[i].title != sd_fs_name: i += 1 print('"Item Found") print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}') item = sdItem[i] item.update(data=sd) print("Overwriting existing feature service…") fs = item.publish(overwrite=True) if shrOrg or shrEveryone or shrGroups: print("Setting sharing options…") fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups) print("Finished updating: {} – ID: {}".format(fs.title, fs.id))
O seguinte mostra o script completo:
import arcpy import os, sys from arcgis.gis import GIS prjPath = r"C:\Users\SampleFolder\Project_Name.aprx" sd_fs_name = "hosted feature service name" portal = "http://www.arcgis.com" # Can also reference a local portal user = "username" password = "password" shrOrg = True shrEveryone = False shrGroups = "" relPath = r'C:\Users\SampleFolder' sddraft = os.path.join(relPath, "temporary service name.sddraft") sd = os.path.join(relPath, "temporary service name.sd") print("Creating SD file") arcpy.env.overwriteOutput = True prj = arcpy.mp.ArcGISProject(prjPath) mp = prj.listMaps()[0] sharing_draft = mp.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", sd_fs_name) sharing_draft.summary = "My Summary" sharing_draft.tags = "My Tags" sharing_draft.description = "My Description" sharing_draft.credits = "My Credits" sharing_draft.useLimitations = "My Use Limitations" sharing_draft.exportToSDDraft("temporary service name.sddraft") arcpy.StageService_server(sddraft, sd) print("Connecting to {}".format(portal)) gis = GIS(portal, user, password) # Find the SD, update it, publish /w overwrite and set sharing and metadata print("Search for original SD on portal…") print(f"Query: {sd_fs_name}") sdItem = gis.content.search(query=sd_fs_name, item_type="Service Definition") i=0 while sdItem[i].title != sd_fs_name: i += 1 print('Item Found') print(f'item[i].title = {sdItem[i].title}, sd_fs_name = {sd_fs_name}') item = sdItem[i] item.update(data=sd) print("Overwriting existing feature service…") fs = item.publish(overwrite=True) if shrOrg or shrEveryone or shrGroups: print("Setting sharing options…") fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups) print("Finished updating: {} – ID: {}".format(fs.title, fs.id))
ID do Artigo:000023164
Obtenha ajuda de especialistas do ArcGIS
Baixe o Esri Support App