How to organize a Python GIS-project with multiple analysis steps? -
i started use arcpy analyse geo-data arcgis. analysis has different steps, executed 1 after other.
here pseudo-code:
import arcpy # create masking variable mask1 = "mask.shp" # create list of raster files files_to_process = ["raster1.tif", "raster2.tif", "raster3.tif"] # step 1 (e.g. clipping of each raster study extent) index, item in enumerate(files_to_process): raster_i = "temp/ras_tem_" + str(index) + ".tif" arcpy.clip_management(item, '#', raster_i, mask1) # step 2 (e.g. change projection of raster files) ... # step 3 (e.g. calculate statistics each raster) ... etc.
this code works amazingly far. however, raster files big , steps take quite long execute (5-60 minutes). therefore, execute steps if input raster data changes. gis-workflow point of view, shouldn't problem, because each step saves physical result on hard disk used input next step.
i guess if want temporarily disable e.g. step 1, put #
in front of every line of step. however, in real analysis, each step might have lot of lines of code, , therefore prefer outsource code of each step separate file (e.g. "step1.py", "step2.py",...), , execute each file.
i experimented execfile(step1.py)
, received error nameerror: global name 'files_to_process' not defined
. seems variables defined in main script not automatically passed scripts called execfile
.
i tried this, received same error above.
i'm total python newbie (as might have figured out misuse of python-related expressions), , thankful advice on how organize such gis project.
i think want build each step function. these functions can stored in same script file or in own module gets loaded import statement (just arcpy). pseudo code this:
#file 1: steps.py def step1(input_files): # step 1 code goes here print 'step 1 complete' return def step2(input_files): # step 2 code goes here print 'step 2 complete' return output # optionally return derivative here #...and on
then in second file in same directory, can import , call functions passing rasters inputs.
#file 2: analyze.py import steps files_to_process = ["raster1.tif", "raster2.tif", "raster3.tif"] steps.step1(files_to_process) #steps.step2(files_to_process) # uncomment when you're ready step 2
now can selectively call different steps of code , requires commenting/excluding 1 line instead of whle chunk of code. understood question correctly.
Comments
Post a Comment