• 3章代码


    1.获得环境变量

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    import string
    def Main():
        environments = arcpy.ListEnvironments()
        # Sort the environment list, disregarding capitalization
        #
        environments.sort(key=string.lower)
    
        for environment in environments:
            # As the environment is passed as a variable, use Python's getattr
            #   to evaluate the environment's value
            #
            envSetting = getattr(arcpy.env, environment)
    
            # Format and print each environment and its current setting
            #
            arcpy.AddMessage( "{0:<30}: {1}".format(environment, envSetting))
        for toolbox in arcpy.ListToolboxes():
            # Toolboxes are printed in the form of "toolbox_name(toolbox_alias)"
            arcpy.AddMessage(toolbox)
    
    
    arcpy.env.overwriteOutput=True
    arcpy.env.addOutputsToMap=True
    arcpy.AddMessage("arcpy.env.scratchGDB="+arcpy.env.scratchGDB)
    arcpy.env.workspace = arcpy.env.scratchWorkspace #默认数据库
    
    
    arcpy.ResetEnvironments()
    
    # Reset a specific environment setting
    arcpy.ClearEnvironment("workspace")
    
    Main()

    2.设置环境变量

    def Main():
        arcpy.env.overwriteOutput=True
        arcpy.env.addOutputsToMap=True
        arcpy.AddMessage("arcpy.env.scratchGDB="+arcpy.env.scratchGDB)
        arcpy.env.workspace = arcpy.env.scratchWorkspace #默认数据库
    
    
        arcpy.ResetEnvironments()
    
        # Reset a specific environment setting
        arcpy.ClearEnvironment("workspace")
    
    
    
    
    Main()
    =======================3D转2D
    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    
    
    def Main():
        arcpy.env.overwriteOutput=True
        arcpy.env.addOutputsToMap=True
        arcpy.env.outputZFlag ="Disabled"
        arcpy.env.tinSaveVersion = "PRE_10.0" #CURRENT
    
    
        indata=arcpy.GetParameterAsText(0)
        outdata=arcpy.GetParameterAsText(1)
    
        arcpy.CopyFeatures_management(indata,outdata)
        #arcpy.Select_analysis(indata,outdata)
    
        arcpy.ResetEnvironments()
    
        # Reset a specific environment setting
        arcpy.ClearEnvironment("workspace")
    
    
    
    
    Main()
    
    

    输出tin,10.0版本的tin

    def Main():
        arcpy.env.overwriteOutput=True
        arcpy.env.addOutputsToMap=True
        arcpy.env.outputZFlag ="Disabled"
        arcpy.env.tinSaveVersion = "PRE_10.0" #CURRENT
        arcpy.CheckOutExtension('3D')
    
    
        indata=arcpy.GetParameterAsText(0)
        outdata=arcpy.GetParameterAsText(1)
    
        arcpy.CreateTin_3d(outdata,spatial_reference="#",in_features=indata+" 标示高程 Hard_Line <None>",constrained_delaunay="DELAUNAY")
    
        arcpy.ResetEnvironments()
    
        # Reset a specific environment setting
        arcpy.ClearEnvironment("workspace")
    
    
    
    
    Main()
     
    
    
     

    3.许可和软件安装信息

    def Main():
        arcpy.AddMessage("ProductInfo:"+arcpy.ProductInfo())
        arcpy.AddMessage("{0:<30}: {1}".format("ArcInfo", arcpy.CheckProduct("ArcInfo")))
        arcpy.AddMessage("{0:<30}: {1}".format("arcview", arcpy.CheckProduct("arcview")))
        arcpy.AddMessage("{0:<30}: {1}".format("arceditor",arcpy.CheckProduct("arceditor")))
        arcpy.AddMessage("{0:<30}: {1}".format("engine",arcpy.CheckProduct("engine")))
        arcpy.AddMessage("{0:<30}: {1}".format("enginegeodb",arcpy.CheckProduct("enginegeodb")))
        arcpy.AddMessage("{0:<30}: {1}".format("arcserver",arcpy.CheckProduct("arcserver")))
        if arcpy.CheckExtension("3D") == "Available":
            arcpy.CheckOutExtension("3D")
            arcpy.CheckInExtension("3D")
            arcpy.AddMessage("有3D")
        else:
            arcpy.AddMessage("没有3D:")
    
        for install in arcpy.ListInstallations():
            arcpy.AddMessage("install:"+install)
    
        d = arcpy.GetInstallInfo()
        for key, value in d.iteritems():
            # Print a formatted string of the install key and its value
            #
            arcpy.AddMessage("{:<13} : {}".format(key, value))
        arcpy.AddMessage(u"安装版本"+arcpy.GetInstallInfo()['Version'])
        arcpy.AddMessage(u"安装路径"+arcpy.GetInstallInfo()['InstallDir'])
    
    
        ##3D —ArcGIS 3D Analyst 扩展模块
        ##Datareviewer —ArcGIS 10.2 Data Reviewer for Desktop
        ##DataInteroperability —适用于 Desktop 的 ArcGIS Data Interoperability 扩展模块
        ##Airports —ArcGIS for Aviation: Airports
        ##Aeronautical —ArcGIS for Aviation: Charting
        ##Bathymetry —ArcGIS for Maritime: Bathymetry
        ##Nautical —ArcGIS for Maritime: Charting
        ##GeoStats —ArcGIS Geostatistical Analyst 扩展模块
        ##Network —ArcGIS Network Analyst 扩展模块
        ##Spatial —ArcGIS Spatial Analyst 扩展模块
        ##Schematics —ArcGIS Schematics 扩展模块
        ##Tracking —ArcGIS Tracking Analyst 扩展模块
        ##JTX —ArcGIS Workflow Manager for Desktop
        ##ArcScan —ArcScan
        ##Business —Business Analyst
        ##Defense —Esri Defense Solution
        ##Foundation —Esri Production Mapping
        ##Highways —Esri Roads and Highways
        ##StreetMap —StreetMap
    Main()

    4.表名和字段是否有效

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    from arcpy.sa import *
    
    
    
    
    def Main():
        arcpy.env.overwriteOutput=True
        arcpy.env.addOutputsToMap=True
    
    
        inworkspace=arcpy.GetParameterAsText(0)
        tablename=arcpy.GetParameterAsText(1)
        fieldname=arcpy.GetParameterAsText(2)
        arcpy.env.workspace = inworkspace
        if arcpy.Exists(tablename):#测试在执行期间当前工作空间中是否存在要素类、
            #表、数据集、shapefile、工作空间、图层和其他文件。函数返回指示元素是否存在的布尔值。
            arcpy.AddMessage(tablename+u"已存在")
            return
    
        tablename1 = arcpy.ValidateTableName(tablename,inworkspace)
        arcpy.AddMessage(tablename+u",修改为"+tablename1)
        arcpy.CreateTable_management(inworkspace, tablename1)
        #字段是否有效
        fieldname1 = arcpy.ValidateFieldName(fieldname, inworkspace)
        arcpy.AddMessage(fieldname+u",修改为"+fieldname1)
    
        arcpy.AddField_management(tablename1, fieldname1, "double")
        data_type="Table"
    
        arcpy.Delete_management(inworkspace+os.sep+tablename1,data_type)
    
    
    Main()

    5.信息输出和编写消息

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    import string
    
    
    
    
    def Main():
        fc = arcpy.GetParameterAsText(0)
        arcpy.GetCount_management(fc)
        # Print the first and last geoprocessing tool messages
        message_count = arcpy.GetMessageCount()
        for i in range(message_count):
            arcpy.AddMessage("{0},{1}".format(i,arcpy.GetMessage(i)))
        feature_count = int(arcpy.GetCount_management(fc).getOutput(0))
        if feature_count == 0:
            arcpy.AddError("{0} has no features.".format(fc))
        else:
            arcpy.AddMessage("{0} has {1} features.".format(fc, feature_count))
    
        arcpy.AddWarning("{0} has {1} features.".format(fc, feature_count))
    
    
    
    Main()

    8.异常处理

    # -*- coding:UTF-8 -*-
    import arcpy
    import os
    import sys
    
    
    try:
        a=1111111
        b=0000
        c=a/b
    except Exception as e:
        arcpy.AddError(e)
        arcpy.AddMessage(e.message)
    
    
    finally:
            #'''不管try语句中的代码是否报错,都会执行finally分支中的代码'''
            #'''去完成一些连接操作的收尾工作'''
        arcpy.AddMessage('finally 被执行了')
    arcpy.AddMessage("***:{0}".format(arcpy.GetReturnCode(0)))
    num = arcpy.GetMessageCount()
    arcpy.AddWarning("{0} AddWarning".format(num))
    
    num = arcpy.GetMessageCount()
    for i in range(num):
        arcpy.AddMessage("{0}==========={1}".format(i,arcpy.GetMessage(i)))
        arcpy.AddReturnMessage(i)
  • 相关阅读:
    数据库更改自增和主键
    修改MySQL的默认数据存储引擎
    tomcat密码的坑
    通过System.getProperties()获取系统参数
    Java语言开发的,直接解压即可使用软件
    一个javascript面试题
    为什么学习差
    IDEA设置
    java关键字
    IDEA快捷键
  • 原文地址:https://www.cnblogs.com/gisoracle/p/13533571.html
Copyright © 2020-2023  润新知