|
DOC
|
I'd like to also add suggestion of copying your code to something like notepad or notepad++ to deal with spacing/wraparound and then past it back into the codeblock per Dan Patterson suggestion in comments found here.
... View more
10-27-2016
07:57 AM
|
1
|
0
|
3248
|
|
POST
|
There are several variations on this out there but all are use the OID/FID of the FC to create the query. I'd like to modify this to accommodate a different field which is still an integer value. The thing is arcpy.Describe makes it damn easy to get a list of FIDs. Am I missing something here or am I going to have to build a list by other means? I do realize you could use a loop to create a super long query using 'OR' but given that the attribute values are integer, I see no reason to use 'IN' for this. Here's some code: import arcpy
#Assumes the layer in ArcMap has a selected features
lyr = arcpy.mapping.Layer("yourLayer")
lyrDesc = arcpy.Describe(lyr)
fieldDesc = lyrDesc.FIDset
selectedList = fieldDesc.split(";")
#Build your query; the below is poor since it assumes "OBJECTID" is present; this is a draft
sql = "{} IN ({})".format("OBJECTID", ", ".join([str(n) for n in selectedList]))
#Apply the definition query
lyr.definitionQuery = sql
... View more
10-27-2016
07:38 AM
|
0
|
1
|
813
|
|
POST
|
Dan, want to thank you for your (as always) high-level view on things here. I've actually had some good success with itertools. I did play around for a while this AM with what you provided.I think that I'm just too much of a novice to put collections to work here. I ended up leveraging summary stats with a da SearchCursor which works pretty slick (albeit far less elegant than what you propose). I'd post it up when its cleaned up, but I'm not sure it will add much to the conversation here. Don't count me out for collections just yet though...I'm going to keep working on the basics.
... View more
10-06-2016
07:59 AM
|
0
|
0
|
1325
|
|
POST
|
Yeah...this could be quite side venture. Its late for me here...my mind can't see a way forward just yet. I would have thought it would be easier to create some kind of validation like this ...maybe i can create something a bit more..uh...crummy? Like looking for a string value to be true...if a certain point ID doesn't exist (the one coinciding with the threshold value) then delete all points in the 'cluster' (like cluster ID value). Seems...crummy. Anyway, per my original post, could you perhaps help me develop a search string I can use to look for something similar on other forums? My grasp of proper terminology is admittedly weak here (but perseverance I'm an ace). 'Get sorted count' comes to mind...'evaluate sorted count', 'evaluate sorted field'....'greater than sorted value'...bah...
... View more
10-05-2016
06:51 PM
|
0
|
0
|
1325
|
|
POST
|
I see what you mean. So is there a way to get at those summarized/sorted values and evaluate them as a basic integer value? FYI, I'm normally hesitant to muddy the waters with the background purpose, but felt it was appropriate here.
... View more
10-05-2016
03:32 PM
|
0
|
2
|
1325
|
|
POST
|
I have a tool which creates a number of points grouped together in 'clusters'. Inevitably, the tool will generate points that fall outside the project boundary. Removing these points is a simple task using select by location. However, I also want to remove any cluster of points that has less than a required threshold value. I'm having some success with using 'collections' much in the same way that Summary Stats would work. However, I'm running into difficulty using an IF statement with the collection....it seems to print anytime the value (regardless of value) is 'collection > X' and prints nothing when ' collection < X' and fails to work when I try ' collection = X'. So it appears the collection is a 'Counter'. Am I on the wrong track here?I couldn't get 'GetCount' to summarize by each value in a field. EDIT: it looks like 'sorted' may be something I can use here, but having a hard time finding examples of this. I want to remove all points in the cluster if the particular cluster contains less than a threshold value. Here is my code so far: def CleanSamplePlots (inPoints, standLayer, outPoints):
#inPoints are the raw output of the desired point generation process
#standLayer is the project stand dataset
#outPoints are the cleaned, validated points
arcpy.MakeFeatureLayer_management(inPoints, "ptLayer")
arcpy.SelectLayerByLocation_management ("ptLayer", "intersect", standLayer,"", "", 'INVERT')
if int(arcpy.GetCount_management("ptLayer").getOutput(0)) > 0:
arcpy.DeleteFeatures_management("ptLayer")
with arcpy.da.SearchCursor("ptLayer", "CLUSTER_ID") as cursor:
count_of_items = collections.Counter(row[0] for row in cursor)
for item in sorted(count_of_items.items(), key=lambda x:x[1]):
if count_of_items < 8:
print "Cluster:{0} Points{1}".format(item[0], item[1])
Cluster:4 Points4
Cluster:14 Points4
Cluster:6 Points7
Cluster:7 Points8
Cluster:12 Points8
Cluster:21 Points9
Cluster:1 Points9
Cluster:2 Points9
Cluster:20 Points10....etc
... View more
10-05-2016
12:18 PM
|
0
|
6
|
2093
|
|
POST
|
Neil Ayres I understand what you guys are getting at now regarding the 'row' (doh!). I've modified the inserts, and the script now runs correctly! Can I get some guidance as to who to mark as the correct answer here? Everyone helped in one way or another. Should I modify the title of the post at all to better reflect what was going on here? Thanks so much! import arcpy, os, sys, math
#Environment settings
outGDB = arcpy.env.scratchGDB
arcpy.env.workspace = outGDB
arcpy.env.overwriteOutput = True
#Local Variables
#startingPt = arcpy.GetParameterAsText(0)
#sampleNumber = int(arcpy.GetParameterAsText(1))
#interDist = int(arcpy.GetParameterAsText(2))
startingPt = "startingPoints"
interDist = 660
outName = "cruisePoints"
#Create target point FC
sr = arcpy.Describe("frontier").spatialReference
outPoint = arcpy.CreateFeatureclass_management(outGDB,outName,"POINT","","","",sr)
arcpy.AddField_management(outPoint, "CLUSTER_ID", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(outPoint, "PLOT_ID", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#Open Insert Cursor
insert = arcpy.da.InsertCursor(outPoint,["SHAPE@XY","CLUSTER_ID","PLOT_ID"])
fields = ["SHAPE@X", "SHAPE@Y", "OID@"]
with arcpy.da.SearchCursor(startingPt, fields) as cursor:
for row in cursor:
#Set Start points
startX = row[0]
startY = row[1]
cluster = row[2]
#Calculate quadrant points
northX = row[0]
northY = row[1] + interDist
eastX = row[0] + interDist
eastY = row[1]
southX = row[0]
southY = row[1] - interDist
westX = row[0] - interDist
westY = row[1]
insert.insertRow([(startX, startY),cluster,"1"])
insert.insertRow([(northX, northY),cluster,"2"])
insert.insertRow([(eastX, eastY),cluster,"3"])
insert.insertRow([(southX, southY),cluster,"4"])
insert.insertRow([(westX, westY),cluster, "5"])
del cursor
... View more
10-04-2016
06:45 AM
|
0
|
1
|
1966
|
|
POST
|
Neil Ayres Ok, so now we get to the background. I have another script which creates points spaced at a regular interval. These are the 'starting points' for field sampling. The field tech navigates to that location, does the sampling work, and then has 4 other points associated with the particular starting point which they need to conduct the same work at. A total of 5 points. These five points constitute a 'cluster'. The points in each each cluster need to be identified/associated with the cluster ("CLUSTER_ID"), but also have a unique value ("PLOT_ID"); concatenating the two would give you a unique value for each point for the entire project. FWIW, the error raised has to do with 'value 1' which seems to be the XY. Anyway, here's a graphic of what I'm trying to achieve.
... View more
10-04-2016
06:22 AM
|
0
|
2
|
1966
|
|
POST
|
bixb0012 Thanks for the response. I tried the modification (and I see your point now...no pun intended), but I'm still getting the same error: Below is a portion of the code, line 48 from the error is line 27 below. #Open Insert Cursor
insert = arcpy.da.InsertCursor(outPoint,["SHAPE@XY","CLUSTER_ID","PLOT_ID"])
fields = ["SHAPE@X", "SHAPE@Y", "OID@"]
with arcpy.da.SearchCursor(startingPt, fields) as cursor:
for row in cursor:
#Set Start points
startX = row[0]
startY = row[1]
cluster = row[2]
#Calculate quadrant points
northX = row[0]
northY = row[1] + interDist
eastX = row[0] + interDist
eastY = row[1]
southX = row[0]
southY = row[1] - interDist
westX = row[0] - interDist
westY = row[1]
#start = arcpy.Point(startX, startY)
#print(start)
#north = arcpy.Point(northX, northY)
#east = arcpy.Point(eastX, eastY)
#south = arcpy.Point(southX, southY)
#west = arcpy.Point(westX, westY)
insert.insertRow([(startX,startY),row,cluster])
insert.insertRow([(northX,northY),row,cluster])
insert.insertRow([(eastX,eastY),row,cluster])
insert.insertRow([(southX,southY),row,cluster])
insert.insertRow([(westX,westY),row,cluster]) Dan_Patterson I'm glad I finally found your codeblock formatting write-up! Thanks for that...it was driving me nuts posting stuff as a quote before.
... View more
10-04-2016
05:38 AM
|
0
|
5
|
1966
|
|
POST
|
My brain is fried...if anyone can/wants to modify the title of the post please do so. I'll attempt to post this w/o giving the background of the tool...if that is necessary I'll happily provide it. Here is my code: import arcpy, os, sys, math
#Environment settings
outGDB = arcpy.env.scratchGDB
arcpy.env.workspace = outGDB
arcpy.env.overwriteOutput = True
#Local Variables
#startingPt = arcpy.GetParameterAsText(0)
#sampleNumber = int(arcpy.GetParameterAsText(1))
#interDist = int(arcpy.GetParameterAsText(2))
startingPt = "startingPoints"
interDist = 660
outName = "cruisePoints"
#Create target point FC
sr = arcpy.Describe(startingPt).spatialReference
outPoint = arcpy.CreateFeatureclass_management(outGDB,outName,"POINT","","","",sr)
arcpy.AddField_management(outPoint, "CLUSTER_ID", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(outPoint, "PLOT_ID", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
#Open Insert Cursor
insert = arcpy.da.InsertCursor(outPoint,["SHAPE@XY","CLUSTER_ID","PLOT_ID"])
fields = ["SHAPE@X", "SHAPE@Y", "OID@"]
with arcpy.da.SearchCursor(startingPt, fields) as cursor:
for row in cursor:
#Set Start points
startX = row[0]
startY = row[1]
cluster = row[2]
print(startX,startY)
#Calculate quadrant points
northX = row[0]
northY = row[1] + interDist
print(northX, northY)
eastX = row[0] + interDist
eastY = row[1]
southX = row[0]
southY = row[1] - interDist
westX = row[0] - interDist
westY = startY
start = arcpy.Point(startX, startY)
print(start)
north = arcpy.Point(northX, northY)
east = arcpy.Point(eastX, eastY)
south = arcpy.Point(southX, southY)
west = arcpy.Point(westX, westY)
insert.insertRow([start,row,cluster])
insert.insertRow([north,row,cluster])
insert.insertRow([east,row,cluster])
insert.insertRow([south,row,cluster])
insert.insertRow([west,row,cluster])
del cursor When pasting into the Python Console in Desktop I get the following error (note the third print as well)
... View more
10-03-2016
12:37 PM
|
0
|
7
|
2882
|
|
POST
|
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!
... View more
09-29-2016
04:45 AM
|
0
|
0
|
928
|
|
POST
|
Neil Ayres 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.
... View more
09-28-2016
10:02 AM
|
0
|
1
|
928
|
|
POST
|
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).
... View more
09-28-2016
08:54 AM
|
0
|
3
|
928
|
|
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
|
928
|
|
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
|
2889
|
| Title | Kudos | Posted |
|---|---|---|
| 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 | |
| 1 | 07-23-2024 08:00 AM |
| Online Status |
Offline
|
| Date Last Visited |
10-24-2025
05:12 AM
|