PythonArcPyExcel数据读取导入图层IDW插值批量掩膜
1 任务需求
首先,我们来明确一下本文所需实现的需求。
部分PM2.5浓度监测站点23个逐小时PM2.5浓度数据Excel各PM2.5浓度监测站点PM2.5PM2.5IDW
各PM2.5浓度监测站点
其中,该矢量图层还包括属性表,属性表内容包括每一个站点的编号、地理位置与中文名称,如下图所示。
部分PM2.5浓度监测站点23个逐小时PM2.5浓度数据ExcelPM2.5
2 代码实现
PythonArcPy
ArcMapPython程序脚本新建工具箱与自定义工具arcpy.GetParameterAsText()IDLEPython程序脚本新建工具箱与自定义工具
arcpy.env.workspacecsv_path.csvshape_file_pathboundary_file_pathspatial_resolutionIDWpowerIDWlook_pointIDWmax_distanceIDWidw_result_dirIDWmask_result_dirIDW
pd.read_csv部分PM2.5浓度监测站点23个逐小时PM2.5浓度数据Excel各PM2.5浓度监测站点232323ExcelIdwExtractByMaskIDW
具体代码如下。
1# -*- coding: utf-8 -*-
2# @author: ChuTianjia
3
4import
5import
6importas
7fromimport
8
90
101
112
128
133
149
154
165
176
187
19
200"gbk"
21
22"hour"
23
240forin1-3
25
26forin31
27forin
28-3
29
30"hour_00""hour_01""hour_02""hour_03""hour_04""hour_05"
31"hour_06""hour_07""hour_08""hour_09""hour_10"
32"hour_11""hour_12""hour_13""hour_14""hour_15"
33"hour_16""hour_17""hour_18""hour_20"
34"hour_21""hour_22""hour_23"
35
360"Name"
37
38# Update the columns in the attribute table
39forin
40"SHORT"
41
420
43
44withas
45forin
46forin
47if0
48forin
491
50
51
52# Find stations that without any data
53if0notin
54
551
560
57
58"Delete {0} site(s) that do not contain any data, and the site(s) name is(are):"
59forin
60
61"\n"
62
63# Perform IDW interpolation
64
65forin
66
67"\\""BJ_"".tif"
68
69"{0} has completed IDW interpolation."
70
71# Perform mask
72"BJ_hour_*""TIF"
73forin
74
75"\\"".tif""_Mask.tif"
76
77"{0} has been masked."".tif"
3 运行结果
ArcMap
例如,下图所示提示可以知道有哪几个站点是没有数据、从而被剔除的。
下图则可以显示出目前代码的运行情况。
同时,在我们设定的结果文件夹中可以看到,23小时的插值图与掩膜图都将自动生成并保存在指定文件夹。
再来看看具体的图片长什么样子。
IDW
IDW