目录:

1. 同一图层的多个要素合并(2种方法)

2. 导入带经纬度坐标的 txt 文件

3. 栅格计算器的实现

4. 添加 shp 文件(显示在 ArcMap)

5. 通过经纬度坐标生成 Polygon

6. 多边形重叠区域次数计算(赤潮发生频次)

7. 批量删除/添加 shp 文件字段

8. 批量修改文件的 Spatial Reference

9. 读取折线或面几何(点坐标)

10. 读取 Geometry(2种方法)

11. 批量修改图例显示内容

12. 批量引用图层样式(Symbology)

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 工具


黑色窗体参考代码:

  1. <table style="border-style: solid; border-color: #ffffff; border-width: 5px; background-color: #ffffff; width: 968px;" border="5" cellspacing="1" cellpadding="1">
  2. <tbody>
  3. <tr style="background-color: #333333;">
  4. <td style="border-style: solid; text-align: center; border-color: #ffffff; width: 50px; border-width: 3px; background-color: #333333;" colspan="5">
  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>
  6. </td>
  7. </tr>
  8. <tr style="background-color: #99ffcc;">
  9. <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>
  10. <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;">&nbsp;</span></td>
  11. <td style="border-style: solid none; border-color: #ffffff; width: 350px; border-width: 3px; text-align: left; background-color: #333333;">
  12. <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;">&lt;&lt;&lt;&lt;</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;">&gt;&gt;&gt;&gt;</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>
  13. <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>
  14. <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;">&lt;&lt;&lt;&lt;</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;">&gt;&gt;&gt;&gt;</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>
  15. <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&nbsp;</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>
  16. <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;">&lt;&lt;&lt;&lt;</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;">&gt;&gt;&gt;&gt;</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>
  17. <p><span style="font-family: Georgia;"><span style="font-size: 13px;"><span style="color: #99ccff;">◈&nbsp; <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;">◈&nbsp; <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;">◈&nbsp; <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>
  18. </td>
  19. <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;">&nbsp;</span></td>
  20. <td style="border-color: #ffffff; border-width: 3px; border-style: solid; background-color: #333333;">&nbsp;</td>
  21. </tr>
  22. <tr style="background-color: #99ffcc;">
  23. <td style="border-style: solid; text-align: center; border-color: #ffffff; width: 50px; border-width: 3px; background-color: #333333;" colspan="5">&nbsp;<span style="font-family: Georgia;">&nbsp;</span><span style="font-family: Georgia;">&nbsp;</span><span style="font-family: Georgia;">&nbsp;</span><span style="font-family: Georgia;"> <br /></span></td>
  24. </tr>
  25. </tbody>
  26. </table>

1. 同一图层的多个要素合并

  方法一:可以通过 Editor 对要素进行编辑,然后选中后通过使用 Merge 工具可以实现。利用 ArcPy 实现自动化处理,需要用到 Dissolve 工具,可以按照相同字段值进行合并融合。

  参考:Alternative to 'Editors Merge' feature using Arcpy

Dissolve (Data Management)

1
 

====<<<< Description >>>>====

基于指定属性聚合要素。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

Dissolve_management (in_features, out_feature_class, {dissolve_field}, {statistics_fields}, {multi_part}, {unsplit_lines})
----------------------------------------------------------------------------------

====<<<< Parameter >>>>====

