5

I have a script to investigate mxd's and save them as lyr symbology/kmz and sld which works fine when run from the .py but does not run when used as a tool from arctoolbox. I have tried arcpy.AddMessage statements to see what's wrong but it just does not work.

Please help...

SCRIPT

# Author: George Corea, Atherton Tablelands GIS
# [email protected]; [email protected]
# Licence:Creative Commons
import arcpy, string, datetime, shutil, os
import arcpy.mapping as MAP
#Read input parameters from script tool
MXDList = string.split(arcpy.GetParameterAsText(0), ";")
ProjectPath = arcpy.GetParameterAsText(1)
#MXDList=r'P:2012円183円_TownPlanning_Symbology\Working\TP_Biodiversity.mxd'
#ProjectPath=r'P:2012円183円_TownPlanning_Symbology\Working'
count=0
#Loop through each MXD and print
#for MXDPath in MXDList:
#MXDFile=r'P:2012円189円_Townplanning_scheme\Working\TRC_PlanningScheme_LayoutMaster_v10.mxd'
count=count+1
arcpy.AddMessage('starting...'+str(MXDList))
##try:
for MXDFile in MXDList:
 mxd=arcpy.mapping.MapDocument(MXDFile)
 outPath = ProjectPath+'\\'+mxd.filePath[mxd.filePath.rfind('\\')+1:mxd.filePath.rfind('.')]
 arcpy.AddMessage('working on 1...'+str(mxd)+str(outPath))
 try:
 os.mkdir(outPath)
 except:
 pass
 dfList = arcpy.mapping.ListDataFrames(mxd)
 arcpy.AddMessage('working on 2...'+str(dfList)+str(outPath))
 for df in dfList:
 #msd = outPath+'.msd'
 #arcpy.mapping.ConvertToMSD(mxd, msd, df, "NORMAL", "NORMAL")
 #arcpy.AddMessage(str(count)+'...' +str(outPath)+'...'+'\n')
 lyrList=arcpy.mapping.ListLayers(mxd, "", df)
 outPath = ProjectPath+'\\'+mxd.filePath[mxd.filePath.rfind('\\')+1:mxd.filePath.rfind('.')]+'\\'+str(df.name)
 #os.mkdir(outPath)
 arcpy.AddMessage('working on 3...'+str(lyrList)+str(outPath))
 for lyrFile in lyrList:
 arcpy.AddMessage(str(lyrFile))
 if lyrFile.isFeatureLayer == True:
 if lyrFile.visible == True:
 arcpy.AddMessage(str(lyrFile)+' is visible')
 #print str(lyrFile)+' is visible...exporting dataset'
 outFileN=str(arcpy.ValidateTableName(lyrFile.longName[lyrFile.longName.rfind('\\')+1:]))
 try:
 arcpy.FeatureClassToFeatureClass_conversion(lyrFile.dataSource, outPath, outFileN)
 arcpy.SaveToLayerFile_management(lyrFile,outPath+'\\'+outFileN+'.lyr', "ABSOLUTE")
 arcpy.LayerToKML_conversion(outPath+'\\'+outFileN+'.lyr', outPath+'\\'+outFileN+'.kmz')
 except:
 errorm=arcpy.GetMessages()
 arcpy.AddMessage('Error...'+str(errorm)+' ...continuing')
 arcpy.AddMessage(str(lyrFile)+' is visible. Not Processing')
 #print str(lyrFile)+' is not visible'
 ##except:
 ## errorM=arcpy.GetMessages()
 ## arcpy.AddMessage(str(count)+str(errorM)+'\n continuing...\n')
 ## print errorM
 ## #MAP.PrintMap(MXD, printer)
 #Remove variable reference to file
 del mxd, msd

ERROR enter image description here and that's it. The text 'working 2...' from the arcpy.AddMessages doesn't work.

When run in the Python Interpreter window you get

