当前位置: 移动技术网 > IT编程>脚本编程>Python > 详解python命令提示符窗口下如何运行python脚本

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

2020年09月12日  | 移动技术网IT编程  | 我要评论
以arcgispro的python脚本为例在arcgispro自带的python窗口下运行python脚本需求:将arcgispro的.aprx项目包中gdb的数据源路径更换为sde数据源路径。示例数

以arcgispro的python脚本为例在arcgispro自带的python窗口下运行python脚本

需求:

将arcgispro的.aprx项目包中gdb的数据源路径更换为sde数据源路径。

示例数据:

演示过程:

方式一:脚本中指定好相关参数设置

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)

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网