2

I am rewriting a 2.7 Python script for compatibility with ArcGIS Pro and organizing it with user defined functions so that I can debug portions of the script as I go. I am new to the process, but I thought it would be a good idea to pull the function out and test it alone. It's supposed to search a database and find an address location based on a SQL query. Right now the location is hardcoded for testing. When I run the script and print the selection variable, I get "None". Any suggestions or advice for debugging this?

import pdb
import arcpy
import os
import string
from io import StringIO
from io import BytesIO
import getpass 

def select_address():
    workspace = "G:\\Code Enforcement\\ACD Admin Operations\\GIS\\Operations\\BSC_Maps\\AutoLayerFiles.gdb"
    arcpy.env.workspace = workspace
    arcpy.env.overwritOutput = True
    location = '1900 McCall Rd'#arcpy.GetParameterAsText(0)
    expression = "FULL_STREET_NAME LIKE '" + location + "%'" #this is an SQL expression and can be modified as necessary
    aprx = arcpy.mp.ArcGISProject("G:\\Code Enforcement\\ACD Admin Operations\\GIS\\Operations\\BSC Maps\\BSC_Template_Auto_Generate_PRO_v1.aprx")
    for m in aprx.listMaps():
        #print(m)
        for lyr in m.listLayers():  
            #print(lyr)
            if lyr.name == "LOCATION.addresspoint":
                CoaAddresses = lyr
                #print(CoaAddresses)
                selection = arcpy.SelectLayerByAttribute_management(CoaAddresses,"NEW_SELECTION", expression)
                print(selection)
                for row in arcpy.SearchCursor(selection): #retrieve address of selected feature
                    selectedAddress = row.FULL_STREET_NAME
                    return(selectedAddress)

selection = select_address()
print(selection)

EDIT: I have tried applying the suggestions made in the comments and tried the following, but I still get None.

import pdb
import arcpy
import os
import string
from io import StringIO
from io import BytesIO
import getpass 
workspace = "G:\\Code Enforcement\\ACD Admin Operations\\GIS\\Operations\\BSC_Maps\\AutoLayerFiles.gdb"
arcpy.env.workspace = workspace
arcpy.env.overwritOutput = True
location = '7808 HART LN'#arcpy.GetParameterAsText(0)
aprx = arcpy.mp.ArcGISProject("G:\\Code Enforcement\\ACD Admin Operations\\GIS\\Operations\\BSC Maps\\BSC_Template_Auto_Generate_PRO_v1.aprx")


def select_address(address):

    expression = "FULL_STREET_NAME LIKE '" + address + "%'" #this is an SQL expression and can be modified as necessary
    for m in aprx.listMaps():
        #print(m)
        for lyr in m.listLayers():  
            if lyr.name == "LOCATION.addresspoint":
                CoaAddresses = lyr
            #print(lyr)
                with arcpy.da.SearchCursor(CoaAddresses, ['FULL_STREET_NAME'], where_clause=expression) as cursor:
                for row in cursor:
                    print(row[1])

selection = select_address(location)
print(selection)
10
  • 3
    Well you’re getting None because the searchcursor didn’t return anything. It probably found the map and layer otherwise it would have errored. So the problem Might be with your selection. You could do the where clause on the search cursor instead of running the GP tool. That may simplify things.
    – KHibma
    Dec 30, 2019 at 18:48
  • Sorry, I'm not following you on that. How would I do the where clause on the search cursor? Isn't the search cursor the GP tool? Dec 30, 2019 at 19:03
  • I tested the SQL within ArcPro and it does find an address so it's not a problem with the SQL expression. Dec 30, 2019 at 19:14
  • 2
    See example #4 desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/… Also, if porting to Pro, perhaps consider updating to arcpy.da.SearchCursor for faster cursors....
    – KHibma
    Dec 30, 2019 at 19:24
  • Can this replace SelectLayersByAttribute_management? Dec 30, 2019 at 19:31

1 Answer 1

1

This chunk of code would produce an exception since your row index is out of range.

            with arcpy.da.SearchCursor(CoaAddresses, ['FULL_STREET_NAME'], where_clause=expression) as cursor:
            for row in cursor:
                print(row[1])

row [1] would produce the exception. You want row [0]. You also will want to return it.

return (row [0])

However since an exception isn't thrown the cursor is never initiated with records.

In your first code selection is a Results object.

>>> selection = arcpy.SelectLayerByAttribute_management ("mains", "", "OBJECTID = 1")
>>> selection
<Result 'mains'>

Results objects can't be used as the input feature class in a cursor.

>>> with arcpy.SearchCursor (selection) as cursor:
...     for row in cursor:
...         print row.OBJECTID
...         
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: __exit__
>>> 

Since you're never receiving an exception I suspect your layer isn't being found. With either instance make sure the following code returns True (a layer name is matched) at some point:

if lyr.name == "LOCATION.addresspoint":

1
  • This helped me narrow down the problem: "LOCATION.address_point" needed an underscore in it. In conjuction with @Emil Brundage's suggestions, I added print statements after each part of the function to narrow down the area where the problem is. Jan 6, 2020 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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