import common, colors, loaders with common.runtool(7) as parameters: zoneLayer, idFld, coreQuery, colorFld, colorFile, randomizeColors, neighTable = parameters shuffle = common.toBool(randomizeColors, 'color randomization switch') if not neighTable: common.progress('computing feature neighbourhood') import conversion neighTable = conversion.generateNeighbourTableFor(zoneLayer, idFld) common.progress('loading neighbourhood') neighbourhood = loaders.NeighbourTableReader(neighTable).read() common.debug(neighbourhood) common.progress('loading color setup') chooser = colors.ColorChooser(neighbourhood, colorFile) common.progress('assigning colors') colored = chooser.colorHeuristically(shuffle=shuffle) common.progress('writing output') loaders.ColorMarker(zoneLayer, inSlots={'id' : idFld}, outSlots={'color' : colorFld}, outCallers={'color' : 'getColor'}, where=coreQuery).mark(colored) # # TODO # common.progress('creating color field') # if colorFld not in common.fieldList(zoneLayer): # arcpy.AddField_management(zoneLayer, colorFld, 'TEXT') # zoneRows = arcpy.UpdateCursor(zoneLayer, coreQuery) # zoneColors = defaultdict(str) # for row in zoneRows: # id = row.getValue(idFld) # if id in colored: # row.setValue(colorFld, colored[id].getColor()) # zoneRows.updateRow(row) # del zoneRows
common.progress('converting to neighbour table') tmpTable = pathman.tmpTable() arcpy.ConvertSpatialWeightsMatrixtoTable_stats(swm, tmpTable) fromFld, toFld = idKeeper.transform(tmpTable, [intIDFld, 'NID']) fm = arcpy.FieldMappings() fm.addFieldMap( common.fieldMap(tmpTable, fromFld, common.NEIGH_FROM_FLD, 'FIRST')) fm.addFieldMap( common.fieldMap(tmpTable, toFld, common.NEIGH_TO_FLD, 'FIRST')) if selfrel: query = common.safeQuery( "[{}] <> '{}'".format(fromFld, EXTERIOR_ID), tmpTable) else: query = common.safeQuery( "[{0}] <> [{1}] AND [{0}] <> '{2}'".format( fromFld, toFld, EXTERIOR_ID), tmpTable) arcpy.TableToTable_conversion(tmpTable, pathman.getLocation(), pathman.getOutputName(), query, fm) common.clearFields(output, [common.NEIGH_FROM_FLD, common.NEIGH_TO_FLD]) return output if __name__ == '__main__': with common.runtool(5) as parameters: zones, idFld, output, exteriorStr, selfrelStr = parameters exterior = common.toBool(exteriorStr, 'exterior relationship record switch') selfrel = common.toBool(selfrelStr, 'self-neighbourhood record switch') table(zones, idFld, output, exterior, selfrel)
jointo = pathman.tmpFC() arcpy.Merge_management([zones, erased], jointo) else: jointo = zones common.progress('finding neighbours') joined = pathman.tmpFC() fm = arcpy.FieldMappings() fm.addFieldMap(common.fieldMap(zones, idFld, common.NEIGH_FROM_FLD, 'FIRST')) fm.addFieldMap(common.fieldMap(jointo, idFld, common.NEIGH_TO_FLD, 'FIRST')) arcpy.SpatialJoin_analysis(zones, jointo, joined, 'JOIN_ONE_TO_MANY', 'KEEP_COMMON', fm, 'INTERSECT', TOLERANCE) common.progress('converting to neighbour table') fm2 = arcpy.FieldMappings() fm.addFieldMap(common.fieldMap(joined, common.NEIGH_FROM_FLD, common.NEIGH_FROM_FLD, 'FIRST')) fm.addFieldMap(common.fieldMap(joined, common.NEIGH_TO_FLD, common.NEIGH_TO_FLD, 'FIRST')) if selfrel: query = common.safeQuery("[{}] <> '{}'".format(common.NEIGH_FROM_FLD, EXTERIOR_ID), joined) else: query = common.safeQuery("[{0}] <> [{1}] AND [{0}] <> '{2}'".format( common.NEIGH_FROM_FLD, common.NEIGH_TO_FLD, EXTERIOR_ID), joined) arcpy.TableToTable_conversion(joined, pathman.getLocation(), pathman.getOutputName(), query, fm2) common.clearFields(output, [common.NEIGH_FROM_FLD, common.NEIGH_TO_FLD]) return output if __name__ == '__main__': with common.runtool(5) as parameters: zones, idFld, output, exteriorStr, selfrelStr = parameters exterior = common.toBool(exteriorStr, 'exterior relationship record switch') selfrel = common.toBool(selfrelStr, 'self-neighbourhood record switch') table(zones, idFld, output, exterior, selfrel)
import arcpy, common from networking import BulkInteractionCreator PARAM_COUNT = 13 with common.runtool(PARAM_COUNT) as parameters: places, placesIDField, network, impedance, cutoff, cutoffFld, numToFind, numToFindFld, searchDist, chosenFields, excludeSelf, location, outputName = parameters transferFieldList = common.parseFields(chosenFields) conn = BulkInteractionCreator(places, placesIDField, location, excludeSelf=common.toBool(excludeSelf, 'self-interaction switch')) conn.loadNetwork(network, impedance, cutoff=cutoff, numToFind=numToFind, cutoffFld=cutoffFld, numToFindFld=numToFindFld, searchDist=searchDist) conn.addPlaceFields(transferFieldList) conn.loadPlaces() conn.solve() conn.output(outputName, PARAM_COUNT) conn.close()
) + ')' def eligibleEnds(rels, endIDs, invert=False): sources = set() targets = set() for source in rels: if (source in endIDs) ^ invert: sources.add(source) for target in rels[source]: if (target in endIDs) ^ invert: targets.add(target) return sources, targets with common.runtool(8) as parameters: table, fromIDField, toIDField, places, placesIDField, placeQuery, invertStr, output = parameters invert = common.toBool(invertStr, 'place search inversion') common.progress('loading places') placeIDs = set(loaders.OneFieldReader(places, placesIDField, where=placeQuery).read()) if placeIDs: # hack to retrieve random element from set for id in placeIDs: break quote = isinstance(id, str) or isinstance(id, unicode) common.progress('loading interactions') interRels = loaders.NeighbourTableReader(table, {'from' : fromIDField, 'to' : toIDField}).read() common.progress('preparing selection query') sources, targets = eligibleEnds(interRels, placeIDs, invert) qry = '(' + createSelectQuery(table, fromIDField, sources, quote=quote) + ') AND (' + createSelectQuery(table, toIDField, targets, quote=quote) + ')' common.progress('selecting interactions') print table, output, qry common.select(table, common.addTableExt(output), qry) else:
import common from networking import TableConnectionCreator with common.runtool(14) as parameters: table, fromIDField, toIDField, interFields, places, placesIDField, placesFields, network, cost, searchDist, speedupStr, speedupDist, location, outName = parameters interFieldList = common.parseFields(interFields) placesFieldList = common.parseFields(placesFields) link = TableConnectionCreator(table, [fromIDField, toIDField], places, placesIDField, location) link.addLinkFields(interFieldList) link.addPlaceFields(placesFieldList) if common.toBool(speedupStr, 'geometry speedup switch'): link.speedupOn(common.toFloat(speedupDist, 'maximum geometry speedup range')) link.loadNetwork(network, cost, searchDist) link.loadPlaces() link.output(outName) link.close()
mass = 0.0 area = 0.0 for zone in zonelist: mass += zone.get('mass') area += zone.get('area') return mass / area def delimitDensityAreals(zones, idFld, popFld, thrDens, minPop, targetFld, neighTable=None, doMergeEnclaves=True): common.progress('loading areal data') loader = loaders.RegionalLoader() # common.progress('calculating zone densities') areaFld = common.ensureShapeAreaField(zones) inSlots = {'id' : idFld, 'mass' : popFld, 'area' : areaFld} loader.sourceOfZones(zones, inSlots, targetClass=DensityZone) loader.possibleNeighbourhood(neighTable, exterior=True) loader.load() zones = loader.getZoneDict() common.progress('delimiting areals') regionalize(zones, thrDens, minPop, doMergeEnclaves) common.progress('saving data') loader.addZoneOutputSlot('assign', targetFld, require=True) loader.outputZones(zones) if __name__ == '__main__': with common.runtool(8) as parameters: parameters[3] = common.toFloat(parameters[3], 'threshold density') parameters[4] = common.toFloat(parameters[4], 'minimum areal population') parameters[7] = common.toBool(parameters[7], 'enclave merge switch') # import cProfile # cProfile.run('delimitDensityAreals(*parameters)') delimitDensityAreals(*parameters)
## IMPORT MODULES import sys sys.path.append('.') import common # useful shortcut functions for arcpy and datamodel import delimit_functional_regions common.debugMode = True common.progress('loading tool parameters') # whoa, have fun with the parameters zoneLayer, \ zoneIDFld, zoneMassFld, zoneCoopFld, zoneRegFld, zoneColFld, coreQuery, \ outRegFld, doOutCoreStr, outColFld, measureFlds, \ interTable, interFromIDFld, interToIDFld, interStrFld, \ neighTable, exclaveReassignStr, oscillationStr, doSubBindStr, doCoreFirstStr, \ aggregSorterStr, threshold1Str, threshold2Str = common.parameters(23) delimit_functional_regions.main(zoneLayer, zoneIDFld, zoneMassFld, zoneCoopFld, zoneRegFld, zoneColFld, coreQuery, outRegFld, doOutCoreStr, outColFld, measureFlds, interTable, interFromIDFld, interToIDFld, interStrFld, neighTable, exclavePenalStr=('100' if common.toBool(exclaveReassignStr, 'exclave reassignment switch') else '0'), aggregSorterStr=aggregSorterStr, verifier1Str='MASS', threshold1Str=threshold1Str, verifier2Str='HINTERLAND_MASS', threshold2Str=threshold2Str, oscillationStr=oscillationStr, doSubBindStr=doSubBindStr, doCoreFirstStr=doCoreFirstStr)
popFld, thrDens, minPop, targetFld, neighTable=None, doMergeEnclaves=True): common.progress('loading areal data') loader = loaders.RegionalLoader() # common.progress('calculating zone densities') areaFld = common.ensureShapeAreaField(zones) inSlots = {'id': idFld, 'mass': popFld, 'area': areaFld} loader.sourceOfZones(zones, inSlots, targetClass=DensityZone) loader.possibleNeighbourhood(neighTable, exterior=True) loader.load() zones = loader.getZoneDict() common.progress('delimiting areals') regionalize(zones, thrDens, minPop, doMergeEnclaves) common.progress('saving data') loader.addZoneOutputSlot('assign', targetFld, require=True) loader.outputZones(zones) if __name__ == '__main__': with common.runtool(8) as parameters: parameters[3] = common.toFloat(parameters[3], 'threshold density') parameters[4] = common.toFloat(parameters[4], 'minimum areal population') parameters[7] = common.toBool(parameters[7], 'enclave merge switch') # import cProfile # cProfile.run('delimitDensityAreals(*parameters)') delimitDensityAreals(*parameters)
ASSIGNED = 'tmp_z057' with common.runtool(11) as parameters: zones, idFld, regionFld, coopFld, regTransFldsStr, nameFldsStr, sumCoreFldsStr, countCoresStr, sumAllFldsStr, countAllStr, outPath = parameters # parse field lists nameFlds = common.parseFields(nameFldsStr) regTransFlds = common.parseFields(regTransFldsStr) if regionFld in regTransFlds: regTransFlds.remove(regionFld) sumCoreFlds = common.parseFields(sumCoreFldsStr) sumAllFlds = common.parseFields(sumAllFldsStr) coreTrans = {fld : 'CORE_' + fld for fld in sumCoreFlds} allTrans = {fld : 'ALL_' + fld for fld in sumAllFlds} outSumFlds = coreTrans.values() + allTrans.values() coreTransItems = coreTrans.items() allTransItems = allTrans.items() countCores = common.toBool(countCoresStr, 'core count switch') countAll = common.toBool(countAllStr, 'zone count switch') zoneSlots = {'id' : idFld, 'assign' : regionFld} if coopFld: zoneSlots['core'] = coopFld for fld in set(nameFlds + regTransFlds + sumCoreFlds + sumAllFlds): zoneSlots[fld] = fld common.progress('reading zone data') zoneData = loaders.DictReader(zones, zoneSlots).read() print zoneData common.progress('computing region statistics') regionData = {} # TODO: pattern dict to be copied, will fasten for zoneDict in zoneData.itervalues():
import arcpy, common from networking import BulkConnectionCreator PARAM_COUNT = 13 with common.runtool(PARAM_COUNT) as parameters: places, placesIDField, network, impedance, cutoff, cutoffFld, numToFind, numToFindFld, searchDist, chosenFields, excludeSelf, location, outputName = parameters transferFieldList = common.parseFields(chosenFields) conn = BulkConnectionCreator(places, placesIDField, location, excludeSelf=common.toBool(excludeSelf, 'self-connection switch')) conn.loadNetwork(network, impedance, cutoff=cutoff, numToFind=numToFind, cutoffFld=cutoffFld, numToFindFld=numToFindFld, searchDist=searchDist) conn.addPlaceFields(transferFieldList) conn.loadPlaces() conn.solve() conn.output(outputName, PARAM_COUNT) conn.close()