Monday, 13 November 2017

How to add Attributes to a polygon with arcpy


I have a question about arcpy. I want to create a polygon FeatureClass with attributes. The code below is just test-code that generates some polygons for speed testing. I want to add some additional attributes like ID (integer) and Material-Type (string) to each polygon. I know that this is possible with the insert.cursor function. But I want to do it with the CopyFeatures function, because this is faster (I have to generate some 100'000 thousand's polygon).


import arcpy
import time

# Variablen definieren
workspace = "E:/GeschiebeGIS/Python/Sandkasten/CreatePolygon"
shapeCursor = "polygonCursor.shp"
sr = arcpy.SpatialReference(4149) # Spatial reference GCS_CH1903


# Zeitmessung starten
startTime = time.clock()

# shape Datei löschen
arcpy.Delete_management(workspace+"/"+shapeCursor)

# shape Datei erstellen
#arcpy.CreateFeatureclass_management(workspace, shapeCursor, "POLYGON", "", "DISABLED", "DISABLED")


# feature List erstellen
featureList = []

# leeres Array erstellen
array = arcpy.Array()

# 100 Testpolygone erstellen
for c1 in range (0, 100):
point1 = arcpy.Point(c1,c1)
point2 = arcpy.Point(c1+1,c1)

point3 = arcpy.Point(c1+1,c1+1)
point4 = arcpy.Point(c1,c1+1)
point5 = arcpy.Point(c1,c1)
array.add(point1)
array.add(point2)
array.add(point3)
array.add(point4)
array.add(point5)
polygon = arcpy.Polygon(array)
array.removeAll()

featureList.append(polygon)

arcpy.CopyFeatures_management(featureList, workspace+"/"+shapeCursor)

print "Fettich"
print time.clock() - startTime, "seconds"

is it possible to add the attributes to the polygon object in the For loop?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...