Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何组织一个包含多个分析步骤的Python GIS项目?_Python_Gis_Arcgis_Arcpy - Fatal编程技术网

如何组织一个包含多个分析步骤的Python GIS项目?

如何组织一个包含多个分析步骤的Python GIS项目?,python,gis,arcgis,arcpy,Python,Gis,Arcgis,Arcpy,我刚刚开始使用ArcPy使用ArcGIS分析地理数据。分析有不同的步骤,这些步骤将一个接一个地执行 下面是一些伪代码: import arcpy # create a masking variable mask1 = "mask.shp" # create a list of raster files files_to_process = ["raster1.tif", "raster2.tif", "raster3.tif"] # step 1 (e.g. clipping of

我刚刚开始使用ArcPy使用ArcGIS分析地理数据。分析有不同的步骤,这些步骤将一个接一个地执行

下面是一些伪代码:

import arcpy

# create a masking variable
mask1 = "mask.shp"    

# create a list of raster files
files_to_process = ["raster1.tif", "raster2.tif", "raster3.tif"]

# step 1 (e.g. clipping of each raster to study extent)
for 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 some statistics for each raster)
...

etc.
到目前为止,这段代码运行得非常好。但是,光栅文件很大,有些步骤需要很长时间才能执行(5-60分钟)。因此,我只希望在输入光栅数据更改时执行这些步骤。从GIS工作流的角度来看,这不应该是一个问题,因为每一步都会在硬盘上保存一个物理结果,然后作为下一步的输入

我想如果我想暂时禁用例如步骤1,我可以简单地在该步骤的每一行前面放一个
。然而,在实际分析中,每个步骤可能有很多行代码,因此我更愿意将每个步骤的代码外包到一个单独的文件中(例如“step1.py”、“step2.py”、…),然后执行每个文件

我尝试了
execfile(step1.py)
,但收到了错误
namererror:global name“files\u to\u process”未定义
。似乎主脚本中定义的变量不会自动传递给由
execfile
调用的脚本

我也试过了,但是我收到了与上面相同的错误


我是一个完全的Python新手(您可能已经通过误用任何Python相关表达式发现了这一点),如果您能就如何组织这样一个GIS项目提供任何建议,我将非常感激

我认为您要做的是将每个步骤构建成一个函数。这些函数可以存储在同一个脚本文件中,也可以存储在它们自己的模块中,这些模块与import语句一起加载(就像arcpy一样)。伪代码如下所示:

#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 a derivative here

#...and so on
然后在同一目录中的第二个文件中,可以导入并调用将光栅作为输入传递的函数

#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 this when you're ready for step 2

现在,您可以有选择地调用代码的不同步骤,它只需要注释/排除一行代码,而不是一大块代码。希望我正确理解了您的问题。

我认为您要做的是将每个步骤构建成一个函数。这些函数可以存储在同一个脚本文件中,也可以存储在它们自己的模块中,这些模块与import语句一起加载(就像arcpy一样)。伪代码如下所示:

#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 a derivative here

#...and so on
然后在同一目录中的第二个文件中,可以导入并调用将光栅作为输入传递的函数

#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 this when you're ready for step 2

现在,您可以有选择地调用代码的不同步骤,它只需要注释/排除一行代码,而不是一大块代码。希望我正确理解了你的问题。

这正是我想要做的,而且效果很好。谢谢这正是我想要做的,而且效果很好。谢谢