【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>
方法一:可以通过 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 文件。
参考代码如下:(若要实现所有数据合并需要通过循环实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 官方说明
在 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 |
====<<<< Description >>>>==== 在类似计算器的界面中,使用 Python 语法构建和执行单个地图代数表达式。 注意:引用函数的名称为:arcpy.gp.RasterCalculator_sa ====<<<< Syntax >>>>==== RasterCalculator (expression, output_raster) ====<<<< Parameter >>>>==== ◈ expression:“地图代数”表达式。(表达式通过单引号连接,因为双引号需要显示在SQL语句中,例如变量名称) |
|
||||
|
||||||
ArcGIS 添加 shp 文件很容易,但是没有可以直接添加 shp 文件的 ArcPy 方法,实现代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 获取当前的 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() |
方法一: 通过多个工具实现,步骤如下
- 将经纬度坐标写入到 txt 文件中,注意收尾的点重合,否则无法合成多边形;
- 利用 MakeXYEventLayer_management 工具将 txt 文件生成 Point 文件;
- 利用 PointsToLine_management 工具将 Point 转换成 Polyline;
- 利用 FeatureToPolygon_management 工具将 Polyline 转换成 Polygon。
方法二:利用 arcpy.Polygon 和 arcpy.Point 来实现,创建 Polyline 类似
实现代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | # 创建经纬度列表 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
步骤如下:
- 将文件名中的点 “.” 去掉,参考博文 删除文件名中的点 “.”;
- 由于原 shp 文件中存在一个文件里面存储多个 polygon,不利于计算,需要通过 Dissolve 工具将其合并为一个 polygon,同时由于原 shp 文件存储在不同的文件夹,在操作此工具时将其存储在一个新的文件夹(/tmp/dissolve);
- 通过 Union 工具,将 2 中生成的所有 shp 文件合并,生成一个 union.shp,此文件是将上面所有文件炸裂的效果;
需要通过 MultipartToSinglepart_management 工具进一步处理;
需要通过 Project 工具统一空间参考; - 通过遍历 union.shp 中每一个 polygon,并与 2 中的每一个 shp 文件进行比较,如果存在包含关系,则增加 1,最终可以计算每一个斑块的出现次数。
参考代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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:任意要素类型的输入要素(单一文件)。 |
|
||||
|
||||||
|
通过 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 文档文件夹中的文件。有关详细信息,请参阅使用空间参考类。
- Polygon 由多个包含 Point 的 Array 组成
- 首先需要读取 Polygon 的每一个部分,也就是一个 Array
- 其次读取每个 Array 中的 Point
- Polyline/Polygon 解析:
一个 Polyline/Polygon 含有多个部分,需要通过 for 循环读取,每个部分是一个 Array 对象(包含 Point 对象)
一个 Array 对象内部包括 N 个 Point 对象,需要通过 for 循环读取每个 Point 对象,并获取其 XY 坐标值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # 获取所有 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 |
参考:读取折线或面几何
方法一:通过 CopyFeatures_management 工具直接读取 shp 文件的 Geometry,得到的结果是一个 Polygon 列表,然后可以读取每一个 Polygon,对于 Polygon 的读取符合 9 中规律。(1 个 Polygon 由多个 Array 组成,每个 Array 都是由 Point 组成)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # 获取文件中所有的 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 列表,其他与 方法一 类似。
1 2 3 | # 查询要素几何,并将其赋值为 Polygon 列表,其他与 方法一 一致 >>> cursor = arcpy.da.SearchCursor( "CNTRY92" , "SHAPE@" ) >>> polygons = [row[ 0 ] for row in cursor] |
参考:读取几何
1 2 3 4 5 6 7 | # 通过循环获取每一个要素 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)) |
所谓修改图例内容,需要修改下图中每一个 label 的内容,如果在 ArcMap 中修改需要逐一修改对应 TOC 内的标签或者 Symbology 里面的标签。然而通过 ArcPy 批量修改图例的显示标签内容也需要批量修改 Symbology 对应的标签值。下面为实现效果和对应代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> 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
需要现在一个图层上建立相应的样式,也就是 Symbology,然后通过 UpdateLayer 方法可以将目标图层的样式进行引用。
第一幅图通过 mapping.UpdateLayer 方法实现的,可见,两者具有相同的分类
第二幅图通过 ApplySymbologyFromLayer_management 工具实现的,此工具将来自图层的符号系统应用到输入图层。
1 2 3 4 | # 方法一 >>> 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] 也会自动变成折叠图层。
1 | >>> 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?
posted on 2018-04-03 15:09 McDelfino 阅读(1778) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理