Last active
August 29, 2015 14:04
-
-
Save morehavoc/46d6cc60fc5a56909206 to your computer and use it in GitHub Desktop.
Collect datasets and information about them from a folder to a CSV!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
from os.path import abspath, join, basename, dirname | |
import csv | |
class Toolbox(object): | |
def __init__(self): | |
self.label = "Collect Info to CSV Toolbox" | |
self.alias = "CollectInfoToCSV" | |
# List of tool classes associated with this toolbox | |
self.tools = [CollectInfoToCSV] | |
class CollectInfoToCSV(object): | |
def __init__(self): | |
self.label = "Collect Info to CSV" | |
self.description = "Collects information about layers and objects and stores them in an output CSV." | |
def getParameterInfo(self): | |
#Define parameter definitions | |
# Input folder to search | |
input_folder = arcpy.Parameter( | |
displayName="Search Folder", | |
name="search_folder", | |
datatype="DEFolder", | |
parameterType="Required", | |
direction="Input") | |
# Derived Output Features parameter | |
output_file = arcpy.Parameter( | |
displayName="Output CSV", | |
name="output_file", | |
datatype="DEFile", | |
parameterType="Required", | |
direction="Output") | |
#output_file.filter.type = "File" | |
output_file.filter.list = ["CSV"] | |
parameters = [input_folder, output_file] | |
return parameters | |
def isLicensed(self): #optional | |
return True | |
def updateParameters(self, parameters): #optional | |
return | |
def updateMessages(self, parameters): #optional | |
return | |
def execute(self, parameters, messages): | |
input_folder = parameters[0].valueAsText | |
output_file = parameters[1].valueAsText | |
results = self.walk_directory(input_folder) | |
with open(output_file, 'wb') as csvfile: | |
data_writer = csv.writer(csvfile, dialect='excel') | |
data_writer.writerow(['Name', 'Path', 'Type']) | |
data_writer.writerows(results) | |
return | |
def walk_directory(self, directory): | |
""" | |
Returns a list of lists (table) where each row is an item that was found. | |
The columns returned are: | |
Name | |
Path | |
Type | |
""" | |
output_list = list() | |
for base_dir, child_dirs, filenames in arcpy.da.Walk(directory): | |
dir_info = arcpy.Describe(base_dir) | |
data = (basename(abspath(base_dir)), abspath(base_dir), getattr(dir_info, 'datasetType', None)) | |
output_list.append(data) | |
for filename in filenames: | |
file_info = arcpy.Describe(join(abspath(base_dir), filename)) | |
data = (filename, join(abspath(base_dir), filename), getattr(file_info, 'datasetType', None)) | |
output_list.append(data) | |
return output_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment