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)

"""
Explanation: NumPy Data Access Using ArcPy
End of explanation
"""


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)


"""
Explanation: SSDataObject

Environment Settings (Except Extent)
Bad Records
Error/Warning Messages
Localization
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..
End of explanation
"""


import pandas as PANDAS
df = ssdo.getDataFrame()
print(df)

"""
Explanation: Using PANDAS to get that R Feel
End of explanation
"""


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)

"""
Explanation: Advanced Analysis [SciPy Example - KMeans]
End of explanation
"""


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

"""
Explanation: Max-P Regions Using PySAL
End of explanation
"""


import Partition as PART
skater = PART.Partition(ssdo, fieldNames, spaceConcept = "GET_SPATIAL_WEIGHTS_FROM_FILE",
                        weightsFile = swmFile, kPartitions = 6)
print(skater.partition)

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)

"""
Explanation: SKATER for Comparison
End of explanation
"""


