Solved! Go to Solution.
import arcpy, os, json, traceback, sys, datetime from arcpy import mapping from arcpy import env mxd_out = r'D:\Temp\HTML.mxd' now = datetime.datetime.now() ## Create error log to capture data to further investigate log = r'D:\Temp\CSD_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) ## Create a dummy blank mxd to utilise mapService = {} jsonDump = json.dumps(mapService) result = mapping.ConvertWebMapToMapDocument(jsonDump) mxd = result.mapDocument my_mxd = mxd.saveACopy(mxd_out) env.workspace = r"D:\Temp\SpatialData" contents = [] 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\Thumbnails" + "\\" + nm + '.jpg') # Export "thumbnail" 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
import os import arcpy.mapping # Crack open the map mxd = arcpy.mapping.MapDocument(my_mxd) data_frame = arcpy.mapping.ListDataFrames(mxd)[0] # Iterate over the feature classes for featureclass in feature_classes: # Make a new geoprocessing layer arcpy.management.MakeFeatureLayer(featureclass, 'temp_layer') # Apply symbology to it result = arcpy.management.ApplySymbologyFromLayer('temp_layer', r'c:\path\to\my\symbology\layer') # getOutput(0) is an arcpy.mapping Layer object layer_object = result.getOutput(0) # Drop onto dataframe arcpy.mapping.AddLayer(data_frame, layer_object) # Set correct extent data_frame.zoomToSelectedFeatures() # Compute an output file name out_file_name = r"c:\thumbnails\{basename}.png".format(basename=os.path.basename(featureclass)) # Export "thumbnail" of data frame arcpy.mapping.ExportToPNG(mxd, out_file_name, data_frame, 128, 128) # Pull the layer out of the data frame to make room for the next one arcpy.mapping.RemoveLayer(data_frame, layer_object) # Delete the GP layer arcpy.management.Delete('temp_layer')
# Make a new geoprocessing layer result = arcpy.management.MakeFeatureLayer(featureclass, 'temp_layer') # getOutput(0) is an arcpy.mapping Layer object layer_object = result.getOutput(0)
import arcpy, os, json, traceback, sys, datetime from arcpy import mapping from arcpy import env mxd_out = r'D:\Temp\HTML.mxd' now = datetime.datetime.now() ## Create error log to capture data to further investigate log = r'D:\Temp\CSD_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) ## Create a dummy blank mxd to utilise mapService = {} jsonDump = json.dumps(mapService) result = mapping.ConvertWebMapToMapDocument(jsonDump) mxd = result.mapDocument my_mxd = mxd.saveACopy(mxd_out) env.workspace = r"D:\Temp\SpatialData" contents = [] 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\Thumbnails" + "\\" + nm + '.jpg') # Export "thumbnail" 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