zoukankan      html  css  js  c++  java
  • 【305】◀▶ ArcPy 相关功能实现

    目录:

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


    黑色窗体参考代码:

    <table style="border-style: solid; border-color: #ffffff; border- 5px; background-color: #ffffff;  968px;" border="5" cellspacing="1" cellpadding="1">
    <tbody>
    <tr style="background-color: #333333;">
    <td style="border-style: solid; text-align: center; border-color: #ffffff;  50px; border- 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;  50px; border- 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;  5px; border- 3px; background-color: #333333;"><span style="font-family: Georgia;">&nbsp;</span></td>
    <td style="border-style: solid none; border-color: #ffffff;  350px; border- 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;">&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>
    <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;">&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>
    <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>
    <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>
    <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>
    </td>
    <td style="border-style: solid none; border-color: #ffffff;  5px; border- 3px; text-align: left; background-color: #333333;"><span style="font-family: Georgia;">&nbsp;</span></td>
    <td style="border-color: #ffffff; border- 3px; border-style: solid; background-color: #333333;">&nbsp;</td>
    </tr>
    <tr style="background-color: #99ffcc;">
    <td style="border-style: solid; text-align: center; border-color: #ffffff;  50px; border- 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>
    </tr>
    </tbody>
    </table>
    View Code

    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:要聚合要素的一个或多个字段。省略的话则对所有要素合并。

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

      方法二:通过 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")
    

      参考: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:输出点事件图层的名称。

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

    3. 栅格计算器的实现

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

    Raster Calculator (Spatial Analyst)

    3  

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

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

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

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

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

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

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

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

    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()
    

    参考:如何通过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 类似

      实现代码如下:

    # 创建经纬度列表
    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. 多边形重叠区域次数计算(赤潮发生频次)

      步骤如下:

    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,最终可以计算每一个斑块的出现次数。

      参考代码如下:

    import arcpy
    arcpy.env.workspace = r"D:1-Working201820180411-HAD_FREQ	mp"
    
    # 通过 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 工具说明

    Union_analysis

    4
     

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

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

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

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

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

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

     
    import arcpy
    import os
    
    rootdir = r"D:CODEHAB_FREQfb"
    
    arcpy.env.workspace = os.path.join(rootdir, "dissolve")
    shps = arcpy.ListFeatureClasses()
    
    arcpy.Union_analysis(shps, os.path.join(rootdir, "union", "union_multi.shp"))
    
    arcpy.env.workspace = os.path.join(rootdir, "union")
    arcpy.MultipartToSinglepart_management("union_multi.shp", "union.shp")
    
       
    --------------------------------------------------
       
        

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

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

    MultipartToSinglepart_management

    5
     

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

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

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

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

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

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

     
    import arcpy
    import os
    
    rootdir = r"D:CODEHAB_FREQfb"
    
    arcpy.env.workspace = os.path.join(rootdir, "dissolve")
    shps = arcpy.ListFeatureClasses()
    
    arcpy.Union_analysis(shps, os.path.join(rootdir, "union", "union_multi.shp"))
    
    arcpy.env.workspace = os.path.join(rootdir, "union")
    arcpy.MultipartToSinglepart_management("union_multi.shp", "union.shp")
    
       
    --------------------------------------------------
       
        

    参考:分离多部分 (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 必须保留一个字段。

     
    import arcpy
    # 获取 shp 的所有字段
    fields = arcpy.ListFields("union")
    # 获取所有字段的名称
    f_names = []
    for f in fields: f_names.append(f.name)
    # 通过字段名称删除字段
    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等)

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

    参考:字段工具集概述

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


    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 的文件或坐标系的字符串表达形式。

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

    参考: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 坐标值

    # 获取所有 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])
    

    参考:mapping.UpdateLayer


    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. 同一图层的多个要素合并

  • 相关阅读:
    【Linux】没有网的情况下如何安装GCC
    【PL/SQL】PLSQL Developer注册码
    【JS】字符串操作
    【java】svn显示&#215;
    线段树
    病毒感染者
    并查集
    最小的N个和(堆)
    priority_queue的用法
    打印杨辉三角
  • 原文地址:https://www.cnblogs.com/alex-bn-lee/p/8708610.html
Copyright © 2011-2022 走看看