Argis uses Python's Arcpy third-party library to perform field calculations, export mxd to jpg images in batches, and merge databases

foreword

  Recently, the company needs to develop Arcgis scripting tools, so I went to learn how to use Arcpy to manipulate Arcgis data. Today I learned field calculation, and the learning results are recorded as follows.

  arcpy help document portal: https://resources.arcgis.com/zh-cn/help/main/10.2/
insert image description here

1. Field calculation

1. Perform field calculations in the form of python functions

  When we open the field calculator, we can call the improved function provided by the system to calculate the field. What if we need a custom field calculation function? How to do it?
insert image description here

  Click to display the code block to call up the pre-logic script code. We can write our custom python function in the pre-logic script code, so that we can call the custom python function to calculate the field.

insert image description here

  Click OK, the CL field is calculated successfully, and the results are as follows:
insert image description here

  The example code is as follows:

def reCal(fd):
    num = 0
    if fd =='村庄':
        num = 1
    else:
        num = 2
    return num

2. Perform field calculations in the form of python scripts

  If you have to write the python function into the pre-logic script code every time, it is very troublesome. We directly calculate the field in the form of python script.

The script code is as follows:

import arcpy
import sys

# 设置工作空间
arcpy.env.workspace = r'E:\arcpy_study\Arcpy实例教程(数据)_小景空间_原始\3Arcpy初识字段计算和python脚本\data.gdb'

# 遍历所有要素(遍历所有记录)
# 要遍历的图层,获取游标,遍历游标
cursor = arcpy.UpdateCursor("DLTB")
# 遍历每一行
for row in cursor:
    # 获取该行的DLMC列的值
    mc = row.getValue("DLMC")
    print mc
    if mc == u'村庄':
        num = 1
    else:
        num = 2
    # 设置该行的CL列的值为num
    row.setValue("CL", int(num))
    # 更新该行
    cursor.updateRow(row)

  Layer and file geodatabase structures are as follows:
insert image description here

  For the meaning of each piece of code, I can write a comment, and the calculation result is the same as method one.

2. Batch export mxd to jpg

  We need to use osand arcpythese two libraries to achieve this function. The os library is used to traverse the mxd files in the folder, and arcpy exports the mxd files to jpg format.

code show as below:

import os
import arcpy

# 解决python2.7中文路径乱码问题
path = unicode(r"E:\arcpy_study\Arcpy实例教程(数据)_小景空间_原始\4什么是Arcpy", 'utf-8')

# 遍历文件夹下的所有文件
# afile = 地图1.mxd  ........
for afile in os.listdir(path):
    # 遍历所有mxd文件
    if afile[-3:] == 'mxd':
        print afile
        # 获取mxd文件完整路径,装换为mxd类
        mxd = arcpy.mapping.MapDocument(os.path.join(path, afile))  # 拼接路径
        # 将mxd输出为jpg图片(mxd类, 输出路径,dpi)
        arcpy.mapping.ExportToJPEG(mxd, os.path.join(path, afile[:-3] + 'jpg'), resolution=300)

3. Merge database

# -*- coding: UTF-8 -*-
"""
@Date   :2023/6/25
"""
# 实现功能:将多个gdb合并成一个gdb(文件地理数据库)
import os
import arcpy


# 合并数据库函数
# (数据库引入路径, 输出路径,输出名称)
def MergeFileGDB(inPath, outPath, outName):
    # 设置工作空间为数据库所在的文件夹
    arcpy.env.workspace = inPath
    # 设置合并后的数据的输出路径  os.sep = \
    mergeFilePath = outPath + os.sep + outName
    # 获取所有文件地理数据库
    workspaces = arcpy.ListWorkspaces('*', 'FileGDB')

    # 遍历所有文件地理数据库
    for workspace in workspaces:
        print workspace
        # 合并数据库时,将工作空间变换为要合并的数据库
        arcpy.env.workspace = workspace
        # 判断输出的数据库是否存在
        mergeFileExist = os.path.exists(mergeFilePath)
        print mergeFileExist
        # 如果输出的数据库存在的话,采用追加的方式合并新的数据库,将新的数据库追加到输出的数据库中
        if mergeFileExist:
            featureClasses = arcpy.ListFeatureClasses()
            # 遍历数据库中的所有图层(要素类)
            for fc in featureClasses:
                print fc
                # 只合并LCA图层
                if fc == "LCA":
                    # os.path.splitext(fc)[0] = LCA
                    # (当前数据库中要合并的图层,输出数据库中要合并的图层)
                    arcpy.Append_management(fc, os.path.join(mergeFilePath, os.path.splitext(fc)[0]), "TEST", "", "")
                    break
                else:
                    continue
        # 如果输出数据库不存在,则先创建数据库,将当前数据库的要素类输出到输出数据库的要素类
        else:
            # 创建文件地理数据库
            arcpy.CreateFileGDB_management(outPath, outName)
            featureClasses = arcpy.ListFeatureClasses()
            for fc in featureClasses:
                if fc == "LCA":
                    # (当前数据库要合并的图层,输出数据库的路径,输出数据库中要合并的图层)
                    arcpy.FeatureClassToFeatureClass_conversion(fc, mergeFilePath, os.path.splitext(fc)[0])

if __name__ == '__main__':
    inPath = unicode(r'E:\arcpy_study\Arcpy实例教程(数据)_小景空间_原始\5Arcpy脚本批处理\data', 'utf-8')

    outPath = unicode(r'E:\arcpy_study\Arcpy实例教程(数据)_小景空间_原始\5Arcpy脚本批处理\data', 'utf-8')

    outName = 'Merge48.gdb'

    MergeFileGDB(inPath, outPath, outName)

 唧唧复唧唧,木兰当户织。不闻机杼声,唯闻女叹息。

问女何所思,问女何所忆。女亦无所思,女亦无所忆。昨夜见军帖,可汗大点兵,军书十二卷,卷卷有爷名。阿爷无大儿,木兰无长兄,愿为市鞍马,从此替爷征。

东市买骏马,西市买鞍鞯,南市买辔头,北市买长鞭。旦辞爷娘去,暮宿黄河边,不闻爷娘唤女声,但闻黄河流水鸣溅溅。旦辞黄河去,暮至黑山头,不闻爷娘唤女声,但闻燕山胡骑鸣啾啾。

万里赴戎机,关山度若飞。朔气传金柝,寒光照铁衣。将军百战死,壮士十年归。

归来见天子,天子坐明堂。策勋十二转,赏赐百千强。可汗问所欲,木兰不用尚书郎,愿驰千里足,送儿还故乡。

爷娘闻女来,出郭相扶将;阿姊闻妹来,当户理红妆;小弟闻姊来,磨刀霍霍向猪羊。开我东阁门,坐我西阁床,脱我战时袍,著我旧时裳。当窗理云鬓,对镜帖花黄。出门看火伴,火伴皆惊忙:同行十二年,不知木兰是女郎。

雄兔脚扑朔,雌兔眼迷离;双兔傍地走,安能辨我是雄雌? 

Guess you like

Origin blog.csdn.net/qq_47188967/article/details/131378315