>>> import os
>>> os.getcwd()
'Q:\\scripts\\py'
>>> MXDFile=r'P:2011円\Job_031_TownPlanning_SeriesProduction\Working\mxd\Nov14_Landslide18円_TownPlanning_B&L_Overlay_Ver4a_Chillagoe.mxd'
>>> mxd=arcpy.mapping.MapDocument(MXDFile)
>>> ProjectPath=r'Q:\scripts\py\junk'
>>> outPath = ProjectPath+'\\'+mxd.filePath[mxd.filePath.rfind('\\')+1:mxd.filePath.rfind('.')]
>>> dfList = arcpy.mapping.ListDataFrames(mxd)
>>> for df in dfList:
... lyrList=arcpy.mapping.ListLayers(mxd, "", df)
... print lyrList
... 
[<map group layer u'Residences'>, <map layer u'Residential Structure'>, <map layer u'Residential Structure'>, <map group layer u'Base_Top'>, <map layer u'Regional Activity Centre'>, <map layer u'Homesteads'>, <map layer u'State Controlled Road'>, <map layer u'Local Road'>, <map layer u'Local Road'>, <map layer u'Wetland'>, <map layer u'Waterbody'>, <map layer u'Protected Area'>, <map layer u'TRC Boundary'>, <map layer u'Property Boundary'>, <map layer u'Watercourse'>, <map layer u'Tri_RiversTRCClip'>, <map layer u'Rural_DrainageTRCClip'>, <map group layer u'Hazard'>, <map group layer u'NewLandslide'>, <map layer u'100k_trc_tiles_TVM'>, <map layer u'BULLOCK_CREEK'>, <map layer u'BULIMBA'>, <map layer u'BLACKDOWN'>, <map layer u'BELLEVUE'>, <map layer u'MAYTOWN'>, <map layer u'M_SURPRISE'>, <map layer u'LYNDBROOK'>, <map layer u'KIRRAMA'>, <map layer u'KINGFISH'>, <map layer u'HIGHBURY'>, <map layer u'CASHMERE'>, <map layer u'CAIRNS'>, <map layer u'MUNGANA'>, <map layer u'MOUNT'>, <map layer u'MOSSMAN'>, <map layer u'GALLOWAY'>, <map layer u'CONJUBOY'>, <map layer u'CHILLAGOE'>, <map layer u'BARTLE_FRERE'>, <map layer u'ATHERTON'>, <map layer u'ABINGDON_DOWNS'>, <map layer u'WALSH'>, <map layer u'WALLABADAH'>, <map layer u'VALLEY_LAGOONS'>, <map layer u'TULLY'>, <map layer u'STRATHMORE'>, <map layer u'ST_RONANS'>, <map layer u'SOUTH'>, <map layer u'RUMULA'>, <map layer u'RAVENSHOE'>, <map layer u'Landslide Zone'>, <map layer u'High Bushfire Hazard'>, <map layer u'High Bushfire Hazard Buffer '>, <map layer u'Medium Bushfire Hazard'>, <map layer u'Medium Bushfire Hazard Buffer'>, <map group layer u'Base_Bottom'>, <map layer u'Protected Area'>]
[<map group layer u'Residences'>, <map layer u'Residential Structure'>, <map layer u'Residential Structure'>, <map group layer u'Base_Top'>, <map layer u'Regional Activity Centre'>, <map layer u'Homesteads'>, <map layer u'State Controlled Road'>, <map layer u'Local Road'>, <map layer u'Local Road'>, <map layer u'Wetland'>, <map layer u'Waterbody'>, <map layer u'Protected Area'>, <map layer u'TRC Boundary'>, <map layer u'Property Boundary'>, <map layer u'River'>, <map layer u'Tri_RiversTRCClip'>, <map layer u'Rural_DrainageTRCClip'>, <map group layer u'Hazard'>, <map group layer u'NewLandslide'>, <map layer u'100k_trc_tiles_TVM'>, <map layer u'BULLOCK_CREEK'>, <map layer u'BULIMBA'>, <map layer u'BLACKDOWN'>, <map layer u'BELLEVUE'>, <map layer u'MAYTOWN'>, <map layer u'M_SURPRISE'>, <map layer u'LYNDBROOK'>, <map layer u'KIRRAMA'>, <map layer u'KINGFISH'>, <map layer u'HIGHBURY'>, <map layer u'CASHMERE'>, <map layer u'CAIRNS'>, <map layer u'MUNGANA'>, <map layer u'MOUNT'>, <map layer u'MOSSMAN'>, <map layer u'GALLOWAY'>, <map layer u'CONJUBOY'>, <map layer u'CHILLAGOE'>, <map layer u'BARTLE_FRERE'>, <map layer u'ATHERTON'>, <map layer u'ABINGDON_DOWNS'>, <map layer u'WALSH'>, <map layer u'WALLABADAH'>, <map layer u'VALLEY_LAGOONS'>, <map layer u'TULLY'>, <map layer u'STRATHMORE'>, <map layer u'ST_RONANS'>, <map layer u'SOUTH'>, <map layer u'RUMULA'>, <map layer u'RAVENSHOE'>, <map layer u'Landslide Zone'>, <map layer u'High Bushfire Hazard'>, <map layer u'High Bushfire Hazard Buffer '>, <map layer u'Medium Bushfire Hazard'>, <map layer u'Medium Bushfire Hazard Buffer'>, <map group layer u'Base_Bottom'>, <map layer u'Protected Area'>]
[<map layer u'Regional Activity Centre'>, <map layer u'State Controlled Road'>, <map layer u'Frame_Extents2'>, <map layer u'TRC Boundary'>]
>>> 

TOOL PARAMS enter image description here

=== RESPONSE to Answers/Comments in post ===

Messages
Executing: InvestigateMXDs P:2012円096円_CCRC_2_Biodiversity\Working\BiodiversityOverlay_70k_Series3b.mxd D:\junk
Start Time: Mon Aug 06 09:01:22 2012
Running script InvestigateMXDs...
Now processing ...
 Map: P:2012円096円_CCRC_2_Biodiversity\Working\BiodiversityOverlay_70k_Series3b.mxd

So basically it hangs on the df list. There is no further error message even if you try to capture it.

Test on simple mxd is below. Note that this works fine when run as a python script directly.

enter image description here

Messages
Executing: InvestigateMXDs P:2012円183円_TownPlanning_Symbology\Working\test.mxd P:2012円183円_TownPlanning_Symbology\Working\junk
Start Time: Tue Aug 07 09:11:35 2012
Running script InvestigateMXDs...
Now processing ...
 Map: P:2012円183円_TownPlanning_Symbology\Working\test.mxd

When run as a python we get the following in less than 30s

starting...['test.mxd']
working on 1...<geoprocessing Map object object at 0x029471E0>P:2012円183円_TownPlanning_Symbology\Working\test
working on 2...[<DataFrame object at 0xee12950[0xee22fc0]>]P:2012円183円_TownPlanning_Symbology\Working\test
working on 3...in: P:2012円183円_TownPlanning_Symbology\Working\test\Layers
TRC_Boundary is visible PROCESSING...
TRC_Boundary is not visible. Not Processing
Map_Extents is visible PROCESSING...
Map_Extents is not visible. Not Processing

and directory structure enter image description here

##Issue with modified script from Polygeo Same problems.

enter image description here === END Response ===

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Aug 2, 2012 at 1:07
2
  • I'm not in a position to test at the moment but if it works without a tool dialog but not with one then I would focus on the tool parameters and the variables set using GetParameterAsText are processed. Perhaps "gut" a test copy of your Python script and try to get it to process the list of MXDs doing something trivial successfully before restoring the "bells and whistles". Commented Aug 2, 2012 at 8:25
  • Thanks. I have tried but as in the screen capture it just fails after reading the data frame. Commented Aug 2, 2012 at 13:52

2 Answers 2

1

I tried it again by using a new script tool -till now I was just changing the link to the python script from the old tool. Now it works fine. For anyone interested the updated code and tool set up is below.

