【287】◀▶ 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:行數。(只讀) ====<<<< Methods >>>>==== ??? save ({name}):永久保存柵格對象引用的數據集。 | ? | # 獲取柵格數據 arcpy.env.workspace=r"D:\01-Working\2017\20171204-IDL_Average\temp\TSM" 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 + "\n")fo.writelines("MAX: " + str(round(ro.maximum, 2)) + "\n")fo.writelines("MIN: " + str(round(ro.minimum, 2)) + "\n")fo.writelines("MEAN: " + str(round(ro.mean, 2)) + "\n\n")fo.close() |
| 02 | Cursor | ? | ====<<<< Description >>>>==== Cursor 是一種數據訪問對象,可用于在表中迭代一組行或者向表中插入新行。游標有三種形式:搜索、插入或更新。游標通常用于讀取和更新屬性。(不同于arcpy.da.SearchCursor) ---------------------------------------------------------------------------------- ====<<<< Methods >>>>==== ?? deleteRow (row):刪除數據庫中的某一行。將刪除與游標當前所在位置相對應的行。 | ? | 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, rowcursor = arcpy.SearchCursor(lyr) for row in cursor:name = row.getValue("NAME")print name# 將其刪除 del cursor, rowcursor = 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):獲取字段值。 | ? | |
| ?04 | Array | ? | ====<<<< Description>>>>==== 數組對象中可包含點和數組,它用于構造幾何對象。 ====<<<< Syntax >>>>==== Array ({items}) ====<<<< Parameters >>>>==== ?? items:項目可以包含列表、點對象或另一個數組對象。 ====<<<< Methods >>>>==== ?? add (value):將點或數組對象添加到數組的結尾處。 ====<<<< 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 坐標。 ====<<<< Methods >>>>==== ?? clone ():克隆點對象。 ====<<<< Attributes >>>>==== ?? ID:唯一標識點的整數。 | ? | ? # 通過元組列表創建 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 ():構造幾何邊界。面→線、線→點、點→空 ====<<<< Attributes >>>>==== ?? extent:幾何范圍。 | ? | ? |
| 07 | Polygon | ? | ====<<<< Description>>>>==== 面對象是指由一系列相連的 x,y 坐標對定義的閉合形狀。 ====<<<< Syntax >>>>==== Polygon (inputs, {spatial_reference}, {has_z}, {has_m}) ====<<<< Parameters >>>>==== ?? inputs:用來創建對象的坐標。數據類型可以是點或者數組對象。 ====<<<< Methods >>>>==== ?? boundary ():構造幾何邊界。面→線、線→點、點→空 ====<<<< Attributes >>>>==== ?? area:面要素的面積。 | ? | ?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 值。 ====<<<< Methods >>>>==== ?? contains / crosses / disjoint / equals / overlaps / touches / within ====<<<< Attributes >>>>==== ?? XMin:范圍 XMin 值。 | ? | >>> 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 |
| ---------- | ? | ? | ? | ? | ? |
轉載于:https://www.cnblogs.com/alex-bn-lee/p/8256775.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【287】◀▶ arcpy 常用类说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 98是什么
- 下一篇: win7中USB音箱没有声音解决的方法