esri的arcpy站点包的扩展功能
arcpyext的Python项目详细描述
arcpyext提供了一个助手函数集合,这些函数使使用esri arcpy site包执行常见任务 更容易完成。它主要是为管理arcgis的命令行工具集(agstools)提供服务 但可以很容易地在其他脚本中使用。
功能
目前,arcpyext具有更改地图文档层的数据源、准备地图文档的功能 在地理数据库的编辑会话中发布和执行CRUD操作。
arcpyext.转换
conversion模块具有将要素类转换为不同输出格式的简单函数。
示例A-将要素类转换为逗号分隔值文件
importarcpyext.conversionINPUT_TABLE="path/to/input_feature_class_or_table"OUTPUT_CSV="path/to/output/csv_file.csv"arcpyext.conversion.table_to_csv(INPUT_TABLE,OUTPUT_CSV)
示例B-将要素类转换为Office Open XML电子表格(Excel电子表格)
importarcpyext.conversionINPUT_TABLE="path/to/input_feature_class_or_table"OUTPUT_WORKBOOK="path/to/output/excel_file.xlsx"arcpyext.conversion.table_to_ooxml_workbook(INPUT_TABLE,OUTPUT_WORKBOOK)
arcpyext.数据
data模块在编辑会话中包装基本的创建、更新和删除操作,并自动启动/ 根据需要停止/中止编辑操作。函数只是包装适当的arcpy.da游标,所以 从功能上讲,它们的工作原理是相同的。还提供了一个方便的函数,用于将行读入列表。
示例
importarcpyimportarcpyext.data#WORKSPACE = "path/to/sde_database.sde"WORKSPACE="path/to/geodatabase.gdb"TABLE="Countries"edit_session=arcpy.da.Editor(WORKSPACE)edit_session.startEditing()try:arcpyext.data.delete_rows(edit_session,TABLE,"Name LIKE 'A%'",('Name'))exceptRuntimeError,re:edit_session.stopEditing(False)edit_session.stopEditing(True)deledit_session
有关更多代码示例,请参阅相关测试。
arcpyext.映射
mapping模块提供以下功能:
- describing a map document/project
- changing the data sources of a layer (or layers) in an ArcGIS Map Document or ArcGIS Project,
- easily checking if a map document/project is in a valid state
- comparing different versions of a map document/project
描述地图文档/arcgis项目
描述地图文档/项目产品的字典,详细说明了地图文档的许多细节。地图 文档/项目的描述如下:
importarcpyextpath_to_mxd_or_project="path/to/arcgis/map_doc.mxd"# or *.aprx file on ArcGIS Prodescription=arcpyext.mapping.describe(path_to_mxd_or_project)
输出说明将具有以下结构:
{"filePath":"C:\\projects\\public\\arcpyext\\tests\\samples\\test_mapping_complex.mxd",# an ordered list of maps contained in the map document/project"maps":[{"name":"Layers","spatialReference":"GEOGCS['GCS_GDA_1994',DATUM['D_GDA_1994',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119521E-09;0.001;0.001;IsHighPrecision",# an ordered list of layers contained in the map"layers":[{"dataSource":"C:\\projects\\public\\arcpyext\\tests\\samples\\statesp020_clip1","database":"C:\\projects\\public\\arcpyext\\tests\\samples","datasetName":"statesp020_clip1","datasetType":"Shapefile Feature Class","definitionQuery":"FID <1","fields":[{"alias":"FID","index":0,"name":"FID","type":"OID","visible":true},{"alias":"SHAPE","index":1,"name":"Shape","type":"Geometry","visible":true},{"alias":"AREA","index":2,"name":"AREA","type":"Double","visible":true},{"alias":"PERIMETER","index":3,"name":"PERIMETER","type":"Double","visible":true},{"alias":"STATE","index":5,"name":"STATE","type":"String","visible":true}],"index":0,"isBroken":false,"isFeatureLayer":true,"isGroupLayer":false,"isNetworkAnalystLayer":false,"isRasterLayer":false,"isRasterizingLayer":null,"isServiceLayer":null,"longName":"Layer 1","name":"Layer 1","server":null,"service":null,"serviceId":1,"userName":null,"visible":true}],# an ordered list of the tables contained in the map"tables":[{"dataSource":"C:\\projects\\public\\arcpyext\\tests\\samples\\statesp020.txt","database":"C:\\projects\\public\\arcpyext\\tests\\samples","datasetName":"statesp020.txt","datasetType":"Text File","definitionQuery":"","fields":[{"alias":"Identification_Information:","index":0,"name":"Identification_Information:","type":"String","visible":true}],"index":0,"isBroken":false,"name":"statesp020.txt","server":null,"service":null,"serviceId":7,"userName":null}]}]}
更改数据源
通过创建具有匹配条件的模板,可以方便地跨地图文档和项目更改数据源, 然后根据地图文档或项目评估这些模板,以生成替换数据源的列表 对于匹配的层。
模板的结构略有不同,这取决于您是在编辑Arcgis地图文档还是在编辑Arcgis项目。
Arcgis地图文件:
"dataSource":{# The *dataSource* property points to the replacement data source# The contents of the property depends on whether your changing data sources on a map document or a project"workspacePath":"path/to/workspace/if/changed","datasetName":"nameOfTheNewDatasetIfChanged","wokspaceType":"workspaceTypeIfChanged","schema":"databaseSchemaNameIfChanged"},"matchCriteria":{# properties that match against properties discovered when describing a layer# strings are compared ingoring case# an empty dictionary is also valid, which will match all layers# Changing user is a common use case for updating data sources"userName":"ExistingUserName"}
Arcgis项目:
"dataSource":{# Any properties decribed at the following address under the *Using the connectionProperties dictionary*# section header are valid: https://pro.arcgis.com/en/pro-app/arcpy/mapping/updatingandfixingdatasources.htm# Example"connection_info":{"database":"path/to/database"},"dataset":"NewDataset"},"matchCriteria":{# properties that match against properties discovered when describing a layer# strings are compared ingoring case# an empty dictionary is also valid, which will match all layers# Changing user is a common use case for updating data sources"userName":"ExistingUserName"}
模板列表可用于创建地图文档或项目的数据源替换列表。
importarcpyextpath_to_mxd_or_project="path/to/arcgis/map_doc.mxd"# or *.aprx file on ArcGIS Prodata_source_templates=[# one or more templates goes hear"dataSource":{"workspacePath":"./newDatabaseConnection.sde"},"matchCriteria":{# match everything}]replacement_data_source_list=arcpyext.mapping.create_replacement_data_sources_list(path_to_mxd_or_project,data_source_templates)
生成的替换数据源列表可以反馈到arcpyext中,以更新所有匹配的层 桌子:
arcpyext.mapping.change_data_sources(path_to_mxd_or_project,replacement_data_source_list)
检查地图是否有效
<> P>一种方便的方法,用于快速测试地图文档/项目是否处于有效状态(即已中断)。
层/表)。这可以称为如下:importarcpyextpath_to_mxd_or_project="path/to/arcgis/map_doc.mxd"# or *.aprx file on ArcGIS Proarcpyext.mapping.is_valid(path_to_mxd_or_project)
arcpyext.发布
publishing模块提供了从第一个映射创建服务定义或草稿的便利功能 在Arcgis地图文档或Arcgis项目中。
从地图文档创建服务定义草稿
此函数在创建服务定义草稿之前检查映射是否没有任何损坏的数据源。
importarcpyextpath_to_mxd_or_project="path/to/arcgis/map_doc.mxd"# or *.aprx file on ArcGIS Prooutput_path="path/to/sddraft/output.sddraft"service_name="ExampleMapService"arcpyext.publishing.convert_map_to_service_draft(path_to_mxd_or_project,output_path,service_name)
从草稿创建服务定义
此函数用于将服务定义草稿文件(.sd draft)转换为服务定义(.sd)。
此函数在子进程上运行转换进程,这似乎有助于解决调用此函数的可靠性问题 工具箱。
importarcpyextpath_to_sd_draft="path/to/sddraft/output.sddraft"output_path="path/to/output.sd"arcpyext.publishing.convert_service_draft_to_staged_service(path_to_sd_draft,output_path)