Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Code review please - script never ends

$
0
0
Not sure how else to say it, but after running the code below, the processing window just flashes continually and the script doesn't ever appear to end. I have to kill ArcMap process to get out of the script.

I'm posting my entire code snippet below, hopefully someone can point out my error.

Warning, new Python coder here, please don't laugh "too hard' at my brute force coding attempts :p

Code:

# Jon Pedder
# MapSAR - 12/15/12
# Make Briefing Map

import arcpy, os, glob

# Set enviroment
# from arcpy import env


# Gather input parameters from user
# 0. TargetFile mxd - string
# 1. Folder to store pdf product - string
# 2. Title of the Map - string
# 3. Name of saved map - string
# 4. Map Scale - string
# 5. Center map on - list, string
# 6. Use base data in current map - boolean

TargetFile = arcpy.GetParameterAsText(0)
PDFlocation = arcpy.GetParameterAsText(1)
aMapTitle = arcpy.GetParameterAsText(2)
aMapName = arcpy.GetParameterAsText(3)
aMapScale = arcpy.GetParameterAsText(4)
aCenterMapOn = arcpy.GetParameterAsText(4)
aBase_Data = arcpy.GetParameterAsText(6)

# Set Vars and overwrite option to true
arcpy.env.overwriteOutput = True
Targetmxd = None
mxd = None

arcpy.AddMessage("Base data = " + aBase_Data)

 ########################################
 ###### BASE DATA COPY STARTS HERE      #
 ########################################

# Gather information to copy over base_data

if aBase_Data == "true":
 
  # Define vars

  mxdlayer = "14 Base_Data_Group"
  LayerFile = "C:\MapSAR\TempDir\Base_Layer"
  LayerName = LayerFile + '.lyr'


  # Save base_data layer file to disk in c:\MapSAR\TempDir from current mxd.
  # If directory does not exist create it
  if os.path.exists('c:\MapSAR\TempDir'):   
          arcpy.SaveToLayerFile_management(mxdlayer,LayerFile,"RELATIVE")   
  else:
          os.makedirs('c:\MapSAR\TempDir')
          arcpy.SaveToLayerFile_management(mxdlayer,LayerFile,"RELATIVE")

  # Message to user       
  arcpy.AddMessage("Base Data Saved as "+ LayerFile)

  Targetmxd = arcpy.mapping.MapDocument(TargetFile)

  # Check for existing Base_Data layers in target. If present remove them
  for df in arcpy.mapping.ListDataFrames(Targetmxd):
        for lyr in arcpy.mapping.ListLayers(Targetmxd, "", df):
            if 'Base_Data' in lyr.name:
                  arcpy.mapping.RemoveLayer(df,lyr)
                 
                  # Message to user that layers have been removed from target
                  arcpy.AddMessage('removing '+ str(lyr) + ' from '+TargetFile)

  # Message to user
  arcpy.AddMessage('Loading Base_Data '+ LayerName +' from disk')

  # Check if the layer exists on disk
  if os.path.isfile(LayerName):
          addLayer = arcpy.mapping.Layer(LayerName)
          arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
          Targetmxd.save()
  else:
          # If not alert user of an error
          arcpy.AddMessage(LayerName +' does not exist')
       

########################################
###### BASE DATA COPY ENDS HERE        #
########################################

########################################
###### EXPORT MAP STARTS HERE          #
########################################


MapTitle = aMapTitle
MapName = aMapTitle + ".pdf"
MapLocation = PDFlocation + "/" + aMapName + ".pdf"

arcpy.AddMessage("Generating Briefing Map " + MapName)
mxd = arcpy.mapping.MapDocument(TargetFile)

for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT","MapTitle"):
        elm.text = "<BOL> " + MapTitle + "</BOL>"

for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT","MapName"):
        elm.text = MapName

fc = "PLS_Subject_Information"
# iQuery = "Name =" + aCenterMapOn
iQuery = ' "Name" = \'Jon Pedder\' '

df = arcpy.mapping.ListDataFrames(mxd, "MapSAR")[0]
lyr = arcpy.mapping.ListLayers(mxd, "PLS_Subject_Information", df)[0]

arcpy.AddMessage("Panning to center on PLS for "+ aCenterMapOn)
# Use the SelectLayerByAttribute tool to select PLS Subject and
#  zoom to the selection
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", iQuery)
df.zoomToSelectedFeatures()
df.scale = aMapScale

# Export the map to a .pdf
arcpy.mapping.ExportToPDF(mxd,MapLocation)

########################################
###### EXPORT MAP ENDS HERE            #
########################################

# Clear vars
del mxd, Targetmxd


Viewing all articles
Browse latest Browse all 2485

Trending Articles