◈  in_features:要聚合的要素。
◈  out_feature_class:要创建的将包含聚合要素的要素类。
◈  dissolve_field:要聚合要素的一个或多个字段。省略的话则对所有要素合并。

 
  1. # 将国家合并成一个要素
  2. arcpy.Dissolve_management("CNTRY92", "world")
  3.  
  4. # 将国家按照 Continent 字段按照大洲合并
  5. arcpy.Dissolve_management("CNTRY92", "world_01", "continent")
   
  1. --------------------------------------------------
   
    

  方法二:通过 Geometry 的 union 方法可以实现几何图形的合并,然后通过 CopyFeatures_Management 方法将生成的集合图形转换成 shp 文件。

  参考代码如下:(若要实现所有数据合并需要通过循环实现)

  1. import arcpy
  2.  
  3. # 获取当前 shp 文件的所有 Geometry,结果是一个 list
  4. geoms = arcpy.CopyFeatures_management("bou2_4p", arcpy.Geometry())
  5.  
  6. geo1 = geoms[0]
  7. geo2 = geoms[1]
  8.  
  9. # 以下两种均可以实现将两个 Geometry 合并,然后将生成的 Geometry 输出
  10. geo_new = geo1.union(geo2)
  11. arcpy.CopyFeatures_management(geo_new, "geo_new1.shp")
  12.  
  13. geo_new01 = arcpy.Geometry.union(geo1, geo2)
  14. arcpy.CopyFeatures_management(geo_new01, "geo_new2.shp")

  参考:Geometry 官方说明

  参考:CopyFeatures_management 官方说明


2. 导入带经纬度坐标的 txt 文件

  在 Desktop 中实现,需要执行如下操作:File -> Add Data -> Add XY Data... 。通过下表的工具可以实现自动处理功能。

Make XY Event Layer (Data Management)

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 坐标)。
◈  in_x_field:输入表中包含 X 坐标的字段。
◈  in_y_field:输入表中包含 Y 坐标的字段。
◈  out_layer:输出点事件图层的名称。

 
  1. # 获取内部的数据
  2. files_txt = arcpy.ListFiles()
  3.  
  4. # 对数据进行遍历,并执行工具操作,命名为 txt 文件的名称
  5. for f in files_txt:
  6. # 将txt数据导入
  7. arcpy.MakeXYEventLayer_management(f, "Longitude", "Latitude", f)
   
  1. --------------------------------------------------
   
     

3. 栅格计算器的实现

  栅格计算器在窗体实现比较方便,通过代码实现要注意表达式的书写问题。

Raster Calculator (Spatial Analyst)

3  

====<<<< Description >>>>====

在类似计算器的界面中,使用 Python 语法构建和执行单个地图代数表达式。

注意:引用函数的名称为:arcpy.gp.RasterCalculator_sa
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

RasterCalculator (expression, output_raster)
----------------------------------------------------------------------------------

====<<<< Parameter >>>>====

◈  expression:“地图代数”表达式。(表达式通过单引号连接,因为双引号需要显示在SQL语句中,例如变量名称)
◈  output_raster:生成输出的信息。(不写扩展名的话会生成带有文件夹形式的栅格数据,如果写 *.tif,则直接生成 tif 文件)

 
  1. lyrs = arcpy.mapping.ListLayers(mxd)
  2.  
  3. # 注意写表达式的时候不能有字符串函数什么的,而且变量名需要用双引号
  4. # 因此可以先print下效果
  5. # SQL函数很无语
  6.  
  7. for i in range(9, 21):
  8. arcpy.gp.RasterCalculator_sa('8.64012 - 14.84545*Log10("' \
  9. + lyrs[i].name + '") + 10.54793*Power(Log10("' + lyrs[i].name \
  10. + '"), 2) - 3.45375 * Power(Log10("' + lyrs[i].name + '"), 3) \
  11. + 0.4254 * Power(Log10("' + lyrs[i].name + '"), 4)', lyrs[i].name + "_sd.tif")
   
  1. --------------------------------------------------
   
     

4. 添加 shp 文件(显示在 ArcMap)

  ArcGIS 添加 shp 文件很容易,但是没有可以直接添加 shp 文件的 ArcPy 方法,实现代码如下所示:

  1. # 获取当前的 mxd
  2. mxd = arcpy.mapping.MapDocument("CURRENT")
  3. # 获取当前的 data frame
  4. df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
  5. # 获取 shp 文件路径
  6. theShape = r"E:\zgl.shp"
  7. # 定义 shp 文件为图层
  8. addLayer = arcpy.mapping.Layer(theShape)
  9. # 将图层添加到当前的 data frame 中
  10. arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
  11. # 刷新视图
  12. arcpy.RefreshActiveView()
  13. arcpy.RefreshTOC()

