Make thumbnails of feature classes using arcpy

3546
2
Jump to solution
05-28-2013 02:53 PM
CPoynter
Occasional Contributor III
Hi All,

I know arcpy can be used to make thumbnails from MXDs, but what about using feature classes or rasters? I have thousands of feature classes / rasters that I need to make thumbnails for and don't want to have to manually do this within ArcCatalog one at a time.

Any thoughts on how to achieve this? I have code to already list the spatial datasets, just need coding to generate thumbnails that will be incorporated into metadata documents.

Regards,

Craig
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
CPoynter
Occasional Contributor III
I adjusted my code to handle both feature classes and raster datasets.

Base code provided by jscheirer was invaluable to solving this issue.

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 
                   

I am making thumbnails at this point for incorporation into metadata pages for exporting to HTML pages (see http://forums.arcgis.com/threads/86942-XSLT-Transformation-Thumbnails for next point of contention).

Regards,

Craig

View solution in original post

0 Kudos
2 Replies
JasonScheirer
Occasional Contributor III
If you have a pre-authored map, you could do something hacky like this:

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')


Note you don't have to apply a symbology, you can just do

    # 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)


if you don't mind it being auto-symbolized like it does in GP tools.
0 Kudos
CPoynter
Occasional Contributor III
I adjusted my code to handle both feature classes and raster datasets.

Base code provided by jscheirer was invaluable to solving this issue.

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 
                   

I am making thumbnails at this point for incorporation into metadata pages for exporting to HTML pages (see http://forums.arcgis.com/threads/86942-XSLT-Transformation-Thumbnails for next point of contention).

Regards,

Craig
0 Kudos