Minus Tool

3819
8
11-15-2011 10:16 AM
ChristopherStrother
New Contributor II
I'm creating a workflow and need to perform an iteration of the Minus Tool. Unfortunately, only one iterator is allowed in a model and I can't tell the tool to get a file from one folder and subtract it from another file from a different folder (724 times). Someone suggested the batch function of the tool, but that requires me to click each file to add it to the list to process. Any work arounds or suggestions?
0 Kudos
8 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Chris,

Are you looking to subtract one raster from 724 other rasters?  You can accomplish this using a python script.  Below is an example:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\Rasters"

arcpy.CheckOutExtension("spatial")

lstRasters = arcpy.ListRasters("*")
for raster in lstRasters:
    outMinus = arcpy.sa.Minus(raster, r"C:\data\raster\TIFF\example1.tif")
    outMinus.save(r"C:\temp\python\Rasters" + "\\" + raster + "_minus.tif")
    print "Successfully subtracted raster"


What the above script is doing is selecting all rasters within the 'env.workspace' directory.  Then it is subtracting the 'C:\data\raster\TIFF\example1.tif' raster from each one, and finally saving the new raster to the 'C:\temp\python\Rasters' directory with '_minus.tif' appended to the original name.
0 Kudos
ChristopherStrother
New Contributor II
Thanks for your help. I wasn't very clear, I'm trying to subtract 724 pairs of rasters. I'm creating normalized Digital Surface Models (nDSMs) from 724 DSMs and 724 DEMs.

Chris
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Chris,

How do you determine which raster needs to be subtracted from which?  Do the rasters have identical names in each directory?
0 Kudos
ChristopherStrother
New Contributor II
Jake,

Yes, for example, one is called 123.tif and the other is 123.img.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
If you have the same amount of rasters in each directory you can do this with the below example:

import arcpy, os
from arcpy import env
arcpy.CheckOutExtension("Spatial")

env.workspace = r"C:\temp\python\Rasters"

list1 = []

lstRasters = arcpy.ListRasters("*")
for raster in lstRasters:
    list1.append(env.workspace + os.sep + raster)

env.workspace = r"C:\temp\python\Rasters2"

list2 = []

lstRasters = arcpy.ListRasters("*")
for raster in lstRasters:
    list2.append(env.workspace + os.sep + raster)

count = len(list1)

x = 0
while x < count:
    raster = list1
    name = raster.split("\\")[-1]
    outMinus = arcpy.sa.Minus(list1, list2)
    outMinus.save(r"C:\temp\python\Output" + os.sep + name.split(".")[0] + "_minus.tif")
    print 'Successfully created raster'
    x += 1

del list1, list2, raster


The script will append each raster to a list, and then subtract each raster from another (ie 123.tif - 123.img).
0 Kudos
MihaKlemencic
New Contributor III

This is kind of similar approach that I am looking for. I have many pairs (2000+) of orthophotos from years 2011 and 2014. They are stored in 2 separate folders. I would also like to find differences between them, but first of all I need to reclassify only one band from orthophoto; orthophotos are composited of red, green and blue band. I need to reclassify only blue band in 2 classes, then subtract them and export result as polygon.

Orthophotos that overlapping each other have same name, except the 8th character is different.

I am attaching picture of model builder process for one set of orthophoto. Orthophotos with name E063462.tif and E063462A.tif are overlapping each other.

  1. I need to import/load only blue band from orthophotos that have first 7 characters the same.
  2. Then blue bands should be reclassified into 2 classes (threshold values is 130).
  3. The reclassified raster should be subtracted (newer minus older).
  4. The result should be exported as polygons.

I am not familiar with python so I need help to write a code to automate this process with 2000+ orthophoto pairs.

0 Kudos
ChristopherStrother
New Contributor II
Jake,

Thanks for all of your time helping me with this. This will enable me to get over a "hump" that I've been stuck on for two weeks.

Chris
0 Kudos
XanderBakker
Esri Esteemed Contributor

Just a small question... do you need them as separated files or is merging them into a single raster an option before you apply the minus tool?

0 Kudos