参考:如何通过Arcpy打开mxd文档并添加数据


5. 通过经纬度坐标生成 Polygon

  方法一: 通过多个工具实现,步骤如下

  1. 将经纬度坐标写入到 txt 文件中,注意收尾的点重合,否则无法合成多边形;
  2. 利用 MakeXYEventLayer_management 工具将 txt 文件生成 Point 文件;
  3. 利用 PointsToLine_management 工具将 Point 转换成 Polyline;
  4. 利用 FeatureToPolygon_management 工具将 Polyline 转换成 Polygon。

  方法二:利用 arcpy.Polygonarcpy.Point 来实现,创建 Polyline 类似

  实现代码如下:

  1. # 创建经纬度列表
  2. coords = [(1, 2), (1, -2), (-1, -2), (-1, 2), (1, 2)]
  3.  
  4. # 创建 Array
  5. ar = arcpy.Array()
  6.  
  7. # 将经纬度以 Point 的形式添加到 Array 中
  8. for x, y in coords:
  9. ar.add(arcpy.Point(x, y))
  10.  
  11. # 创建 Polygon 并输出
  12. polygon = arcpy.Polygon(ar)
  13. arcpy.CopyFeatures_management(polygon, "polygon01")

  参考: Create a shapefile from geometry


6. 多边形重叠区域次数计算(赤潮发生频次)

  步骤如下:

  1. 将文件名中的点 “.” 去掉,参考博文 删除文件名中的点 “.”
  2. 由于原 shp 文件中存在一个文件里面存储多个 polygon,不利于计算,需要通过 Dissolve 工具将其合并为一个 polygon,同时由于原 shp 文件存储在不同的文件夹,在操作此工具时将其存储在一个新的文件夹(/tmp/dissolve);
  3. 通过 Union 工具,将 2 中生成的所有 shp 文件合并,生成一个 union.shp,此文件是将上面所有文件炸裂的效果;
    需要通过 MultipartToSinglepart_management 工具进一步处理;
    需要通过 Project 工具统一空间参考;
  4. 通过遍历 union.shp 中每一个 polygon,并与 2 中的每一个 shp 文件进行比较,如果存在包含关系,则增加 1,最终可以计算每一个斑块的出现次数。

  参考代码如下:

  1. import arcpy
  2. arcpy.env.workspace = r"D:\01-Working\2018\20180411-HAD_FREQ\tmp"
  3.  
  4. # 通过 union 工具可以将 多个polygon 打碎成小块
  5. arcpy.Union_analysis(["New_Shapefile", "New_ShapefileCopy", "New_ShapefileCopy2"], "union")
  6.  
  7. mxd = arcpy.mapping.MapDocument("current")
  8. df = arcpy.mapping.ListDataFrames(mxd)[0]
  9. shps = arcpy.ListFeatureClasses()
  10.  
  11. # 选取一列都是 0 的数据
  12. cursor = arcpy.da.UpdateCursor("union", "Id")
  13. # 用来记录列表的索引值
  14. i = 0
  15. # 获取图斑的所有 Geometry
  16. geoms = arcpy.CopyFeatures_management("union", arcpy.Geometry())
  17. for row in cursor:
  18. geom = geoms[i]
  19. for shp in shps:
  20. geo2 = arcpy.CopyFeatures_management(shp, arcpy.Geometry())
  21. g2 = geo2[0]
  22. # 判断包含关系,如果成立,则增加1,以此类推
  23. if g2.contains(geom):
  24. row[0] = row[0] + 1
  25. cursor.updateRow(row)
  26. i = i + 1
  27.  
  28. # 最终 Id 列就会显示每一个斑块的出现频率

  Union 工具说明

Union_analysis

4
 

====<<<< Description >>>>====

计算输入要素的几何并集。将所有要素及其属性都写入输出要素类。。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

Union_analysis (in_features, out_feature_class, {join_attributes}, {cluster_tolerance}, {gaps})
----------------------------------------------------------------------------------

====<<<< Parameter >>>>====

◈  in_features:输入要素类或图层列表。
◈  out_feature_class:将包含结果的要素类。

 
  1. import arcpy
  2. import os
  3.  
  4. rootdir = r"D:\CODE\HAB_FREQ\fb"
  5.  
  6. arcpy.env.workspace = os.path.join(rootdir, "dissolve")
  7. shps = arcpy.ListFeatureClasses()
  8.  
  9. arcpy.Union_analysis(shps, os.path.join(rootdir, "union", "union_multi.shp"))
  10.  
  11. arcpy.env.workspace = os.path.join(rootdir, "union")
  12. arcpy.MultipartToSinglepart_management("union_multi.shp", "union.shp")
   
  1. --------------------------------------------------
   
    

参考:对点数据求buffer生成圆后,如何对这些圆的相交部分进行重叠次数统计,并按重叠的次数的不同进行渲染?

  特别说明,在处理多重叠文件后,会有一部分生成了 Multipart 的部分,因此需要通过 MultipartToSinglepart_management 将其进一步转化,如下所示:

MultipartToSinglepart_management

5
 

====<<<< Description >>>>====

创建包含通过分割多部件输入要素而生成的单部件要素的要素类。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

MultipartToSinglepart_management (in_features, out_feature_class)
----------------------------------------------------------------------------------

====<<<< Parameter >>>>====

◈  in_features:任意要素类型的输入要素(单一文件)。
◈  out_feature_class:要创建的将包含聚合要素的要素类。

 
  1. import arcpy
  2. import os
  3.  
  4. rootdir = r"D:\CODE\HAB_FREQ\fb"
  5.  
  6. arcpy.env.workspace = os.path.join(rootdir, "dissolve")
  7. shps = arcpy.ListFeatureClasses()
  8.  
  9. arcpy.Union_analysis(shps, os.path.join(rootdir, "union", "union_multi.shp"))
  10.  
  11. arcpy.env.workspace = os.path.join(rootdir, "union")
  12. arcpy.MultipartToSinglepart_management("union_multi.shp", "union.shp")
   
  1. --------------------------------------------------
   
    

参考:分离多部分 (multipart) 要素(拆分)


7. 批量删除/添加 shp 文件字段

  通过 union 工具生成的 shp 文件含有超多的字段,手动删除很麻烦,因此需要借用 DeleteField_management 工具来实现。

DeleteField_management

6
 

====<<<< Description >>>>====

此工具可从表、要素类、要素图层或栅格数据集中删除一个或多个字段。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

DeleteField_management (in_table, drop_field)
----------------------------------------------------------------------------------

====<<<< Parameter >>>>====

◈  in_table:包含要删除字段的表。将修改现有输入表。(矢量图层)
◈  drop_field、[drop_field,...]:要从输入表中删除的字段 (名称或者列表)。必填字段不能删除。除了 FID 和 SHAPE 必须保留一个字段。

 
  1. import arcpy
  2. # 获取 shp 的所有字段
  3. fields = arcpy.ListFields("union")
  4. # 获取所有字段的名称
  5. f_names = []
  6. for f in fields: f_names.append(f.name)
  7. # 通过字段名称删除字段
  8. arcpy.DeleteField_management("union", f_names[3:])

AddField_management

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:要添加指定字段的输入表。该字段将被添加到现有输入表,并且不会创建新的输出表。
◈  field_name:要添加到输入表的字段的名称。
◈  field_type:新字段的字段类型。(TEXT、FLOAT等)

 
  1. from arcpy import *
  2. # 添加一个字符串字段,长度默认
  3. AddField_management("union", "Name", "TEXT")
  4. # 长度为 5
  5. AddField_management("union", "Name2", "TEXT", field_length=5)
   
  1. --------------------------------------------------
   
    

参考:字段工具集概述

  “字段”工具集包含一组用于向要素类表添加字段或对该表中的字段进行更改的工具。字段是表中的列;每个字段包含一个属性的值。表中可包含任意数量的字段。可指定字段设置,如字段类型和可存储在字段中的最大数据量。


8. 批量修改文件的 Spatial Reference456852

  承接 6 中的问题,由于 shp 文件提取是通过 ENVI 基于不同的卫星提取的,因此 shp 文件自动加入不同的卫星的 Spatial Reference,导致在进行 contains 计算时出现错误,因此需要将所有的 shp 文件批量转化成相同的 Spatial Reference。

  通过 Project 工具实现,由于相关参数过于复杂,可以通过工具箱操作一次,然后获取相应代码,在进行批量的操作。

Project_management

8
 

====<<<< Description >>>>====

将空间数据从一种坐标系投影到另一种坐标系。
----------------------------------------------------------------------------------

====<<<< Syntax >>>>====

Project_management (in_dataset, out_dataset, out_coor_system, {transform_method}, {in_coor_system}, {preserve_shape}, {max_deviation})
----------------------------------------------------------------------------------

====<<<< Parameter >>>>====

◈  in_dataset:要投影的要素类、要素图层或要素数据集。
◈  out_dataset:将要写入结果的输出数据集。
◈  out_coor_systemev:有效值是空间参考对象、扩展名为 .prj 的文件或坐标系的字符串表达形式。

 
  1. # 通过坐标系工厂代码创建
  2. >>> sr = arcpy.SpatialReference(4326)
  3. >>> sr.name
  4. u'GCS_WGS_1984'
  5. >>> arcpy.Project_management("union", "union2.shp", sr)
  6.  
  7. # 通过已知文件获取
  8. >>> sr1 = arcpy.Describe("20171127_GF1_172").spatialReference
  9.  
  10. # 通过工具箱转换
  11. >>> arcpy.Project_management('union', 'union3.shp',
  12. "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',
  13. SPHEROID['WGS_1984',6378137.0,298.257223563]],
  14. PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
   
  1. --------------------------------------------------
   
    

参考:GIS 专业人员需了解的投影基础知识

参考:有关坐标系名称和工厂代码的详细信息,请参阅 geographic_coordinate_systems.pdfprojected_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 坐标值

  1. # 获取所有 polygon
  2. >>> cursor = arcpy.da.SearchCursor("CNTRY92", "SHAPE@")
  3. >>> shapes = [row[0] for row in cursor]
  4. >>> len(shapes)
  5. 147
  6.  
  7. # 获取第一个 polygon
  8. >>> shape_0 = shapes[0]
  9. >>> shape_0
  10. <Polygon object at 0xac33290[0xad06f00]>
  11.  
  12. # 获取 polygon 的每一部分,每个 part 是一个 Array 对象
  13. >>> for part in shape_0:
  14. ... for pnt in part: # 每个 pnt 对应着每一个 Point 对象
  15. ... print("{}, {}".format(pnt.X, pnt.Y))  # 获取每个 Point 对象的属性
  16. ...
  17. 6.02716398239, 49.5240783691
  18. 6.08806419373, 49.6805076599
  19. 5.94872617722, 49.7722320557
  20. 5.94158744812, 49.9194297791
  21. 6.03742027283, 50.0643806458

参考:读取折线或面几何


10. 读取 Geometry(2种方法)

  方法一:通过 CopyFeatures_management 工具直接读取 shp 文件的 Geometry,得到的结果是一个 Polygon 列表,然后可以读取每一个 Polygon,对于 Polygon 的读取符合 9 中规律。(1 个 Polygon 由多个 Array 组成,每个 Array 都是由 Point 组成)

  1. # 获取文件中所有的 Polygon,是一个列表文件
  2. >>> polygons = arcpy.CopyFeatures_management("CNTRY92", arcpy.Geometry())
  3. >>> type(polygons)
  4. <type 'list'>
  5.  
  6. # 获取第一个 Polygon
  7. >>> polygon_1 = polygons[0]
  8.  
  9. # 将此 Polygon 的所有部分组成一个列表,为多个 Array
  10. >>> polygon_1s = [pnt for pnt in polygon_1]
  11.  
  12. # 获取第一个 Array,含有所有 Point
  13. >>> polygon = polygon_1s[0]
  14. >>> type(polygon)
  15. <class 'arcpy.arcobjects.arcobjects.Array'>

  方法二:通过 da.SearchCursor 方法读取 "SHAPE@" 字段获取 Geometry,通过遍历游标可以获取 Polygon 列表,其他与 方法一 类似。

  1. # 查询要素几何,并将其赋值为 Polygon 列表,其他与 方法一 一致
  2. >>> cursor = arcpy.da.SearchCursor("CNTRY92", "SHAPE@")
  3. >>> polygons = [row[0] for row in cursor]

参考:读取几何

  1. # 通过循环获取每一个要素
  2. for row in arcpy.da.SearchCursor("test.shp", ["OID@", "SHAPE@"]):
  3. # 通过循环获取要素的每一个部分(Array)
  4. for part in row[1]:
  5. # 通过循环获取 Array 中的每一个 Point 信息
  6. for pnt in part:
  7. print("{}, {}".format(pnt.X, pnt.Y))

11. 批量修改图例显示内容

  所谓修改图例内容,需要修改下图中每一个 label 的内容,如果在 ArcMap 中修改需要逐一修改对应 TOC 内的标签或者 Symbology 里面的标签。然而通过 ArcPy 批量修改图例的显示标签内容也需要批量修改 Symbology 对应的标签值。下面为实现效果和对应代码:

 

  1. >>> import arcpy
  2. >>> mxd = arcpy.mapping.MapDocument("current")
  3. >>> df = arcpy.mapping.ListDataFrames(mxd)[0]
  4. >>> lyr = arcpy.mapping.ListLayers(df)[0]
  5. >>> sym = lyr.symbology
  6. # 获取标签列表
  7. >>> list_labels = sym.classLabels
  8. >>> list_labels
  9. [u'Africa', u'Antarctica', u'Asia', u'Australia', u'Europe',
  10. u'North America', u'Oceania', u'South America']
  11. # 修改标签内容并赋值
  12. >>> list_labels_new = [f.upper() for f in list_labels]
  13. >>> list_labels_new
  14. [u'AFRICA', u'ANTARCTICA', u'ASIA', u'AUSTRALIA',
  15. u'EUROPE', u'NORTH AMERICA', u'OCEANIA', u'SOUTH AMERICA']
  16. >>> sym.classLabels = list_labels_new
  17. >>> arcpy.RefreshActiveView()

参考:mapping.UniqueValuesSymbology


12. 批量引用图层样式(Symbology)

  需要现在一个图层上建立相应的样式,也就是 Symbology,然后通过 UpdateLayer 方法可以将目标图层的样式进行引用。

 

  第一幅图通过 mapping.UpdateLayer 方法实现的,可见,两者具有相同的分类

  第二幅图通过 ApplySymbologyFromLayer_management 工具实现的,此工具将来自图层的符号系统应用到输入图层。

  1. # 方法一
  2. >>> arcpy.mapping.UpdateLayer(df, lyrs[0], lyrs[1])
  3. # 方法二
  4. >>> arcpy.ApplySymbologyFromLayer_management(lyrs[0],lyrs[1])

参考:mapping.UpdateLayer


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?


14. 同一图层的多个要素合并


15. 同一图层的多个要素合并

