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: