以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)
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 姚璎格《发烧女中音》DSD版[低速原抓WAV+CUE][1G]
- 张玮伽《微风细雨DSD》发烧大碟[WAV+CUE][1.1G]
- 群星《2024好听新歌14》十倍音质 U盘音乐 [WAV分轨][966M]
- s14全球总决赛T1战队队员都有谁 LOLs14全球总决赛T1战队介绍
- 英雄联盟faker身价有10亿吗 英雄联盟faker身价介绍一览
- faker大魔王称号怎么来的 faker大魔王称号来源介绍
- PS5 Pro上的蒂法更美了!博主盛赞新机1000%值得购买
- 腾讯互娱再离职一员大将!或因供应商贪腐
- Ayaneo3游戏掌机预热:旗舰定位、造型圆润自带底键
- 动力火车.1999-背叛情歌【上华】【WAV+CUE】
- 刘力扬.2019-Neon.Lit虹【摩登天空】【FLAC分轨】
- 群星.2002-恋爱物语情歌对唱精选2CD(引进版)【滚石】【WAV+CUE】
- 群星《闽南情24K德国HD金碟》2CD[WAV+CUE]
- 周传雄《恋人创世纪》环球唱片[WAV+CUE]
- 关淑怡-《真假情话K2HD》(日本压制)【WAV+CUE】