NumPy Data Access Using ArcPy


In [ ]:
import arcpy as ARCPY
import arcpy.da as DA
inputFC = r'../data/CA_Polygons.shp'
tab = DA.TableToNumPyArray(inputFC, ['PCR2000', 'POP2000'])
print(tab)

SSDataObject

  1. Environment Settings (Except Extent)
  2. Bad Records
  3. Error/Warning Messages
  4. Localization
  5. Feature Accounting

    • Cursors and DataAccess are not assured to read attributes in order.

    • Keeps track of the shapes and their attributes so that one can create output features w/o post-joins.

    • Unique ID works with Spatial Weights Formats in ArcGIS, PySAL, R, Matlab, GeoDa etc..


In [ ]:
import SSDataObject as SSDO
ssdo = SSDO.SSDataObject(inputFC)
ssdo.obtainData(ssdo.oidName, ['PCR2000', 'POP2000'])
print(ssdo.fields['PCR2000'].data)

Using PANDAS to get that R Feel


In [ ]:
import pandas as PANDAS
ids = [ssdo.order2Master[i] for i in range(ssdo.numObs)]
convertDictDF = {}
for fieldName, fieldObject in ssdo.fields.items():
    convertDictDF[fieldName] = fieldObject.data
df = PANDAS.DataFrame(convertDictDF, index = ids)
print(df)

Advanced Analysis [SciPy Example - KMeans]


In [ ]:
import numpy as NUM
import scipy.cluster.vq as CLUST
import arcgisscripting as ARC
X = NUM.empty((ssdo.numObs,2), dtype = float)
X[:,0] = df['PCR2000']
X[:,1] = df['POP2000']
whiteData = CLUST.whiten(X)
centers, distortion = CLUST.kmeans(whiteData, 5)
groups = ARC._ss.closest_centroid(whiteData, centers)
print(groups)

In [ ]:
ARCPY.env.overwriteOutput = True
outputFC = r'C:\Data\UC\DevSummit15\OpenSource\output\kmeans_ca.shp'
outField = SSDO.CandidateField('SS_GROUP', 'LONG', groups)
outFields = {'SS_GROUP': outField}
ssdo.output2NewFC(outputFC, outFields, appendFields = ["NEW_NAME", "PERCNOHS"])

In [ ]: