Wednesday, 12 June 2013

Raster Data Information in ArcMap

This somewhat lengthy script allows you to determine various properties of raster data.  It can be associated with a tool in ArcToolbox for your own purposes or as a standalone script.  See the comments within the script for the necessary changes.

This script also demonstrates how one might document your own work providing guidance to users on how to set tool properties etc.  Have fun

'''
RasterDataInfo.py

Author
  Dan Patterson
  Dept of Geography and Environmental Studies
  Carleton University, Ottawa, Canada
 
Dan_Patterson@carleton.ca
Date:     Feb 2, 2008
Modified: June 12, 2013 Rewritten for arcpy module

Purpose:
  Lists the properties of raster data, optionally writes to a file.

Properties (right-click on the tool and specify the following)
General
  Name  RasterDataInfo
  Label Raster Data Info
  Desc  List rasters in a folder.
        All raster classes within the folder will be described.
        You can optionally create a text file containing all output.
        Simply select an output folder then specify an output filename.

Source script RasterDataInfo.py
Parameter list  (examples below)
                                                Parameter Properties
           Display Name          Data type      Type      Direction  MultiValue
  argv[1]  Select a folder       Workspace      Required  Input      No
  argv[2]  Output text filename  Optional       Optional  Input      No


Import the standard modules
  Get the folder
  Get an optional output filename
  Get a list of rasters
  cycle through the list and add their properties

'''
import sys, os, string
import arcpy

#arcpy.env.workspace = sys.argv[1]  #uncomment if used in a tool
#aFileName=sys.argv[2]              #uncomment if used in a tool
arcpy.env.workspace = "c:/temp"     #comment out if used in a tool
aFileName = "c:/temp/raster.txt"    #comment out if used in a tool
#
msg = ["\nRaster data in folder: " + arcpy.env.workspace]
#
#Determine if output is to go to a file as well
#
if aFileName <> '#':         #check to see if a filename is specified
  aFile=open(aFileName, 'w')
  msg.append("Output saved to: %s \n Processing...\n" % (aFileName))
else:
  msg.append("Optional text file not created...")

leaders = ["Number of bands", "Compression type",
           "Format", "Pixel type", "Integer", "Temporary",
           "Nodata value", "Uncompressed size",
           "Has table", "Spatial Reference",
           "Height", "Width", "Cell size X", "Cell size Y",
           "Minimum", "Maximum", "Mean", "Standard deviation",
           "Extent XMin", "       YMin",
           "       XMax", "       YMax"]
aCounter = 0
rasters = arcpy.ListRasters()  #a list, change as desired
for raster in rasters:
  desc = arcpy.Describe(raster)  #Describe object
  r = arcpy.Raster(desc.CatalogPath)      #Raster object

  info = [r.bandCount, r.compressionType, r.format, r.pixelType,
          r.isInteger, r.isTemporary, r.noDataValue,
          r.uncompressedSize, r.hasRAT, r.spatialReference.name,
          r.height, r.width, r.meanCellHeight, r.meanCellWidth,
          r.minimum, r.maximum, r.mean, r.standardDeviation,
          r.extent.XMin, r.extent.YMin,
          r.extent.XMax, r.extent.YMax]

  msg.append("%-5s (%3s): %s" % ("File", aCounter, r.catalogPath))
  for i in range(0,len(leaders)):
    msg.append("  %-20s  :  %s" % (leaders[i],info[i]))
  aCounter += 1

#output to optional file, print to screen
if aFileName <> '#':
  for aVal in msg:
    aFile.write(aVal + "\n")
  aFile.flush()
  aFile.close()

for aVal in msg:
  print aVal
  arcpy.AddMessage(aVal)

del msg
del r

No comments:

Post a Comment