1

I have a table of points that looks something like this:

year,   latitude, longitude
2018,   42.02,    -87.68
2018,   41.91,    -87.78
2018,   41.64,    -87.68
2017,   41.63,    -87.56
2017,   41.89,    -87.67
2017,   41.67,    -87.80

Which I used Import XY to import from CSV as a set of points.

Now I want to perform some operation on it, but looping over the year attribute.

So far I got something like this:

import arcpy
imported_points = "imported_points.shp"

for yr in (2017, 2018):
  new_file = "poly_" + yr + ".shp"
  arcpy.SelectLayerByAttribute_management(imported_points , 
                                          "NEW_SELECTION",
                                          "[year] = " + yr)
  # Some operation, using this as an example          
  arcpy.DirectionalDistribution_stats(imported_points , new_file, "2_STANDARD_DEVIATIONS", "#", "#")

However, when I try this I get the following error:

ExecuteError: Failed to execute. Parameters are not valid.
The value cannot be a feature class
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).

What does this mean? What is the best way to do some operation like this?

-2

In ArcGIS you can't use ".shp" file directly for SelectLayerByAttribute_management analysis. First you must make it layer. Try to run your code after modifying like below.

import arcpy

path = "imported_points.shp"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame
layer = arcpy.mapping.Layer(path)
arcpy.mapping.AddLayer(df, layer)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
years = (2017,2018)
arcpy.SelectLayerByAttribute_management(layer.name,"NEW_SELECTION",""" \"year\" IN {} """.format(str(years)))
  • 3
    Why invoke the arcpy mapping module? – Aaron Jun 22 '19 at 20:54
  • It's not necessary when you run the code in ArcMap Python Console. – moortea Jun 24 '19 at 9:01

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