详解python命令提示符窗口下如何运行python脚本

详解python命令提示符窗口下如何运行python脚本,第1张

详解python命令提示符窗口下如何运行python脚本

以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:rvttestaprxtestaprx.aprx'
    mapName = 'Map'
    sourceDb = r'D:rvttestaprxData.gdb'
    targetDb = r'D:rvttestaprxtestsde.sde'
    output_aprx_path = r'D:rvttestaprxoutputexport5.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:rvttestaprx>python test.py D:rvttestaprxtestaprx.aprx Map D:rvttestaprxData.gdb D:rvttestaprxtestsde.sde D:rvttestaprxoutputexport5.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:rvttestaprxtestaprx.aprx'
    # mapName = 'Map'
    # sourceDb = r'D:rvttestaprxData.gdb'
    # targetDb = r'D:rvttestaprxtestsde.sde'
    # output_aprx_path = r'D:rvttestaprxoutputexport5.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)

到此这篇关于详解python命令提示符窗口下如何运行python脚本的文章就介绍到这了,更多相关python命令运行python脚本内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3209306.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-03
下一篇 2022-10-03

发表评论

登录后才能评论

评论列表(0条)

保存