以arcgispro的python脚本为例在arcgispro自带的python窗口下运行python脚本
需求:
将arcgispro的.aprx项目包中gdb的数据源路径更换为sde数据源路径。
示例数据:testaprx_jb51.rar
演示过程:
方式一:脚本中指定好相关参数设置
import arcpy import json import sys import os import argparse import re result = "" jsontext = {'success': {}, 'msg': ''} def checkSavedAprxDatasource(output_aprx_path, mapName, targetDb): check_result = True desc = arcpy.Describe(targetDb) saved_aprx = arcpy.mp.ArcGISProject(output_aprx_path) for saved_map in saved_aprx.listMaps(): if saved_map.name == mapName: for lyr in saved_map.listLayers(): if lyr.isFeatureLayer: # print(lyr.connectionProperties) if lyr.connectionProperties['workspace_factory'].upper() == 'SDE'.upper( ) and lyr.connectionProperties['connection_info']['instance'] == desc.connectionProperties.instance: pass else: check_result = False return check_result if __name__ == '__main__': try: # linux # aprxPath = str(sys.argv[1]) # mapName = str(sys.argv[2]) # sourceDb = str(sys.argv[3]) # targetDb = str(sys.argv[4]) # output_aprx_path = str(sys.argv[5]) # local pc aprxPath = r'D:\rvt\testaprx\testaprx.aprx' mapName = 'Map' sourceDb = r'D:\rvt\testaprx\Data.gdb' targetDb = r'D:\rvt\testaprx\testsde.sde' output_aprx_path = r'D:\rvt\testaprx\output\export5.aprx' aprx = arcpy.mp.ArcGISProject(aprxPath) for m in aprx.listMaps(): if m.name == mapName: m.updateConnectionProperties(sourceDb, targetDb) if os.path.exists(output_aprx_path): os.remove(output_aprx_path) aprx.saveACopy(output_aprx_path) checkResult = checkSavedAprxDatasource( output_aprx_path, mapName, targetDb) if checkResult: jsontext['success'] = True result = json.dumps(jsontext) sys.stdout.write(result) else: jsontext['success'] = False jsontext['msg'] = 'Failed to replace data source' result = json.dumps(jsontext) sys.stdout.write(result) except Exception as e: jsontext['success'] = False jsontext['msg'] = e.args result = json.dumps(jsontext) sys.stdout.write(result)
方式二:使用sys.argv[ ]的形式设置相关参数
(arcgispro-py3) D:\rvt\testaprx>python test.py D:\rvt\testaprx\testaprx.aprx Map D:\rvt\testaprx\Data.gdb D:\rvt\testaprx\testsde.sde D:\rvt\testaprx\output\export5.aprx
import arcpy import json import sys import os import argparse import re result = "" jsontext = {'success': {}, 'msg': ''} def checkSavedAprxDatasource(output_aprx_path, mapName, targetDb): check_result = True desc = arcpy.Describe(targetDb) saved_aprx = arcpy.mp.ArcGISProject(output_aprx_path) for saved_map in saved_aprx.listMaps(): if saved_map.name == mapName: for lyr in saved_map.listLayers(): if lyr.isFeatureLayer: # print(lyr.connectionProperties) if lyr.connectionProperties['workspace_factory'].upper() == 'SDE'.upper( ) and lyr.connectionProperties['connection_info']['instance'] == desc.connectionProperties.instance: pass else: check_result = False return check_result if __name__ == '__main__': try: # linux aprxPath = str(sys.argv[1]) mapName = str(sys.argv[2]) sourceDb = str(sys.argv[3]) targetDb = str(sys.argv[4]) output_aprx_path = str(sys.argv[5]) print("aprx路径: "+aprxPath) print("地图视图的名称: "+mapName) print("当前的数据源路径: "+sourceDb) print("目标数据源路径: "+targetDb) print("aprx另存为路径: "+output_aprx_path) # local pc # aprxPath = r'D:\rvt\testaprx\testaprx.aprx' # mapName = 'Map' # sourceDb = r'D:\rvt\testaprx\Data.gdb' # targetDb = r'D:\rvt\testaprx\testsde.sde' # output_aprx_path = r'D:\rvt\testaprx\output\export5.aprx' aprx = arcpy.mp.ArcGISProject(aprxPath) for m in aprx.listMaps(): if m.name == mapName: m.updateConnectionProperties(sourceDb, targetDb) if os.path.exists(output_aprx_path): os.remove(output_aprx_path) aprx.saveACopy(output_aprx_path) checkResult = checkSavedAprxDatasource( output_aprx_path, mapName, targetDb) if checkResult: jsontext['success'] = True result = json.dumps(jsontext) sys.stdout.write(result) else: jsontext['success'] = False jsontext['msg'] = 'Failed to replace data source' result = json.dumps(jsontext) sys.stdout.write(result) except Exception as e: jsontext['success'] = False jsontext['msg'] = e.args result = json.dumps(jsontext) sys.stdout.write(result)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“详解python命令提示符窗口下如何运行python脚本”评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]