我正在尝试创建一个1x336列数组,并用336个要素类的属性表中的数据填充它。我想我已经完成了这项任务。接下来,我想将此1x336阵列重新排列为16x21阵列,并将其保存为txt文件。
非常感谢任何帮助。
import arcpy
from arcpy import env
import numpy
arcpy.env.workspace="C:\Users\mbs7038\Documents\New_Landsat_Imagery\For_Area_Calc\Minimize_DEM_Analysis\PolyConvert\out_prj.gdb"
arcpy.env.overwriteOutput=True
filename="C:\Users\mbs7038\Documents\New_Landsat_Imagery\For_Area_Calc\Minimize_DEM_Analysis\PolyConvert\areatable.txt"
fcs=arcpy.ListFeatureClasses("*")
areafield="Area_km"
n=16
areaColumn=[]
arealist=numpy.zeros([16,21],dtype=numpy.float64)
areaset=set()
print arealist
for fc in fcs:
print fc
rows = arcpy.SearchCursor(fc)
for row in rows:
area=row.getValue(areafield)
print area
print 'appending area'
if area not in areaset:
areaColumn.append(area)
print'adding area'
areaset.add(area)
print areaColumn
for i in range(n):
for j in range(0,21):
arealist[i,j]=areaColumn[:] #This is where I have been trying to call each row from areaColumn
print arealist
我也尝试过重塑功能但没有成功:
finalarea=arealist.reshape(arealist.size//16,21)
print finalarea
答案 0 :(得分:1)
你应该知道重塑需要一个元组作为参数。以下应该有效:
finalarea = arealist.reshape((16,21))
答案 1 :(得分:0)
在阅读了有关列表和数组的更多内容后想出来。
import arcpy
from arcpy import env
import numpy
# Setup workspace
arcpy.env.workspace=r"C:\Users\mbs7038\Documents\New_Landsat_Imagery\For_Area_Calc\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'
initial=numpy.array(areaColumn,dtype=float) # convert the 1x336 list to a 1x336 array
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 array
print finalArea # print the resized array, "finalarea"
print 'saving finalarea array'
numpy.savetxt(filename,finalArea) # save "finalarea" array to a text file