Adding All Raster Layer with the Same Name format in ArcMap

476
5
06-06-2018 01:24 PM
Sourav_Abdullah
New Contributor

In a folder, I have 150+ raster files - some of them are pansharpened images, some are panchromatic images, and some are in other formats. Among them, 30+ pansharpened raster has the same naming format of date_ZoneName_pansharpened.tif  (e.g. 20180511_TX1_pansharpened.tif,  20180511_TX2_pansharpened.tif,  20180511_TX2_pansharpened.tif and etc.). Now, I am adding them to ArcMap one by one with “Add data” menu. It’s time-consuming and not efficient. Is there any way of running a python script that goes through the folder and looks for any files with “_pansharpened.tif” in its name and imports it automatically to ArcMap?

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

Did you give arcpy.ListRasters a try? http://pro.arcgis.com/en/pro-app/arcpy/functions/listrasters.htm

you can specify a wildcard, presumably with your desired partial file name

0 Kudos
Sourav_Abdullah
New Contributor

Thanks Dan for your reply. I think it will return to a list. Can I import the rasters by iterating through the list? For example, if I get a list of list = [raster1.tif, raster2.tif, raster3.tif], can do the following -

for i in list:

   some_function(i)

All of those layers will be imported to ArcMap automatically.

Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

I suppose using Make Raster Layer http://pro.arcgis.com/en/pro-app/tool-reference/data-management/make-raster-layer.htm

but someone may suggest making a mosaic dataset instead.  

I am just not sure the advantage of spending time doing the coding rather than sorting the rasters in catalog and dragging the lot over at once.  Or do you have to do this hundreds of times?

0 Kudos
Sourav_Abdullah
New Contributor

I have 7 different fields, each field with 15-30 rasters. In addition,  I get one set of new rasters each week, thus importing 100-150 rasters/week for next couples of months is really a boring task. 

0 Kudos
Sourav_Abdullah
New Contributor

Using the following code, I am getting the list you wrote about. Just need to find a way to import them all.

from os import *

path = 'E:/Yield Program/Texas Section 1/01/20180601_202233_ssc8d2_0011'
text_files = [f for f in listdir(path) if f.endswith('pansharpened.tif')]
print (text_files)

0 Kudos