2013-07-12 117 views
2

我有一個帶有多邊形的shapefile。我想找到每個的質心並將它們的xy座標放置在EastingNorthing字段下的屬性表中。我能找到eastingnorthing,但我無法將它放入屬性表(它表示爲空)。Python - 將值添加到字段

import arcpy 
arcpy.env.workspace = folderpath 
arcpy.env.overwriteOutput = True 

inputRoofs = "Roof" 

print "working" 


table = inputRoofs 

arcpy.AddField_management(inputRoofs, 'Easting', 'DOUBLE') 

arcpy.AddField_management(inputRoofs, 'Northing', 'DOUBLE') 


print "fields added" 

roofList_x = [] 

roofList_y = [] 

roof_cursor = arcpy.UpdateCursor("Roof") 
for roof in roof_cursor: 
    roof_geom = roof.Shape 
    roofList_x.append(roof_geom.centroid.X) 
    roofList_y.append(roof_geom.centroid.Y) 

print roofList_x 
print roofList_y 
print "done" 
+0

你真的想用Python嗎?右鍵單擊屬性表中的Easting和Northing字段名稱將使您計算幾何。 – PolyGeo

回答

2

下面是一個使用CalculateField到質心值分配給新字段一個實現。

# Name: CalculateField_Centroids.py 
# Description: Use CalculateField to assign centroid values to new fields 


# Import system modules 
import arcpy 
from arcpy import env 

try: 
    # Set environment settings 
    env.workspace = "C:/data/airport.gdb" 

    # Set local variables 
    inFeatures = "parcels" 
    fieldName1 = "xCentroid" 
    fieldName2 = "yCentroid" 
    fieldPrecision = 18 
    fieldScale = 11 
    # Expressions are calculated using the Shape Field's geometry property 
    expression1 = "float(!SHAPE.CENTROID!.split()[0])" 
    expression2 = "float(!SHAPE.CENTROID!.split()[1])" 

    # Execute AddField 
    arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE", 
           fieldPrecision, fieldScale) 
    arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE", 
           fieldPrecision, fieldScale) 

    # Execute CalculateField 
    arcpy.CalculateField_management(inFeatures, fieldName1, expression1, 
            "PYTHON") 
    arcpy.CalculateField_management(inFeatures, fieldName2, expression2, 
            "PYTHON") 
except Exception, e: 
    # If an error occurred, print line number and error message 
    import traceback, sys 
    tb = sys.exc_info()[2] 
    print "Line %i" % tb.tb_lineno 
    print e.message