I have a tiff layer in an ArcGIS Pro project. I would like to publish it on my ArcGIS Online portal, with Arcpy. I try to publish it as a Web Tile Layer, but could be another layer type, if anyone has another suggestion.
Previously I published it manually from ArGIS Pro, as TPK. Then I updated the TPK and the Tile Layer from my python script. However now, I would like to directly create the Web Tile Layer with my script - that is the layer does not yet exist on the portal.
First I created an ArcGIS Pro project. I add the tiff layer to the project. This tiff layer will change each year and the script will publish this new layer as a new web layer on the portal.
from arcgis import features
from arcgis import mapping
import arcpy
import sys
# Connection to AGOL
gis = agol_connect()
dir = 'K:/WEB-SIG/PROD/URBAINE/'
agglo = 'LILLE'
year = '2018'
pol='PM25'
metrique='moy'
project_name ="TEST_%s_%s" % (agglo,year)
project_path = dir + r"ARCGIS_projects\%s\%s.aprx" % (project_name,project_name)
map_name = agglo+'_'+ pol +'_'+ metrique
# Parameters for Arcgis Pro
arcpy.env.overwriteOutput = True
arcpy.env.workspace = dir + "ARCGIS_projects"
# Connection to the project in ArcGIS pro
project = arcpy.mp.ArcGISProject(project_path)
map = project.listMaps(map_name)[0]
layer = map.listLayers()[0] #this is the tiff layer
Now I tried to publish it but I didn’t succeed ..
First attempt : Using the WebMap function of mapping module. But it does not work with tile Layer.
wm = mapping.WebMap()
wm.add_layer(layer)
error message with WebMap() : uncorrect layer type
Second attempt : I tried to create a TileSharingDraft and to publish it. I got the following error.
# Create TileSharingDraft and set service properties
service = "LILLE_2018_PM25_moy"
sharing_draft = map.getWebLayerSharingDraft("HOSTING_SERVER", "TILE", "TileDraftSharing", layer)
sharing_draft.summary = service
sharing_draft.tags = "modele"
sharing_draft.description = "Modelisation urbaine"
sharing_draft.credits = "Atmo Hauts-de-France"
# Create Service Definition Draft file
draft_output = dir + "WebLayer/" + service + ".ssdraft"
sharing_draft.exportToSDDraft(draft_output)
# Stage Service
sd_filename = service + ".sd"
sd_output = dir + "WebLayer/" + sd_filename
arcpy.StageService_server(draft_output, sd_output)
# Share to portal
print("Uploading Service Definition...")
arcpy.UploadServiceDefinition_server(sd_output, "My Hosted Services")
error message when trying to export to sddraft However this way seems a bit complicated for the simple publication I want to do. I supposed that there is a more direct way to do it, as for TPK, a bit like this :
#with the arcGIS Pro Map
arcpy.CreateMapTileLayer_management(map,layer_name_online)
#or with the layer
arcpy.CreateMapTileLayer_management(layer, layer_name_online)
Does anyone have an idea?
1 Answer 1
I finally found a solution : the second attempt was a good attempt, but as @KHibma mentionned, I crossed arcpy and arcgis-python-api.
I tried to connect to AGOL with argis.gis.GIS while I should connect with arcpy. As the error message were not saying anything about this, it was quite difficult to understand !
Here is my code if it can help ! To summarize : 1) create SDDraft from tiff in ArcGIS Pro project 2) Create SD file from the draft 3) Publish Tile Layer from Sd file
from arcgis.gis import GIS
from atmo.connect import *
from shutil import copyfile
import arcpy
import os
######################
#connexion au portail
arcpy.SignInToPortal(url_agol,id_agol,mdp_agol)
# Set output file names
annee = '2018'
ville = 'LILLE'
outdir = dir + '/%s/%s' %(ville,annee)
print(outdir)
print('Publication des données pour ' + ville + ' et l\'année ' + annee)
service = ville + "_"+annee+"_PM25_moy"
print('Publication des données de ' + service)
sddraft_file = outdir + '\\' + service + '.sddraft'
##### ETAPE 1 - Création du fichier SDDraft depuis un projet ArcGIS Pro
# Reference map to publish
aprx = arcpy.mp.ArcGISProject(dir + r"/ARCGIS_projects/" + ville + '/' + ville + ".aprx")
map = aprx.listMaps("LILLE_PM25_moy")[0]
# Create Tile Layer SharingDraft from map in ArcGisPro project
sharing_draft = map.getWebLayerSharingDraft("HOSTING_SERVER", "TILE", service)
print('Le brouillon de la WebLayer est créé.')
# Create Service Definition Draft file
sharing_draft.exportToSDDraft(sddraft_file)
print('Le fichier du brouillon du service de Définition est créé')
##### ETAPE 2 - Création du fichier SD (Service Definition) depuis le fichier SDDraft
# Create Service definition file
sd_file = outdir + '\\' + service + ".sd"
arcpy.StageService_server(sddraft_file, sd_file)
print('Le fichier de Service définition est créé')
##### ETAPE 3 - Publication de la couche de tuiles depuis le fichier SD
# Share to portal as Tile Layer
print("Uploading Service Definition...")
arcpy.UploadServiceDefinition_server(sd_file, "My Hosted Services") #connexion à AGOL
print("Successfully Uploaded service.")
Explore related questions
See similar questions with these tags.
wm.add_layer(layer)
is where you're trying to pass an arcpy thing into an arcgis-python-api thing. It wont work. So I'd go back and be sure of your process, and probably remove this bit from your attempt and re-format your question/error.