|
POST
|
1. You can try using raw_input() 3. Use os.path to construct and deconstruct file paths.
... View more
04-10-2015
04:52 PM
|
1
|
0
|
1107
|
|
POST
|
Xander, I recently unlocked the mysteries of enumeration and noticed your cnt variable does something similar. Because I'm still learning, I thought something like this would work instead: with arcpy.da.SearchCursor(fc_house, flds_house) as curs_in:
for cnt, row_in in enumerate([i for i in curs_in], start=1):
curs_in.reset() ## Return cursor back to the first row after enumeration
if cnt % 25 == 0:
print "Processing connection: {0}".format(cnt)
pnt1 = row_in[0]
parcel_id = row_in[1]
date_sale = row_in[2]
fire_oid = row_in[3]
# Continue processing rows in cursor... After some tinkering, I realized that when you enumerate a cursor like this, it goes through all the rows. The problem is that the cursor object ends with no more rows and you have to call reset() on the cursor to start it back at the first row again. Since there is still more processing to be done, you'll essentially be iterating over all rows in the cursor twice with enumeration instead of once with your original counter variable. Do you think the extra time it takes to create the enumeration of the cursor is worth it in a case like this? Maybe only for cursors with a small number of rows?
... View more
04-09-2015
11:18 AM
|
0
|
4
|
9038
|
|
POST
|
You can copy and paste the code because it is still just text. However, keep in mind that the editor in GeoNet is a rich text editor so it will try to keep any formatting that was on the original text. So if you copy a small portion of text from a code block with syntax highlighting and paste it into your reply, it will display with the same color and font but might be missing the background that's added when it's actual syntax highlighting. So if you try to copy and paste code like that, it might look weird. I find it's better to paste it into notepad (or some text editor that strips formatting) and then copy it from there and paste it into GeoNet. Once it's back in the GeoNet editor, you can apply syntax highlighting again and it will look like it should.
... View more
04-09-2015
10:21 AM
|
0
|
0
|
1510
|
|
POST
|
Are you having an issue running this code or are you just looking for suggestions to improve your code that's already working?
... View more
04-09-2015
10:14 AM
|
0
|
1
|
1602
|
|
POST
|
Almost, but not quite. That would give you an error because there is nothing at index 1. The field_names parameter for the SearchCursor can be declared as a list directly in the call to the method or you can assign it to a variable ahead of time and then pass that variable containing the field names list as the field_names parameter; don't do both. In your example, you've created a fields variable that contains a list of field name strings. Then, when you create the cursor, you explicitly tell it to use only "SomeField". What you'd get are rows as a single item tuple with one field value for SomeField. If you instead wanted access to the field values from all three fields you specified in the fields variable, create the cursor with fields as the field_names parameter (instead of just ["SomeField"] like I did in the example just before. These two examples would both do the same thing: print the field value of SomeField for every row in the feature class. fc = r'PathToLayer\LayerNamel'
fields = ["ObjectID", "SomeField", "Area"]
with arcpy.da.SearchCursor(fc, fields) as sCursor:
for row in sCursor:
print row[1] fc = r'PathToLayer\LayerNamel'
with arcpy.da.SearchCursor(fc, ["SomeField"]) as sCursor:
for row in sCursor:
print row[0] Choose one or the other, but don't combine them both.
... View more
04-08-2015
03:21 PM
|
2
|
0
|
1332
|
|
POST
|
Yes. Since I don't know exactly what outFld contains, I'll draft an example... fc = r'PathToLayer\LayerNamel'
fields = ["ObjectID", "SomeField", "Area"]
with arcpy.da.SearchCursor(fc, fields) as sCursor:
for row in sCursor:
print row ## Prints tuple of all field values
print row[0] ## Prints single field value in first field, ObjectID
print row[1] ## Prints single field value in second field, SomeField
print row[2] ## Prints single field value in third field, Area The example here using the with statement is the more typical way you'll see the cursors being used. The example I posted earlier uses the cursor in what's called list comprehension, which uses shorthand code to create a list from an iterable. The code from James Crandall also uses list comprehension to create a list of field names.
... View more
04-08-2015
02:58 PM
|
1
|
2
|
1332
|
|
POST
|
Yes, that's correct. I think what Dan is getting at is that although ListFields returns an iterator, the field objects it's iterating can't be compared directly, you have to call the property of the field object; like with .name
... View more
04-08-2015
02:51 PM
|
2
|
1
|
1332
|
|
POST
|
It's referring to an index of the list of values in the row variable. The search cursor returns each row as a tuple (which is a kind of list in Python). The number of indexes in that tuple is equal to the number of fields you used in the arguments to create the cursor. In this case, there's only one field so it returns a tuple with only one value as (23456.93426,) The comma in there is important because a single item tuple still has a comma afterwards (unlike a list). So when I say row[0], it's getting the value of the first item, which is also known as index 0. Index 1 would be the second item, and so on. If I just said row, it would return all field values in the whole row as a tuple instead of just the single value as a number.
... View more
04-08-2015
02:40 PM
|
0
|
5
|
1332
|
|
POST
|
The regular (non da) cursors are also older and will eventually get depreciated by Esri. The new ones are more future proof.
... View more
04-08-2015
02:17 PM
|
2
|
0
|
3157
|
|
POST
|
To expand on what James Crandall posted and accomplish your second task of getting the total area for all features in the shapefile, you could use a SearchCursor like Darren Wiens mentioned, or the Summary Statistics that you mentioned yourself. If you go with the Summary Statistics, you could use in-memory workspace for the output table, but you'd still have to use a SearchCursor to get the values out of it, so you might as well just do your own adding. import arcpy
fc = r'PathToLayer\LayerNamel'
field_names = [f.name for f in arcpy.ListFields(fc)]
if 'Area' not in field_names:
arcpy.AddField_management(fc, "Area", "FLOAT", 6, 3)
arcpy.CalculateField_management(
fc,
"Area",
"!SHAPE.area!",
"PYTHON_9.3",
)
fcAreas = [row[0] for row in arcpy.da.SearchCursor(fc, ["Area"])]
print sum(fcAreas) EDIT: After thinking about this, it might actually be better if you used Summary Statistics instead of calculating the field because if the geometry of the features change, the Area field will not get updated unless you run this code again. The values in the Area field could be misleading.
... View more
04-08-2015
02:12 PM
|
2
|
7
|
3157
|
|
POST
|
It looks like you spent a lot of time formatting your Python code to highlight syntax like the IDE. There's a handy way to let the GeoNet forums do that for you! Posting Code blocks in the new GeoNet
... View more
04-08-2015
01:41 PM
|
0
|
1
|
3157
|
|
POST
|
I have two questions for you, Richard: Does using OID@ for the field name let the cursor dynamically find the ObjectID field even if it isn't called ObjectID? Could you explain the first colon in the list comprehension on line 8? (row[0:])
... View more
04-08-2015
01:28 PM
|
0
|
3
|
3859
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 12-01-2025 06:19 AM | |
| 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 |