# Copies, clips and creates symbology of all visibile layers in mxd's in the current directory. Also creates a text file with metadata for use.
# Author: George Corea, Atherton Tablelands GIS
# [email protected]; [email protected]
# Licence:Creative Commons
import arcpy, string, datetime, shutil, os, glob
import arcpy.mapping as MAP
arcpy.env.workspace = os.getcwd()
arcpy.env.outputCoordinateSystem=r'L:\Vector_Data\Administrative\Boundaries\Local_Govt\TRC\trc_boundary_Polygon.prj' #to maintain all datasets in the same projection
arcpy.env.overwriteOutput = True
MXDList=string.split(arcpy.GetParameterAsText(0), ";")
ProjectPath=arcpy.GetParameterAsText(1)
rootPath=ProjectPath
#MXDList=glob.glob('*.mxd')
#ProjectPath=r'P:2012円183円_TownPlanning_Symbology\Working' # root output directory
clip_features=r'L:\Vector_Data\Administrative\Boundaries\Local_Govt\TRC\trc_boundary_Polygon.shp' # polygon to clip data to AOI
AOI='_trc' #appended as suffix to dataset
xy_tolerance=1 #clip tolerance
# No edits should be required below this line.
VisibleLyrList = []
count=1
def layer_details(outPath, outFileN, lyrFile, type): #Generates the metadata
 descLayer = arcpy.Describe(lyrFile.dataSource)
 ReviewLog=outPath+'\\'+type+'_'+outFileN+'_log.txt'
 f = open(ReviewLog, 'a')
 f.write(str(lyrFile.name)+': name{'+str(lyrFile.datasetName)+\
 '}; query{'+str(lyrFile.definitionQuery)+\
 '}; source{'+str(lyrFile.dataSource)+\
 '}; description{'+str(lyrFile.description)+\
 '}; symbology{'+ str(lyrFile.symbologyType)+\
 '}; original projection{'+str(descLayer.spatialReference.name)+\
 '}; extent(x,y){'+str(descLayer.extent.XMax)+','+str(descLayer.extent.XMin)+','+str(descLayer.extent.YMax)+','+str(descLayer.extent.YMin)+\
 '}; format{'+str(descLayer.shapeType)+\
 '}; size(bytes) ~{'+str(os.path.getsize(lyrFile.dataSource))+\
 '} @{'+str(datetime.datetime.now())+'}'\
 )
 f.close()
print 'starting...'+str(MXDList)
for MXDFile in MXDList:
 mxd=arcpy.mapping.MapDocument(MXDFile)
 outPath = ProjectPath+'\\'+mxd.filePath[mxd.filePath.rfind('\\')+1:mxd.filePath.rfind('.')]
 arcpy.AddMessage ('Working on file #' + str(count) +' ...'+str(mxd.filePath))
 try:
 os.mkdir(outPath)
 except:
 pass
 dfList = arcpy.mapping.ListDataFrames(mxd)
 #print arcpy.GetMessages()
 #print 'working on 2...'+str(dfList)+str(outPath)
 for df in dfList:
 #msd = outPath+'.msd'
 #arcpy.mapping.ConvertToMSD(mxd, msd, df, "NORMAL", "NORMAL")
 #print (str(count)+'...' +str(outPath)+'...'+'\n')
 arcpy.AddMessage ('Working on dataframe ... ' +str(df.name))
 lyrList=arcpy.mapping.ListLayers(mxd, "", df)
 outPath = ProjectPath+'\\'+mxd.filePath[mxd.filePath.rfind('\\')+1:mxd.filePath.rfind('.')]+'\\'+str(df.name)
 try:
 os.mkdir(outPath)
 except:
 pass
 print ('working on 3...in: '+str(outPath))
 for lyrFile in lyrList:
 #print (str(lyrFile))
 if lyrFile.isFeatureLayer == True:
 if lyrFile.visible == True:
 if lyrFile.name not in VisibleLyrList:
 VisibleLyrList.append(lyrFile.name)
 arcpy.AddMessage (str(lyrFile)+' is visible PROCESSING...')
 outFileN=str(arcpy.ValidateTableName(lyrFile.longName[lyrFile.longName.rfind('\\')+1:]))
 try:
 rows = arcpy.SearchCursor(lyrFile.dataSource)
 row = rows.next()
 if row:
 arcpy.FeatureClassToFeatureClass_conversion(lyrFile.dataSource, outPath, outFileN)
 #arcpy.Copy_management(lyrFile.dataSource, outPath+'//'+outFileN, "")
 arcpy.Clip_analysis(outPath+'\\'+outFileN+'.shp', clip_features, outPath+'\\'+outFileN+AOI+'.shp', xy_tolerance)
 #updateLayer = outPath+'\\'+outFileN+'.lyr'
 sourceLayer = arcpy.mapping.Layer(outPath+'\\'+outFileN+'.shp')
 sourceLayer_AOI = arcpy.mapping.Layer(outPath+'\\'+outFileN+AOI+'.shp')
 #arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
 arcpy.SaveToLayerFile_management(lyrFile,outPath+'\\'+outFileN+'_sym.lyr', "ABSOLUTE")
 arcpy.SaveToLayerFile_management(sourceLayer_AOI,outPath+'\\'+outFileN+AOI+'.lyr', "ABSOLUTE")
 arcpy.ApplySymbologyFromLayer_management (outPath+'\\'+outFileN+AOI+'.lyr', outPath+'\\'+outFileN+'_sym.lyr')
 descLayer = arcpy.Describe(sourceLayer)
 layer_details(outPath, outFileN, lyrFile,"COMPLETED")
 arcpy.Delete_management(outPath+'\\'+outFileN+'_sym.lyr')
 arcpy.Delete_management(outPath+'\\'+outFileN+'.shp')
 #arcpy.LayerToKML_conversion(outPath+'\\'+outFileN+'.lyr', outPath+'\\'+outFileN+'.kmz')
 else:
 arcpy.AddMessage ("!!!Datasource Issue!!!...continuing")
 layer_details(outPath, outFileN,lyrFile,"ISSUE")
 IssueLog=rootPath+'\\'+'Issue_log.txt'
 f = open(IssueLog, 'a')
 f.write(str(lyrFile.name)+': name{'+str(lyrFile.datasetName)+ '}; source{'+str(lyrFile.dataSource)+'}'+'\n')
 f.close()
 except:
 errorm=arcpy.GetMessages()
 arcpy.AddMessage ('!!! ERROR !!!!...'+str(errorm)+' ...continuing')
 layer_details(outPath, outFileN, lyrFile,"ERROR")
 #break
 ErrorLog=rootPath+'\\'+'Error_log.txt'
 f = open(ErrorLog, 'a')
 f.write(str(lyrFile.name)+': name{'+str(lyrFile.datasetName)+ '}; source{'+str(lyrFile.dataSource)+'}'+'\n')
 f.close()
 else:
 pass
 else:
 arcpy.AddMessage (str(lyrFile)+' Is NOT Visible. Not Processing')
 else:
 arcpy.AddMessage (str(lyrFile)+' Is NOT Feature Layer. Not Processing')
 count=count+1
 #print str(lyrFile)+' is not visible'
 #Remove variable reference to file
 del mxd, outPath, lyrFile

