Create Areakm Table Py
Create Areakm Table Py
py # Purpose: Loop through each polygon feature class and: # 1) identify the value stored in the "Area_km" field # 2) append value to a list # 3) convert list to array # 4) resize the array # 5) save to .txt # # Author: mbs7038 # # Created: 07/04/2014 # Copyright: (c) mbs7038 2014 # Licence: <your licence> #------------------------------------------------------------------------------import arcpy from arcpy import env import numpy # Setup workspace arcpy.env.workspace=r"C:\Users\mbs7038\Documents\New_Landsat_Imagery\For_Area_Ca lc\Minimize_DEM_Analysis\PolyConvert\out_prj.gdb" # Enable overwriting arcpy.env.overwriteOutput=True # final output text file to be created filename=r"C:\Users\mbs7038\Documents\New_Landsat_Imagery\For_Area_Calc\Minimize _DEM_Analysis\PolyConvert\areatable_Sorted.txt" areaColumn=[] #create an empty list where area data can be stored (ends up being 1x336 after completing the first for loop) areaset=set() #=============================================================================== # get a list of all feature classes in env.workspace fcs=arcpy.ListFeatureClasses("*") fcCount=len(fcs) #print a count of the number of feature classes in the gdb print fcCount # feature class column where area in km2 is stored areafield="Area_km" fcs.sort() for fc in fcs: #loop through each feature class print fc rows = arcpy.SearchCursor(fc) #list all rows in the feature class for row in rows: #loop through each row in the feature class area=row.getValue(areafield)#get the value stored in the field "Area_km" print area print 'appending area' if area not in areaset: areaColumn.append(area)#add the value to AreaColumn array print'adding area' areaset.add(area) print areaColumn print'convert areaColumn list to an array' # convert the 1x336 list to a 1x336 array initial=numpy.array(areaColumn,dtype=float) print initial # print the new array, "initial"
initial.shape print'resizing to 16x21 array' finalArea=numpy.resize(initial,(16,21)) # resize the 1x336 array to a 16x21 arra y print finalArea # print the resized array, "finalarea" print 'saving finalarea array' numpy.savetxt(filename,finalArea) print 'script worked!' # save "finalarea" array to a text file