#!/usr/bin/env python # coding: utf-8 # # NumPy Data Access Using ArcPy # In[1]: import arcpy as ARCPY import arcpy.da as DA inputFC = r'../data/CA_Polygons.shp' fieldNames = ['PCR2000', 'POP2000', 'PERCNOHS'] tab = DA.TableToNumPyArray(inputFC, fieldNames) 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[38]: import SSDataObject as SSDO import os as OS inputFC = r'../data/CA_Polygons.shp' fullFC = OS.path.abspath(inputFC) fullPath, fcName = OS.path.split(fullFC) ssdo = SSDO.SSDataObject(inputFC) uniqueIDField = "MYID" fieldNames = ['PCR2010', 'POP2010', 'PERCNOHS'] ssdo.obtainData(uniqueIDField, fieldNames) ssdo = SSDO.SSDataObject(inputFC) ssdo.obtainData("MYID", fieldNames) print(ssdo.fields['POP2010'].data) # # Using PANDAS to get that R Feel # In[39]: import pandas as PANDAS df = ssdo.getDataFrame() print(df) # # Advanced Analysis [SciPy Example - KMeans] # In[40]: import numpy as NUM import scipy.cluster.vq as CLUST import arcgisscripting as ARC X = df.as_matrix() whiteData = CLUST.whiten(X) centers, distortion = CLUST.kmeans(whiteData, 6) groups = ARC._ss.closest_centroid(whiteData, centers) print(groups) # # Max-P Regions Using PySAL # In[41]: import pysal as PYSAL import pysal2ArcGIS as PYSAL_UTILS swmFile = OS.path.join(fullPath, "rook_bin.swm") w = PYSAL_UTILS.swm2Weights(ssdo, swmFile) maxp = PYSAL.region.Maxp(w, X[:,0:2], 3000000., floor_variable = X[:,2]) maxpGroups = NUM.empty((ssdo.numObs,), int) for regionID, orderIDs in enumerate(maxp.regions): maxpGroups[orderIDs] = regionID maxpGroups # # SKATER for Comparison # In[42]: import Partition as PART skater = PART.Partition(ssdo, fieldNames, spaceConcept = "GET_SPATIAL_WEIGHTS_FROM_FILE", weightsFile = swmFile, kPartitions = 6) print(skater.partition) # In[43]: ARCPY.env.overwriteOutput = True outputFC = r'../data/cluster_output.shp' outK = SSDO.CandidateField('KMEANS', 'LONG', groups + 1) outMax = SSDO.CandidateField('MAXP', 'LONG', maxpGroups + 1) outSKATER = SSDO.CandidateField('SKATER', 'LONG', skater.partitionOutput) outFields = {'KMEANS': outK, 'MAXP': outMax, 'SKATER': outSKATER} appendFields = fieldNames + ["NEW_NAME"] ssdo.output2NewFC(outputFC, outFields, appendFields = appendFields) # In[ ]: