I'll preface this by saying I'm pretty weak on cursors but trying to boot camp myself to basic proficiency.
I'm trying to create a simple script that will take a point feature class and create lines from them. These points are the start point of the the line and for sake of demonstration here, the endpoints are defined mathematically from them. Simple 2-point lines.
startingPt = "STARTING_POINTS" #point feature layer in MXD
fields = ['SHAPE@X', 'SHAPE@Y']
point = arcpy.Point()
array = arcpy.Array()
featureList = []
insert = arcpy.da.InsertCursor(r"D:\Temp\Lines.shp",['SHAPE@'])
with arcpy.da.SearchCursor(startingPt, fields) as cursor:
for row in cursor:
startX = row[0]
startY = row[1]
array.add(point)
endX = startX + 660
endY = startY + 660
array.add(point)
polyline = arcpy.Polyline(array)
insert = arcpy.da.InsertCursor(r"D:\Temp\Lines.shp",['SHAPE@'])
insert.insertRow([polyline])
print "Start: {},{} End: {},{}".format(startX,startY,endX,endY)
del cursor
The script successfully writes rows to the target, but the geometry is empty. I'm sure there is a simple (and stupid) mistake in the code. Any help is appreciated and I'm totally open to suggestions. I'd rather not have a copy features or append operation but just an insert unless my logic is totally flawed here.
Thanks!
Solved! Go to Solution.
I broke it. No errors. Output FC is getting created but nothing is getting inserted. Previously I was just using an existing FC I made for sake of demonstration. Now I'm want to have the script create the FC. I've also tried using the old temp shape as a template and still not inserting. FWIW, shouldn't be anything to do with the rotation...the coordinates print right etc and I had this working up until I tried to have the cursor insert to a newly created FC.
If I explicitly set the existing output shapefile in the insert cursor like:
#Open Insert Cursor
insert = arcpy.da.InsertCursor(r"D:\Temp\Lines.shp",['SHAPE@'])
Then it works.
The current code below fails to write/insert any rows to the target FC. I've also tried to have the input points and output lines exist in different GDBs as a test; this didn't resolve the issue. Curiously, often when trying to open the empty output line FC, ArcMap crashes.
import arcpy, os, sys, math
from itertools import cycle#Environment settings
arcpy.env.overwriteOutput = True
outGDB = arcpy.env.scratchGDB#Local Variables
#startingPt = arcpy.GetParameterAsText(0)
#rotation = int(arcpy.GetParameterAsText(1))
#dist = int(arcpy.GetParameterAsText(2))
startingPt = "STARTING_POINTS"
dist = 660
angles = [0,90,180,270]
rotation = cycle(angles)
outName = "crz_lines"#Create target polyline FC
sr = arcpy.Describe(startingPt).spatialReference
outLine = arcpy.CreateFeatureclass_management(outGDB,outName,"POLYLINE","","","",sr)#Create an array object
array = arcpy.Array()#Open Insert Cursor
insert = arcpy.da.InsertCursor(outLine,['SHAPE@'])fields = ['SHAPE@X', 'SHAPE@Y']
with arcpy.da.SearchCursor(startingPt, fields) as cursor:
for row in cursor:
#Set Start points
startX = row[0]
startY = row[1]
#Convert rotation values in list from degrees to radians
rad = math.radians(next(rotation))
#Calculate endpoints
endX= startX + (math.cos(rad) * dist)
endY= startY + (math.sin(rad) * dist)
#Add points to array
array.add(arcpy.Point(startX, startY))
array.add(arcpy.Point(endX, endY))
#Create Polyine object based on the array of points
polyline = arcpy.Polyline(array,sr)
#Insert the feature
insert.insertRow([polyline])
array.removeAll()print ("Start: {},{} End: {},{}".format(startX,startY,endX,endY))
del cursor
delete the insert at the end as well.
Otherwise the cursor is still open and the insert haven't yet been written or committed.
Hi Neil, thanks for the response. That doesn't seem to be the issue.
To be clear, if i explicitly set the insert cursor to the existing shapefile, the features are written as expected. If I use the newly created FC in the fGDB, then nothing is written (not even empty geometry).
Humour me, create a specific fgdb in a location on disk "C:\Temp\MyData.gdb" or something.
Point the create features at that. Make sure you set the environment.workspace variable correctly.
I can't see from your original code where the input points are coming from.
Neil you're on to something here. Here are my application level (arcgis desktop) environments:
Creating an fGDB as recommended and using
outGDB = r"D:\TEMP\test.gdb"
Writes the output as expected.
As mentioned earlier, I also tried to have the input points located (default.gdb) separate from the target lines (scratch.gdb) but that didn't work. Perhaps I have to explicitly set my workspace in the script.
I'm not understanding why I can't write to the scratchGDB.
If you or anyone suggests I start a new post instead, I'll be glad to.
UPDATE:
when I run this as a script tool the cursor inserts to the FC in the scratch GDB as expected.
dont work in that 'Users' space. That area gets bloated... trust me... we ban our students from that area completely
Dan,
I'll heed that advice. I've become used to relying upon the scratchGDB because the script readily translates into the published environment on our AGS.
I'm still at a loss why it worked as a script tool, but not in the Python console. Oh well! Thanks for everyone's help!