【305】◀▶ ArcPy 相关功能实现的更多相关文章

  1. 测试functional的bind以及相关功能

    注:在VS2010 UPDATE1下测试通过 /*测试functional的bind以及相关功能*/ #include <iostream> #include <functional ...

  2. JobTracker等相关功能模块初始化

    [Hadoop代码笔记]Hadoop作业提交之JobTracker等相关功能模块初始化 一.概要描述 本文重点描述在JobTracker一端接收作业.调度作业等几个模块的初始化工作.想过模块的介绍会在 ...

  3. 在github上最热门好评高的ROS相关功能包

    在github上最热门最受欢迎的ROS相关功能包 下面依次列出,排名不分先后: 1  Simulation Tools In ROS https://github.com/ros-simulation ...

  4. Android高级控件(三)—— 使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 现在的二维码可谓是烂大街了,到处都是二维码,什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  5. LoadRunner(四)——深度了解LR相关功能

    参考学习感谢:<精通软件性能测试与LoadRunner实战> 相关功能: 1 无工具情况下的性能测试 2性能测试工具LoadRunner的工作原理 3 VuGen应用介绍 4 协议的类型及 ...

  6. Android高级控件(三)——&#160;使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 如今的二维码可谓是烂大街了.到处都是二维码.什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...

  7. 零元学Expression Blend 4 - Chapter 25 以Text相关功能就能简单做出具有设计感的登入画面

    原文:零元学Expression Blend 4 - Chapter 25 以Text相关功能就能简单做出具有设计感的登入画面 本章将交大家如何运用Blend 4 内的Text相关功能做出有设计感的登 ...

  8. Excel催化剂开源第31波-pdf相关功能实现及类库介绍

    在Excel催化剂刚推出的pdf相关功能中,反馈很热烈,不止是用户层面好多人喜欢,也听到在.NET开发群里有询问pdf在winform上展现的功能诉求,一段时间没写开源篇,生怕大家以为Excel催化剂 ...

  9. Nginx Rewrite相关功能-防盗链

    Nginx Rewrite相关功能-防盗链 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

随机推荐

  1. log4j的使用配置

    1.与spring整合,web.xml中配置详情 <!-- 加载log4j的配置文件log4j.properties --> <context-param> <param ...

  2. 监听器(Listener)学习(二)在开发中的常见应用

    监听器在JavaWeb开发中用得比较多,下面说一下监听器(Listener)在开发中的常见应用: 统计当前在线人数 自定义Session扫描器 一.统计当前在线人数 在JavaWeb应用开发中,有时候 ...

  3. PHP 数组转json_encode,单个数组下标为了0时不对??

    在 php 数组转json时,假如 有一个数组下标是顺序的,他json_encode后会直接变成一个简版二维json, $arr = ['1'=>1,'2'=>2]; echo (json ...

  4. oracle之 等待事件LOG FILE SYNC (awr)优化

    log file sycn是ORACLE里最普遍的等待事件之一,一般log file sycn的等待时间都非常短 1-5ms,不会有什么问题,但是一旦出问题,往往都比较难解决.什么时候会产生log f ...

  5. Linux之 find之 ctime,atime,mtime

    在Linux操作系统中,每个文件都有很多的时间参数,其中有三个比较主要,分别是ctime,atime,mtime atime 最后一次访问时间, 如 ls, more 等, 但 chmod, chow ...

  6. http请求发生了两次(options请求)

    前言 自后台restful接口流行开来,请求了两次的情况(options请求)越来越普遍.笔者也在实际的项目中遇到过这种情况,做一下整理总结. 文章书写思路: 为什么发生两次请求 http的请求方式, ...

  7. TOMCATE8下面项目启动问题

    1.将servlet-api.jar替换项目中的servlet-api2.4 2.<servlet>           <servlet-name>dwr-invoker&l ...

  8. WPF ComboBox下拉绑定Treeview 功能的实现

    因为项目需要,接触到这个功能点,借助网络还有自己的一点摸索,实现了这个功能.相关代码如下: XAML部分的代码: <ComboBox Grid.Row=" RenderTransfor ...

  9. 解决Intel SSD 330i 240G在Windows 8启动慢的问题

    笔者半年前更新了一下老迈的Thinkpad T410i笔记本电脑,在美国亚马逊海购了Intel SSD 330i 240G.(PS:购入价USD 129.99 ,现在最新的335i 240G差不多要U ...

  10. laravel里面使用event

    模式:大概是通过一个自定义的event,一个handler,还有一个binder,然后用来简化通知模型 生成自定义的event ./artisan make:event MyEvent 生成自定义的h ...