def initialize(self): """Populates the instance of the Spatial Statistics Data Object (SSDataObject) and resolves a default distance threshold if none given. """ #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard weightsFile = self.weightsFile swmFileBool = self.swmFileBool masterField = ssdo.masterField field = ssdo.fields[varName] self.y = field.returnDouble() self.numObs = ssdo.numObs maxSet = False #### Distance Threshold #### if wType in [0, 1, 7]: if threshold == None: threshold, avgDist = WU.createThresholdDist(ssdo, concept=concept) #### Assures that the Threshold is Appropriate #### gaExtent = UTILS.get92Extent(ssdo.extent) fixed = (wType == 1) threshold, maxSet = WU.checkDistanceThreshold(ssdo, threshold, weightType=wType) #### If the Threshold is Set to the Max #### #### Set to Zero for Script Logic #### if maxSet: #### All Locations are Related #### if self.numObs > 500: ARCPY.AddIDMessage("Warning", 717) self.thresholdStr = ssdo.distanceInfo.printDistance(threshold) else: self.thresholdStr = "None" #### Set Attributes #### self.maxSet = maxSet self.threshold = threshold self.master2Order = ssdo.master2Order self.swmFileBool = swmFileBool
def initialize(self): """Populates the instance of the Spatial Statistics Data Object (SSDataObject) and resolves a default distance threshold if none given. """ #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept threshold = self.threshold exponent = self.exponent wType = self.wType rowStandard = self.rowStandard weightsFile = self.weightsFile swmFileBool = self.swmFileBool masterField = ssdo.masterField field = ssdo.fields[varName] self.y = field.returnDouble() self.numObs = ssdo.numObs maxSet = False #### Distance Threshold #### if wType in [0, 1, 7]: if threshold == None: threshold, avgDist = WU.createThresholdDist(ssdo, concept = concept) #### Assures that the Threshold is Appropriate #### gaExtent = UTILS.get92Extent(ssdo.extent) fixed = (wType == 1) threshold, maxSet = WU.checkDistanceThreshold(ssdo, threshold, weightType = wType) #### If the Threshold is Set to the Max #### #### Set to Zero for Script Logic #### if maxSet: #### All Locations are Related #### if self.numObs > 500: ARCPY.AddIDMessage("Warning", 717) self.thresholdStr = ssdo.distanceInfo.printDistance(threshold) else: self.thresholdStr = "None" #### Set Attributes #### self.maxSet = maxSet self.threshold = threshold self.master2Order = ssdo.master2Order self.swmFileBool = swmFileBool
def initialize(self): """Populates the instance of the Spatial Statistics Data Object (SSDataObject) and resolves a default distance threshold if none given. """ #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept threshold = self.threshold exponent = self.exponent wType = self.wType weightsFile = self.weightsFile swmFileBool = self.swmFileBool masterField = ssdo.masterField potentialField = self.potentialField #### Get Data Array #### field = ssdo.fields[varName] self.y = field.returnDouble() self.numObs = ssdo.numObs maxSet = False self.fieldNames = [varName] #### Distance Threshold #### if wType in [0, 1, 7]: if threshold == None: threshold, avgDist = WU.createThresholdDist(ssdo, concept=concept) #### Assures that the Threshold is Appropriate #### gaExtent = UTILS.get92Extent(ssdo.extent) fixed = (wType == 1) threshold, maxSet = WU.checkDistanceThreshold(ssdo, threshold, weightType=wType) #### If the Threshold is Set to the Max #### #### Set to Zero for Script Logic #### if maxSet: #### All Locations are Related #### if self.numObs > 500: ARCPY.AddIDMessage("WARNING", 717) #### Resolve Self Potential Field (Default to 1.0) #### if potentialField: potField = ssdo.fields[potentialField] self.potVals = potField.returnDouble() self.fieldNames.append(potentialField) #### Warn if Negative Self Weights #### sumNeg = NUM.sum(self.potVals < 0.0) if sumNeg: ARCPY.AddIDMessage("WARNING", 940) #### Set Negative Weights to Zero #### self.potVals = NUM.where(self.potVals < 0.0, 0.0, self.potVals) else: if weightsFile and not swmFileBool: self.potVals = None else: self.potVals = NUM.ones(self.numObs) #### Set Attributes #### self.maxSet = maxSet self.threshold = threshold self.master2Order = ssdo.master2Order self.swmFileBool = swmFileBool
def distance2SWM(inputFC, swmFile, masterField, fixed = 0, concept = "EUCLIDEAN", exponent = 1.0, threshold = None, kNeighs = 1, rowStandard = True): """Creates a sparse spatial weights matrix (SWM) based on k-nearest neighbors. INPUTS: inputFC (str): path to the input feature class swmFile (str): path to the SWM file. masterField (str): field in table that serves as the mapping. fixed (boolean): fixed (1) or inverse (0) distance? concept: {str, EUCLIDEAN}: EUCLIDEAN or MANHATTAN exponent {float, 1.0}: distance decay threshold {float, None}: distance threshold kNeighs (int): number of neighbors to return rowStandard {bool, True}: row standardize weights? """ #### Create SSDataObject #### ssdo = SSDO.SSDataObject(inputFC, templateFC = inputFC, useChordal = True) #### Validation of Master Field #### verifyMaster = ERROR.checkField(ssdo.allFields, masterField, types = [0,1]) #### Read Data #### ssdo.obtainDataGA(masterField, minNumObs = 2) N = ssdo.numObs gaTable = ssdo.gaTable if fixed: wType = 1 else: wType = 0 #### Set Default Progressor for Neigborhood Structure #### ARCPY.SetProgressor("default", ARCPY.GetIDMessage(84143)) #### Set the Distance Threshold #### concept, gaConcept = WU.validateDistanceMethod(concept, ssdo.spatialRef) if threshold == None: threshold, avgDist = WU.createThresholdDist(ssdo, concept = concept) #### Assures that the Threshold is Appropriate #### gaExtent = UTILS.get92Extent(ssdo.extent) threshold, maxSet = WU.checkDistanceThreshold(ssdo, threshold, weightType = wType) #### If the Threshold is Set to the Max #### #### Set to Zero for Script Logic #### if maxSet: #### All Locations are Related #### threshold = SYS.maxint if N > 500: ARCPY.AddIDMessage("Warning", 717) #### Assure k-Nearest is Less Than Number of Features #### if kNeighs >= N and fixed: ARCPY.AddIDMessage("ERROR", 975) raise SystemExit() #### Create Distance/k-Nearest Neighbor Search Type #### gaSearch = GAPY.ga_nsearch(gaTable) gaSearch.init_nearest(threshold, kNeighs, gaConcept) neighWeights = ARC._ss.NeighborWeights(gaTable, gaSearch, weight_type = wType, exponent = exponent, row_standard = False) #### Set Progressor for Weights Writing #### ARCPY.SetProgressor("step", ARCPY.GetIDMessage(84127), 0, N, 1) #### Initialize Spatial Weights Matrix File #### swmWriter = WU.SWMWriter(swmFile, masterField, ssdo.spatialRefName, N, rowStandard, inputFC = inputFC, wType = wType, distanceMethod = concept, exponent = exponent, threshold = threshold) #### Unique Master ID Dictionary #### masterDict = {} #### Unique Master ID Dictionary #### masterSet = set([]) for row in xrange(N): masterID = int(gaTable[row][2]) if masterID in masterSet: ARCPY.AddIDMessage("Error", 644, masterField) ARCPY.AddIDMessage("Error", 643) raise SystemExit() else: masterSet.add(masterID) neighs, weights = neighWeights[row] neighs = [ gaTable[nh][2] for nh in neighs ] #### Add Spatial Weights Matrix Entry #### swmWriter.swm.writeEntry(masterID, neighs, weights) #### Set Progress #### ARCPY.SetProgressorPosition() swmWriter.close() del gaTable #### Report Warning/Max Neighbors #### swmWriter.reportNeighInfo() #### Add Linear/Angular Unit (Distance Based Only) #### distanceOut = ssdo.distanceInfo.outputString distanceOut = [ARCPY.GetIDMessage(84344).format(distanceOut)] #### Report Spatial Weights Summary #### swmWriter.report(additionalInfo = distanceOut) #### Report SWM File is Large #### swmWriter.reportLargeSWM()
def initialize(self): """Populates the instance of the Spatial Statistics Data Object (SSDataObject) and resolves a default distance threshold if none given. """ #### Shorthand Attributes #### ssdo = self.ssdo varName = self.varName concept = self.concept threshold = self.threshold exponent = self.exponent wType = self.wType weightsFile = self.weightsFile swmFileBool = self.swmFileBool masterField = ssdo.masterField potentialField = self.potentialField #### Get Data Array #### field = ssdo.fields[varName] self.y = field.returnDouble() self.numObs = ssdo.numObs maxSet = False self.fieldNames = [varName] #### Distance Threshold #### if wType in [0, 1, 7]: if threshold == None: threshold, avgDist = WU.createThresholdDist(ssdo, concept=concept) #### Assures that the Threshold is Appropriate #### gaExtent = UTILS.get92Extent(ssdo.extent) fixed = wType == 1 threshold, maxSet = WU.checkDistanceThreshold(ssdo, threshold, weightType=wType) #### If the Threshold is Set to the Max #### #### Set to Zero for Script Logic #### if maxSet: #### All Locations are Related #### if self.numObs > 500: ARCPY.AddIDMessage("WARNING", 717) #### Resolve Self Potential Field (Default to 1.0) #### if potentialField: potField = ssdo.fields[potentialField] self.potVals = potField.returnDouble() self.fieldNames.append(potentialField) #### Warn if Negative Self Weights #### sumNeg = NUM.sum(self.potVals < 0.0) if sumNeg: ARCPY.AddIDMessage("WARNING", 940) #### Set Negative Weights to Zero #### self.potVals = NUM.where(self.potVals < 0.0, 0.0, self.potVals) else: if weightsFile and not swmFileBool: self.potVals = None else: self.potVals = NUM.ones(self.numObs) #### Set Attributes #### self.maxSet = maxSet self.threshold = threshold self.master2Order = ssdo.master2Order self.swmFileBool = swmFileBool