我试图获取.shp文件中每个多边形的区域,然后将其附加到属性表中的新列。
import os
import arcpy
import math
folderpath = 'C:\Users\Michaelf\Desktop\GEOG M173'
arcpy.env.workspace = folderpath
arcpy.env.overwriteOutput = True
input_shp = folderpath + r'\lower48_county_2012_election.shp'
equal_shape = folderpath + r'\project_lower48.shp'
out_point = folderpath + r'\lower_48_centroid.shp'
out_shp = folderpath + r'\48_State_Centroids.shp'
totarea = []
arcpy.AddField_management(input_shp, "totarea")
geometryField = arcpy.Describe(totarea).shapeFieldName
cursor = arcpy.UpdateCursor(totarea)
for row in cursor:
AreaValue = row.getValue(geometryField).area
row.setValue("total_area",AreaValue)
cursor.updateRow(row)
del row, cursor
print AreaValue
以下是我收到的一条建议,但我不太明白
with arcpy.da.SearchCursor(input_shp, ("OID@", "SHAPE@AREA")) as cursor:
for row in cursor:
print("Feature {0} has an area of {1}".format(row[0], row[1]))
这是我的错误消息:
Traceback (most recent call last):
File "C:/Users/Michaelf/Desktop/GEOG M173/test2.py", line 16, in <module>
geometryField = arcpy.Describe(totarea).shapeFieldName
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\__init__.py", line 1246, in Describe
return gp.describe(value)
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\geoprocessing\_base.py", line 374, in describe
self._gp.Describe(*gp_fixargs(args, True)))
RuntimeError: Object: Describe input value is not valid type
答案 0 :(得分:0)
由于它是以几何为中心的数据库,因此Arc会自动将多边形的总面积存储为属性。没有必要计算它,这就是为什么(我假设)建议你的代码片段:
with arcpy.da.SearchCursor(input_shp, ("OID@", "SHAPE@AREA")) as cursor:
for row in cursor:
print("Feature {0} has an area of {1}".format(row[0], row[1]))
执行以下操作:
input_shp
,只提取两个属性(对象ID和总面积)。row[0]
(对象ID)和row[1]
(总面积)。如需进一步阅读,请查看this excellent answer on GIS.SE。它详细探讨了访问和计算几何的一些选项。
(最后注意:shape@area
属性为in the units of the data's projection。如果您收到意外数字,请注意这一点。)