Wednesday, 15 February 2012

python - how to manipulate numpy arrays for use with ESRI's arcpy.da.NumPyArrayToTable -



python - how to manipulate numpy arrays for use with ESRI's arcpy.da.NumPyArrayToTable -

esri gives access moving info tables arrays , back. have script takes census info api phone call , converts arrays, simple math, , then, ideally, puts out table. math, array cannot rec array. no combination of vstack, hstack, or concatenate seemed give result. resorted creating individual 1-d arrays recarrays, , using merge function in np.lib.recfunctions.merge_arrays. certainly there improve way.

esri's homecoming tabletonumpyarray:

>>> testarray array([ (41039000100.0, 2628.0, 100.0, 2339.0, 135.0, 18.0, 22.0, 16.0, 25.0, 0.0, 92.0, 0.0, 92.0, 0.0, 92.0, 0.0, 92.0, 6.0, 9.0, 249.0, 90.0, 0.0, 92.0, 1, u'41039000100'), ... dtype=[('geo_id', '<f8'), ('totalunits', '<f8'), ('moe_total', '<f8'), >('total_1_detached', '<f8'), ('moe_total_1_detached', '<f8'), ('total_1_attached', >'<f8'), ('moe_total_1_attached', '<f8'), ('total_2', '<f8'), ('moe_total_2', '<f8'), >('total_3_or_4', '<f8'), ('moe_total_3_or_4', '<f8'), ('total_5_to_9', '<f8'), >('moe_total_5_to_9', '<f8'), ('total_10_to_19', '<f8'), ('moe_total_10_to_19', '<f8'), >('total_20_to_49', '<f8'), ('moe_total_20_to_49', '<f8'), ('total_50_or_more', '<f8'), >('moe_total_50_or_more', '<f8'), ('total_mobile_home', '<f8'), ('moe_total_mobile_home', '<f8'), ('total_boat_rv_van_etc', '<f8'), ('moe_total_boat_rv_van_etc', '<f8'), >('objectid', '<i4'), ('geo_id_t', '<u50')])

my snippet of code looks

try: # assign geo_id array geo_id_array = b25008_001e_array[...,0] tpop_array = b25008_001e_array[...,1] tunits_array = b25024_001e_array[...,1] # split sero possible real rowns , definite end-of-file # tract, convert nan's in hhsize_array zero's nan_to_num # hhsize_array = tpop_array.view(np.float32)/tunits_array.view(np.float32) hhsize_array = tpop_array/tunits_array hhsize_array = nan_to_num(hhsize_array) # table_array = array(vstack((geo_id_array, tpop_array, tunits_array, hhsize_array)), dtype = ([('geo_id', '|s13'), ('tpop', np.int32), ('tunits_array', np.int32), ('hhsize', np.float32)])) # table_array = np.hstack((geo_id_array, tpop_array, tunits_array, hhsize_array)) geo_id_recarray = np.array(geo_id_array, dtype = ([('geo_id', '|s13')])) tpop_recarray = np.array(tpop_array, dtype = ([('tpop', np.int32)])) tunits_recarray = np.array(tunits_array, dtype = ([('tunits_array', np.int32)])) hhsize_recarray = np.array(hhsize_array, dtype = ([('hhsize', np.float32)])) arrays = [geo_id_recarray, tpop_recarray, tunits_recarray, hhsize_recarray] mergedarray = np.lib.recfunctions.merge_arrays(arrays, usemask=false) print print except exception e: # if error occurred, print line number , error message import traceback, sys tb = sys.exc_info()[2] print "an error occured on line %i" % tb.tb_lineno print str(e)

i'd prefer merge/join/stack arrays before structuring them, think. thoughts?

you should able utilize structured arrays (technically you're not using recarrays) "simple math." i'm not sure if you're showing math you'd do, illustration if want do:

hhsize_array = tpop_array/tunits_array

but don't want have separate arrays, math on views of main (merged array), let's phone call data:

data['hhsize'] = data['tpop']/data['tunits']

where hhsize, tpop, , tunits field names in 1 structured array called data, such you'd have

>>> data.dtype dtype([('geo_id', '|s13'), ('tpop', np.int32), ('tunits_array', np.int32), ('hhsize', np.float32)])

python numpy arcgis arcpy

No comments:

Post a Comment