-1

I have 55 rasters located in "G:\desktop\Project\lyr\50k", and i try to add them to MXD file - only if the rasters extent are in the dataframe extent of the MXD file, using python code (in ArcGIS 10.3):

import arcpy

arcpy.env.workspace = r"G:\desktop\Project\lyr\50k"
for rasterFile in arcpy.ListFiles("*.jpg"):
    print rasterFile
    mxd = arcpy.mapping.MapDocument(r"G:\desktop\Project\project.mxd")
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),df.spatialReference)
    if arcpy.SelectLayerByLocation_management(rasterFile , "INTERSECT", dfAsFeature):
        addLayer = arcpy.mapping.Layer(rasterFile)
        arcpy.mapping.AddLayer(df, addLayer, "TOP")
        print 'add to map'
    mxd.save()
del mxd, addLayer

but i got en error and i don't understand what is the problem- i placed the rasterFlie and dfAsFeature variable in the arcpy.SelectLayerByLocation_management row, and got mistake:

Traceback (most recent call last):
  File "g:\Users\yaron\Desktop\abc.py", line 9, in <module>
if arcpy.SelectLayerByLocation_management(rasterFile , "INTERSECT", dfAsFeature):
  File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 7359, in SelectLayerByLocation
raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000368: Invalid input data.
Failed to execute (SelectLayerByLocation).

I red:

Checking if layers in dataframe using ArcPy?

0

Although we cannot see your full error message (including line number) and output from your print statement, it looks to me like you should review the Select Layer By Location help because in the tool syntax of:

SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}, {invert_spatial_relationship})

the in_layer is expected to be one of:

Feature Layer; Mosaic Layer; Raster Catalog Layer

You are providing a raster dataset instead of a raster layer so I suggest using Make Raster Layer first.

  • how do i integrated in in my code? – newGIS May 24 '18 at 7:51

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.