• 15章代码


    1. 打开所有图层

    没有参数,代码如下

    #coding: UTF-8

    import arcpy
    import os
    import types
    import string

    def main():
        ap=arcpy.mp.ArcGISProject("CURRENT")
        pMap=ap.activeMap
        for lyr in pMap.listLayers():
            lyr.visible=True

    if
    __name__ == '__main__':
        main()

    1. 关闭所有图层

    #coding: UTF-8
    #######################
    import arcpy
    import os
    import types
    import string

    def main():
        ap=arcpy.mp.ArcGISProject("CURRENT")
        pMap=ap.activeMap
        for lyr in pMap.listLayers():
            lyr.visible=False

    if
    __name__ == '__main__':
        main()

    1. 删除所有图层和表格

    #coding: UTF-8
    #######################
    import arcpy
    import os
    import types
    import string

    def main():
        ap=arcpy.mp.ArcGISProject("CURRENT")
        pMap=ap.activeMap

        for lyr in pMap.listLayers():
            if lyr!=None:
                pMap.removeLayer(lyr)
        for table in pMap.listTables():
            if table!=None:
                pMap.removeTable(table)

    if __name__ == '__main__':
        main()

    15.2.1 当前工程布局导出图片

    #coding: UTF-8
    #######################
    import arcpy
    import os
    import types
    import string
    
    def main():
        imgFMT = arcpy.GetParameterAsText(0)
        aprx=arcpy.mp.ArcGISProject("current")
        out_path=aprx.homeFolder;
        for lyt in  aprx.listLayouts():
            if (imgFMT==".PDF"):
                lyt.exportToPDF(out_path+"\"+lyt.name+".pdf", resolution=300)
            elif  (imgFMT==".JPG"):
                lyt.exportToJPEG(out_path+"\"+lyt.name+".jpg", resolution=300)
            elif  (imgFMT==".PNG"):
                lyt.exportToPNG(out_path+"\"+lyt.name+".PNG", resolution=300)
    
    if __name__ == '__main__':
        main()

    15.2.2 代码实现页面驱动

    #coding: UTF-8
    #######################
    import arcpy
    import os
    import types
    import string
    import time
    def main():
        in_Layer = arcpy.GetParameterAsText(0)
        fieldName =("ShAPE@",arcpy.GetParameterAsText(1))
        in_Fileext = arcpy.GetParameterAsText(2).upper()
        aprx=arcpy.mp.ArcGISProject("current")
        list = aprx.listLayouts()
        num=len(list)
        arcpy.AddMessage("num="+str(num))
        if num<1:
            arcpy.AddMessage("没有布局视图=======")
            return
    
        lyt = aprx.listLayouts()[0]
        if lyt==None:
            arcpy.AddMessage("没有布局视图")
            return
        mf = lyt.listElements("MAPFRAME_ELEMENT")[0]
        presolution=300
        pPath=aprx.homeFolder+"/"
        mf.map.clearSelection()
        arcpy.management.RepairGeometry(in_Layer, "DELETE_NULL", "ESRI") #不修复几何,会自动退出
    
        with arcpy.da.SearchCursor(in_Layer, fieldName) as cursor:
            for row in cursor:
                try:
                    pgeo=row[0]
                    fieldValue=row[1]
                    pExtent=pgeo.extent
                    mf.camera.setExtent (pExtent)
                    pscale= mf.camera.scale
                    myScale=100000
                    pscale=(int(pscale/myScale)+1)*myScale
                    arcpy.AddMessage("比例尺:"+str(myScale))
                    mf.camera.scale=pscale
                    time.sleep(10)
    
                    if in_Fileext=="PDF":
                        out_pdf=pPath+fieldValue+".pdf"
                        arcpy.AddMessage("out_pdf="+out_pdf)
                        lyt.exportToPDF(out_pdf, resolution=presolution)
                    elif in_Fileext=="JPG":
                        out_pdf = pPath + fieldValue + ".jpg"
                        arcpy.AddMessage("out_pdf=" + out_pdf)
                        lyt.exportToJPEG(out_pdf, resolution=presolution)
                    else:
                        out_pdf = pPath + fieldValue + ".png"
                        arcpy.AddMessage("out_pdf=" + out_pdf)
                        lyt.exportToPNG(out_pdf, resolution=presolution)
                except arcpy.ExecuteError:
                    arcpy.AddError(arcpy.GetMessages(2))
    
    if __name__ == '__main__':
        main()

    15.2.3 地图系列批量打印

    #coding: UTF-8
    #######################
    import arcpy
    import os
    import types
    import string
    
    def expone(aprx,out_Path):
        for l in  aprx.listLayouts():
            if not l.mapSeries is None:
                ms = l.mapSeries
                if ms.enabled:
                    arcpy.AddMessage(out_Path+"\"+l.name+".pdf")
                    ms.exportToPDF(out_Path+"\"+l.name+".pdf", "ALL", resolution=300)
            else:
                arcpy.AddMessage(l.name+"没有序列")
    def expmany(aprx, out_Path):
        for l in  aprx.listLayouts():
            if not l.mapSeries is None:
                ms = l.mapSeries
                if ms.enabled:
                    for pageNum in range(1, ms.pageCount + 1):
                        ms.currentPageNumber = pageNum
                        #FieldName=ms.pageNameField.name
                        #arcpy.AddMessage("ms.pageNameField="+FieldName)
    
                        pRow=ms.pageRow
                        #arcpy.AddMessage(type(pRow))
                        pageName = pRow[1]
                        arcpy.AddMessage("pageName=" + str(pageName))
    
                        #l.exportToPNG(os.path.join(relpath, "Output", f"Ex2_{ms.pageRow.STATE_NAME}.png"))
                        arcpy.AddMessage(out_Path+"\"+l.name+"_"+pageName+".pdf")
                        ms.exportToPDF(out_Path+"\"+l.name+"_"+pageName+".pdf", "CURRENT", resolution=300)
            else:
                arcpy.AddMessage(l.name + "没有序列")
    
    def main():
        in_aprx = arcpy.GetParameterAsText(0)
        out_Path=arcpy.GetParameterAsText(1)
    
        isone = arcpy.GetParameter(2)
        if in_aprx=="":
            aprx=arcpy.mp.ArcGISProject("current")
        else:
            aprx = arcpy.mp.ArcGISProject(in_aprx)
        if out_Path=="":
            out_Path=aprx.homeFolder
        if isone:#合并成一个
            expone(aprx,out_Path)
        else:
            expmany(aprx, out_Path)
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    C#中实现窗体间传值开发者在线 Builder.com.cn 更新时间:20080719作者: 来源:
    Visual C# 2005中如何产生与比较哈希值开发者在线 Builder.com.cn 更新时间:20080720作者: 来源:
    C#多线程编程实例实战开发者在线 Builder.com.cn 更新时间:20080720作者: 来源:
    如何构造一个C#语言的爬虫程序开发者在线 Builder.com.cn 更新时间:20080720作者: 来源:
    用C#读取Windows注册表中的多重字串和二进制字串开发者在线 Builder.com.cn 更新时间:20080720作者: 来源:
    Visual C#多线程参数传递浅析开发者在线 Builder.com.cn 更新时间:20080719作者: 来源:
    C#操作消息队列的代码开发者在线 Builder.com.cn 更新时间:20080720作者: 来源:
    深入解析C#编程中的事件开发者在线 Builder.com.cn 更新时间:20080719作者: 来源:
    理解C# 3.0新特性之Extension方法浅议开发者在线 Builder.com.cn 更新时间:20080719作者: 来源:
    什么东西打印出来好查,什么东西放在电脑好查
  • 原文地址:https://www.cnblogs.com/gisoracle/p/13623813.html
Copyright © 2020-2023  润新知