def get_nogrowthmaps(urllist): """getNoGrowthLayers - downloads 0 or more shapefiles, convert them to raster and merge into the noGrowth layers To use grass GIS to import vector file requires a new folder to be created, and such folder name exist(error and fail otherwise). v.in.org Thus, each time during import, a tmp folder name is given. # To run this, a Temp directory should be created. """ runlog.p("--import nogrowth layers......") # importNoGrowth -- overlay multiple nogrowth maps into a initial blank one script = './bin/importNoGrowth' logname = './Log/' + os.path.basename(script) + '.log' createdirectorynotexist('./Temp/') shplist = [] for url in urllist: layername = get_shapefile(url, flag=1) import_vectormap(layername, layer='nogrowth') vec2rast('nogrowth') layername = os.path.basename(layername) shplist.append('"%s"' % layername) check_call('%s %s > %s 2>&1' % (script, ' '.join(shplist), logname), shell=True)
def frequencyanalysis_cost(cost_res_arr, cost_arr, cost_arr_x, RESCOM, COSTFREQ, COSTMAX, COSTBASE, CST=CST): xlen = len(cost_arr_x) cost_res_arr_nb = cost_res_arr[(cost_res_arr < COSTMAX)&(cost_res_arr > COSTBASE)] print "NUM COST " + RESCOM + " CELLS CONSIDERED: ", len(cost_res_arr_nb) cost_res_arr_nbsort = np.sort(cost_res_arr_nb) cost_res_basketsize_last = len(cost_res_arr[cost_res_arr == COSTMAX]) cost_res_freq = [] cost_arr_freq = [] cur1 = cost_arr_x[0] for i in xrange(1, xlen): #i is for cur2. in total ATTRBASKETNUM baskets. cur2 = cost_arr_x[i] mask = (cost_res_arr >= cur1) & (cost_res_arr < cur2) cost_res_freq.append(len(cost_res_arr[mask])) mask = (cost_arr >= cur1) & (cost_arr < cur2) cost_arr_freq.append(len(cost_arr[mask])) cur1 = cur2 cost_res_freq.append(len(cost_res_arr[cost_res_arr >= cur1])) cost_arr_freq.append(len(cost_arr[cost_arr >= cur1])) print "---------------------cost_"+RESCOM.lower()+"_freq----------------\n",[int(i) for i in cost_res_freq] print "---------------------cost_arr_freq----------------\n",[int(i) for i in cost_arr_freq] cost_res_y = np.divide(cost_res_freq, cost_arr_freq, dtype=np.float) cost_res_y = np.nan_to_num(cost_res_y) cost_res_y[cost_res_y > 100] = 100 print "---------------------cost_"+RESCOM.lower()+"_y----------------\n",cost_res_y outgraphfname = COSTFREQ[:-4]+"-"+str(sys.argv[2])+".png" outdatafname = COSTFREQ[:-4]+"-"+str(sys.argv[2])+".csv" createdirectorynotexist(outgraphfname) plotgraph(cost_arr_x, cost_res_y, xlen, outgraphfname, RESCOM, CST) print outdatafname outdata_arr = np.asarray([cost_arr_x, cost_res_freq, cost_arr_freq, cost_res_y]) outdata_arr = np.transpose(outdata_arr) np.savetxt(outdatafname, outdata_arr, fmt='%5.5f',delimiter=',', header="x,res/com,original,y", comments='')
def frequencyanalysis_attr(attr_res_arr, attr_arr, attr_arr_x, attrbasketsize_1st, RESCOM, ATTRFREQ, ATT=ATT, ATTRBASKETNUM=ATTRBASKETNUM, ATTRBASE=ATTRBASE): attr_res_arr_nobase = attr_res_arr[attr_res_arr > ATTRBASE] print "NUM RES CELLS CONSIDERED: ", len(attr_res_arr_nobase) attr_res_arr_nbsort = np.sort(attr_res_arr_nobase) attr_res_basketsize_1st = len(attr_res_arr[attr_res_arr == ATTRBASE]) attr_res_freq = [attr_res_basketsize_1st] attr_arr_freq = [attrbasketsize_1st] cur1 = attr_arr_x[1] for i in xrange(2, ATTRBASKETNUM+1): #i is for cur2. in total ATTRBASKETNUM baskets. cur2 = attr_arr_x[i] mask = (attr_res_arr >= cur1) & (attr_res_arr < cur2) attr_res_freq = np.append(attr_res_freq, len(attr_res_arr[mask])) mask = (attr_arr >= cur1) & (attr_arr < cur2) attr_arr_freq = np.append(attr_arr_freq, len(attr_arr[mask])) cur1 = cur2 attr_res_freq = np.append(attr_res_freq, len(attr_res_arr[attr_res_arr >= cur1])) attr_arr_freq = np.append(attr_arr_freq, len(attr_arr[attr_arr >= cur1])) print "---------------------attr_com_freq----------------\n",attr_res_freq.astype(np.int) print "---------------------attr_arr_freq----------------\n",attr_arr_freq.astype(np.int) attr_res_y = np.divide(attr_res_freq*1.0, attr_arr_freq) attr_res_y = np.nan_to_num(attr_res_y) print "---------------------attr_com_y----------------\n",attr_res_y outgraphfname = ATTRFREQ[:-4]+"-"+str(ATTRBASKETNUM)+".png" outdatafname = ATTRFREQ[:-4]+"-"+str(ATTRBASKETNUM)+".csv" createdirectorynotexist(outgraphfname) plotgraph(attr_arr_x, attr_res_y, ATTRBASKETNUM, outgraphfname, RESCOM, ATT) outdata_arr = np.asarray([attr_arr_x, attr_res_freq, attr_arr_freq, attr_res_y]) outdata_arr = np.transpose(outdata_arr) np.savetxt(outdatafname, outdata_arr,fmt='%5.5f',delimiter=',', header="x,res/com,original,y", comments='')
def frequencyanalysis_attr(attr_res_arr, attr_arr, attr_arr_x, RESCOM, ATTRFREQ, ATTRBASE, ATT=ATT): xlen = len(attr_arr_x) attr_res_arr_nb = attr_res_arr[attr_res_arr > ATTRBASE] print "NUM ATTR " + RESCOM + " CELLS CONSIDERED: ", len(attr_res_arr_nb) attr_res_arr_nbsort = np.sort(attr_res_arr_nb) attr_res_basketsize_1st = len(attr_res_arr[attr_res_arr == ATTRBASE]) attr_basketsize_1st = len(attr_arr [attr_arr == ATTRBASE]) attr_res_freq = [attr_res_basketsize_1st] attr_arr_freq = [attr_basketsize_1st] cur1 = attr_arr_x[1] for i in xrange(2, xlen): #i is for cur2. in total ATTRBASKETNUM baskets. cur2 = attr_arr_x[i] mask = (attr_res_arr > cur1) & (attr_res_arr <= cur2) attr_res_freq.append(len(attr_res_arr[mask])) mask = (attr_arr > cur1) & (attr_arr <= cur2) attr_arr_freq.append(len(attr_arr[mask])) cur1 = cur2 attr_res_freq.append(len(attr_res_arr[attr_res_arr > cur1])) attr_arr_freq.append(len(attr_arr[attr_arr > cur1])) print "---------------------attr_"+RESCOM.lower()+"_freq----------------\n",[int(i) for i in attr_res_freq] print "---------------------attr_arr_freq----------------\n",[int(i) for i in attr_arr_freq] attr_res_y = np.divide(attr_res_freq, attr_arr_freq, dtype=np.float) attr_res_y = np.nan_to_num(attr_res_y) print "---------------------attr_"+RESCOM.lower()+"_y----------------\n",attr_res_y outgraphfname = ATTRFREQ[:-4]+"-"+str(sys.argv[1])+".png" outdatafname = ATTRFREQ[:-4]+"-"+str(sys.argv[1])+".csv" createdirectorynotexist(outgraphfname) plotgraph(attr_arr_x, attr_res_y, xlen, outgraphfname, RESCOM, ATT) outdata_arr = np.asarray([attr_arr_x, attr_res_freq, attr_arr_freq, attr_res_y]) outdata_arr = np.transpose(outdata_arr) np.savetxt(outdatafname, outdata_arr,fmt='%5.5f',delimiter=',', header="x,res/com,original,y", comments='')
def frequencyanalysis_cost(cost_res_arr, cost_arr, cost_arr_x, RESCOM, COSTFREQ, CST=CST, COSTBASKETNUM=COSTBASKETNUM, COSTMAX=COSTMAX, COSTBASE=COSTBASE): cost_res_arr_nobase = cost_res_arr[(cost_res_arr < COSTMAX) & (cost_res_arr > COSTBASE)] print "NUM RES CELLS CONSIDERED: ", len(cost_res_arr_nobase) cost_res_arr_nbsort = np.sort(cost_res_arr_nobase) cost_res_basketsize_last = len(cost_res_arr[cost_res_arr == COSTMAX]) cost_res_freq = [] cost_arr_freq = [] cur1 = cost_arr_x[0] xlen = len(cost_arr_x) for i in xrange(1, xlen): #i is for cur2. in total ATTRBASKETNUM baskets. cur2 = cost_arr_x[i] mask = (cost_res_arr >= cur1) & (cost_res_arr < cur2) cost_res_freq = np.append(cost_res_freq, len(cost_res_arr[mask])) mask = (cost_arr >= cur1) & (cost_arr < cur2) cost_arr_freq = np.append(cost_arr_freq, len(cost_arr[mask])) cur1 = cur2 mask = (cost_res_arr >= cur1) & (cost_res_arr < COSTMAX) cost_res_freq = np.append(cost_res_freq, len(cost_res_arr[mask])) mask = (cost_arr >= cur1) & (cost_arr < COSTMAX) cost_arr_freq = np.append(cost_arr_freq, len(cost_arr[mask])) #cost_res_freq = np.append(cost_res_freq, cost_res_basketsize_last) #cost_arr_freq = np.append(cost_arr_freq, costbasketsize_last) print "---------------------cost_res_freq----------------\n", cost_res_freq.astype( np.int) print "---------------------cost_arr_freq----------------\n", cost_arr_freq.astype( np.int) cost_res_y = np.divide(cost_res_freq, cost_arr_freq) cost_res_y = np.nan_to_num(cost_res_y) cost_res_y[cost_res_y > 100] = 100 print "---------------------cost_res_y----------------\n", cost_res_y outgraphfname = COSTFREQ[:-4] + "-" + str(COSTBASKETNUM) + ".png" outdatafname = COSTFREQ[:-4] + "-" + str(COSTBASKETNUM) + ".csv" createdirectorynotexist(outgraphfname) plotgraph(cost_arr_x, cost_res_y, COSTBASKETNUM, outgraphfname, RESCOM, CST) print outdatafname outdata_arr = np.asarray( [cost_arr_x, cost_res_freq, cost_arr_freq, cost_res_y]) outdata_arr = np.transpose(outdata_arr) np.savetxt(outdatafname, outdata_arr, fmt='%5.5f', delimiter=',', header="x,res/com,original,y", comments='')
def _init_logger(self, name, level=logging.DEBUG, fname='./Log/run.log'): logger = logging.getLogger(name) logger.setLevel(level) createdirectorynotexist(fname) handler = logging.FileHandler(fname, mode='w') handler.setLevel(level) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) return logger
def _init_logger(self, name, level=logging.DEBUG, fname='./Log/run.log'): logger = logging.getLogger(name) logger.setLevel(level) createdirectorynotexist(fname) handler = logging.FileHandler(fname, mode='w') handler.setLevel(level) formatter = logging.Formatter( '%(asctime)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) return logger
def writeDemand(yearincrpoplist, yearincremplist, title, filename=DEMANDGRAPH): # concatenate the year, population to string to demand.graph file. outstrlist = ['# %s\n\nPopulation\n' % title] for yr, pop in yearincrpoplist: outstrlist.append('%s, %s\n' % (str(yr), str(pop))) outstrlist.append('\n\nEmployment\n') for yr, emp in yearincremplist: outstrlist.append('%s, %s\n' % (str(yr), str(emp))) createdirectorynotexist(filename) with open(filename, 'w') as f: f.write(''.join(outstrlist))
def writeDemand(yearincrpoplist, yearincremplist, title, filename=DEMANDGRAPH): # concatenate the year, population to string to demand.graph file. outstrlist = ['# %s\n\nPopulation\n'%title] for yr, pop in yearincrpoplist: outstrlist.append('%s, %s\n' % (str(yr), str(pop))) outstrlist.append('\n\nEmployment\n') for yr, emp in yearincremplist: outstrlist.append('%s, %s\n' % (str(yr), str(emp))) createdirectorynotexist(filename) with open(filename, 'w') as f: f.write(''.join(outstrlist))
def get_shapefile(url, downloaddir='./Inputs', flag = 0): """Download zipped shape file from the url to downloaddir @output: name of the shapefile with downloaddir and unzipped folder """ # Note: we currently read the entire uncompressed content of # a file into a string and then write it to file. Python 2.6 # provides a mechanssm for reading files piecemeal. try: if url.startswith('file://'): z = ZipFile(url.replace('file://','')) else: z = ZipFile(site.getURL(url)) except BadZipfile: raise RuntimeError('%s is not zip file' % url) # processes each file in the zipfile because embedded # directories are also part of the namelist we exclude # any filename ending with a trailing slash shapefile = None for zname in z.namelist(): if zname.endswith('/'): continue else: fname = os.path.basename(zname) #shapefolder = fname.split('.')[0] #print "test shapefolder" + shapefolder if flag == 0: shapefolder = fname.split('.')[0] print "test shapefolder" + shapefolder else: shapefolder = "nogrowth" print "test shapefolder" + shapefolder fname = '%s/%s/%s' %(downloaddir, shapefolder, fname) if fname.endswith('.shp'): shapefile = fname createdirectorynotexist(fname) with open(fname, 'wb') as f: content = z.read(zname) f.write(content) if not shapefile: raise RuntimeError('%s did not contain a shapefile' % url) # print shapefile name print "sharefolder is " + shapefolder print "fname is " + fname print "shapefile is" + shapefile return shapefile
def get_shapefile(url, downloaddir='./Inputs', flag=0): """Download zipped shape file from the url to downloaddir @output: name of the shapefile with downloaddir and unzipped folder """ # Note: we currently read the entire uncompressed content of # a file into a string and then write it to file. Python 2.6 # provides a mechanssm for reading files piecemeal. try: if url.startswith('file://'): z = ZipFile(url.replace('file://', '')) else: z = ZipFile(site.getURL(url)) except BadZipfile: raise RuntimeError('%s is not zip file' % url) # processes each file in the zipfile because embedded # directories are also part of the namelist we exclude # any filename ending with a trailing slash shapefile = None for zname in z.namelist(): if zname.endswith('/'): continue else: fname = os.path.basename(zname) #shapefolder = fname.split('.')[0] #print "test shapefolder" + shapefolder if flag == 0: shapefolder = fname.split('.')[0] print "test shapefolder" + shapefolder else: shapefolder = "nogrowth" print "test shapefolder" + shapefolder fname = '%s/%s/%s' % (downloaddir, shapefolder, fname) if fname.endswith('.shp'): shapefile = fname createdirectorynotexist(fname) with open(fname, 'wb') as f: content = z.read(zname) f.write(content) if not shapefile: raise RuntimeError('%s did not contain a shapefile' % url) # print shapefile name print "sharefolder is " + shapefolder print "fname is " + fname print "shapefile is" + shapefile return shapefile
def frequencyanalysis_attr(attr_res_arr, attr_arr, attr_arr_x, attrbasketsize_1st, RESCOM, ATTRFREQ, ATT=ATT, ATTRBASKETNUM=ATTRBASKETNUM, ATTRBASE=ATTRBASE): attr_res_arr_nobase = attr_res_arr[attr_res_arr > ATTRBASE] print "NUM RES CELLS CONSIDERED: ", len(attr_res_arr_nobase) attr_res_arr_nbsort = np.sort(attr_res_arr_nobase) attr_res_basketsize_1st = len(attr_res_arr[attr_res_arr == ATTRBASE]) attr_res_freq = [attr_res_basketsize_1st] attr_arr_freq = [attrbasketsize_1st] cur1 = attr_arr_x[1] for i in xrange(2, ATTRBASKETNUM + 1): #i is for cur2. in total ATTRBASKETNUM baskets. cur2 = attr_arr_x[i] mask = (attr_res_arr >= cur1) & (attr_res_arr < cur2) attr_res_freq = np.append(attr_res_freq, len(attr_res_arr[mask])) mask = (attr_arr >= cur1) & (attr_arr < cur2) attr_arr_freq = np.append(attr_arr_freq, len(attr_arr[mask])) cur1 = cur2 attr_res_freq = np.append(attr_res_freq, len(attr_res_arr[attr_res_arr >= cur1])) attr_arr_freq = np.append(attr_arr_freq, len(attr_arr[attr_arr >= cur1])) print "---------------------attr_com_freq----------------\n", attr_res_freq.astype( np.int) print "---------------------attr_arr_freq----------------\n", attr_arr_freq.astype( np.int) attr_res_y = np.divide(attr_res_freq * 1.0, attr_arr_freq) attr_res_y = np.nan_to_num(attr_res_y) print "---------------------attr_com_y----------------\n", attr_res_y outgraphfname = ATTRFREQ[:-4] + "-" + str(ATTRBASKETNUM) + ".png" outdatafname = ATTRFREQ[:-4] + "-" + str(ATTRBASKETNUM) + ".csv" createdirectorynotexist(outgraphfname) plotgraph(attr_arr_x, attr_res_y, ATTRBASKETNUM, outgraphfname, RESCOM, ATT) outdata_arr = np.asarray( [attr_arr_x, attr_res_freq, attr_arr_freq, attr_res_y]) outdata_arr = np.transpose(outdata_arr) np.savetxt(outdatafname, outdata_arr, fmt='%5.5f', delimiter=',', header="x,res/com,original,y", comments='')
def main(argv): start = time.time() cellnum = int(sys.argv[1]) if cellnum >= 100: print "Error: the cellnum choice should be less than 100" exit(0) # read only one popcenter specified by cellnum from popcenterlist with open(CENTERLIST, 'r') as p: popcenterlist = p.readlines() (disW, disN, weight) = popcenterlist[cellnum].strip('\n').split(',') print disW, disN, weight # redirect stdout to log file logname = TRAVELCOSTPATH + "/cell_" + disW + "_" + disN + "/log.txt" createdirectorynotexist(logname) sys.stdout = open(logname, 'w') RandomWalk(int(disW), int(disN)) #distW, distN end = time.time() print(end - start)
def main(argv): start = time.time() cellnum = int(sys.argv[1]) if cellnum >= 100: print "Error: the cellnum choice should be less than 100" exit(0) # read only one popcenter specified by cellnum from popcenterlist with open(CENTERLIST, 'r') as p: popcenterlist = p.readlines() (disW, disN, weight) = popcenterlist[cellnum].strip('\n').split(',') print disW, disN, weight # redirect stdout to log file logname = TRAVELCOSTPATH+"/cell_" + disW + "_" + disN + "/log.txt" createdirectorynotexist(logname) sys.stdout = open(logname, 'w') RandomWalk(int(disW),int(disN)) #distW, distN end = time.time() print (end-start)
def frequencyanalysis_cost(cost_res_arr, cost_arr, cost_arr_x, RESCOM, COSTFREQ, CST=CST, COSTBASKETNUM=COSTBASKETNUM, COSTMAX=COSTMAX, COSTBASE=COSTBASE): cost_res_arr_nobase = cost_res_arr[(cost_res_arr < COSTMAX)&(cost_res_arr > COSTBASE)] print "NUM RES CELLS CONSIDERED: ", len(cost_res_arr_nobase) cost_res_arr_nbsort = np.sort(cost_res_arr_nobase) cost_res_basketsize_last = len(cost_res_arr[cost_res_arr == COSTMAX]) cost_res_freq = [] cost_arr_freq = [] cur1 = cost_arr_x[0] xlen = len(cost_arr_x) for i in xrange(1, xlen): #i is for cur2. in total ATTRBASKETNUM baskets. cur2 = cost_arr_x[i] mask = (cost_res_arr >= cur1) & (cost_res_arr < cur2) cost_res_freq = np.append(cost_res_freq, len(cost_res_arr[mask])) mask = (cost_arr >= cur1) & (cost_arr < cur2) cost_arr_freq = np.append(cost_arr_freq, len(cost_arr[mask])) cur1 = cur2 mask = (cost_res_arr >= cur1) & (cost_res_arr < COSTMAX) cost_res_freq = np.append(cost_res_freq, len(cost_res_arr[mask])) mask = (cost_arr >= cur1) & (cost_arr < COSTMAX) cost_arr_freq = np.append(cost_arr_freq, len(cost_arr[mask])) #cost_res_freq = np.append(cost_res_freq, cost_res_basketsize_last) #cost_arr_freq = np.append(cost_arr_freq, costbasketsize_last) print "---------------------cost_res_freq----------------\n",cost_res_freq.astype(np.int) print "---------------------cost_arr_freq----------------\n",cost_arr_freq.astype(np.int) cost_res_y = np.divide(cost_res_freq, cost_arr_freq) cost_res_y = np.nan_to_num(cost_res_y) cost_res_y[cost_res_y > 100] = 100 print "---------------------cost_res_y----------------\n",cost_res_y outgraphfname = COSTFREQ[:-4]+"-"+str(COSTBASKETNUM)+".png" outdatafname = COSTFREQ[:-4]+"-"+str(COSTBASKETNUM)+".csv" createdirectorynotexist(outgraphfname) plotgraph(cost_arr_x, cost_res_y, COSTBASKETNUM, outgraphfname, RESCOM, CST) print outdatafname outdata_arr = np.asarray([cost_arr_x, cost_res_freq, cost_arr_freq, cost_res_y]) outdata_arr = np.transpose(outdata_arr) np.savetxt(outdatafname, outdata_arr, fmt='%5.5f',delimiter=',', header="x,res/com,original,y", comments='')
def get_nogrowthmaps(urllist): """getNoGrowthLayers - downloads 0 or more shapefiles, convert them to raster and merge into the noGrowth layers To use grass GIS to import vector file requires a new folder to be created, and such folder name exist(error and fail otherwise). v.in.org Thus, each time during import, a tmp folder name is given. # To run this, a Temp directory should be created. """ runlog.p("--import nogrowth layers......") script = './bin/importNoGrowth' logname = './Log/'+ os.path.basename(script)+'.log' createdirectorynotexist('./Temp/') shplist = [] for url in urllist: layername = get_shapefile(url, flag=1) layername = os.path.basename(layername) shplist.append('"%s"' % layername) check_call('%s %s > %s 2>&1' % (script, ' '.join(shplist), logname), shell=True)