zoukankan      html  css  js  c++  java
  • 【287】◀▶ arcpy 常用类说明

    1. ArcPy 类列表(按字母顺序)

    01   Raster 创建一个可在 Python 脚本或地图代数表达式中使用的栅格对象。
    02   Cursor Cursor 是一种数据访问对象,可用于在表中迭代一组行或者向表中插入新行。
    03   Row 行对象表示表中的某一行。行对象会从 InsertCursor、SearchCursor 和 UpdateCursor 中返回。
    04   Array 数组对象中可包含点和数组,它用于构造几何对象。
    05   Point 点对象经常与光标配合使用。点要素将返回单个点对象而不是点对象数组。
    06   Polyline 折线对象是由一个或多个路径定义的形状,其中路径是指一系列相连线段。
    07   Polygon 面对象是指由一系列相连的 x,y 坐标对定义的闭合形状。
    08   Extent 范围是在地图单位下提供左下角和右上角坐标指定的一个矩形。

    序号 类名称  

    功能说明

      语法 & 举例
    01 Raster  

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

    创建一个可在 Python 脚本或地图代数表达式中使用的栅格对象。
    ----------------------------------------------------------------------------------

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

    Raster (inRaster)
    ----------------------------------------------------------------------------------

    ====<<<< Parameters >>>>====

    ◈  inRaster:输入栅格数据集。
    ----------------------------------------------------------------------------------

    ====<<<< Properties >>>>====

    ◈  height:行数。(只读)
    ◈  width:列数。(只读)
    ◈  maximum:栅格数据集的最大值。(只读)
    ◈  minimum:栅格数据集的最小值。(只读)

    ◈  mean:栅格数据集的平均值。(只读)
    ◈  meanCellHeight:y 方向上的象元大小。(只读)
    ◈  meanCellWidth:x 方向上的象元大小。(只读)
    ◈  name/path/

    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈   save ({name}):永久保存栅格对象引用的数据集。
      name:分配给磁盘上的栅格数据集的名称。(String)

     
    # 获取栅格数据
    arcpy.env.workspace=r"D:1-Working201720171204-IDL_Average	empTSM"
    rs = arcpy.ListRasters()
    
    # 遍历栅格数据获取统计信息
    # 首先需要建立栅格文件
    # 将数据结果保留两位小数
    # 输出到txt文档中
    
    fo = open("D:\01-Working\2017\20171204-IDL_Average\temp\tsm_stats.txt", "w+")
    for r in rs:
        ro = arcpy.Raster(r)
        fo.writelines(ro.name + "
    ")
        fo.writelines("MAX: " + str(round(ro.maximum, 2)) + "
    ")
        fo.writelines("MIN: " + str(round(ro.minimum, 2)) + "
    ")
        fo.writelines("MEAN: " + str(round(ro.mean, 2)) + "
    
    ")
        
    fo.close()
    
    02 Cursor  

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

    Cursor 是一种数据访问对象,可用于在表中迭代一组行或者向表中插入新行。游标有三种形式:搜索、插入或更新。游标通常用于读取和更新属性。(不同于arcpy.da.SearchCursor)

    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈  deleteRow (row):删除数据库中的某一行。将删除与游标当前所在位置相对应的行。
    ◈  insertRow (row):向数据库中插入新行。
    ◈  newRow ():创建空行对象。
    ◈  next ():返回当前索引中的下一个对象。(返回 Row)
    ◈  reset ():将当前枚举索引(由 next 方法使用)设置回第一个元素。
    ◈  updateRow (row):updateRow 方法可用于对更新游标当前所在的行进行更新。

     
    import arcpy
    mxd = arcpy.mapping.MapDocument("CURRENT")
    lyr = arcpy.mapping.ListLayers(mxd)[0]
    
    # 获取指定图层的游标  
    cursor = arcpy.UpdateCursor(lyr)
    
    # 遍历
    for row in cursor:
        # 添加数值
        row.setValue("AREA", 200)
        # 将数据进行更新
        cursor.updateRow(row)
        
    # 将其删除
    del cursor, row
        
    cursor = arcpy.SearchCursor(lyr)
    for row in cursor:
        name = row.getValue("NAME")
        print name
    
    # 将其删除
    del cursor, row
    
    cursor = arcpy.InsertCursor(lyr)
    for i in range(1, 10):
        row = cursor.newRow()
        row.setValue("NAME", "阿拉斯加")
        row.setValue("ID", i)
        cursor.insertRow(row)
        
    # 将其删除
    del cursor, row
    
    03 Row

     

     

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

    行对象表示表中的某一行。行对象会从 InsertCursor、SearchCursor 和 UpdateCursor 中返回。
    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈   getValue (field_name):获取字段值。
    ◈   isNull (field_name):字段值是否为空。
    ◈   setNull (field_name):将字段值设置为空。
    ◈   setValue (field_name):设置字段值。

     
     04 Array
     

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

    数组对象中可包含点和数组,它用于构造几何对象。
    ----------------------------------------------------------------------------------

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

    Array ({items})
    ----------------------------------------------------------------------------------

    ====<<<< Parameters >>>>====

    ◈  items:项目可以包含列表、点对象或另一个数组对象。
    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈  add (value):将点或数组对象添加到数组的结尾处。
    ◈  append (value):在数组中的最后一个位置追加一个对象。
    ◈  clone (point_object):克隆点对象。

    ◈  extend (items):通过追加元素扩展数组。
    ◈  getObject (index):返回数组中给定索引位置上的对象。
    ◈  insert (index, value):在数组中的指定索引处添加一个对象。
    ◈  next ():返回当前索引中的下一个对象。
    ◈  remove (index):从数组中的指定索引位置移除对象。
    ◈  removeAll ():移除所有值并创建一个空对象。
    ◈  replace (index, value):替换数组中指定索引位置上的对象。
    ◈  reset ():将当前枚举索引(由 next 方法使用)设置回第一个元素。
    ----------------------------------------------------------------------------------

    ====<<<< Attributes >>>>====

    ◈  count:数组的元素个数。

     
    # 创建以元组为元素的列表
    >>> coords = [(1, 2), (1, -2), (-1, -2), (-1, 2), (1, 2)]
    
    # 创建空的数组对象
    >>> ar = arcpy.Array()
    >>> ar
    <Array []>
    
    # 通过循环,将列表信息以点的形式添加到数组对象中
    >>> for x, y in coords:
    ...     ar.add(arcpy.Point(x, y))
    ...     
    
    # 查看数组对象
    >>> ar
    <Array [<Point (1.0, 2.0, #, #)>, <Point (1.0, -2.0, #, #)>, 
    <Point (-1.0, -2.0, #, #)>, <Point (-1.0, 2.0, #, #)>, 
    <Point (1.0, 2.0, #, #)>]>
    
    # 创建列表
    >>> coords = [(1, 2), (1, -2), (-1, -2), (-1, 2), (1, 2)]
    
    # 转化为含有 Point 对象的列表
    >>> points = [arcpy.Point(x, y) for x, y in coords]
    
    # 定义数组对象并显示
    >>> ar1 = arcpy.Array(points)
    >>> ar1
    <Array [<Point (1.0, 2.0, #, #)>, <Point (1.0, -2.0, #, #)>, 
    <Point (-1.0, -2.0, #, #)>, <Point (-1.0, 2.0, #, #)>, 
    <Point (1.0, 2.0, #, #)>]>
    
    05
     Point
     

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

    点对象经常与光标配合使用。点要素将返回单个点对象而不是点对象数组。而其他要素类型(面、折线和多点)都将返回一个点对象数组,并且当这些要素具有多个部分时,则返回包含多个点对象数组的数组
    ----------------------------------------------------------------------------------

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

    Point ({X}, {Y}, {Z}, {M}, {ID})
    ----------------------------------------------------------------------------------

    ====<<<< Parameters >>>>====

    ◈  X:点的 X 坐标。
    ◈  Y:点的 Y 坐标。
    ◈  Z:点的 Z 坐标。
    ◈  M:点的 M 值。
    ◈  ID:点的形状 ID。
    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈  clone ():克隆点对象。
    ◈  contains (second_geometry):包含
    ◈  crosses (second_geometry):交叉
    ◈  disjoint (second_geometry):不相交
    ◈  equals (second_geometry):相同
    ◈  overlaps (second_geometry):重叠
    ◈  touches (second_geometry):接触
    ◈  within (second_geometry):内部
    ----------------------------------------------------------------------------------

    ====<<<< Attributes >>>>====

    ◈  ID:唯一标识点的整数。
    ◈  M:点的 measure value。
    ◈  X:点的横坐标。
    ◈  Y:点的纵坐标。
    ◈  Z:点的高程值。

       
    # 通过元组列表创建 Point 数组
    >>> coords = [(1, 2), (1, -2), (-1, -2), (-1, 2), (1, 2)]
    
    # 通过这样的形式创建 Point
    >>> points = [arcpy.Point(x, y) for x, y in coords]
    
    # 创建 Point 数组并显示
    >>> arr = arcpy.Array(points)
    >>> arr
    <Array [<Point (1.0, 2.0, #, #)>, <Point (1.0, -2.0, #, #)>, 
    <Point (-1.0, -2.0, #, #)>, <Point (-1.0, 2.0, #, #)>, 
    <Point (1.0, 2.0, #, #)>]>
    
    # 通过列表列表创建Point数组
    >>> coords = [[1, 2], [1, -2], [-1, -2], [-1, 2], [1, 2]]
    
    # 通过这样的形式创建 Point
    >>> points = [arcpy.Point(*p) for p in coords]
    
    # 创建 Point 数组并显示
    >>> arr = arcpy.Array(points)
    >>> arr
    <Array [<Point (1.0, 2.0, #, #)>, <Point (1.0, -2.0, #, #)>, 
    <Point (-1.0, -2.0, #, #)>, <Point (-1.0, 2.0, #, #)>, 
    <Point (1.0, 2.0, #, #)>]>
    
    # 另外一种读取方法
    >>> coords = [[1, 2], [1, -2], [-1, -2], [-1, 2], [1, 2]]
    >>> points = [arcpy.Point(x, y) for x, y in coords]
    
    06 Polyline  

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

    折线对象是由一个或多个路径定义的形状,其中路径是指一系列相连线段。
    ----------------------------------------------------------------------------------

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

    Polyline (inputs, {spatial_reference}, {has_z}, {has_m})
    ----------------------------------------------------------------------------------

    ====<<<< Parameters >>>>====

    ◈  inputs:用来创建对象的坐标。数据类型可以是点或者数组对象。
    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈  boundary ():构造几何边界。面→线、线→点、点→空
    ◈  buffer (distance):在距几何的指定距离处构造一个面。
    ◈  clip (envelope):构造几何体与指定范围(extent)的交集。
    ◈  convexHull ():构造具有最小边界多边形的几何,以便所有外角均为凸角。
    ◈  cut (cutter):将该几何分割到剪切折线的左右两侧。
    ◈  distanceTo (other):返回两个几何之间的最小距离。

    ◈  contains (second_geometry):包含
    ◈  crosses (second_geometry):交叉
    ◈  disjoint (second_geometry):不相交
    ◈  intersect (other, dimension):相交(结果 Geometry,1-点、2-线、4-面)
    ◈  symmetricDifference (other):交集取反
    ◈  union (other):联合
    ◈  equals (second_geometry):相同
    ◈  overlaps (second_geometry):重叠
    ◈  touches (second_geometry):接触
    ◈  within (second_geometry):内部

    ◈  getLength ({measurement_type}, {units}):使用测量类型返回要素的长度。
    ◈  getPart ({index}):返回几何特定部分的点对象数组,或包含多个数组(每个数组对应一个部分)的数组。

    ----------------------------------------------------------------------------------

    ====<<<< Attributes >>>>====

    ◈  extent:几何范围。
    ◈  firstPoint:第一个几何坐标点。
    ◈  isMultipart:如果此几何的部分数大于一,则为 True。
    ◈  lastPoint:要素的最后一个坐标。
    ◈  length:线状要素的长度。点和多点要素类型为零。
    ◈  partCount:要素几何部分的数目。
    ◈  pointCount:要素的总点数。
    ◈  type:几何类型:面、折线、点、多点、多面体、尺寸或注记。

       
    07 Polygon  

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

    面对象是指由一系列相连的 x,y 坐标对定义的闭合形状。
    ----------------------------------------------------------------------------------

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

    Polygon (inputs, {spatial_reference}, {has_z}, {has_m})
    ----------------------------------------------------------------------------------

    ====<<<< Parameters >>>>====

    ◈  inputs:用来创建对象的坐标。数据类型可以是点或者数组对象。
    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈  boundary ():构造几何边界。面→线、线→点、点→空
    ◈  buffer (distance):在距几何的指定距离处构造一个面。
    ◈  clip (envelope):构造几何体与指定范围(extent)的交集。
    ◈  convexHull ():构造具有最小边界多边形的几何,以便所有外角均为凸角。
    ◈  cut (cutter):将该几何分割到剪切折线的左右两侧。
    ◈  distanceTo (other):返回两个几何之间的最小距离。

    ◈  difference (other):差异
    ◈  contains (second_geometry):包含
    ◈  crosses (second_geometry):交叉
    ◈  disjoint (second_geometry):不相交
    ◈  intersect (other, dimension):相交(结果 Geometry,1-点、2-线、4-面)
    ◈  symmetricDifference (other):交集取反
    ◈  union (other):联合
    ◈  equals (second_geometry):相同

    ◈  overlaps (second_geometry):重叠
    ◈  touches (second_geometry):接触
    ◈  within (second_geometry):内部

    ◈  getArea ({type}, {units}):使用测量类型返回要素的面积。
    ◈  getLength ({measurement_type}, {units}):使用测量类型返回要素的长度。
    ◈  getPart ({index}):返回几何特定部分的点对象数组,或包含多个数组(每个数组对应一个部分)的数组。

    ----------------------------------------------------------------------------------

    ====<<<< Attributes >>>>====

    ◈  area:面要素的面积。
    ◈  centroid:如果质心位于要素之内或要素之上则为真;否则返回标注点。返回点对象。
    ◈  trueCentroid:要素的重心。
    ◈  extent:几何范围。
    ◈  firstPoint:第一个几何坐标点。
    ◈  isMultipart:如果此几何的部分数大于一,则为 True。
    ◈  lastPoint:要素的最后一个坐标。
    ◈  length:线状要素的长度。点和多点要素类型为零。
    ◈  partCount:要素几何部分的数目。
    ◈  pointCount:要素的总点数。
    ◈  type:几何类型:面、折线、点、多点、多面体、尺寸或注记。

     

     Polygon 解析:

        一个 Polygon 含有多个部分,需要通过 for 循环读取,每个部分是一个 Array 对象

        一个 Array 对象内部包括 N 个 Point,需要通过 for 循环读取每个 Point

    # 获取 China 所对应的 Geometry
    >>> cursor = arcpy.da.SearchCursor("CNTRY92", "SHAPE@", "NAME = 'China'")
    
    # 读取符合条件的 Geometry 为一个列表,只有一个元素
    >>> polygons = [row[0] for row in cursor]
    >>> len(polygons)
    1
    
    # 获取 China 对应的 Polygon
    >>> china = polygons[0]
    
    # 判断 China 对应的 Polygon 是否为多部分的
    >>> china.isMultipart
    True
    >>> china.partCount
    2
    >>> china.pointCount
    839
    
    # 获取每一部分为一个列表,part 对应 Array 对象
    >>> lands = [part for part in china]
    >>> lands
    
    # 包括 2 个部分,每个部分都是包含 Point 的 Array 对象
    >>> len(lands)
    2
    
    # 将 Array 转换为 list
    >>> points = [pnt for pnt in lands[0]]
    
    # 通过 Array 建立新的 Polygon 并输出
    >>> mainland = arcpy.Polygon(lands[1])
    >>> arcpy.CopyFeatures_management(mainland, "mainland.shp")
    <Result 'D:\McDelfino\Documents\ArcGIS\mainland.shp'>
    
    08 Extent  

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

    范围是在地图单位下提供左下角和右上角坐标指定的一个矩形
    ----------------------------------------------------------------------------------

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

    Extent ({XMin}, {YMin}, {XMax}, {YMax}, {ZMin}, {ZMax}, {MMin}, {MMax})
    ----------------------------------------------------------------------------------

    ====<<<< Parameters >>>>====

    ◈  XMin:范围 XMin 值。
    ◈  YMin:范围 YMin 值。
    ◈  XMax:范围 XMax 值。
    ◈  YMin:范围 YMax 值。
    ----------------------------------------------------------------------------------

    ====<<<< Methods >>>>====

    ◈  contains / crosses / disjoint / equals / overlaps / touches / within
    ◈  方法使用与 Polygon 类似
    ◈  union (other):联合◈  equals (second_geometry):相同◈  overlaps (second_geometry):重叠◈  touches (second_geometry):接触◈  within (second_geometry):内部
    ----------------------------------------------------------------------------------

    ====<<<< Attributes >>>>====

    ◈  XMin:范围 XMin 值。
    ◈  YMin:范围 YMin 值。
    ◈  XMax:范围 XMax 值。
    ◈  YMin:范围 YMax 值。
    ◈  height:范围高度值。( YMax - YMin )
    ◈  width:范围宽度值。( XMax - XMin )


    ◈  lowerLeft:左下角属性:将返回点对象。
    ◈  lowerRight:右下角属性:将返回点对象。
    ◈  upperLeft:左上角属性:将返回点对象。
    ◈  upperRight:右上角属性:将返回点对象。

    ◈  polygon:以多边形对象的形式返回范围。
    ◈  spatialReference:范围的空间参考。

     
    >>> e2 = df.extent
    >>> e2.XMax
    149.1029612143459
    >>> e2.XMin
    64.02167430592488
    >>> e2.YMax
    50.20513847572508
    >>> e2.YMin
    18.71509185629973
    >>> e2.MMax
    >>> e2.ZMax
    >>> pnt = e2.lowerLeft
    >>> pnt
    <Point (64.0216743059, 18.7150918563, #, #)>
    >>> e2.height
    31.49004661942535
    >>> e2.width
    85.08128690842102
    
    -
    
    ----
       
    --------------------------------------------------
    
       
  • 相关阅读:
    狐狸和兔子
    Arguments to main
    strncpy
    atoi
    sscanf
    集体的智慧:为什么SaaS打败企业软件?
    终于来热风了,又一次感觉到什么叫温暖!
    博士生的毕设
    淡淡的
    endnote如何修改输出格式?
  • 原文地址:https://www.cnblogs.com/alex-bn-lee/p/8256775.html
Copyright © 2011-2022 走看看