# Name: CreateMapServerCache.py
# Description: The following stand-alone script demonstrates how to create map
# using standard tiling schema for weblayers published to ArcGIS Enterprise and ArcGIS Online.
# and for map and image services on a stand alone ArcGIS Server
# Requirements: os, sys, time & traceback modules
# Any line that begins with a pound sign is a comment and will not be executed
# Empty quotes take the default value.
# To accept arguments from the command line replace values of variables to
# "sys.argv[]"
# Import system modules
import arcpy
from arcpy import env
import os, sys, time, datetime, traceback, string
import traceback, sys
# Set environment settings
env.workspace = "C:/data"
# Sign in to portal
myPortal= "https://MyPortal.domain.com/portalwebadaptor"
arcpy.SignInToPortal(myPortal, "siteadminValue", "siteadminPassword")
myServer="https://Myserver.domain.com/serverwebadaptor"
serviceName= "SampleWorldCities"
serviceType= "MapServer"
myPortalServiceURL = (myServer + "/" + "rest/services" +"/" + serviceName + "/"
+ serviceType)
print (myPortalServiceURL)
# Stand alone ArcGIS Server
##target_server_connection = r"C:\share\python3\arcgis on MyServer"
##serviceName= "States"
##serviceType= "MapServer"
##myServerServiceURL= target_server_connection + "/" + serviceName + "." + serviceType
serviceCacheDirectory = "C:\\arcgisserver\\arcgiscache\\"
tilingSchemeType = "NEW"
scalesType = "STANDARD"
numOfScales = "4"
scales = ""
dotsPerInch = "96"
tileOrigin = ""
scales = ""
tileSize = "256 x 256"
cacheTileFormat = "PNG32"
tileCompressionQuality = ""
storageFormat = "COMPACT"
predefinedTilingScheme = ""
ready_to_serve_format="READY_TO_SERVE_FORMAT"
#variables for reporting
currentTime = datetime.datetime.now()
arg1 = currentTime.strftime("%H-%M")
arg2 = currentTime.strftime("%Y-%m-%d %H:%M")
file = 'E:/data/report_%s.txt' % arg1
# print results of the script to a report
report = open(file,'w')
try:
## starttime = time.clock()
result = arcpy.server.CreateMapServerCache(myPortalServiceURL,
serviceCacheDirectory,
tilingSchemeType, scalesType,
numOfScales, dotsPerInch,
tileSize, predefinedTilingScheme,
tileOrigin, scales,
cacheTileFormat,
tileCompressionQuality,
storageFormat,
ready_to_serve_format)
## finishtime = time.clock()
## elapsedtime = finishtime - starttime
# print messages to a file
while result.status < 4:
time.sleep(0.2)
resultValue = result.getMessages()
report.write ("completed " + str(resultValue))
## print("Created cache schema with 4 scales & default properties for" + \
## myServerServiceURL + " in " + str(elapsedtime) + " sec \n on " + arg2)
print("Created cache schema with 4 scales & default properties for" + \
myServerServiceURL + " in " + arg2)
except Exception as e:
# If an error occurred, print line number and error message
tb = sys.exc_info()[2]
report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)
#report.write(e.message)
report.write(str(e))
print("Executed creation of Map server Cache schema ")
report.close()