Hi all,
I was wondering how modify service defition draft for cache level before uploading to server.
Following sample 6 of help page (CreateMapSDDraft—Help | ArcGIS Desktop) I was able to insert some further steps from the original code:
- turn on cache
- set "create tile on demand"
- set minimum and maximum scale for cache
Here is my code
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env
<SPAN class="keyword token">import</SPAN> xml<SPAN class="punctuation token">.</SPAN>dom<SPAN class="punctuation token">.</SPAN>minidom <SPAN class="keyword token">as</SPAN> DOM
<SPAN class="keyword token">import</SPAN> os<SPAN class="punctuation token">,</SPAN> sys<SPAN class="punctuation token">,</SPAN> time<SPAN class="punctuation token">,</SPAN> datetime<SPAN class="punctuation token">,</SPAN> traceback<SPAN class="punctuation token">,</SPAN> string
pathFile <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
server <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
serviceFolder <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
serviceName <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN>
summary <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN>
tags <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># define local variables</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
wrkspc <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>scratchFolder
connectionFolder <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Users\<User>\AppData\Roaming\ESRI\Desktop10.5\ArcCatalog"</SPAN>
mapServer <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>connectionFolder<SPAN class="punctuation token">,</SPAN> server<SPAN class="punctuation token">,</SPAN> serviceName <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.MapServer'</SPAN><SPAN class="punctuation token">)</SPAN>
connectionFile <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>connectionFolder<SPAN class="punctuation token">,</SPAN> server <SPAN class="operator token">+</SPAN><SPAN class="string token">'.ags'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># create service definition draft</SPAN>
copy_data_to_server <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
sddraft <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">,</SPAN> serviceName <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.sddraft'</SPAN><SPAN class="punctuation token">)</SPAN>
sd <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">,</SPAN> serviceName <SPAN class="operator token">+</SPAN> <SPAN class="string token">'.sd'</SPAN><SPAN class="punctuation token">)</SPAN>
analysis <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>CreateMapSDDraft<SPAN class="punctuation token">(</SPAN>mapDoc<SPAN class="punctuation token">,</SPAN> sddraft<SPAN class="punctuation token">,</SPAN> serviceName<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ARCGIS_SERVER'</SPAN><SPAN class="punctuation token">,</SPAN> connectionFile<SPAN class="punctuation token">,</SPAN> copy_data_to_server<SPAN class="punctuation token">,</SPAN> serviceFolder<SPAN class="punctuation token">,</SPAN> summary<SPAN class="punctuation token">,</SPAN> tags<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># The Server Object Extension (SOE) to disable.</SPAN>
soe <SPAN class="operator token">=</SPAN> <SPAN class="string token">'KmlServer'</SPAN>
<SPAN class="comment token"># Read the sddraft xml.</SPAN>
doc <SPAN class="operator token">=</SPAN> DOM<SPAN class="punctuation token">.</SPAN>parse<SPAN class="punctuation token">(</SPAN>sddraft<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Find all elements named TypeName. This is where the server object extension (SOE) names are defined.</SPAN>
typeNames <SPAN class="operator token">=</SPAN> doc<SPAN class="punctuation token">.</SPAN>getElementsByTagName<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'TypeName'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> typeName <SPAN class="keyword token">in</SPAN> typeNames<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Get the TypeName we want to disable.</SPAN>
<SPAN class="keyword token">if</SPAN> typeName<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">==</SPAN> soe<SPAN class="punctuation token">:</SPAN>
extension <SPAN class="operator token">=</SPAN> typeName<SPAN class="punctuation token">.</SPAN>parentNode
<SPAN class="keyword token">for</SPAN> extElement <SPAN class="keyword token">in</SPAN> extension<SPAN class="punctuation token">.</SPAN>childNodes<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Disabled SOE.</SPAN>
<SPAN class="keyword token">if</SPAN> extElement<SPAN class="punctuation token">.</SPAN>tagName <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Enabled'</SPAN><SPAN class="punctuation token">:</SPAN>
extElement<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">=</SPAN> <SPAN class="string token">'false'</SPAN>
<SPAN class="comment token"># turn on caching in the configuration properties</SPAN>
configProps <SPAN class="operator token">=</SPAN> doc<SPAN class="punctuation token">.</SPAN>getElementsByTagName<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'ConfigurationProperties'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
propArray <SPAN class="operator token">=</SPAN> configProps<SPAN class="punctuation token">.</SPAN>firstChild
propSets <SPAN class="operator token">=</SPAN> propArray<SPAN class="punctuation token">.</SPAN>childNodes
<SPAN class="keyword token">for</SPAN> propSet <SPAN class="keyword token">in</SPAN> propSets<SPAN class="punctuation token">:</SPAN>
keyValues <SPAN class="operator token">=</SPAN> propSet<SPAN class="punctuation token">.</SPAN>childNodes
<SPAN class="keyword token">for</SPAN> keyValue <SPAN class="keyword token">in</SPAN> keyValues<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>tagName <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Key'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">==</SPAN> <SPAN class="string token">"isCached"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># turn on caching</SPAN>
keyValue<SPAN class="punctuation token">.</SPAN>nextSibling<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">=</SPAN> <SPAN class="string token">"true"</SPAN>
<SPAN class="keyword token">for</SPAN> keyValue <SPAN class="keyword token">in</SPAN> keyValues<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>tagName <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Key'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">==</SPAN> <SPAN class="string token">"cacheOnDemand"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># turn on caching on demand</SPAN>
keyValue<SPAN class="punctuation token">.</SPAN>nextSibling<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">=</SPAN> <SPAN class="string token">"true"</SPAN>
<SPAN class="keyword token">for</SPAN> keyValue <SPAN class="keyword token">in</SPAN> keyValues<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>tagName <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Key'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">==</SPAN> <SPAN class="string token">"minScale"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># set min scale</SPAN>
keyValue<SPAN class="punctuation token">.</SPAN>nextSibling<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">=</SPAN> <SPAN class="string token">"591657527,591555"</SPAN>
<SPAN class="keyword token">for</SPAN> keyValue <SPAN class="keyword token">in</SPAN> keyValues<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>tagName <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Key'</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> keyValue<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">==</SPAN> <SPAN class="string token">"maxScale"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># set max scale</SPAN>
keyValue<SPAN class="punctuation token">.</SPAN>nextSibling<SPAN class="punctuation token">.</SPAN>firstChild<SPAN class="punctuation token">.</SPAN>data <SPAN class="operator token">=</SPAN> <SPAN class="string token">"1128,497176"</SPAN>
<SPAN class="comment token"># Output to a new sddraft.</SPAN>
outXml <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>wrkspc<SPAN class="punctuation token">,</SPAN> serviceName <SPAN class="operator token">+</SPAN> <SPAN class="string token">'Update.sddraft'</SPAN><SPAN class="punctuation token">)</SPAN>
f <SPAN class="operator token">=</SPAN> open<SPAN class="punctuation token">(</SPAN>outXml<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'w'</SPAN><SPAN class="punctuation token">)</SPAN>
doc<SPAN class="punctuation token">.</SPAN>writexml<SPAN class="punctuation token">(</SPAN> f <SPAN class="punctuation token">)</SPAN>
f<SPAN class="punctuation token">.</SPAN>close<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
sddraft <SPAN class="operator token">=</SPAN> outXml
<SPAN class="comment token"># stage and upload the service if the sddraft analysis did not contain errors</SPAN>
<SPAN class="keyword token">if</SPAN> analysis<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'errors'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Execute StageService</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>StageService_server<SPAN class="punctuation token">(</SPAN>sddraft<SPAN class="punctuation token">,</SPAN> sd<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Execute UploadServiceDefinition</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>UploadServiceDefinition_server<SPAN class="punctuation token">(</SPAN>sd<SPAN class="punctuation token">,</SPAN> connectionFile<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># if the sddraft analysis contained errors, display them</SPAN>
<SPAN class="keyword token">print</SPAN> analysis<SPAN class="punctuation token">[</SPAN><SPAN class="string token">'errors'</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>But this is the result

Anyone can help me?
Stefano