POST
|
Since selections is defaultdict(dict), I think the error can appear only because there is no row[0], row[1] or row[2]. Please try to print it and see it you get correct results from cursor.
... View more
11-21-2017
10:36 AM
|
0
|
0
|
189
|
POST
|
This is my code, I hope it helps you James Crandall
import arcpy
from collections import defaultdict
vector = 'yourShapefile.shp'
# glac_id is my area ID, FID is the very first column in ArcMap attribute table and I calculate length of the shape on the fly
fields = ["glac_id", "FID", "SHAPE@LENGTH"]
selections = defaultdict(dict)
top3 = defaultdict(list)
with arcpy.da.SearchCursor(vector, fields) as cursor:
for row in cursor:
selections[row[0]][row[1]] = row[2]
for i in selections.keys():
# I take 3 last items (3 biggest) or sorted list of values. I could also reverse it and get 3 first ones.
top3[i] = sorted(selections[i], key=selections[i].get)[-3:] The if I print top3, it will give me: defaultdict(<type 'list'>, {u'G012161E78870N': [41, 46, 47], u'G011895E78886N': [5, 3, 18], u'G012039E78878N': [44, 42, 17]}) which are FIDs of 3 longest lines per area. Now all you have to do is extract lines by ID from original file (or do whatever you want).
... View more
11-20-2017
01:49 PM
|
1
|
2
|
704
|
POST
|
Thank you everyone for your help. I missed the part of docs saying that ORDER BY won't work with shapefiles. Fortunately a where clause works, so I can get my output in a little bit longer way. I just have to iterate over entire cursor and find max (or top 3) from the results.
... View more
11-20-2017
01:25 PM
|
0
|
4
|
704
|
POST
|
Thank you for your code. I was too fast though. It appears that your and my code correctly use"where" clause, but totally omit "sql_clause". With the code you supplied, when I iterate over "result" (removing .next() beforehand) I get all records belonging to my area ID, but unsorted. Maybe sql_clause needs some more parameters which aren't documented that well?
... View more
11-20-2017
12:41 PM
|
0
|
1
|
704
|
POST
|
This one works! Partly though. I can get correct result if I specify where_clause for a given area, but not a general highest result. When I don't know exact area IDs, this method will be hard to use. Without specifying WHERE, the sql_clause does not give any effect.
... View more
11-20-2017
12:32 PM
|
0
|
1
|
1030
|
POST
|
This is correct and it would be the next step I did, but I cannot even get to this point, because ArcPy doesn't honour any ORDER BY clause. What more, trying to put `'glac_id = "G012039E78878N"'` as `WHERE` clause doesn't modify results at all either.
... View more
11-20-2017
12:20 PM
|
0
|
6
|
1030
|
POST
|
Thanks Joshua Bixby and Randy Burton, I know how TOP and ORDER BY should work, but the point here is that none of example queries in `sql_clause` work, I couldn't get any ORDER BY or GROUP BY to work, or anything else. This is the code I tried right now: import arcpy
arcpy.env.overwriteOutput=True
testVector = r"C:\Users\adam\Desktop\testarcpy\cont_int.shp"
fields = ["glac_id", "length"]
sql = (None, 'ORDER BY length DESC')
# One method (get ordered list)
with arcpy.da.SearchCursor(testVector, fields, None, None, False, sql) as cursor:
for r in cursor:
print r
# Other method (just give me longest one)
OID = arcpy.da.SearchCursor(testVector, fields, sql_clause = sql).next()
print OID With above I always get unsorted values from the table. Even if 'length' was a string (which is not, it's a float), it is not ordered in any way, I get rows in the same order as they are in attribute table in ArcMap. There are no duplicate columns in the file obviously, no Nulls, nothing special really. I also tried ordering by SHAPE@LENGTH with no luck (same result again). Path to file is correct (I've just triple-checked it), column names are fine. I also tried with a test WHERE clause: 'length > 100.0' with no luck again. Very same results. I hoped this was a simple syntax error I just couldn't spot, but it looks like a bigger issue now.
... View more
11-20-2017
11:56 AM
|
0
|
8
|
1030
|
POST
|
Thanks. I am iterating over a simple shapefile, and if I stop using TOP and change clause to eg. `sql = (None, 'ORDER BY "length" ')`, I still get every record.
... View more
11-19-2017
12:21 PM
|
0
|
11
|
1030
|
POST
|
I have a shapefile where I have polylines and their fields are: - length - area into which they fall in Now I need 3 longest lines from each area. I have 3 areas and 50 polylines, so I should get 3*3=9 records as a result. What I try to do is: import arcpy
myFile = somePath
sql = ('TOP 3', 'ORDER BY "length" DESC GROUP BY "area_id" ')
cursor = arcpy.da.SearchCursor(myFile , "*", None, None, False, sql)
with cursor:
for r in cursor:
print r I get all of 50 records here. The same happens when I set `sql` to just `('TOP 1', None) or anything else. Is the syntax wrong here?
... View more
11-19-2017
11:45 AM
|
0
|
21
|
2578
|
POST
|
Thank you, that made it. Afterwards I only had to select by attributes (where X/Y > Z) and do some Joins and Spatial Joins to get a clean attribute table with glacier name and the value. In my case, where more than one "ranges" were selected I also also summarized them by original FID, so I got an average of all selected boundaries (ranges).
... View more
08-24-2017
04:51 AM
|
1
|
0
|
5460
|
POST
|
I have a raster representing glacierized area with 2 classes: distinction between ice and snow. I also have DEM of the area. I would like to calculate an average height of the boundary between those two classes. I came up with an idea I could somehow calculate how many pixels of each class belong to a given height range (like 500-515m a.s.l.). For example (with a picture): 100% of snow in this range isn't what I'm looking for (it's not a boundary) 100% of ice neither 80% of ice and 20% snow is not a boundary (could be a snow patch) 40% of snow and 60% of ice probably is a boundary (values are arbitrary and I'd like to evaluate them) So I reclassified DEM to 15m intervals and vectorized it. And now I have no clue. The Zonal Statistics tool only allows me to calculate statistics like MIN, MAX, MAJ etc., whereas I need a percentage - a custom equation. Or maybe there is a different way?
... View more
08-19-2017
05:37 PM
|
0
|
4
|
12680
|
POST
|
Hi, thanks. This doesn't have any effect on pixel depth though. The advantage is only that you don't need to run separate Clip Raster tool (which takes a long time) - of course in case you actually need any clip.
... View more
08-14-2017
09:20 AM
|
0
|
0
|
6547
|
POST
|
Thank you Dan and Günter. Although I was looking for the smoothing function from Image Analysis, it was faster to implement the Filter tool with low pass filtering and the result was similar, so that was sufficient for me.
... View more
08-14-2017
03:19 AM
|
0
|
0
|
637
|
POST
|
Hi, I would like to apply a smoothing function on multiple rasters using arcpy. How can I achieve that? The function is shown below: I suspect it should be in the "Add Function" menu and then I could export it, but I couldn't really find anything related.
... View more
08-13-2017
03:31 PM
|
0
|
4
|
1324
|
POST
|
You're right, but the Pricipal Components just doesn't honour this Environment setting.
... View more
08-12-2017
01:34 PM
|
0
|
2
|
6547
|
Title | Kudos | Posted |
---|---|---|
1 | 08-06-2017 07:24 AM | |
1 | 11-20-2017 01:49 PM | |
1 | 03-18-2017 05:33 AM | |
1 | 08-24-2017 04:51 AM | |
1 | 08-05-2017 05:38 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:25 AM
|