1

I'm writing a script that parses GPS events from an SDE table to try and create lines in a feature class representing when street sweeper brushes were turned on.

I'm appending the lat and long into respective lists then trying to use an insert cursor to create the lines.

cursor = arcpy.da.SearchCursor(tbl, ["TIME", "EVENT", "LATITUDE", "LONGITUDE"])
icursor = arcpy.da.InsertCursor(SweptRoadsFC, ["SHAPE@"])

XPointList = []
YPointList = []
Sweeping = 0
array = arcpy.Array()

for row in cursor:
    xpnt = float(format(row[3]))
    ypnt = float(format(row[2]))
    if Sweeping == 0:           
        if format(row[1]) == "Brushes On":
            Sweeping = 1
            XPointList.append(xpnt)
            YPointList.append(ypnt)
    else:
        if format(row[1]) != "Brushes Off":
            print "Brushes are on - " + format(row[1])
            XPointList.append(xpnt)
            YPointList.append(ypnt)
        else:
            Sweeping = 0
            print "Brushes are now off - " + format(row[1])
            point = arcpy.Point()
            count1 = 0
            for x in XPointList:
                point.X = x
                point.Y = YPointList[count1]                 
                count1 += 1
                array.append(point)
            geometry = arcpy.Polyline(array)
            icursor.insertRow([geometry])
            array = []
            XPointList = []
            YPointList = []

Currently, I'm getting "RuntimeError: Object: CreateObject cannot create geometry from inputs" on the insertRow line.

I believe it's some sort of data type or formatting error, as when I hard-code the first three points into lists it runs fine, like this:

XPointList = [-97.930334, -97.9302865, -97.9302068]
YPointList = [29.8852428, 29.8852505, 29.8851645]

icursor = arcpy.da.InsertCursor(SweptRoadsFC, ["SHAPE@"])
array = arcpy.Array()        
point = arcpy.Point()
count1 = 0
for x in XPointList:
    point.X = x
    point.Y = YPointList[count1]
    count1 += 1
    array.append(point)
geometry = arcpy.Polyline(array)
icursor.insertRow([geometry])

Any ideas on what would be throwing the error?

I've tested the conditionals with print statements and the array contains the correct coordinates when the first polyline is trying to be created.

1

I thought your issue is related to Sweeping == 0 after your second else which should be Sweeping = 0 to turn the switch off but think your method to empty the array is faulty. You should use removeAll() method of array instead of array = [].

Also you need to make sure the length of the array should be at least two to be able to draw a line. Additionally, I think your code seems doing what you are trying to do the hard way.

  • I know the conditionals look ugly, but using a bunch of print statements I've followed the logic and the array contains the correct set of coordinates when the first polyline is trying to be created. So, I'm fairly certain it's not the conditionals (although I did fix that == that should've been = and it still throws the same error). And I probably am doing this the hard way, I'm an ineloquent coder at best. =) – Auz Beauxdine Nov 1 '17 at 14:11
1

So I realized it was drawing the first polyline but then crashing on the subsequent ones. I determined the problem was with trying to clear the contents of the array.

array = []

The object is actually an arcpy array and not just a python array, so you can't do an assignment like that which is what caused the error. I needed to use the removeAll() method from the arcpy Array class which solved it.

array.removeAll()

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.