0

I am trying to learn python for ArcGIS.

Currently on this tutorial exercise:

https://www.e-education.psu.edu/geog485/node/191

 import arcpy
featureClass = r"C:\Users\A\Desktop\ARC\USA.gdb\StateBoundaries"
state = "Wyoming"
field = "NAME"
item = ""
try:
    arcpy.MakeFeatureLayer_management(featureClass, "USALayer")
    arcpy.MakeFeatureLayer_management(featureClass,
                                      "SelectionLayer",
                                      '"' + str(field) + '" =' + "'" + str(state) + "'")
    arcpy.SelectLayerByLocation_management("USALayer","BOUNDARY_TOUCHES","SelectionLayer")

    rows = arcpy.SearchCursor(USALayer,"","",field)
    for row in rows:
        if item != row.NAME:
            item = row.NAME
        print item

except:
    print arcpy.GetMessages()

arcpy.Delete_management("USALayer")
arcpy.Delete_management("SelectionLayer")

I am getting following message:

Executing: SelectLayerByLocation USALayer BOUNDARY_TOUCHES SelectionLayer # NEW_SELECTION Start Time: Wed Feb 28 11:15:49 2018 Succeeded at Wed Feb 28 11:15:49 2018 (Elapsed Time: 0.00 seconds)

I think I am making mistake in converting arcpy.da.SearchCursor code that tutorial uses to arcpy.SearchCursor since SelectLayberByLocation is executing successfully but I am unable to print items in 'NAME' variable.

Using ArcGIS 10.0, hence need to change code.

closed as unclear what you're asking by BERA, nmtoken, whyzar, Ian Turton Mar 1 '18 at 15:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    What is the code supposed to do? We should not have to follow links to understand your question. – BERA Feb 28 '18 at 7:29
  • 1
    Your try/except statements may be masking error messages that would otherwise help you. – PolyGeo Feb 28 '18 at 9:38
  • The current version of ArcGIS is 10.6. ArcGIS 10.0 has been in Retired support status for 26 months. There isn't really much point in learning "Python for ArcGIS" with a version of ArcGIS that barely used Python (and an elderly version of Python at that). If you are using software significantly older than the release used to design the tutorial, you are likely to encounter many examples which do not work. – Vince Feb 28 '18 at 11:47
2

I would not try to convert the code from arcpy.da.SearchCursor to arcpy.SearchCursor as the da version is much faster. arcpy.SearchCursor is there for legacy reasons. Your time is better spent in learning the syntax of the new da style cursor. ESRI will not be supporting the old style cursor in future releases.

0

Although I agree with Hornbydd's answer, the issue you are having should be related with the way you are trying to access the row's field values.

The arcpy.da.SearchCursor returns a da.SearchCursor object and each row you access through this cursor is a tuple (native Python object).

The arcpy.SearchCursor returns a arcpy.arcobjects.arcobjects.Cursor object and each row you access through this cursor is a arcpy.arcobjects._base.Row object. With this row object you access the field values through the getValue method:

row.getValue('NAME')

0

Use da.Cursors. If you really need to access data by field name, you can use this snippet:

import itertools
fc = 'path/to/fc'
fields = [field1, field2, field3]
with arcpy.da.SearchCursor(fc, fields) as sc:
    for row in itertools.imap(lambda row: {key: val for key, val in zip(fields, row)}, sc):
        print row[field1]

It returns a row as a dictionary with keys as fields names and values as data from corresponding column. I've tested it on very large feature class with up to 100 fields and it works ~2 times slower than original da.SearchCursor. However, it's only a time used for iteration, so total overheading seems to be relatively small comparing to overall processing time.

And yes, upgrade to at least 10.2 because it's senseless to learn an old-style package that isn't supported now.

Not the answer you're looking for? Browse other questions tagged or ask your own question.