利用arcpy进行地图整饰


1、前言
最近项目需要通过软件出宗地图,但是软件输出的mxd未能达到成果要求,但是图的数量很大,人工修改会增加工作量,故想到通过arcpy批处理进行宗地图的整饰。
2、整饰要素
主要通过三个字段计算到需标注字段内,并进行符号编辑和标注。
a 首先获取文件夹下的所有mxd文档:

def GetAllFiles(dir):
    allmxd = []
    dirs = os.listdir(dir)
    for mxdfile in dirs:
        if mxdfile[-3:].lower() == 'mxd':
            allmxd.append(mxdfile)
    return allmxd

b 然后计算字段并标注

def LabelAndExport(mxd):
    export = "yes"
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == "FW_J_320507":          ##FW进行标注
            fc = lyr.dataSource
            cursor = arcpy.da.UpdateCursor(fc,["BZ","FWJG","FWCS","JGRQ"])  ##遍历游标
            for row in cursor:                 ##判断缺省值
                if row[1] == "0" or row[1] is None:
                    arcpy.AddError("FWJG属性存在0或者空值,请检查。")
                    arcpy.AddError("此mxd未整饰成功!")
                    export = "no"
                    break
                elif row[2] == "0" or row[2] is None:
                    arcpy.AddError("FWCS属性存在0或者空值,请检查。")
                    arcpy.AddError("此mxd未整饰成功!")
                    export = "no"
                    break
                elif row[3] is None:
                    arcpy.AddError("JGRQ属性存在0或者空值,请检查。")
                    arcpy.AddError("此mxd未整饰成功!")
                    export = "no"
                    break
                else:                       ##更新BZ字段并标注
                    if caculate:
                        row[0] = "3"+str(row[1])+"0"+str(row[2])+str(row[3])[0:4]
                        cursor.updateRow(row)
                        lyr.showLabels = True
                        for lblclass in lyr.labelClasses:
                            lblclass.showClassLabels = True
                            lblclass.expression = '"<FNT name=""黑体"" size=""8"">"&[BZ]&vbnewline&"<und>"&[JZMJ]&"</und>"&"</FNT>"'
                        export = "yes"
            mxd.save()

c 如果需要导出图片可加

    if (EXPORT):  ##导出图片
        if export == "yes":
            arcpy.mapping.ExportToJPEG(mxd, os.path.join(jpgpath, mxdFile[:-3] + 'jpg'), resolution=300)
            arcpy.AddMessage("正在输出宗地图...")
        else:
            arcpy.AddMessage("未输出宗地图...")
        del mxd

3、完整代码

# -*- coding: utf8 -*-
import arcpy
import os,time
dir = r'E:/'

def GetAllFiles(dir):
    allmxd = []
    dirs = os.listdir(dir)
    for mxdfile in dirs:
        if mxdfile[-3:].lower() == 'mxd':
            allmxd.append(mxdfile)
    return allmxd


def LabelAndExport(mxd):
    export = "yes"
    for lyr in arcpy.mapping.ListLayers(mxd):
        if lyr.name == "FW_J_320507":          ##FW进行标注
            fc = lyr.dataSource
            cursor = arcpy.da.UpdateCursor(fc,["BZ","FWJG","FWCS","JGRQ"])  ##遍历游标
            for row in cursor:                 ##判断缺省值
                if row[1] == "0" or row[1] is None:
                    arcpy.AddError("FWJG属性存在0或者空值,请检查。")
                    arcpy.AddError("此mxd未整饰成功!")
                    export = "no"
                    break
                elif row[2] == "0" or row[2] is None:
                    arcpy.AddError("FWCS属性存在0或者空值,请检查。")
                    arcpy.AddError("此mxd未整饰成功!")
                    export = "no"
                    break
                elif row[3] is None:
                    arcpy.AddError("JGRQ属性存在0或者空值,请检查。")
                    arcpy.AddError("此mxd未整饰成功!")
                    export = "no"
                    break
                else:                       ##更新BZ字段并标注
                    if caculate:
                        row[0] = "3"+str(row[1])+"0"+str(row[2])+str(row[3])[0:4]
                        cursor.updateRow(row)
                        lyr.showLabels = True
                        for lblclass in lyr.labelClasses:
                            lblclass.showClassLabels = True
                            lblclass.expression = '"<FNT name=""黑体"" size=""8"">"&[BZ]&vbnewline&"<und>"&[JZMJ]&"</und>"&"</FNT>"'
                        export = "yes"
            mxd.save()

    if (EXPORT):  ##导出图片
        if export == "yes":
            arcpy.mapping.ExportToJPEG(mxd, os.path.join(jpgpath, mxdFile[:-3] + 'jpg'), resolution=300)
            arcpy.AddMessage("正在输出宗地图...")
        else:
            arcpy.AddMessage("未输出宗地图...")
        del mxd


allmxds = GetAllFiles(dir)

for mxdFile in allmxds:

    arcpy.AddMessage(mxdFile + ":")

    arcpy.AddWarning("程序开始" + "  " + str(time.ctime()))

    mxd = arcpy.mapping.MapDocument(os.path.join(dir, mxdFile))

    LabelAndExport(mxd)

    arcpy.AddWarning("程序结束" + "  " + str(time.ctime()))

转载自:https://blog.csdn.net/qq_38350792/article/details/78684092

You may also like...