POST
|
I'm using the arcpy.da module with the updatecursor class to populate specific values for specific fields in several hundred shapefiles. I'm specifiying the fields of interest for the updatecursor in a list but the catch is that the fields in the list are not all present in every shapefile. So when the cursor trys to iterate through the fields of each shapefile and a field doesn't exist it throws an error saying the 'column is not specified'. I tried several ways of verifying if the fields exists but have had not luck. Any suggestions, see code below. Thanks def field_vals():
env.workspace = 'some_wspace'
field_list = ['field1', 'field2', 'field3', 'field4', 'field5']
val_list =[23,45,34,99,76]
fcList = arcpy.ListFeatureClasses()
for fc in fcList:
with arcpy.da.UpdateCursor(fc,field_list) as cursor:
for row in cursor:
if row[4] == 0 and row[0] not in val_list and row[0] != 0:
row[4] = row[1]
elif row[4] == 0 and row[1] not in val_list and row[1] != 0:
row[4] = row[2]
elif row[4] == 0 and row[2] not in val_list and row[2] != 0:
row[4] = row[3]
cursor.updateRow(row)
field_vals()
... View more
02-07-2013
07:18 AM
|
0
|
2
|
955
|
POST
|
Is it possible to show other features in a profile graph? For example if I have a point or line that runs through the profile can I show the location of that feature on the graph?
... View more
12-06-2012
09:19 AM
|
0
|
2
|
1053
|
POST
|
Yes, using more parameters is a good idea. The following code worked, thanks to rfairhur24, aka Jalopena. hasVal = 0
for rows in arcpy.SearchCursor(fcName,query,None,fieldName):
hasVal = 1
break
del rows
if hasValue:
print(Value + " Found")
# do some stuff here
else:
print(Value + " Not found.....") The first thing I would recommend is using more parameters in the search cursor to increase performance. I would start by hardcoding the values in a very simplistic cursor, once you have that working, I would try to build in the remainder of the logic. {Example} SearchCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields}) I noticed in your code you are building a query string and also search on a particular field. To increase performance, I would not recommend doing a full table scan each time and searching for a field in a loop. I would limit the fields in the cursor itself which should prevent full table scans, also by specifying the where_clause in the cursor itself you should notice a performance gain I would try something like this to start;
fieldName = "Name"
rows = arcpy.SearchCursor("yourlayername","where_clause="build your query here", "",fieldName,"A")
for row in rows:
print row.getValue(fieldName)
Esri's now recommends doing it this way at 10.1. Notice the {0} corresponds to fieldName and {1} to gisNum. This allows more flexibility in formatting strings rather than mixing double and single quotes with plus signs. A little cleaner as well. query = "Name = " + "'" + "g" + gisNum + ".tif" + "'"
fieldName = "Name"
querystring = " UPPER(\"{0}\") = UPPER('g{1}.tif') ".format(fieldName,gisNum))
rows = arcpy.SearchCursor("yourlayername","where_clause=querystring, "",fieldName,"A")
for row in rows:
print row.getValue(fieldName)
If you are at 10.1 look into arcpy.da.SearchCursor, it will outperform the 10.0 SearchCursor. At 10.0 there were performance issues with the SearchCursor especially when you have Subtypes or Domains in your data and if your data was in SDE.
... View more
08-17-2012
04:44 AM
|
0
|
0
|
527
|
POST
|
The following code always returns false, even when true, and the 'AddError' after the 'else' doesn't trigger. When I comment out the 'else:' it works fine, when it's true. The 'except:' doesn't trigger when it's false either? Any thoughts? import arcpy, os, sys stateName = arcpy.GetParameterAsText(0) gisNum = arcpy.GetParameterAsText(1) gisNumStr = "g" + gisNum + ".tif" arcpy.AddMessage("Searching.....") query = "Name = " + "'" + "g" + gisNum + ".tif" + "'" gisName = ("'" + "g" + gisNum + ".tif" + "'") mxd = arcpy.mapping.MapDocument("CURRENT") pdf = arcpy.mapping.ListDataFrames(mxd)[0] vLayer = arcpy.mapping.Layer(stateName) arcpy.mapping.ListLayers(mxd, stateName) try: arcpy.AddMessage("Starting.....") arcpy.AddMessage(gisNumStr) rows = arcpy.SearchCursor(stateName) for row in rows: if row.getValue("Name") == gisNumStr: arcpy.AddMessage(gisNumStr) arcpy.AddMessage(vLayer.name) vLayer.definitionQuery = query arcpy.AddMessage(query) arcpy.SelectLayerByAttribute_management(vLayer, "NEW_SELECTION", query) pdf.zoomToSelectedFeatures() arcpy.RefreshActiveView () arcpy.SelectLayerByAttribute_management(vLayer, "CLEAR_SELECTION") arcpy.RefreshActiveView () arcpy.AddMessage("Search completed..........") del mxd, pdf, vLayer, query, row, rows else: arcpy.AddError("Item does not exist"); sys.exit() del row del rows except: arcpy.AddError("No");sys.exit() del row del rows
... View more
08-16-2012
10:44 AM
|
0
|
2
|
1185
|
POST
|
Replace python.exe with pythonw.exe in your subprocess call. Pythonw is a non-console version of the python executable. Greatly appreciated, I'll give this a shot.
... View more
01-02-2012
03:24 PM
|
0
|
0
|
272
|
POST
|
I'm calling a subprocess from a py script that loops through several feature classes. Every time the subprocess processes each feature class the python exe window pops up. Is there a way to not have this window pop up? ArcGIS 9.3.1 Thanks
... View more
12-28-2011
05:33 AM
|
0
|
2
|
432
|
POST
|
I tried that, it's not the problem. I can watch the memory build up, once it gets to a certain threshhold it bombs out????
... View more
07-22-2011
09:15 AM
|
0
|
0
|
392
|
POST
|
Thanks for the suggestion, the code below ended up working....kind of. There's over 800 files and the code works like a dream until it gets to the 417th file, then I get a C++ Runtime Error! for Program: pythonw.exe???? Create the Geoprocessor object
gp = arcgisscripting.create(9.3)
dir = r"C:\temp"
output = r"C:\temp\shp"
gp.workspace = dir
fcs = gp.ListFeatureClasses("*")
x = '"%s"' % ';'.join(fcs)
print x
gp.Append(x, output, "NO_TEST")
print gp.getmessages()
... View more
07-12-2011
10:01 AM
|
0
|
0
|
392
|
POST
|
I have a folder with hundreds of shapefiles each representing a diff. state, each file starts with the prefix for that specific state e.g., al001r.shp, al003r.shp, fl001.shp, fl003.shp...etc. What I'm trying to do is Append the shapefiles for each state into one shapefile via python, in the end having a single shapefile for each state. I'm thinking I need to create a list of each of the prefixes and enumerate through the list thus creating a list of inputs for a multivalue input for the append? This is about as far as I've gotten, can't figure out how to select just the first iteration and push to a list for multi-input? dir = r"C:\temp"
gp.workspace = dir
print(dir)
files = os.listdir(dir)
alist = ['al', 'ct', 'dc', 'de', 'fl']
for file in files:
sfile = file[0:2]
if sfile in alist:
alist[alist.index(sfile)]
print(sfile)
... View more
07-06-2011
06:32 AM
|
0
|
4
|
588
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|