def calcMaxDensity (allPaths, limit, field): global MAXTAZ max = -1 # If there isn't a TAZ, we should just skip it gracefully. for start in LIM.getStudyArea(): paths = allPaths[start] if len(paths[start]) > 0: (num, avg, sd) = TAZ.calculateDestinationData(paths, field) if avg > max: max = avg return max
def outputDestinationsWithinLimit(allPaths, limit): global MAXTAZ # First, write out the file that contains all of the destinations # under this time limit. # This file creates a list of origin TAZs and all of the destinations # that match criteria and can be reached within the time limit. # Useful for exploration (creating sample maps) and understanding # the implications of a low (or high) travel time limit. dest = open ('destinations-within-%s.csv' % limit, 'w') # If there isn't a TAZ, we should just skip it gracefully. for start in LIM.getStudyArea(): paths = allPaths[start] if len(paths[start]) > 0: # Output the possible destinations for k, v in paths.iteritems(): dest.write("%s, %s\n" % (start, TAZ.stripBrackets(map(lambda l: l[0], v)))) dest.close()
def generateResultsTable (dbName, limit): global MAXTAZ print "Precalculating all paths in the transporation zone." counter = 0 allPaths = {} for start in LIM.getStudyArea(): paths = TAZ.getPossibleDestinations(start, limit) allPaths[start] = paths print '.', counter += 1 if counter % 10 == 0: print if counter % 100 == 0: print # Max avg. job density: 45242.506294 # Max avg. food density: 2.137048 # First, generate all the destinations within the time limit. destinationPaths = outputDestinationsWithinLimit(allPaths, limit) # Calculate maximums maxAverageJobDensity = calcMaxDensity(allPaths, limit, 'emp_dens') maxAverageFoodDensity = calcMaxDensity(allPaths, limit, 'food_store_dens') # Calculate the ranges for the rating system. # Why divide by six? It worked well for separating out the data. # Also, ArcGIS does not do well with more than 5 or 6 colors in terms # of visualizing data, so we limited our categorization to six. jobRatingDiv = maxAverageJobDensity / 6 foodRatingDiv = maxAverageFoodDensity / 6 print print "Max avg. job density: %s" % maxAverageJobDensity print "Max avg. food density: %s" % maxAverageFoodDensity print # This file holds the CSV output of our calculations. final = open ('final-%s.csv' % limit, 'w') # Write a header to the CSV file. final.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" % \ ('SrcTAZ', 'AccessRate', \ 'MedIncSrcTAZ', 'SumTotPopSrcTAZ', 'PopDensSrcTAZ', \ 'NumDest', \ 'JobNum', 'AvgJobDensAtDests', 'JobDensRating', 'SDJobDens', \ 'FoodNum', 'AvgFoodDensAtDest', 'FoodDensRating', 'SDFoodDens' \ )) # If there isn't a TAZ, we should just skip it gracefully. for start in LIM.getStudyArea(): paths = allPaths[start] if len(paths[start]) > 0: (jobNum, jobAvg, jobSD) = TAZ.calculateDestinationData(paths, 'emp_dens') (foodNum, foodAvg, foodSD) = TAZ.calculateDestinationData(paths, 'food_store_dens') (income, income_quart) = TAZ.getIncome(start) (sum_totpop, pop_dens) = TAZ.getPopDens(start) # We want a rating that is an integer value. So, we use //, which means # "do integer division". This means that 10 // 3 = 3. # However, because // does a "floor" operation (meaning, it rounds down) jobRating = int(jobAvg // jobRatingDiv) + 1 foodRating = int(foodAvg // foodRatingDiv) + 1 accessRating = jobRating + foodRating if LIM.isGoodRating(accessRating) and LIM.isIncomeLimit(income): final.write("%i,%i,%i,%i,%.2f,%i,%i,%.2f,%i,%.2f,%i,%.2f,%i,%.2f\n" % \ (start, accessRating, \ income, sum_totpop, pop_dens, \ len(paths[start]), \ jobNum, jobAvg, jobRating, jobSD, \ foodNum, foodAvg, foodRating, foodSD)) # Cleanup nicely. final.close()