enter image description here

answered Aug 7, 2012 at 4:09
1
  • It looks like your MXDList is now coming as data type File rather than ArcMap Document. Is it still a Multivalue and filtered by *.mxd to make for easy selection? Anyway, I'm glad its now working. Commented Aug 7, 2012 at 9:37
0

This is far from a complete Answer as yet, but if you put the test script below behind your existing tool dialog (with its multivalue ArcMap Document parameter), will it run and list all your chosen maps, with their data frames and layers? If it does then the shell works and we can start to look more closely at what it needs to be done with each data frame object the script creates.

import arcpy
MXDList = str(arcpy.GetParameterAsText(0)).split(";")
arcpy.AddMessage("Now processing ...")
arcpy.AddMessage("MXDList of: " + str(MXDList))
for MXDFile in MXDList:
 MXDFile = MXDFile.strip("'")
 mxd = arcpy.mapping.MapDocument(str(MXDFile))
 arcpy.AddMessage(" Map: " + mxd.filePath)
 dfList = arcpy.mapping.ListDataFrames(mxd)
 for df in dfList:
 arcpy.AddMessage(" Data frame: " + df.name)
 lyrList = arcpy.mapping.ListLayers(mxd,"*",df)
 for lyr in lyrList:
 arcpy.AddMessage(" Layer: " + lyr.name)
GeorgeC
8,3588 gold badges59 silver badges149 bronze badges
answered Aug 5, 2012 at 2:45
6
  • Thanks....nope still doesn't work. I have updated your answer with the message I get. Commented Aug 5, 2012 at 23:02
  • That's weird, but at least gives another angle to investigate from. Is there any chance that when the df line hangs, that it is not just slow rather than halted? Does the code work when you choose one or two of the simplest MXDs you have on your system? If it works for simple MXDs (like it does for me) then maybe we need to see what is special about the ones you are working with. Commented Aug 6, 2012 at 23:02
  • sorry...same issue. See edits.\ Commented Aug 6, 2012 at 23:12
  • I've not had responses edited into my Answer before and am thinking that may make it confusing. Would it work for you to take your response out into a separate Answer that just you Edit? For now I've updated my test script so that it reports the MXDList being processed and lets me see what you are getting back from the tool dialog. Commented Aug 7, 2012 at 0:32
  • Sorry about this. I haven't done this before either and I'll chalk it up to an experiment that didn't work. Commented Aug 7, 2012 at 1:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.