|
POST
|
When doing this with very long lists, I find it helpful to also format the code so it's more readable. Here I organized it by the first letter. cnty = [
"Archuleta",
"Boulder","Broomfield",
"Chaffee","Cheyenne","Clear_Creek","Conejos","Costilla","Custer",
"Delta","Denver","Dolores","Douglas",
"Eagle","Elbert","El_Paso",
"Fremont",
"Garfield","Gilpin","Grand","Gunnison",
"Huerfano",
"Jefferson",
"Kit_Carson",
"Lake","La_Plata","Larimer","Logan",
"Mesa","Moffat","Montezuma","Montrose",
"Ouray",
"Pitkin","Prowers","Pueblo",
"Rio_Blanco","Rio_Grande","Routt",
"Saguache","San_Miguel","Summit",
"Teller",
"Weld",
"Yuma"
] # End cnty list You could also organize by any other logical groups with comments to make maintenance easier. code = [
## Some Group
"007","013","014","015","017","019","021","023",
## Another Group
"027","029","031","033","035","037","039","041","043","045","047","049",
## Third Group
"051","055","059","063","065","067","069",
## More Grouping
"075","077","081","083","085","091","097","099",
## Still Grouping
"101","103","105","107","109","113","117","119","123",
## Last Group
"125"
] # End code list And it works the same way with dictionaries!
... View more
02-05-2015
10:20 AM
|
1
|
0
|
2050
|
|
POST
|
Ah, yes, forgot about that. The actions for the original post are at the top of the post off to the right (under the following, share, bookmark, and like options).
... View more
02-05-2015
09:53 AM
|
0
|
0
|
2888
|
|
POST
|
Does using a search cursor in a list (dictionary) comprehension like this automatically close the cursor when it's finished like when using it in a with block? EDIT: I did a quick test and it looks like the lock does go away. Spiffy!
... View more
02-05-2015
09:41 AM
|
0
|
0
|
2888
|
|
POST
|
Post manage options (edit and delete) are under the Actions menu at the extreme bottom left of each post.
... View more
02-05-2015
09:22 AM
|
0
|
2
|
2888
|
|
POST
|
Instead of checking each row to see if it meets the condition, could you only bring back the rows that meet the condition and delete them all? import arcpy
arcpy.env.workspace = r"C:\SouthA\Bolivia.gdb"
listFCs = arcpy.ListFeatureClasses("*")
field_names = ["field1", "field2", "field6", "field7"]
where_clause = "field1 = 0 OR field2 = 0 OR field6 = 0 OR field7 = 0"
for fc in listFCs:
with arcpy.da.UpdateCursor(fc, field_names, where_clause) as cur:
for row in cur:
cur.deleteRow()
... View more
02-05-2015
08:36 AM
|
3
|
2
|
1249
|
|
POST
|
Does something like this get you what you need? def main():
import arcpy
import os
inputgdb = r"C:\temp\input.gdb"
outputgdb = r"C:\temp\output.gdb"
arcpy.env.workspace = inputgdb
for i in ["A", "B", "C", "D"]:
wild_card = "Test{}_area*".format(i)
fclist = arcpy.ListFeatureClasses(wild_card)
outputfc = os.path.join(outputgdb, "Test{}_area".format(i))
arcpy.Merge_management(fclist, outputfc)
arcpy.ClearWorkspaceCache_management() ## Cleanup
if __name__ == '__main__':
main() This assumes you do not have any feature classes in a feature dataset. If you do, the code will be slightly different.
... View more
02-04-2015
07:33 AM
|
2
|
2
|
1499
|
|
POST
|
Either you've been in the trenches of this project too long or I'm not fully understanding the problem, but it seems like this is just a simple inner join. You should be able to use Add Join with the KEEP_COMMON option. You'll get all of the fields from both data sources (SignSupport and SignPanel) but you can pare it down to just what you need when you export it (or whatever else you want to do).
... View more
02-03-2015
03:46 PM
|
1
|
18
|
2601
|
|
POST
|
I think you've really got two field calcs here: The IF statement to make the two letter codes The long concatenation expression I don't know how those two letter codes fit in to the concatenation expression. ArcGIS Help 10.2 - Calculate Field (Data Management) needs an expression type if you've got a code block to execute. The IF statement you have posted here is a code block in VB. For future compatibility and for the sake of this forum, I'll write it in Python. codeblock = """def findTwoLetter(sccatdesc):
output = None
if sccatdesc == "MAT":
output = "SB"
elif sccatdesc == "MBE":
output = "ME"
elif sccatdesc == "ME":
output = "TEST"
return output""" With that codeblock, you will need to include a call to the code block function (I temporarily called it findTwoLetter) with the field name as the argument. Something like expression = "findTwoLetter (!SCCatDesc!) " The final calculate field code would then be like arcpy.CalculateField_management(PeterTable, "PtrText", expression, "PYTHON_9.3", codeblock) Again, this does not include your concatenation because I don't know how it fits in.
... View more
02-03-2015
03:07 PM
|
1
|
1
|
650
|
|
POST
|
In your example you only have four feature classes with different prefixe variations (A, B, C, and D). Are those four prefixes always fixed or could they change? If there are more than just those four or if they change, please describe the formatting and/or range of the possible prefixes. Also, I noticed some inconsistencies with your imports: If you import arcpy (like you do on the first line), it includes env. It is redundant to have from arcpy import env if you've already imported everything in arcpy; especially if you're still calling workspace with the full arcpy.env.workspace You import os but I don't see that you're using it anywhere. Might save a little time to just not import it if you're not using it.
... View more
02-03-2015
12:44 PM
|
0
|
1
|
1499
|
|
POST
|
Here is what I was thinking. Where you do the update in the second part of your original script, maybe do it this way. I have not tested this though, code may have bugs/errors. # now do the update:
flds = correctFieldList(arcpy.ListFields(fc_out))
with arcpy.da.InsertCursor(fc_out, flds) as curs_out:
for p_id, p_owner in dct.items():
where_clause = '''"{0}" = {1}'''.format(fld_id, p_id)
with arcpy.da.SearchCursor(fc_in, flds, where_clause) as curs_in:
lst_row = list(curs_in.next())
lst_row[flds.index(fld_owner)] = p_owner
curs_out.insertRow(tuple(lst_row)) Not sure if recreating the search cursor every time is going to slow it down though (compared to writing to a dictionary or reading all rows). EDIT: The code syntax highlight keeps adding two extra single quote characters at the beginning of the where clause assignment. EDIT 2: I just noticed the InsertCursor documentation says the insertRow can be a list or tuple of values. Maybe it's not necessary to convert the lst_row back to a tuple on the last line? Sorry if I'm beating a dead horse, I'm still learning.
... View more
02-03-2015
10:47 AM
|
0
|
1
|
684
|
|
POST
|
Ah, yes, the where clause when you create the cursor! But then you would have to use a dynamic where clause and create a new cursor each time. I agree with you then, it would be silly to do that compared to just popping the item from the dictionary. Thanks for the clarification Xander!
... View more
02-03-2015
08:38 AM
|
0
|
4
|
1313
|
|
POST
|
Thanks Xander, I see it now. I'm just trying to understand the code and your process. So you are iterating through the fc_in rows and then checking if the current ID is in the dictionary before writing the data to fc_out. I was thinking it might be easier to iterate through the dictionary instead of fc_in so you wouldn't have to remove the dictionary item but then I don't know how you would find the correct row in fc_in to copy the data in the rest of the fields. Is there a way to do that? Seems like it could be a faster because you wouldn't have to touch every row of in_fc; you're done after checking every item in the dictionary.
... View more
02-03-2015
08:25 AM
|
0
|
6
|
1313
|
|
POST
|
What is the purpose of removing (and returning) the item from the dictionary just before you write it to the shapefile? I don't see the "tmp" variable used anywhere else. tmp = dct.pop(p_id)
... View more
02-03-2015
07:10 AM
|
0
|
8
|
1313
|
|
POST
|
At first you mentioned you want the results in a shapefile but now you're saying you want a "list." If some kind of printed report is really what you're after, you could try this: import arcpy
inputTable = r"C:\temp\myworking.gdb\mytablename"
fields = ["ID","OWNER"]
uniqueID = {}
with arcpy.da.SearchCursor(inputTable, fields) as s_cursor:
for row in s_cursor:
# Underscore in front of variable names only because "id" is a Python reserved word
_id = row[0]
_owner = row[1]
uniqueID.setdefault(_id, []).append(_owner)
for _id, _owners in uniqueID.items():
print _id, _owners It uses an arcpy data access search cursor to read through your data table one row at a time. With each row, it loads the ID and owner into a dictionary (like Geoff Olson mentioned below). However, as each row is loaded, the dictionary setdefault method checks to see if that ID has already been loaded. If it has not, it will create a new dictionary key for that ID and an empty list for the dictionary value. The list append method is the one that actually adds the owner to the list. You're left with a dictionary that has keys of only unique IDs and values as a list of owners. It just prints the results to the Python interpreter window. You could also write the data to a CSV or a new feature class or shapefile. Here's the credit for the logic I used to populate the dictionary.
... View more
02-02-2015
09:32 AM
|
1
|
0
|
2789
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 4 weeks ago | |
| 1 | 07-31-2025 11:59 AM | |
| 1 | 07-31-2025 09:12 AM | |
| 2 | 06-18-2025 03:00 PM | |
| 1 | 06-18-2025 02:50 PM |