0

I keep getting this error

Traceback (most recent call last):
  File "\\drafting9\Drafting9\ShanePrice\Scripts\Python\Scripts\Cruise_Plan\Cruise_Plan_Model_Final_SP.py", line 77, in <module>
    arcpy.SelectLayerByLocation_management(Cruise_Plots, 'INTERSECT', Timber_Type_Polygon)
  File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\management.py", line 7320, in SelectLayerByLocation
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
Failed to execute (SelectLayerByLocation).


Failed to execute (CruisePlan).

from the arcpy.SelectByLocation.management line towards the end of this script.

# Import arcpy module
import arcpy
import os
#arcpy.env.overwriteOutput = True

arcpy.Delete_management("in_memory")

# Script Parameters
Timber_Type_Polygon = arcpy.GetParameterAsText(0)
Cell_Size_Height = arcpy.GetParameterAsText(1)
Cell_Size_Width = arcpy.GetParameterAsText(2)
Output_Coordinate_System = arcpy.GetParameterAsText(3)
Output_Folder = arcpy.GetParameterAsText(4)


# Set Geoprocessing environments
arcpy.env.outputCoordinateSystem = Output_Coordinate_System
arcpy.env.geographicTransformations = ""

# Process: Project
arcpy.AddMessage('...Projecting TimberType to Desired Projection')
arcpy.Project_management(Timber_Type_Polygon, os.path.join(Output_Folder, "Cruise_Polygon.shp"), Output_Coordinate_System, "", "PROJCS['NAD_1983_BC_Environment_Albers',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',1000000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-126.0],PARAMETER['Standard_Parallel_1',50.0],PARAMETER['Standard_Parallel_2',58.5],PARAMETER['Latitude_Of_Origin',45.0],UNIT['Meter',1.0]]", "NO_PRESERVE_SHAPE", "", "NO_VERTICAL")

NAR_U9 = os.path.join(Output_Folder, "Cruise_Polygon.shp")

# Process: Create Fishnet
desc = arcpy.Describe(NAR_U9)
arcpy.AddMessage('...Generating Cruise Grid')
arcpy.CreateFishnet_management("in_memory/fishnet", str(desc.extent.XMin) + " " + str(desc.extent.YMin), str(desc.extent.XMin) + " " + str(desc.extent.YMin + 10), Cell_Size_Width, Cell_Size_Height, "0", "0", str(desc.extent.XMax) + " " + str(desc.extent.YMax), "NO_LABELS", NAR_U9, "POLYLINE")

# Process: Clip
arcpy.AddMessage('...Clipping Grid to Block Area')
arcpy.Clip_analysis("in_memory/fishnet", NAR_U9, os.path.join(Output_Folder, "Cruise_Grid.shp"), "")

Cruise_Grid = os.path.join(Output_Folder, "Cruise_Grid.shp")

# Process: Intersect
arcpy.AddMessage('...Generating Cruise Plots')
arcpy.Intersect_analysis(Cruise_Grid, "in_memory/plots_before_dissolve", "NO_FID", "", "POINT")

# Process: Add XY Coordinates
arcpy.AddXY_management("in_memory/plots_before_dissolve")

# Process: Dissolve
arcpy.Dissolve_management("in_memory/plots_before_dissolve", os.path.join(Output_Folder, "Cruise_Plots.shp"), "POINT_X;POINT_Y", "", "SINGLE_PART", "DISSOLVE_LINES")

Cruise_Plots = os.path.join(Output_Folder, "Cruise_Plots.shp")

# Process: Add Sort Field
arcpy.AddField_management(Cruise_Plots, "Plot_ID", "SHORT", "", "", "5", "", "NULLABLE", "NON_REQUIRED", "")

# Sort Plots
arcpy.AddMessage('...Sorting Plots')
cursor = arcpy.UpdateCursor(Cruise_Plots, "", Output_Coordinate_System, "POINT_Y; POINT_X", "POINT_Y ASCENDING; POINT_X ASCENDING")

counter = 1
for row in cursor:
    row.Plot_ID = counter
    cursor.updateRow(row)
    counter += 1

# Process: Add Field (2)
arcpy.AddField_management(Cruise_Plots, "Type", "TEXT", "", "", "5", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(Cruise_Plots, "Type", "Reclass(!Plot_ID!)", "PYTHON", "def Reclass (Plot_ID):\\n  if Plot_ID % 2: \\n    return \"M\"\\n  else :\\n    return \"C\"\\n\\n\\n")

# Count Total Plots in Each TimberType
arcpy.AddMessage('Counting Plots in Each Timber Type')
with arcpy.da.UpdateCursor(Timber_Type_Polygon, ['PLOTS']) as cursor:
    for row in cursor:
        arcpy.SelectLayerByLocation_management(Cruise_Plots, 'INTERSECT', Timber_Type_Polygon)
        number_of_plots = int(arcpy.GetCount_management(Cruise_Plots).getOutput(0))
        row[0] = number_of_plots
        cursor.updateRow(row)

To me the parameters make sense and it should work, shouldn't it? Or do the referenced parameters still need to be within quotations?

1

Per http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/select-layer-by-location.htm:

The input must be a feature layer; it cannot be a feature class.

Cruise_Plots is a shapefile, not a layer. Select by Location or Select by Attributes both need an input of layers, not feature classes. Use arcpy.MakeFeatureLayer_management(Cruise_Plots, "Cruise_Plots_lyr") and then call with

arcpy.SelectLayerByLocation_management("Cruise_Plots_lyr", 'INTERSECT', Timber_Type_Polygon)

  • Perfect. That was the problem. Thank you! – S. Price May 1 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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