1

I'm trying to add an attribute value to a new PointGeometry before I write it into the new file. How do I do that with arcpy?

Let's say in this example I want to write the ID of the line feature into the new point feature:

points = []
for row in arcpy.da.SearchCursor('lineFeature', ("ID", "SHAPE@")): 
    length = int(row[1].length)
    for i in xrange(0, length, 10): 
        point = row[1].positionAlongLine(i)
        # should write the ID here
        # something like 
        # point.setValue('sourceID', row[0])
        points.append(point)    
arcpy.CopyFeatures_management(points, 'pointsFeature') 

The command point.setValue('sourceID', row[0]) point. doesn't work, it returns:

Runtime error
Traceback (most recent call last):
File "<string>", line 8, in <module>
AttributeError: 'PointGeometry' object has no attribute 'setValue'

2 Answers 2

4

point is not a row object, which would have a setValue command, it is a geometry object, which does not have any type of function to store row attributes.

There's many ways you could go about fixing this, but one possibility is to make points a list of tuples, and store the data as

points.append((row[0], point))

And then, instead of using copy features to add all the points, you would need to use arcpy.da.InsertCursor in a loop over the points list and set the id and geometry of each new feature you insert. For example, something like this (I am on an old version of Arc, so I don't have the arcpy.da package to test)

# Create new feature
arcpy.CreateFeatureclass_management('out_path', 'pointsFeature', "POINT") 
# add field to feature
arcpy.AddField_management('pointsFeature', "ID", "SHORT")
# add points to feature
cursor = arcpy.da.InsertCusor('pointsFeature', ("ID", "SHAPE@"))
for pt in points:
    cursor.insertRow(pt)
1

It seems you are trying to place points at regular interval along the line. To do so I'd suggest using:

point=arcpy.Point()

Outside your loop. Inside it use:

        theP=feat.positionAlongLine (i)
        point.X=theP.firstPoint.X
        point.Y=theP.firstPoint.Y
        pnt=arcpy.PointGeometry(point,SR)

and

points.append((pnt,ID))

Don't forget to obtain spatial reference (SR here) from line feature class. The rest as per @WMrenn solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.