欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-11 13:11:57
以arcgispro的python脚本为例在arcgispro自带的python窗口下运行python脚本需求:将arcgispro的.aprx项目包中gdb的数据源路径更换为sde数据源路径。示例数...

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

需求:

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

示例数据:

演示过程:

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

详解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

详解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])
    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脚本内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!