【305】◀▶ ArcPy 相关功能实现
目录:
13. 批量修改栅格图层 Background Vlue 值
工具目录:
Dissolve_management 工具
Geometry.union 方法
MakeXYEventLayer_management 工具
RasterCalculator 工具Polygon 对象
Point 对象
Union_analysis 工具
MultipartToSinglepart_management 工具
DeleteField_management 工具
AddField_management 工具
Project_management 工具
SpatialReference 对象
Describe 对象
ApplySymbologyFromLayer_management 工具
黑色窗体参考代码:
- <table style="border-style: solid; border-color: #ffffff; border-width: 5px; background-color: #ffffff; width: 968px;" border="5" cellspacing="1" cellpadding="1">
- <tbody>
- <tr style="background-color: #333333;">
- <td style="border-style: solid; text-align: center; border-color: #ffffff; width: 50px; border-width: 3px; background-color: #333333;" colspan="5">
- <p class="Syntax_IDL"><a class="selected_IDL" href="http://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/data-management-toolbox/dissolve.htm" target="_blank"><span style="font-size: 15px; font-family: Georgia;"><strong>Dissolve (Data Management)</strong></span></a></p>
- </td>
- </tr>
- <tr style="background-color: #99ffcc;">
- <td style="border-style: solid; text-align: center; border-color: #ffffff; width: 50px; border-width: 3px; background-color: #333333;"><span style="font-family: Georgia; color: #ff9900;"><strong><span style="font-size: 16px;">01<br /></span></strong></span></td>
- <td style="border-style: solid none; text-align: center; border-color: #ffffff; width: 5px; border-width: 3px; background-color: #333333;"><span style="font-family: Georgia;"> </span></td>
- <td style="border-style: solid none; border-color: #ffffff; width: 350px; border-width: 3px; text-align: left; background-color: #333333;">
- <p><span style="font-size: 13px; font-family: Georgia;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">====<span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;"><<<<</span></span></span></span></span></span></span></span></span></strong></span></span> </span></span></span></span></span></span></span></span></span>Description <span style="font-size: 13px;"><span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">>>>></span></span></span></span></span></span></span></span></span></strong></span></span></strong></span></span></span><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">====</span></span></span></span></span></span></span></span></span></strong></span></span><br /></span></span></p>
- <p><span style="font-family: Georgia; font-size: 13px;"><span style="color: #99ccff;">基于指定属性聚合要素。<br /><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">----------------------------------------------------------------------------------<br /></span></span></span></span></span></span></span></p>
- <p><span style="font-family: Georgia; font-size: 13px;"><span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">====<span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;"><<<<</span></span></span></span></span></span></span></span></span></strong></span></span></span></span></span></span></span></span></span></span></span> Syntax <span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">>>>></span></span></span></span></span></span></span></span></span></strong></span></span><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">====</span></span></span></span></span></span></span></span></span><br /></strong></span></span></span></p>
- <p><span style="font-family: Georgia; font-size: 13px;"><span style="color: #99ffcc; background-color: #333333;"><strong><span style="color: #ff99cc;"><strong style="font-size: 14.4px !important; line-height: 19.6px !important;">Dissolve_management </strong></span></strong>(in_features, out_feature_class, {dissolve_field}, {statistics_fields}, {multi_part}, {unsplit_lines})<br /><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">----------------------------------------------------------------------------------</span></span></span></span></span><br /></span></span></p>
- <p><span style="font-size: 13px; font-family: Georgia;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">====<span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;"><<<<</span></span></span></span></span></span></span></span></span></strong> Parameter <span style="font-size: 13px;"><span style="color: #cc99ff;"><strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">>>>></span></span></span></span></span></span></span></span></span></strong><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px;"><span style="color: #99ccff;"><span style="font-size: 13px; color: #99ccff;"><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span class="IDLkeyword hint--top hint--rounded hint--bounce" data-hint="Executes one or more statements repeatedly, incrementing or decrementing a variable with each repetition, until a condition is met."><span style="color: #99ccff;"><span style="color: #ffcc00;">====</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></strong></span></span></span></span></span></p>
- <p><span style="font-family: Georgia;"><span style="font-size: 13px;"><span style="color: #99ccff;">◈ <span style="color: #99ccff;"><span style="font-size: 13px; color: #ffcc99;"><span style="font-size: 13px;">in_features</span></span>:要聚合的要素。<br /></span></span></span><span style="font-size: 13px;"><span style="font-family: Georgia;"><span style="font-size: 13px;"><span style="color: #99ccff;">◈ <span style="color: #99ccff;"><span style="font-size: 13px; color: #ffcc99;"><span style="font-size: 13px;">out_feature_class<span style="color: #99ccff;">:要创建的将包含聚合要素的要素类。</span></span></span></span></span></span></span><br /><span style="font-family: Georgia;"><span style="font-size: 13px;"><span style="color: #99ccff;">◈ <span style="color: #99ccff;"><span style="font-size: 13px; color: #ffcc99;"><span style="font-size: 13px;">dissolve_field<span style="color: #99ccff;">:要聚合要素的一个或多个字段。省略的话则对所有要素合并。</span></span></span></span></span></span></span></span></span></p>
- </td>
- <td style="border-style: solid none; border-color: #ffffff; width: 5px; border-width: 3px; text-align: left; background-color: #333333;"><span style="font-family: Georgia;"> </span></td>
- <td style="border-color: #ffffff; border-width: 3px; border-style: solid; background-color: #333333;"> </td>
- </tr>
- <tr style="background-color: #99ffcc;">
- <td style="border-style: solid; text-align: center; border-color: #ffffff; width: 50px; border-width: 3px; background-color: #333333;" colspan="5"> <span style="font-family: Georgia;"> </span><span style="font-family: Georgia;"> </span><span style="font-family: Georgia;"> </span><span style="font-family: Georgia;"> <br /></span></td>
- </tr>
- </tbody>
- </table>
1. 同一图层的多个要素合并
方法一:可以通过 Editor 对要素进行编辑,然后选中后通过使用 Merge 工具可以实现。利用 ArcPy 实现自动化处理,需要用到 Dissolve 工具,可以按照相同字段值进行合并融合。
参考:Alternative to 'Editors Merge' feature using Arcpy
1 |
====<<<< Description >>>>==== 基于指定属性聚合要素。 ====<<<< Syntax >>>>==== Dissolve_management (in_features, out_feature_class, {dissolve_field}, {statistics_fields}, {multi_part}, {unsplit_lines}) ====<<<< Parameter >>>>==== ◈ in_features:要聚合的要素。 |
|
||
|
||||
方法二:通过 Geometry 的 union 方法可以实现几何图形的合并,然后通过 CopyFeatures_Management 方法将生成的集合图形转换成 shp 文件。
参考代码如下:(若要实现所有数据合并需要通过循环实现)
- import arcpy
- # 获取当前 shp 文件的所有 Geometry,结果是一个 list
- geoms = arcpy.CopyFeatures_management("bou2_4p", arcpy.Geometry())
- geo1 = geoms[0]
- geo2 = geoms[1]
- # 以下两种均可以实现将两个 Geometry 合并,然后将生成的 Geometry 输出
- geo_new = geo1.union(geo2)
- arcpy.CopyFeatures_management(geo_new, "geo_new1.shp")
- geo_new01 = arcpy.Geometry.union(geo1, geo2)
- arcpy.CopyFeatures_management(geo_new01, "geo_new2.shp")
参考:CopyFeatures_management 官方说明
2. 导入带经纬度坐标的 txt 文件
在 Desktop 中实现,需要执行如下操作:File -> Add Data -> Add XY Data... 。通过下表的工具可以实现自动处理功能。
2 |
====<<<< Description >>>>==== 根据源表中定义的 X 和 Y 坐标创建新的点要素图层。如果源表包含 Z 坐标(高程值),则可以在创建事件图层时指定该字段。由此工具创建的图层是临时图层。相当于 File -> Add Data -> Add XY Data ====<<<< Syntax >>>>==== MakeXYEventLayer_management (table, in_x_field, in_y_field, out_layer, {spatial_reference}, {in_z_field}) ====<<<< Parameter >>>>==== ◈ table:定义要创建的点要素位置的表(包含 X 和 Y 坐标)。 |
|
||
|
||||
3. 栅格计算器的实现
栅格计算器在窗体实现比较方便,通过代码实现要注意表达式的书写问题。
3 |
====<<<< Description >>>>==== 在类似计算器的界面中,使用 Python 语法构建和执行单个地图代数表达式。 注意:引用函数的名称为:arcpy.gp.RasterCalculator_sa ====<<<< Syntax >>>>==== RasterCalculator (expression, output_raster) ====<<<< Parameter >>>>==== ◈ expression:“地图代数”表达式。(表达式通过单引号连接,因为双引号需要显示在SQL语句中,例如变量名称) |
|
||
|
||||
4. 添加 shp 文件(显示在 ArcMap)
ArcGIS 添加 shp 文件很容易,但是没有可以直接添加 shp 文件的 ArcPy 方法,实现代码如下所示:
- # 获取当前的 mxd
- mxd = arcpy.mapping.MapDocument("CURRENT")
- # 获取当前的 data frame
- df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
- # 获取 shp 文件路径
- theShape = r"E:\zgl.shp"
- # 定义 shp 文件为图层
- addLayer = arcpy.mapping.Layer(theShape)
- # 将图层添加到当前的 data frame 中
- arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
- # 刷新视图
- arcpy.RefreshActiveView()
- arcpy.RefreshTOC()
5. 通过经纬度坐标生成 Polygon
方法一: 通过多个工具实现,步骤如下
- 将经纬度坐标写入到 txt 文件中,注意收尾的点重合,否则无法合成多边形;
- 利用 MakeXYEventLayer_management 工具将 txt 文件生成 Point 文件;
- 利用 PointsToLine_management 工具将 Point 转换成 Polyline;
- 利用 FeatureToPolygon_management 工具将 Polyline 转换成 Polygon。
方法二:利用 arcpy.Polygon 和 arcpy.Point 来实现,创建 Polyline 类似
实现代码如下:
- # 创建经纬度列表
- coords = [(1, 2), (1, -2), (-1, -2), (-1, 2), (1, 2)]
- # 创建 Array
- ar = arcpy.Array()
- # 将经纬度以 Point 的形式添加到 Array 中
- for x, y in coords:
- ar.add(arcpy.Point(x, y))
- # 创建 Polygon 并输出
- polygon = arcpy.Polygon(ar)
- arcpy.CopyFeatures_management(polygon, "polygon01")
参考: Create a shapefile from geometry
6. 多边形重叠区域次数计算(赤潮发生频次)
步骤如下:
- 将文件名中的点 “.” 去掉,参考博文 删除文件名中的点 “.”;
- 由于原 shp 文件中存在一个文件里面存储多个 polygon,不利于计算,需要通过 Dissolve 工具将其合并为一个 polygon,同时由于原 shp 文件存储在不同的文件夹,在操作此工具时将其存储在一个新的文件夹(/tmp/dissolve);
- 通过 Union 工具,将 2 中生成的所有 shp 文件合并,生成一个 union.shp,此文件是将上面所有文件炸裂的效果;
需要通过 MultipartToSinglepart_management 工具进一步处理;
需要通过 Project 工具统一空间参考; - 通过遍历 union.shp 中每一个 polygon,并与 2 中的每一个 shp 文件进行比较,如果存在包含关系,则增加 1,最终可以计算每一个斑块的出现次数。
参考代码如下:
- import arcpy
- arcpy.env.workspace = r"D:\01-Working\2018\20180411-HAD_FREQ\tmp"
- # 通过 union 工具可以将 多个polygon 打碎成小块
- arcpy.Union_analysis(["New_Shapefile", "New_ShapefileCopy", "New_ShapefileCopy2"], "union")
- mxd = arcpy.mapping.MapDocument("current")
- df = arcpy.mapping.ListDataFrames(mxd)[0]
- shps = arcpy.ListFeatureClasses()
- # 选取一列都是 0 的数据
- cursor = arcpy.da.UpdateCursor("union", "Id")
- # 用来记录列表的索引值
- i = 0
- # 获取图斑的所有 Geometry
- geoms = arcpy.CopyFeatures_management("union", arcpy.Geometry())
- for row in cursor:
- geom = geoms[i]
- for shp in shps:
- geo2 = arcpy.CopyFeatures_management(shp, arcpy.Geometry())
- g2 = geo2[0]
- # 判断包含关系,如果成立,则增加1,以此类推
- if g2.contains(geom):
- row[0] = row[0] + 1
- cursor.updateRow(row)
- i = i + 1
- # 最终 Id 列就会显示每一个斑块的出现频率
Union 工具说明
4 |
====<<<< Description >>>>==== 计算输入要素的几何并集。将所有要素及其属性都写入输出要素类。。 ====<<<< Syntax >>>>==== Union_analysis (in_features, out_feature_class, {join_attributes}, {cluster_tolerance}, {gaps}) ====<<<< Parameter >>>>==== ◈ in_features:输入要素类或图层列表。 |
|
||
|
||||
参考:对点数据求buffer生成圆后,如何对这些圆的相交部分进行重叠次数统计,并按重叠的次数的不同进行渲染?
特别说明,在处理多重叠文件后,会有一部分生成了 Multipart 的部分,因此需要通过 MultipartToSinglepart_management 将其进一步转化,如下所示:
5 |
====<<<< Description >>>>==== 创建包含通过分割多部件输入要素而生成的单部件要素的要素类。 ====<<<< Syntax >>>>==== MultipartToSinglepart_management (in_features, out_feature_class) ====<<<< Parameter >>>>==== ◈ in_features:任意要素类型的输入要素(单一文件)。 |
|
||
|
||||
7. 批量删除/添加 shp 文件字段
通过 union 工具生成的 shp 文件含有超多的字段,手动删除很麻烦,因此需要借用 DeleteField_management 工具来实现。
6 |
====<<<< Description >>>>==== 此工具可从表、要素类、要素图层或栅格数据集中删除一个或多个字段。 ====<<<< Syntax >>>>==== DeleteField_management (in_table, drop_field) ====<<<< Parameter >>>>==== ◈ in_table:包含要删除字段的表。将修改现有输入表。(矢量图层) |
|
||
7 |
====<<<< Description >>>>==== 向表或要素类表、要素图层、栅格目录和/或带属性表的栅格添加新字段。 ====<<<< Syntax >>>>==== AddField_management (in_table, field_name, field_type, {field_precision}, {field_scale}, {field_length}, {field_alias}, {field_is_nullable}, {field_is_required}, {field_domain}) ====<<<< Parameter >>>>==== ◈ in_table:要添加指定字段的输入表。该字段将被添加到现有输入表,并且不会创建新的输出表。 |
|
||
|
||||
参考:字段工具集概述
“字段”工具集包含一组用于向要素类表添加字段或对该表中的字段进行更改的工具。字段是表中的列;每个字段包含一个属性的值。表中可包含任意数量的字段。可指定字段设置,如字段类型和可存储在字段中的最大数据量。
8. 批量修改文件的 Spatial Reference456852
承接 6 中的问题,由于 shp 文件提取是通过 ENVI 基于不同的卫星提取的,因此 shp 文件自动加入不同的卫星的 Spatial Reference,导致在进行 contains 计算时出现错误,因此需要将所有的 shp 文件批量转化成相同的 Spatial Reference。
通过 Project 工具实现,由于相关参数过于复杂,可以通过工具箱操作一次,然后获取相应代码,在进行批量的操作。
8 |
====<<<< Description >>>>==== 将空间数据从一种坐标系投影到另一种坐标系。 ====<<<< Syntax >>>>==== Project_management (in_dataset, out_dataset, out_coor_system, {transform_method}, {in_coor_system}, {preserve_shape}, {max_deviation}) ====<<<< Parameter >>>>==== ◈ in_dataset:要投影的要素类、要素图层或要素数据集。 |
|
||
|
||||
参考:有关坐标系名称和工厂代码的详细信息,请参阅 geographic_coordinate_systems.pdf 和 projected_coordinate_systems.pdf ArcGIS 文档文件夹中的文件。有关详细信息,请参阅使用空间参考类。
9. 读取折线或面几何(点坐标)
- Polygon 由多个包含 Point 的 Array 组成
- 首先需要读取 Polygon 的每一个部分,也就是一个 Array
- 其次读取每个 Array 中的 Point
- Polyline/Polygon 解析:
一个 Polyline/Polygon 含有多个部分,需要通过 for 循环读取,每个部分是一个 Array 对象(包含 Point 对象)
一个 Array 对象内部包括 N 个 Point 对象,需要通过 for 循环读取每个 Point 对象,并获取其 XY 坐标值
- # 获取所有 polygon
- >>> cursor = arcpy.da.SearchCursor("CNTRY92", "SHAPE@")
- >>> shapes = [row[0] for row in cursor]
- >>> len(shapes)
- 147
- # 获取第一个 polygon
- >>> shape_0 = shapes[0]
- >>> shape_0
- <Polygon object at 0xac33290[0xad06f00]>
- # 获取 polygon 的每一部分,每个 part 是一个 Array 对象
- >>> for part in shape_0:
- ... for pnt in part: # 每个 pnt 对应着每一个 Point 对象
- ... print("{}, {}".format(pnt.X, pnt.Y)) # 获取每个 Point 对象的属性
- ...
- 6.02716398239, 49.5240783691
- 6.08806419373, 49.6805076599
- 5.94872617722, 49.7722320557
- 5.94158744812, 49.9194297791
- 6.03742027283, 50.0643806458
参考:读取折线或面几何
10. 读取 Geometry(2种方法)
方法一:通过 CopyFeatures_management 工具直接读取 shp 文件的 Geometry,得到的结果是一个 Polygon 列表,然后可以读取每一个 Polygon,对于 Polygon 的读取符合 9 中规律。(1 个 Polygon 由多个 Array 组成,每个 Array 都是由 Point 组成)
- # 获取文件中所有的 Polygon,是一个列表文件
- >>> polygons = arcpy.CopyFeatures_management("CNTRY92", arcpy.Geometry())
- >>> type(polygons)
- <type 'list'>
- # 获取第一个 Polygon
- >>> polygon_1 = polygons[0]
- # 将此 Polygon 的所有部分组成一个列表,为多个 Array
- >>> polygon_1s = [pnt for pnt in polygon_1]
- # 获取第一个 Array,含有所有 Point
- >>> polygon = polygon_1s[0]
- >>> type(polygon)
- <class 'arcpy.arcobjects.arcobjects.Array'>
方法二:通过 da.SearchCursor 方法读取 "SHAPE@" 字段获取 Geometry,通过遍历游标可以获取 Polygon 列表,其他与 方法一 类似。
- # 查询要素几何,并将其赋值为 Polygon 列表,其他与 方法一 一致
- >>> cursor = arcpy.da.SearchCursor("CNTRY92", "SHAPE@")
- >>> polygons = [row[0] for row in cursor]
参考:读取几何
- # 通过循环获取每一个要素
- for row in arcpy.da.SearchCursor("test.shp", ["OID@", "SHAPE@"]):
- # 通过循环获取要素的每一个部分(Array)
- for part in row[1]:
- # 通过循环获取 Array 中的每一个 Point 信息
- for pnt in part:
- print("{}, {}".format(pnt.X, pnt.Y))
11. 批量修改图例显示内容
所谓修改图例内容,需要修改下图中每一个 label 的内容,如果在 ArcMap 中修改需要逐一修改对应 TOC 内的标签或者 Symbology 里面的标签。然而通过 ArcPy 批量修改图例的显示标签内容也需要批量修改 Symbology 对应的标签值。下面为实现效果和对应代码:
- >>> import arcpy
- >>> mxd = arcpy.mapping.MapDocument("current")
- >>> df = arcpy.mapping.ListDataFrames(mxd)[0]
- >>> lyr = arcpy.mapping.ListLayers(df)[0]
- >>> sym = lyr.symbology
- # 获取标签列表
- >>> list_labels = sym.classLabels
- >>> list_labels
- [u'Africa', u'Antarctica', u'Asia', u'Australia', u'Europe',
- u'North America', u'Oceania', u'South America']
- # 修改标签内容并赋值
- >>> list_labels_new = [f.upper() for f in list_labels]
- >>> list_labels_new
- [u'AFRICA', u'ANTARCTICA', u'ASIA', u'AUSTRALIA',
- u'EUROPE', u'NORTH AMERICA', u'OCEANIA', u'SOUTH AMERICA']
- >>> sym.classLabels = list_labels_new
- >>> arcpy.RefreshActiveView()
参考:mapping.UniqueValuesSymbology
12. 批量引用图层样式(Symbology)
需要现在一个图层上建立相应的样式,也就是 Symbology,然后通过 UpdateLayer 方法可以将目标图层的样式进行引用。
第一幅图通过 mapping.UpdateLayer 方法实现的,可见,两者具有相同的分类
第二幅图通过 ApplySymbologyFromLayer_management 工具实现的,此工具将来自图层的符号系统应用到输入图层。
- # 方法一
- >>> arcpy.mapping.UpdateLayer(df, lyrs[0], lyrs[1])
- # 方法二
- >>> arcpy.ApplySymbologyFromLayer_management(lyrs[0],lyrs[1])
13. 批量修改栅格图层 Background Vlue 值
对于导入 ArcMap 中的遥感影像一般会有黑色的边框,此部分为没有数据的区域,通常通过 Layer Properties》Symbology》RGB Composite》Display Background Value 复选框选中,可以将此部分赋值为透明,而针对大量图层,可以通过 ApplySymbologyFromLayer_management 工具实现,前提需要有一个图层设置完成,后面的图层引用相同的样式即可。
若 lyrs[1] 是折叠图层,操作如下代码后 lyrs[0] 也会自动变成折叠图层。
- >>> arcpy.ApplySymbologyFromLayer_management(lyrs[0],lyrs[1])
参考:Python to set Layer properties/symbology/display back ground value RGB 0 0 0
参考:Why is ArcPy script to apply symbology not working?
14. 同一图层的多个要素合并
15. 同一图层的多个要素合并
【305】◀▶ ArcPy 相关功能实现的更多相关文章
- 测试functional的bind以及相关功能
注:在VS2010 UPDATE1下测试通过 /*测试functional的bind以及相关功能*/ #include <iostream> #include <functional ...
- JobTracker等相关功能模块初始化
[Hadoop代码笔记]Hadoop作业提交之JobTracker等相关功能模块初始化 一.概要描述 本文重点描述在JobTracker一端接收作业.调度作业等几个模块的初始化工作.想过模块的介绍会在 ...
- 在github上最热门好评高的ROS相关功能包
在github上最热门最受欢迎的ROS相关功能包 下面依次列出,排名不分先后: 1 Simulation Tools In ROS https://github.com/ros-simulation ...
- Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系
Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 现在的二维码可谓是烂大街了,到处都是二维码,什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...
- LoadRunner(四)——深度了解LR相关功能
参考学习感谢:<精通软件性能测试与LoadRunner实战> 相关功能: 1 无工具情况下的性能测试 2性能测试工具LoadRunner的工作原理 3 VuGen应用介绍 4 协议的类型及 ...
- Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系
Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 如今的二维码可谓是烂大街了.到处都是二维码.什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...
- 零元学Expression Blend 4 - Chapter 25 以Text相关功能就能简单做出具有设计感的登入画面
原文:零元学Expression Blend 4 - Chapter 25 以Text相关功能就能简单做出具有设计感的登入画面 本章将交大家如何运用Blend 4 内的Text相关功能做出有设计感的登 ...
- Excel催化剂开源第31波-pdf相关功能实现及类库介绍
在Excel催化剂刚推出的pdf相关功能中,反馈很热烈,不止是用户层面好多人喜欢,也听到在.NET开发群里有询问pdf在winform上展现的功能诉求,一段时间没写开源篇,生怕大家以为Excel催化剂 ...
- Nginx Rewrite相关功能-防盗链
Nginx Rewrite相关功能-防盗链 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
随机推荐
- log4j的使用配置
1.与spring整合,web.xml中配置详情 <!-- 加载log4j的配置文件log4j.properties --> <context-param> <param ...
- 监听器(Listener)学习(二)在开发中的常见应用
监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用: 统计当前在线人数 自定义Session扫描器 一.统计当前在线人数 在JavaWeb应用开发中,有时候 ...
- PHP 数组转json_encode,单个数组下标为了0时不对??
在 php 数组转json时,假如 有一个数组下标是顺序的,他json_encode后会直接变成一个简版二维json, $arr = ['1'=>1,'2'=>2]; echo (json ...
- oracle之 等待事件LOG FILE SYNC (awr)优化
log file sycn是ORACLE里最普遍的等待事件之一,一般log file sycn的等待时间都非常短 1-5ms,不会有什么问题,但是一旦出问题,往往都比较难解决.什么时候会产生log f ...
- Linux之 find之 ctime,atime,mtime
在Linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime atime 最后一次访问时间, 如 ls, more 等, 但 chmod, chow ...
- http请求发生了两次(options请求)
前言 自后台restful接口流行开来,请求了两次的情况(options请求)越来越普遍.笔者也在实际的项目中遇到过这种情况,做一下整理总结. 文章书写思路: 为什么发生两次请求 http的请求方式, ...
- TOMCATE8下面项目启动问题
1.将servlet-api.jar替换项目中的servlet-api2.4 2.<servlet> <servlet-name>dwr-invoker&l ...
- WPF ComboBox下拉绑定Treeview 功能的实现
因为项目需要,接触到这个功能点,借助网络还有自己的一点摸索,实现了这个功能.相关代码如下: XAML部分的代码: <ComboBox Grid.Row=" RenderTransfor ...
- 解决Intel SSD 330i 240G在Windows 8启动慢的问题
笔者半年前更新了一下老迈的Thinkpad T410i笔记本电脑,在美国亚马逊海购了Intel SSD 330i 240G.(PS:购入价USD 129.99 ,现在最新的335i 240G差不多要U ...
- laravel里面使用event
模式:大概是通过一个自定义的event,一个handler,还有一个binder,然后用来简化通知模型 生成自定义的event ./artisan make:event MyEvent 生成自定义的h ...