def startStates(tempList, resolution, outFolder): try: gp = arcgisscripting.create(9.3) gp.overwriteOutput = True stateList = [table.strip("\'") for table in tempList] for state in stateList: gp.AddMessage(state) infeatures = "//REMOVE/REMOVE/LULCTrends/Vectors/States/states_generalized_Albers.shp" test = r'in_memory\teststates' for state in stateList: stateabbrev = 'usa_' + state.replace(" ", "", 10) if AnalysisNames.isInAnalysisNames(gp, stateabbrev): deleteAnalysisName.deleteAnalysisName(stateabbrev) AnalysisNames.updateAnalysisNames(gp, stateabbrev) analysisNum = AnalysisNames.getAnalysisNum(gp, stateabbrev) gp.AddMessage("startStratified: state = " + state + " analysisNum = " + str(analysisNum)) #create a folder for the results subFolder = os.path.join(outFolder, state) if not os.path.isdir(subFolder): os.mkdir(subFolder) where_clause = "NAME10 = " + "'" + state + "'" gp.Select_analysis(infeatures, test, where_clause) newEcos, strats, splits = CustomDataAccess.accessCustomData( test, resolution, stateabbrev, subFolder) if len(newEcos) > 0: testStudyAreaStats.buildStudyAreaStats(stateabbrev, analysisNum, newEcos, strats, splits) for eco in newEcos: newExcel = CustomWorkbook(gp, stateabbrev, analysisNum, resolution, subFolder, eco) newExcel.build_workbook(gp) newExcel = SummaryWorkbook(gp, stateabbrev, analysisNum, resolution, subFolder) newExcel.build_workbook(gp) del newExcel else: deleteAnalysisName.deleteAnalysisName(stateabbrev) if gp.Exists(test): gp.delete_management(test) except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())
def makeExcelFiles(runType, ecoString, runName, resolution, outFolder): try: gp = arcgisscripting.create(9.3) gp.OverwriteOutput = True #if any eco numbers entered in ecoString, clean up and # break into a list ecoList = TrendsUtilities.parseNumberList(gp, ecoString) ecoList.sort() #Check for valid analysis name if not AnalysisNames.isInAnalysisNames(gp, runName): gp.AddWarning( "No database information stored for that analysis name") raise TrendsUtilities.JustExit() #check for valid combination of analysis name, type and ecolist if runType == 'Trends': runNum = TrendsNames.TrendsNum if not len(ecoList): gp.AddWarning( "No ecoregions were selected for Trends file generation.") raise TrendsUtilities.JustExit() elif runType == 'Custom': if not len(ecoList): gp.AddWarning( "No ecoregions were selected for custom file generation.") raise TrendsUtilities.JustExit() runNum = AnalysisNames.getAnalysisNum(gp, runName) else: #summary if len(ecoList): gp.AddWarning( "Ecoregion numbers are ignored for summary file generation." ) del ecoList ecoList = [0] runNum = AnalysisNames.getAnalysisNum(gp, runName) #Generate the appropriate excel file(s) for eco in ecoList: if runType == 'Trends': newExcel = TrendsWorkbook(gp, runName, runNum, resolution, outFolder, eco) elif runType == 'Custom': newExcel = CustomWorkbook(gp, runName, runNum, resolution, outFolder, eco) else: #summary newExcel = SummaryWorkbook(gp, runName, runNum, resolution, outFolder) newExcel.build_workbook(gp) del newExcel except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())
def startLCCs(tempList, resolution, outFolder): try: gp = arcgisscripting.create(9.3) gp.overwriteOutput = True lccList = [table.strip("\'") for table in tempList] for lcc in lccList: gp.AddMessage(lcc) infeatures = "//REMOVE/REMOVE/LULCTrends/Vectors/LCCs/LCC_areas_US_alb_areas.shp" test = r'in_memory\testlccs' for lcc in lccList: lccname = 'lcc_' + lcc if AnalysisNames.isInAnalysisNames(gp, lccname): deleteAnalysisName.deleteAnalysisName(lccname) AnalysisNames.updateAnalysisNames(gp, lccname) analysisNum = AnalysisNames.getAnalysisNum(gp, lccname) gp.AddMessage("startStratified: lcc = " + lccname + " analysisNum = " + str(analysisNum)) subfolder = os.path.join(outFolder, lccname) if not os.path.isdir(subfolder): os.mkdir(subfolder) where_clause = "Area_Num = " + str( lccnamelist[lcc.encode('utf-8')]) gp.Select_analysis(infeatures, test, where_clause) newEcos, strats, splits = CustomDataAccess.accessCustomData( test, resolution, lccname, subfolder) if len(newEcos) > 0: testStudyAreaStats.buildStudyAreaStats(lccname, analysisNum, newEcos, strats, splits) for eco in newEcos: newExcel = CustomWorkbook(gp, lccname, analysisNum, resolution, subfolder, eco) newExcel.build_workbook(gp) newExcel = SummaryWorkbook(gp, lccname, analysisNum, resolution, subfolder) newExcel.build_workbook(gp) del newExcel else: deleteAnalysisName.deleteAnalysisName(lccname) if gp.Exists(test): gp.delete_management(test) except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())
def startBoundaries(infolder, prefix, resolution, outFolder, deleteResults): try: gp = arcgisscripting.create(9.3) gp.overwriteOutput = True os.chdir(infolder) fileList = glob.glob("*.shp") for boundary in fileList: temp = boundary.split(".") newname = prefix + "_" + temp[0] if AnalysisNames.isInAnalysisNames(gp, newname): deleteAnalysisName.deleteAnalysisName(newname) AnalysisNames.updateAnalysisNames(gp, newname) analysisNum = AnalysisNames.getAnalysisNum(gp, newname) gp.AddMessage("startStratified: boundary = " + boundary + " and analysis name = " + newname) subFolder = os.path.join(outFolder, newname) if not os.path.isdir(subFolder): os.mkdir(subFolder) newEcos, strats, splits = CustomDataAccess.accessCustomData( boundary, resolution, newname, subFolder) if len(newEcos) > 0: testStudyAreaStats.buildStudyAreaStats(newname, analysisNum, newEcos, strats, splits) for eco in newEcos: newExcel = CustomWorkbook(gp, newname, analysisNum, resolution, subFolder, eco) newExcel.build_workbook(gp) newExcel = SummaryWorkbook(gp, newname, analysisNum, resolution, subFolder) newExcel.build_workbook(gp) del newExcel else: gp.AddError( "No sample blocks are within the custom boundary " + boundary + ". No Trends processing done.") deleteResults = 'Yes' if deleteResults == "Yes": #clean out name from database deleteAnalysisName.deleteAnalysisName(newname) except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())
def startJust3s(ecoString, resolution, outFolder): try: gp = arcgisscripting.create(9.3) runName = 'Trends' ecoList = TrendsUtilities.parseNumberList(gp, ecoString) ecoList.sort() #check for invalid entries in ecolist if ecoList[0] < 1 or ecoList[-1] > 84: gp.AddError( "Invalid ecoregion number. Numbers must be between 1 and 84.") raise TrendsUtilities.JustExit() analysisNum = AnalysisNames.getAnalysisNum(gp, runName) #Get block counts for each ecoregion and start processing newEcos, strats, splits = TrendsDataAccess.accessTrendsData( ecoList, analysisNum, resolution) testStudyAreaStats.buildStudyAreaStats(runName, analysisNum, newEcos, strats, splits) for eco in ecoList: newExcel = TrendsWorkbook(gp, runName, analysisNum, resolution, outFolder, eco) newExcel.build_workbook(gp) del newExcel except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())
def startOneSummary(ecoString, newname, resolution, outFolder): try: gp = arcgisscripting.create(9.3) gp.overwriteOutput = True #create a file to log failures for tool return statusfile = False try: statusfile = open( os.path.join( outFolder, "analysis_log.txt"), 'w') except Exception: pass ecoList = TrendsUtilities.parseNumberList( gp, ecoString ) ecoList.sort() #check for invalid entries in ecolist if ecoList: if ecoList[0] < 1 or ecoList[-1] > 84: msg = "Invalid ecoregion number. Numbers must be between 1 and 84." if statusfile: statusfile.write( msg + "\n" ) gp.AddError(msg) raise TrendsUtilities.JustExit() if len(ecoList) < 2: msg = "Ecoregion list for " +newname+ " does not contain at least 2 ecoregions. No summary can be done." if statusfile: statusfile.write( msg + "\n" ) gp.AddError(msg) raise TrendsUtilities.JustExit() if AnalysisNames.isInAnalysisNames( gp, newname): msg = "The name \'" + newname + "\' is already in use in the database. Please choose another." if statusfile: statusfile.write( msg + "\n" ) gp.AddError(msg) raise TrendsUtilities.JustExit AnalysisNames.updateAnalysisNames( gp, newname ) analysisNum = AnalysisNames.getAnalysisNum( gp, newname ) newEcos, strats, splits = TrendsDataAccess.accessTrendsData( ecoList, analysisNum, resolution ) testStudyAreaStats.buildStudyAreaStats( newname, analysisNum, newEcos, strats, splits ) newExcel = SummaryWorkbook( gp, newname, analysisNum, resolution, outFolder ) newExcel.build_workbook( gp ) del newExcel #clean out name from database deleteAnalysisName.deleteAnalysisName( newname ) statusfile.write("Create Summary Analysis tool execution is complete.\n") except arcgisscripting.ExecuteError: # Get the geoprocessing error messages msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) errmsg = "The summary analysis tool failed with the following ArcGIS error:\n" if statusfile: statusfile.write(errmsg + msgs + "\n") except TrendsUtilities.JustExit: pass except Exception: pymsg = traceback.format_exc() gp.AddMessage(pymsg) errmsg = "The summary analysis tool failed with the following Python error:\n" if statusfile: statusfile.write(errmsg + pymsg + "\n") finally: try: if statusfile: statusfile.close() except Exception: pass
def startStatusTrends(tempList, resolution, outFolder): try: gp = arcgisscripting.create(9.3) gp.OverwriteOutput = True tableList = [table.strip("\'") for table in tempList] for table in tableList: gp.AddMessage(table) for sumname in tableList: summary = "st_" + sumname #Delete old summary data from tables before creating new data if AnalysisNames.isInAnalysisNames(gp, summary): deleteAnalysisName.deleteAnalysisName(summary) ecoList = [] #Get the ecoregion numbers for each summary name in the list if sumname == "AllUS": ecoList = [x for x in range(1, 85)] #want list of ecos 1-84 elif sumname == "Western": ecoList = [x for x in range(1, 25)] + [41, 77, 78, 79, 80, 81] elif sumname == "GreatPlains": ecoList = [x for x in range(25, 35) ] + [40, 42, 43, 44, 46, 47, 48] elif sumname == "MidwestSouthCentral": ecoList = [35, 36, 37, 38, 39] + [x for x in range(49, 58) ] + [61, 72, 73] elif sumname == "Eastern": ecoList = [45, 58, 59, 60] + [x for x in range(62, 72) ] + [74, 75, 76, 82, 83, 84] elif sumname == "NorthGreatPlains": ecoList = [42, 43, 46, 48] else: gp.AddError("Unknown summary name") AnalysisNames.updateAnalysisNames(gp, summary) analysisNum = AnalysisNames.getAnalysisNum(gp, summary) gp.AddMessage("startStratified: summary = " + summary + " analysisNum = " + str(analysisNum)) #Get the ecoregions and samples for the new summary newEcos, strats, splits = TrendsDataAccess.accessTrendsData( ecoList, analysisNum, resolution) #Start the Trends processing path = os.path.dirname(sys.argv[0]) testStudyAreaStats.buildStudyAreaStats(summary, analysisNum, newEcos, strats, splits) newExcel = SummaryWorkbook(gp, summary, analysisNum, resolution, outFolder) newExcel.build_workbook(gp) del newExcel except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddError(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())
def startOneCustom(Input_Features, newname, resolution, outFolder): try: gp = arcgisscripting.create(9.3) gp.overwriteOutput = True #create a file to log failures for tool return statusfile = False try: statusfile = open(os.path.join(outFolder, "analysis_log.txt"), 'w') except Exception: pass if AnalysisNames.isInAnalysisNames(gp, newname): msg = "The name \'" + newname + "\' is already in use. Please choose another." if statusfile: statusfile.write(msg + "\n") gp.AddError(msg) raise TrendsUtilities.JustExit AnalysisNames.updateAnalysisNames(gp, newname) analysisNum = AnalysisNames.getAnalysisNum(gp, newname) #create a folder for the results subFolder = os.path.join(outFolder, newname) if not os.path.isdir(subFolder): os.mkdir(subFolder) newEcos, strats, splits = CustomDataAccess.accessCustomData( Input_Features, resolution, newname, subFolder) if len(newEcos) > 0: testStudyAreaStats.buildStudyAreaStats(newname, analysisNum, newEcos, strats, splits) for eco in newEcos: newExcel = CustomWorkbook(gp, newname, analysisNum, resolution, subFolder, eco) newExcel.build_workbook(gp) newExcel = SummaryWorkbook(gp, newname, analysisNum, resolution, subFolder) newExcel.build_workbook(gp) del newExcel else: msg = "No sample blocks are within the custom boundary. No Trends processing done." if statusfile: statusfile.write(msg + "\n") gp.AddError(msg) #clean out name from database deleteAnalysisName.deleteAnalysisName(newname) statusfile.write( "Create Custom Analysis tool execution is complete.\n") except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) errmsg = "The summary analysis tool failed with the following ArcGIS error:\n" if statusfile: statusfile.write(errmsg + msgs + "\n") except TrendsUtilities.JustExit: pass except Exception: pymsg = traceback.format_exc() gp.AddMessage(pymsg) errmsg = "The summary analysis tool failed with the following Python error:\n" if statusfile: statusfile.write(errmsg + pymsg + "\n") finally: try: if statusfile: statusfile.close() except Exception: pass
for eco in ecoList: if eco < 1 or eco > 84: gp.AddError( "Invalid ecoregion number. Numbers must be between 1 and 84." ) raise TrendsUtilities.JustExit() if runName[0].isdigit(): gp.AddError("Analysis name must begin with a letter") raise TrendsUtilities.JustExit() if len(runName) > 25: gp.AddError("Maximum length for analysis name is 25 characters") raise TrendsUtilities.JustExit() found = AnalysisNames.isInAnalysisNames(gp, runName) if found and runName.upper() != TrendsNames.name: gp.AddError( "Analysis name is already in use. To use this name again, delete previous results from database." ) raise TrendsUtilities.JustExit() else: AnalysisNames.updateAnalysisNames(gp, runName) analysisNum = AnalysisNames.getAnalysisNum(gp, runName) if runName.upper() == TrendsNames.name: gp.AddWarning( "The Trends run name causes a refresh of data and statistics in the database for the given ecoregions." ) gp.AddWarning("No summary statistics will be generated.")
def startFromFile(filename, resolution, outFolder, deleteResults): try: gp = arcgisscripting.create(9.3) gp.overwriteOutput = True try: summaryfile = open(filename, 'r') contents = summaryfile.readlines() summaryfile.close() except: gp.AddError("Unable to access summary list file") raise Exception numlines = len(contents) for line in range(0, numlines, 2): newname = contents[line].rstrip() ecoString = contents[line + 1].rstrip() ecoList = TrendsUtilities.parseNumberList(gp, ecoString) ecoList.sort() if len(ecoList) < 2: gp.AddError( "Ecoregion list for " + newname + " does not contain at least 2 ecoregions. No summary can be done." ) continue if ecoList[0] < 1 or ecoList[-1] > 84: gp.AddError( "Ecoregion list for " + newname + " contains invalid values. Accepted range is 1-84.") continue if AnalysisNames.isInAnalysisNames(gp, newname): deleteAnalysisName.deleteAnalysisName(newname) AnalysisNames.updateAnalysisNames(gp, newname) analysisNum = AnalysisNames.getAnalysisNum(gp, newname) gp.AddMessage("starting summary: analysis name = " + newname) newEcos, strats, splits = TrendsDataAccess.accessTrendsData( ecoList, analysisNum, resolution) testStudyAreaStats.buildStudyAreaStats(newname, analysisNum, newEcos, strats, splits) newExcel = SummaryWorkbook(gp, newname, analysisNum, resolution, outFolder) newExcel.build_workbook(gp) del newExcel if deleteResults == "Yes": #clean out name from database deleteAnalysisName.deleteAnalysisName(newname) except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddMessage(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())
def startScenarios(tempList, resolution, outFolder): try: gp = arcgisscripting.create(9.3) gp.OverwriteOutput = True tableList = [table.strip("\'") for table in tempList] for table in tableList: gp.AddMessage(table) Hier1 = TrendsNames.dbLocation + "EcoregionHierarchy" Eco1 = TrendsNames.dbLocation + "Level1Ecoregions" Eco2 = TrendsNames.dbLocation + "Level2Ecoregions" for sumname in tableList: summary = "sm_" + sumname #Delete old summary data from tables before creating new data if AnalysisNames.isInAnalysisNames(gp, summary): deleteAnalysisName.deleteAnalysisName(summary) ecoList = [] #Get the ecoregion numbers for each summary name in the list if sumname in [ "WESTERNMOUNTAINSFORESTS", "EASTERNUS", "GREATPLAINS", "WESTERNARID" ]: where_clause = "AnalysisName = \'" + sumname + "\'" rows = gp.SearchCursor(Eco1, where_clause) row = rows.Next() eco1 = row.EcoLevel1ID gp.AddMessage("EcoLevel1ID = " + str(eco1)) #now get the level 3s for this level 1 where_clause = "EcoLevel1ID = " + str(eco1) rows = gp.SearchCursor(Hier1, where_clause) row = rows.Next() while row: ecoList.append(int(row.Ecolevel3ID)) gp.AddMessage("adding eco " + str(ecoList[-1]) + " to ecoList") row = rows.Next() else: where_clause = "AnalysisName = \'" + sumname + "\'" rows = gp.SearchCursor(Eco2, where_clause) row = rows.Next() eco2 = row.EcoLevel2ID gp.AddMessage("EcoLevel2ID = " + str(eco2)) #now get the level 3s for this level 2 where_clause = "EcoLevel2ID = " + str(eco2) rows = gp.SearchCursor(Hier1, where_clause) row = rows.Next() while row: ecoList.append(int(row.Ecolevel3ID)) gp.AddMessage("adding eco " + str(ecoList[-1]) + " to ecoList") row = rows.Next() AnalysisNames.updateAnalysisNames(gp, summary) analysisNum = AnalysisNames.getAnalysisNum(gp, summary) gp.AddMessage("startStratified: summary = " + summary + " analysisNum = " + str(analysisNum)) #Get the intermediate tables ready for the new summary newEcos, strats, splits = TrendsDataAccess.accessTrendsData( ecoList, analysisNum, resolution) #Start the Trends processing path = os.path.dirname(sys.argv[0]) testStudyAreaStats.buildStudyAreaStats(summary, analysisNum, newEcos, strats, splits) newExcel = SummaryWorkbook(gp, summary, analysisNum, resolution, outFolder) newExcel.build_workbook(gp) del newExcel except arcgisscripting.ExecuteError: msgs = gp.GetMessage(0) msgs += gp.GetMessages(2) gp.AddError(msgs) except TrendsUtilities.JustExit: pass except Exception: gp.AddMessage(traceback.format_exc())