|
POST
|
Darren Wiens Dan Patterson 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
... View more
09-28-2016
08:15 AM
|
0
|
6
|
1628
|
|
POST
|
Darren, thanks for picking up on my bonehead move there! Thanks for the additional insight regarding the cursors. Much appreciated!
... View more
09-28-2016
05:28 AM
|
0
|
0
|
3883
|
|
POST
|
Dan, thanks for the insight and what a treasure trove in the article you wrote! Thanks!
... View more
09-28-2016
05:25 AM
|
0
|
0
|
3883
|
|
POST
|
Dan_Patterson Thanks so much for the suggestions! I'm currently in the process of adding rotation to each line with some basic trig. Regarding your suggestions: I implemented this; was totally unaware of this. Thanks! Are you suggesting I un-nest the two cursors? Again, I'm a real hack here, so if you had an example or reference that would be great. Any harsh words/warnings regarding poor practice of the structure of cursors as they are now?
... View more
09-27-2016
01:12 PM
|
0
|
4
|
3883
|
|
POST
|
Thanks! I just stumbled across another post wherein which someone was recalling words from you Dan Patterson about setting the spatial reference...tried that and it wasn't the issue. I cleaned up the code a bit. Again, I'm totally up to suggestions, but this works! startingPt = "STARTING_POINTS" #point feature layer in MXD fields = ['SHAPE@X', 'SHAPE@Y'] #point = arcpy.Point() array = arcpy.Array() sr = arcpy.Describe(startingPt).spatialReference 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] endX = startX + 660 endY = startY + 660 array.add(arcpy.Point(startX, startY)) array.add(arcpy.Point(endX, endY)) insert = arcpy.da.InsertCursor(r"D:\Temp\Lines.shp",['SHAPE@']) polyline = arcpy.Polyline(array,sr) insert.insertRow([polyline]) array.removeAll() print "Start: {},{} End: {},{}".format(startX,startY,endX,endY) del cursor
... View more
09-27-2016
12:45 PM
|
0
|
6
|
3883
|
|
POST
|
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!
... View more
09-27-2016
12:03 PM
|
0
|
17
|
8640
|
|
POST
|
Jake, this is great and will totally get me there. To be clear, is there a way to build the sql query using a logical operator 'AND'? Here's an attempt but the query fails: def SelectRandomByCount (layer, count): import random layerCount = int (arcpy.GetCount_management (layer).getOutput (0)) if layerCount < count: print "input count is greater than layer count" return oids = [oid for oid, in arcpy.da.SearchCursor (layer, "OID@")] oidFldName = arcpy.Describe (layer).OIDFieldName delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName) TypeField = arcpy.AddFieldDelimiters (layer, "OS_TYPE") #TypeField = "OS_TYPE" Type = 'S3A' randOids = random.sample (oids, count) oidsStr = ", ".join (map (str, randOids)) sql = """ {0} IN ({1}) AND {2} = '{3}' """.format (delimOidFld, oidsStr, TypeField, Type) print sql arcpy.SelectLayerByAttribute_management (layer, "", sql) [EDIT: fixed...i just had to for I ask this because what I'm actually looking to do this for the same field to satisfy 5 different queries...say for "FIELD" = 'Value1', then "FIELD" = 'Value2'...etc. I guess that would be a loop within a loop? If this qualifies as another question altogether, I'll mark yours as correct. I'll hammer away at this, but do appreciate the insight!
... View more
09-22-2016
08:16 AM
|
1
|
2
|
1309
|
|
POST
|
Basically I'm trying to create a selection that satisfies a random criteria AND a query based on an attribute field. Here is some sample code I found on StackExchange: def SelectRandomByCount (layer, count): import random layerCount = int (arcpy.GetCount_management (layer).getOutput (0)) if layerCount < count: print "input count is greater than layer count" return oids = [oid for oid, in arcpy.da.SearchCursor (layer, "OID@")] oidFldName = arcpy.Describe (layer).OIDFieldName delimOidFld = arcpy.AddFieldDelimiters (layer, oidFldName) randOids = random.sample (oids, count) oidsStr = ", ".join (map (str, randOids)) sql = "{0} IN ({1})".format (delimOidFld, oidsStr) arcpy.SelectLayerByAttribute_management (layer, "", sql) What I need is this in conjunction with a query (from the same input later) where FIELD = Value (for a string field). I have reviewed the ESRI help document related to Specifying a query in Python, but am having difficulty understanding how to implement this.
... View more
09-22-2016
07:47 AM
|
0
|
4
|
2336
|
|
POST
|
I got it to work!!! Nice way to close out the day...for some reason the OID field needed single quotes too. If you want to modify your code I'll mark your answer as accepted. Cluster_Plot = '"{}-{}"'.format(i,'!OBJECTID!')
... View more
09-14-2016
01:03 PM
|
1
|
1
|
3256
|
|
POST
|
Hi Micah...I'm just getting a generic 'invalid syntax' error. for the first option. The second wont work because the OIDs aren't coming from the FC used to run the loop/search cursor.
... View more
09-14-2016
11:53 AM
|
0
|
3
|
3256
|
|
POST
|
OK, so I feel like I've tried just about everything here. What I'm looking to do is to concatenate a iteration value (?) from a search cursor with the object ID of a feature class to create a unique ID field. I want these two values separated by a hyphen '-'. Target field (PLOT_ID) is obviously a string. There is what I've got working so far. i = 1 ptCur = arcpy.da.SearchCursor (ptFC, fldLst) for row in ptCur: [various things happen removed for brevity] Cluster_Plot = str(i) + '!OBJECTID!' arcpy.AddMessage(Cluster_Plot) arcpy.CalculateField_management(outPointsDissolve,"PLOT_ID",Cluster_Plot,"PYTHON_9.3") things are appended to a a target FC and the target field looks like this: So it appears to be treating the expression inputs as strings. From here I've tried to get a - in between the two values with no luck....even when converting the OID to a string : 'str(!OBJECTID!)' I just can't get things to escape right i guess. Things I've tried: Cluster_Plot = str(i) + '-' + '!OBJECTID!' Here the hyphen apparently is interpreted as an operator: Cluster_Plot = str(i) + " + '-' + " + '!OBJECTID!' Cluster_Plot = str(i) + " + '-' + " + 'str(!OBJECTID!)' I dont get this because you can use the + operator in the field calculator just fine (if you format the field as a string using str) Could someone please help me out of my misery here?
... View more
09-14-2016
11:36 AM
|
0
|
5
|
4856
|
|
POST
|
I've been searching high and low for this (Stackoverflow/exchange, etc, Geonet...). Here it goes. I have a table with a number of records. I have a field called 'Rotation' I would like to populate with a repeating set of values [0,90,180,270]. So imagine the following: PT_ID ROTATION 1 0 2 90 3 180 4 270 5 0 6 90 7 180 8 270 9 0 I've tried to simplify my question as much as possible, but its literally as simple as that. Most of the searches return information related to deleting duplicate/repeated records & values.
... View more
09-09-2016
08:47 AM
|
0
|
3
|
1860
|
|
POST
|
Or, should I just be thinking about hosting my own GP service as a solution?
... View more
08-16-2016
10:19 AM
|
0
|
1
|
1255
|
| Title | Kudos | Posted |
|---|---|---|
| 9 | 03-24-2026 11:41 AM | |
| 1 | 11-06-2024 06:58 AM | |
| 1 | 12-16-2022 07:01 AM | |
| 1 | 08-09-2024 06:55 AM | |
| 1 | 08-13-2024 05:58 PM |
| Online Status |
Offline
|
| Date Last Visited |
03-24-2026
12:51 PM
|