def test_create_tool(self): """Verify Create Tool for ISODATAClassification task creates a valid toolbox file.""" # Create tool. result = arcpy.CreateTool_gsf(self.task_url, self.output_toolbox_path) # Verify. self.assertEqual(result[0], self.output_toolbox_path) self.assertTrue(os.path.isfile(self.output_toolbox_path_with_ext)) # Import toolbox and make sure it is available. arcpy.ImportToolbox(self.output_toolbox_path_with_ext) toolbox_list = arcpy.ListToolboxes() toolbox_name = self.test_config['outputToolboxName'] + '(' + self.test_config['alias'] + ')' self.assertTrue(toolbox_name in toolbox_list)
def test_create_tool(self): """Verify create tool generates a GPTool""" result = arcpy.CreateENVIToolbox_envi(self.task_name_iso, self.output_toolbox) self.assertEqual(result[0], self.output_toolbox) self.assertTrue(os.path.isfile(self.output_toolbox)) arcpy.ImportToolbox(self.output_toolbox) toolbox_list = arcpy.ListToolboxes() toolbox_name = '('.join((self.output_toolbox_name, self.alias)) + ')' self.assertTrue(toolbox_name in toolbox_list, 'Toolbox failed to import') envi_tools_list = arcpy.ListTools('*_envi') self.assertTrue(self.tool_name_iso in envi_tools_list, 'GPTool not created: ' + self.tool_name_iso)
def test_create_tool_extension(self): """ Verify Create Tool for ISODATAClassification task creates a valid toolbox file when output toolbox is specified with .pyt extension. """ # Remove pyt file. if os.path.isfile(self.output_toolbox_path_with_ext): os.remove(self.output_toolbox_path_with_ext) # Create tool. output_toolbox = self.output_toolbox_path_with_ext result = arcpy.CreateTool_gsf(self.task_url, output_toolbox) # Verify. self.assertEqual(result[0], self.output_toolbox_path_with_ext) self.assertTrue(os.path.isfile(self.output_toolbox_path_with_ext)) # Import toolbox and make sure it is available. arcpy.ImportToolbox(self.output_toolbox_path_with_ext) toolbox_list = arcpy.ListToolboxes() toolbox_name = self.test_config['outputToolboxName'] + '(' + self.test_config['alias'] + ')' self.assertTrue(toolbox_name in toolbox_list)
# 实验内容 # 1、 使用ListTools()函数查看特定工具箱(例如分析工具箱)包含的工具; # 2、 使用Clip_analysis()函数进行裁切分析; # 3、 ☆使用脚本实现一个工具的输出作为另一个工具的输入(工具链); # 4、 ☆创建自定义地理处理工具实现从逗号分割的文本文件中读取数据,并将数据写入到已定义好的要素类中。 # 1 使用ListTools()函数查看特定工具箱(例如分析工具箱)包含的工具 import arcpy tools = arcpy.ListTools('*_analysis') for tool in tools: print(tool) # 获取工具箱别名 import arcpy boxlist = arcpy.ListToolboxes() for box in boxlist: print(box) # 2 使用Clip_analysis()函数进行裁切分析 import arcpy in_feature = r'E:\ArcPyStudy\Data\ArcpyBook\data\CityOfSanAntonio.gdb\Burglary' clip_feature = r'E:\ArcPyStudy\Data\ArcpyBook\Ch5\EdgewoodSD.shp' out_feature_class = r'E:\ArcPyStudy\Data\ArcpyBook\Ch5\ClpBurglary2.shp' arcpy.Clip_analysis(in_feature, clip_feature, out_feature_class) print("Output Done") # ☆3 使用脚本实现一个工具的输出作为另一个工具的输入(工具链) import arcpy arcpy.env.workspace = r'E:\ArcPyStudy\Data\ArcpyBook\data\TravisCounty' streams = r'E:\ArcPyStudy\Data\ArcpyBook\data\TravisCounty\Streams.shp' streamsBuffer = r'StreamsBuffer.shp'
# Set the Geoprocessing environment try: # Make sure all temporary files go in the Intermediate folder arcpy.workspace = interws # Make sure the user has the necessary versions of ArcGIS and ArcHydro install_info = arcpy.GetInstallInfo("desktop") if (install_info["Version"] <> "10.0"): arcpy.AddError( "Error: ArcGIS 10 and ArcHydro 2.0 are required to run this tool." ) raise Exception toolboxList = arcpy.ListToolboxes("archydro") if "archydro" in toolboxList[0]: arcpy.AddMessage("\nArcHydro toolbox found") else: arcpy.AddError( "\nError: Can't find ArcHydro in the ArcGIS toolbox list. Please verify the ArcHydro installation." ) raise Exception ArcHydroTools.SetTargetLocations("HydroConfig", "Layers", interws, sshed_gdb) except: arcpy.AddError("\nError setting geoprocessing environment: " + arcpy.GetMessages(2))