0

I am trying to create a script with Arcpy that buffers a feature layer point of Salt Lake City.I have been having trouble getting my select by location function to run. I don't know if it's from my distance parameter value in my select by location which is to see all other cities 200 miles within Salt Lake, but I keep getting Error output like such. I tried altering the variables but was having no luck.

#Data variables

import arcpy
arcpy.env.workspace = (r"C:\Arcpy\MapAutomation.gdb")
Places =  r"C:\GIS_Data_Files\shp\ne_10m_populated_places.shp"

The above code is the are variables for input shapefiles and local workspace.

PlacesQuery = "name = 'Salt Lake City'"
arcpy.MakeFeatureLayer_management(Places,"SaltLake",PlacesQuery)
SLPlaces200mi = arcpy.SelectLayerByLocation_management(Places,"WITHIN_A_DISTANCE" "SaltLake","200 MILES","NEW_SELECTION")

The above code Creates a feature layer from the places shapefile and inputs a variable for a definition query to return Salt Lake City. Then the select by location looks at the places layer and sees if there are other values that are 200 miles away from Salt Lake. This is where I run into my error:

arcgisscripting.ExecuteError: ERROR 000622: Failed to execute (Select Layer By Location). Parameters are not valid.

ERROR 000628: Cannot set input into parameter search_distance.

0

1 Answer 1

1

Places is not a layer and in your query names is missing field delimiters (like double quotes, single quotes, brackets etc.)

Try using AddFieldDelimiters with format when your are creating the queries. Note that many tools are returning a Result object, not for example a layer, see your 6th line.

import arcpy
arcpy.env.overwriteOutput=True

Places =  r"C:\GIS\data\dffddsf.shp"
namefieldname = 'TEXT'
print arcpy.GetCount_management(in_rows=Places)

PlacesQuery = "{0} <> ' Ballekvare'".format(arcpy.AddFieldDelimiters(datasource=Places, field=namefieldname))
arcpy.MakeFeatureLayer_management(in_features=Places, out_layer="Most_places")

PlacesQuery = "{0} = ' Ballekvare'".format(arcpy.AddFieldDelimiters(datasource=Places, field=namefieldname))
arcpy.MakeFeatureLayer_management(in_features=Places, out_layer="B", where_clause=PlacesQuery)
print arcpy.GetCount_management(in_rows="B")

SLPlaces200mi = arcpy.SelectLayerByLocation_management(in_layer="Most_places", overlap_type="WITHIN_A_DISTANCE", select_features="B",
                                                       search_distance="200 MILES", selection_type="NEW_SELECTION") #Missing comma and Places is not a layer
print type(SLPlaces200mi)

arcpy.SelectLayerByLocation_management(in_layer="Most_places", overlap_type="WITHIN_A_DISTANCE", select_features="B",
                                       search_distance="200 MILES", selection_type="NEW_SELECTION")

print arcpy.GetCount_management(in_rows="Most_places")

enter image description here

3
  • I thought the namefieldname variable would hold the sql expression query that is used as the input value for the field parameter in the AddFieldDelimiter function. So the string value in the namefieldname variable would be namefieldname = "NAME ='Salt Lake City'". Is that what the "TEXT" value is used for in your example? Also in the places query variable, does the does the string Ballekvare act as an example city for the city name being queried?
    – Ryan Mags
    Jun 22, 2021 at 22:36
  • does what I said make any sense in the comment above.
    – Ryan Mags
    Jul 1, 2021 at 23:33
  • if your fieldname is called name you need to enclose it in ", ', [ (field delimiters) , depending or what kind of data source you have.
    – BERA
    Jul 2, 2021 at 5:55

Your Answer

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

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