如何在文件GDB、ArcGIS中设置工作空间



我正在创建一个脚本,该脚本创建一个新的、空的File GDB和FeatureDataset,但我不知道如何设置脚本的所有输出将自动保存在File GDB或Feature Dataset中。现在我使用了一个界面,这样用户就必须为所有分析指定输出,但因为我有很多输出,我想减少界面中的输出数量(例如点、线、多边形等)。示例:

import arcpy
GDB_Location = arcpy.GetParameterAsText(0)
GDB_name = arcpy.GetParameterAsText(1)
GDB_file = arcpy.CreateFileGDB_management(GDB_Location, GDB_name)
out_dataset_path = GDB_file
out_dataset_name = arcpy.GetParameterAsText(2)
feature_dataset = arcpy.CreateFeatureDataset_management(out_dataset_path,out_dataset_name)
point= arcpy.GetParameterAsText(3)
line = arcpy.GetParameterAsText(4)
poly = arcpy.GetParameterAsText(5)

…。

您希望在代码中包含两件事:

  1. 创建地理数据库的步骤
  2. 一个变量,您可以在后续步骤中使用该变量来引用该地理数据库

创建地理数据库(GDB)或特征数据集的ArcPy函数不会同时生成变量。将其视为两个独立的步骤,然后您就可以为输出工作区创建一个变量。

import arcpy
import os
GDB_Location = arcpy.GetParameterAsText(0)
GDB_name = arcpy.GetParameterAsText(1)
# function to create the GDB
arcpy.CreateFileGDB_management(GDB_Location, GDB_name)
# variable to refer to the GDB
gdb = os.path.join(GDB_Location, GDB_name)
dataset_name = arcpy.GetParameterAsText(2)
# function to create the dataset
arcpy.CreateFeatureDataset_management(gdb, dataset_name)
# variable to refer to the dataset
dataset = os.path.join(gdb, dataset_name)

如果您想减少用户需要填写的输入参数的数量,可以在代码中自己生成名称——这实际上取决于最终用户想要什么。

一个"介于两者之间"的替代方案是提供一个默认值,该值表明用户可能想要使用什么。通过这种方式,他们可以选择简单的方式(使用默认值),或者做更多的工作来自定义输出特性名称。

# EITHER have user provide the output feature class name
pointName = arcpy.GetParameterAsText(3)
# OR name the points yourself
pointName = "output_point"
# and then concatenate that together with the geodatabase and feature dataset
pointFeature = os.path.join(gdb, dataset, pointName)
# then use variable "pointFeature" in subsequent functions to create that feature

最新更新