How to export a set of layers into multiple PDF(orAI)files, one layer at a time?

559
2
06-26-2014 07:07 AM
JingruZhang
New Contributor
I am completely new to the arcpy.mapping module.I have a dataset contains files named as "1_A, 1_B, 1_C....12_A,12_B, 12_c,....200_A, 200_B, 200_C..etc."  I need to export each feature layer, one at a time , to a PDF or AI file through a COMPLETELY AUTOMATED process.  I tried to use Addlayer and Removelayer tool in Model Builder but they all run for one single feature layer once at a time and I can neither connect  the add and remove process to each other, nor could I connect the process through Iterator to the script tool anyway. This means although I use model builder or write python scrip, I have to change the layers (or the name of layers) for hundereds of times and save every layer into a new MXD file and export then to pdfs.

I was thinking about turnning on one of the layers at a time, export a .ai file with that layer visible, then turn that layer off and move on to the next layer in the list. I'm wondering if you have any suggestion about building the model or the scripts?

Thanks A Lot!!
0 Kudos
2 Replies
CraigPoynter
Occasional Contributor III
Try the following:



import arcpy, os, json, traceback, sys, datetime
from arcpy import mapping
from arcpy import env

mxd_out = r'D:\Temp\Blank.mxd'

now = datetime.datetime.now()
log = r'D:\Temp\Invertory_' + now.strftime("%Y-%m-%d" + '_' + "%H_%M_%S") + '_Error.log'

if arcpy.Exists(log):
                          arcpy.Delete_management(log)

if arcpy.Exists(mxd_out):
                          arcpy.Delete_management(mxd_out)

mapService = {}
jsonDump = json.dumps(mapService)
result = mapping.ConvertWebMapToMapDocument(jsonDump)
mxd = result.mapDocument
my_mxd = mxd.saveACopy(mxd_out)

env.workspace = r"D:\Temp\Layers"

contents = []

print 'Starting'

for dirpath, dirnames, datatypes in arcpy.da.Walk(env.workspace):
        contents.append(os.path.join(dirpath))

        for item in contents:
            if arcpy.Exists(item):
               
                arcpy.env.workspace = item

        listType = [arcpy.ListFeatureClasses, arcpy.ListDatasets]
        for list in listType:

            datasetList = list("*", 'All')

            # Iterate over the feature classes
            for dataset in datasetList:
               
                    data = item + '\\' + dataset

                    desc = arcpy.Describe(data)

                    print data + ' : ' + desc.dataType

                    # Crack open the map
                    mxd_new = arcpy.mapping.MapDocument(mxd_out)

                    df = arcpy.mapping.ListDataFrames(mxd_new, '*')[0]

                    try:

                            lyrFile = arcpy.mapping.Layer(data)

                            if desc.dataType == 'RasterDataset':
                                layerType = arcpy.management.MakeRasterLayer
                           
                                result = layerType(lyrFile, 'temp_layer')
                                layer_object = result.getOutput(0)
                                arcpy.mapping.AddLayer(df, layer_object)

                            else:
                                result = arcpy.management.MakeFeatureLayer(lyrFile, 'temp_layer')
                                layer_object = result.getOutput(0)
                                arcpy.mapping.AddLayer(df, layer_object)
                         
                            # Set correct extent
                            df.zoomToSelectedFeatures()

                            nm = os.path.splitext(dataset)[0]

                            # Compute an output file name
                            out_file_name = (r"D:\Temp\Output" + "\\" + nm + '.jpg')

                            # Export Image of data frame
                            arcpy.mapping.ExportToJPEG(mxd_new, out_file_name, df, 300, 300, )

                            # Pull the layer out of the data frame to make room for the next one
                            arcpy.mapping.RemoveLayer(df, layer_object)

                            # Delete the GP layer
                            arcpy.management.Delete('temp_layer')

                    except:

                        arcpy.ExecuteError
                        arcpy.AddError(arcpy.GetMessage(2))
                        f=open(log, 'at')
                        f.write(data + '\n')
                        f.close()

del mxd, mxd_new

if arcpy.Exists(mxd_out):
                          arcpy.Delete_management(mxd_out)

del mxd_out



There are a few additional items in there you won't need like the error log, but this should create you an output. You will need to change the ExportToJPEG to ExportToPDF command options.

Regards,

Craig
0 Kudos
JingruZhang
New Contributor
Thanks a lot!I'll try and reply ASAP.
0 Kudos