您好,登錄后才能下訂單哦!
python命令提示符窗口下如何運行python腳本解析?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
方式一:腳本中指定好相關參數設置
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)
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。