Hi, I need to know the scale of an mxd in a web application. For that I am running a geoprocessing service. I need to know the scale when there is a definition query and the map zooms in to that feature. I can see in the message that the scale changes, but the output in the web application is always the same scale, although the input feature changes. What am I doing wrong? Thank you for any help.
import arcpy
import os
import uuid
# Input Layout template
Layout_Template = arcpy.GetParameterAsText(0)
if Layout_Template == '#' or not Layout_Template:
Layout_Template = "StewardshipTractBoundaryMap"
layouttemplatemxd = Layout_Template
templatePath = 'D:/ArcGISData/Geoprocessing_Examples/Advanced_HighQualityPrinting3'
Selecting_Features = arcpy.GetParameterAsText(1)
Input_Polygons = "Stewardship"
mxd0 = arcpy.mapping.MapDocument(r"D:\ArcGISData\Geoprocessing_Examples\Advanced_HighQualityPrinting3\\" + layouttemplatemxd + ".mxd")
df1 = arcpy.mapping.ListDataFrames(mxd0, "DataFrame1")[0]
mxd0.activeView = df1
lyr1 = arcpy.mapping.ListLayers(mxd0, Input_Polygons, df1)[0]
lyr1.definitionQuery = ""
lyr1.definitionQuery = '"PlanID" = \'%s\'' % Selecting_Features
df1.panToExtent(lyr1.getSelectedExtent())
lyr1.getExtent()
lyr1extent = lyr1.getExtent()
df1.extent = lyr1extent
Scale = str(df1.scale)
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd0.save()
arcpy.SetParameterAsText(2, Scale)
lyr1.definitionQuery = ""
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
arcpy.AddMessage(Scale)
mxd0.save()
del mxd0, df1, lyr1
I like sys.argv although others prefer getparameterastext
sys.argv[0] is always the script name
sys.argv[1] ... sys.argv
I am sure get...astext works the same way, but it starts at 0 since it doesn't know the script name.
If the tool works as a regular tool before publishing, but not as a service, your parameter index are probably correct.
My guess is when publishing it hasn't updated, or incorrectly updated the paths to either the templates, or the selected features you're passing in isn't working correctly.
The first and easiest test is to consume the service back in ArcMap - ArcMap will always pass the features correctly. If you get the correct result there, but not the webapp, its something to do with how the service + web application is setup.
If that isn't the problem, I'd try changing your first bit of code
import os
layouttemplatemxd = arcpy.GetParameterAsText(0) if arcpy.GetParameterAsText(0) else "StewardshipTractBoundaryMap" #I think this should work
Layout_Template = arcpy.GetParameterAsText(0)
if Layout_Template == '#' or not Layout_Template:
Layout_Template = "StewardshipTractBoundaryMap"
layouttemplatemxd = Layout_Template
templatePath = 'D:/ArcGISData/Geoprocessing_Examples/Advanced_HighQualityPrinting3'
Selecting_Features = arcpy.GetParameterAsText(1)
Input_Polygons = "Stewardship"
mxd0 = arcpy.mapping.MapDocument(os.path.join(templatePath, layouttemplatemxd + '.mxd')
And if none of that does it, it might be how you're doing the select itself. It looks a little strange to me, setting the definition query then doing a select. Is that needed?
Thank you Kevin. The gp gives the correct result in ArcMap so I am guessing is something in the web app. However, when I debug the web app it shows the selected features varies as the input but the output is always the same. I need to investigate more why this is happening in the web app. I will post the code here later. Thanks again.