1

I am creating a custom Python script in ArcGIS that takes a rasters as a multivalue input and have run into a problem which I don't think is a duplicate (having scoured SE for many hours)

My problem is that I want my script to look at the name of the raster and mosaic it if appears more than once in the list of inputs. Note that the directory path can be different, but the raster name the same i.e. G:\DELIVERABLES\ABC.gdb\RASTER and G:\DELIVERABLES\123.gdb\RASTER

Where I am failing to understand is why it does not seem to preserve the data source (rasterpath) when I use the Counter tool from the collections module. I know I am probably going wrong in terms of how I am indenting here the script but I have script fatigue.

If I have an inputRasters value of:

G:\DELIVERABLES\ABC.gdb\RASTER;G:\DELIVERABLES\ABC.gdb\RASTER_1;G:\DELIVERABLES\123.gdb\RASTER_2;G:\DELIVERABLES\123.gdb\RASTER

The print results of arcpy.AddMessage(rasterpath) will give me a rasterpath value of 'G:\DELIVERABLES\ABC.gdb\RASTER_1'. It's giving me the data source of the third raster.

The print results of arcpy.AddMessage(rasterlist) below are:['RASTER','RASTER','RASTER_1','RASTER_2']

import arcpy, sys,os
import re
import collections
rasterlist = []
inputRasters = arcpy.GetParameterAsText(0)
for rasterpath in sorted(inputRasters.split(';')):
   Ras = str(os.path.basename(rasterpath))
   rasterlist.append(str(Ras))
   rasterlist = sorted(rasterlist)
arcpy.AddMessage(rasterlist)
counter = collections.Counter(rasterlist)
for key, value in counter.items():
   if value > 1:
      arcpy.AddMessage(rasterpath)

Can anyone tell me where I am going wrong?

I have tried varying the indentations to no avail.

Do I have to use split up the rasterpath using a "/" instead of using os.path.basename?

Script tool input

2

The variable rasterpath is a loop variable and contains the last iterated value from the previous loop, and can not contain the path of the reoccurring raster.

Try something like this instead [sorry - bit clumsy and not tested]:

import arcpy, sys,os
import re
import collections
rasterlist = []
inputRasters = arcpy.GetParameterAsText(0)
for rasterpath in sorted(inputRasters.split(';')):
   Ras = str(os.path.basename(rasterpath))
   rasterlist.append(str(Ras))
   rasterlist = sorted(rasterlist)
arcpy.AddMessage(rasterlist)
counter = collections.Counter(rasterlist)
for key, value in counter.items():
   if value > 1:
      arcpy.AddMessage(key)
      for rasterpath in sorted(inputRasters.split(';')):
          Ras = str(os.path.basename(rasterpath))
          if Ras == key:
              #this is where you have identically named rasters and can do the mosaic
              #in this instance the rasterpath variable does contain the path+name of a raster which has the same name as another
1
  • Thank you JimT. That helped a lot. I did think it was something as simple as that.
    – noddy33
    Sep 16 '19 at 9:22

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.