Returning names of rasters with maximum value

1061
11
Jump to solution
07-30-2013 11:07 AM
JeremyProville
New Contributor
Hi,

I have a pile of 23 different rasters, on which I'm performing various calculations. One of them is a simple Maximization, using Cell Statistics, and one thing I'd like to see is a raster showing me which of the 23 layers contained the maximum value that was chosen. I'm finding this is pretty hard to do.

In Arc 9.3, I solved this problem using a long string of Con() and Max() statements in the raster calculator, to assign an integer index value to each raster that was selected in the maximization. This is no longer possible in Arc 10 since Max() has been removed. I tried an approach in Python using the code below, but it does not really work. What I'm looking for is a way to return a raster with either index values from 1-23, or ideally the names of each input raster. Each name would be assigned to the cell if it was chosen as a maximum value.

Hopefully this makes sense, I can elaborate if not. Any ideas here would be hugely helpful. Here's some sample code I've been playing with, but there has to be a simpler, more elegant solution - perhaps an obvious tool I'm missing?

Thanks!

opt2 = Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "blank",-9999, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "CottonLB" ,1, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "FodderLB" ,2, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "FruitsLB" ,3, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "GroundnutLB" ,4, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "JatrophaLB" ,5, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "MaizeLB" ,6, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "MilletLB" ,7, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "MiscanthusLB" ,8, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "NarcostimulantsLB" ,9, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "OilpalmLB" ,10, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "PulsesLB" ,11, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "RapeseedLB" ,12, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "RiceLB" ,13, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "RootsLB" ,14, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "SorghumLB" ,15, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "SoybeanLB" ,16, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "SugarbeetLB" ,17, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "SugarcaneLB" ,18, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "SunflowerLB" ,19, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "SwitchgrassLB" ,20, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "TubersLB" ,21, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "VegetablesLB" ,22, Con(CellStatistics(["CottonLB","FodderLB","FruitsLB","GroundnutLB","JatrophaLB","MaizeLB","MilletLB","MiscanthusLB","NarcostimulantsLB","OilpalmLB","PulsesLB","RapeseedLB","RiceLB","RootsLB","SorghumLB","SoybeanLB","SugarbeetLB","SugarcaneLB","SunflowerLB","SwitchgrassLB","TubersLB","VegetablesLB","WheatLB","blank"],"MAXIMUM","DATA") == "WheatLB" ,23,-9999))))))))))))))))))))))))
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
Use the HighestPosition function/tool. Used to be called UPos in earlier versions.

View solution in original post

0 Kudos
11 Replies
Luke_Pinner
MVP Regular Contributor
Use the HighestPosition function/tool. Used to be called UPos in earlier versions.
0 Kudos
JeremyProville
New Contributor
Use the HighestPosition function/tool. Used to be called UPos in earlier versions.


Thanks lpinner, this was spot-on! In theory it is doing what I need, however in practice it's not working out exactly as planned. I have a follow up question I'm hoping I can get some help with:

Running the tool with three or more input datasets seems to include only results that are an intersection of all the inputs. All other non-intersecting areas are NoData. What I would like is to have a dataset represented in the results, even if it is the only one with a value in a specific cell.
I tried get around this by adding in a constant value as an input, or a blank raster with constant values everywhere, such that there would be a 'dummy' for comparison for every cell. This didn't seem to work and I'd love to hear about any ideas or further insight into how this tool works.

Much appreciated!
0 Kudos
TOMKONG
Occasional Contributor II
Thanks lpinner, this was spot-on! In theory it is doing what I need, however in practice it's not working out exactly as planned. I have a follow up question I'm hoping I can get some help with:
...
Much appreciated!


Did you check that HighestPosition (or Upos) function works well or not?
I never use this function, but it seems the illustration from ESRI book is not correct - the return is not always the cell with Max value. ig: the downside left conner cell values from input raster datasets are 4, 3, 1, but the return value for this cell is 1.
But the illustration for Max (9 version) function is correct - this cell return value is 4.


If the HighestPosition works and your source rasters does't include the 0 value, you can asign the NoData to 0 first;
After you get the HighestPosition output, you can asign the 0 value back to NoData (those 0 value celles indicates all input raster datasets are the 0 value in those locations. You don't need to have any value for those celles) .
0 Kudos
Luke_Pinner
MVP Regular Contributor
Running the tool with three or more input datasets seems to include only results that are an intersection of all the inputs. All other non-intersecting areas are NoData. What I would like is to have a dataset represented in the results, even if it is the only one with a value in a specific cell.


It's working as expected.  The default processing extent is the intersection of input extents, change this to union - see the help for more info.  However, any cell with NoData will be NoData in the output.  So change it to a low number first.

i.e. in the raster calculator:

HighestPosition(Con(IsNull(raster1), -999, raster1), Con(IsNull(raster2), -999, raster2), Con(IsNull(raster3), -999, raster3))



Did you check that HighestPosition (or Upos) function works well or not?
I never use this function, but it seems the illustration from ESRI book is not correct - the return is not always the cell with Max value. ig: the downside left conner cell values from input raster datasets are 4, 3, 1, but the return value for this cell is 1.
But the illustration for Max (9 version) function is correct - this cell return value is 4.


No, the return value of 1 is correct. HighestPosition/Upos returns the position of the raster with the highest value in the list, not the maximum value itself.

For your example of 4, 3, 1, 4 is the max and it is at position 1.  For something like 9, 12, 7, the HighestPosition/Upos value would be 2 as the raster with the highest value is at the 2nd position in the list.
0 Kudos
TOMKONG
Occasional Contributor II
It's working as expected.  The default processing extent is the intersection of input extents, change this to union - see the help for more info.  However, any cell with NoData will be NoData in the output.  So change it to a low number first.

i.e. in the raster calculator:

HighestPosition(Con(IsNull(raster1), -999, raster1), Con(IsNull(raster2), -999, raster2), Con(IsNull(raster3), -999, raster3))


No, the return value of 1 is correct. HighestPosition/Upos returns the position of the raster with the highest value in the list, not the maximum value itself.

For your example of 4, 3, 1, 4 is the max and it is at position 1.  For something like 9, 12, 7, the HighestPosition/Upos value would be 2 as the raster with the highest value is at the 2nd position in the list.


Thanks for your explanation about HighestPosition output.

If the output of the HighestPosition is the number of raster dataset, this function is not suitable for Jproville's purpose, its return is still to be replaced by the ralated dataset cell original value, and to eliminate the "NoData " output value.

The Max function is fit this application, but why ESRI removed this tool in the 10 version?
0 Kudos
Luke_Pinner
MVP Regular Contributor
If the output of the HighestPosition is the number of raster dataset, this function is not suitable for Jproville's purpose, its return is still to be replaced by the ralated dataset cell original value, and to eliminate the "NoData " output value.


I understood from jproville's question that he wanted to find out which raster had the max value:
...one thing I'd like to see is a raster showing me which of the 23 layers contained the maximum value that was chosen.


The Max function is fit this application, but why ESRI removed this tool in the 10 version?

The Max functionality is still there, it's just been moved into a new tool. ESRI changed a lot of things at version 10, names (i.e. UPos -> HighestPosition) and merged some tools (Max/Min/etc = CellStatistics, FocalMax/FocalMean/etc = FocalStatistics etc...).
0 Kudos
JeremyProville
New Contributor
Thanks to both for your responses.
TKONG, indeed lpinner's first answer was right, I was looking for the HighestPosition tool and not the max tool.

lpinner, appreciate your code suggestion - I tried a great deal of variations on it, but I continue to be baffled by the fact that I only keep getting the intersection of the inputs rather than the union. No matter what I seem to try (e.g. tinkering with the GP extent, con() isnull() and setnull() in raster calculator), I get the same result. I've also tried forcing -999 NoData values through Catalog.

In any case, thanks for all your help and I'll post back if I figure out the problem. Let me know if you have any further ideas.
0 Kudos
curtvprice
MVP Esteemed Contributor
I tried a great deal of variations on it, but I continue to be baffled by the fact that I only keep getting the intersection of the inputs rather than the union.


Are you saying the output extent is wrong (this is what the environment controls) or just the particular cells are only getting a value if all the inputs from the stack are data cells? If the latter, Luke's suggestion,

Con(IsNull(raster), -999, raster)


should work.
0 Kudos
JeremyProville
New Contributor
Are you saying the output extent is wrong (this is what the environment controls) or just the particular cells are only getting a value if all the inputs from the stack are data cells? If the latter, Luke's suggestion,

Con(IsNull(raster), -999, raster)


should work.



It is the latter - I've tried the code you mentioned and variations of it, but even by forcing NoData values into all input datasets, it still only gives me the intersection of all inputs, and does not consider all areas that were previously NoData. It is very strange.

The closest I've gotten to understanding this is by going in ArcCatalog and forcing in NoData values to -9999. But it still is erratic, I'll illustrate with an example:

Rasters A and B contain data. I force NoData values in these rasters to be -9999, either in Catalog or by using raster calculator with the code above. If I now use HighestPosition on these, it still only returns intersecting data in areas where values were anything but -9999... i.e. it seems to know where those NoData areas were, and omits them.
However, if I put in Raster A, and a constant dummy value of e.g. -1 as the inputs to HighestPosition, it works and includes all '-9999' areas in the output. Putting in Rasters A, B and the dummy constant makes it fail again, and the output ignores -9999 areas in it's calculation.

Any insight much appreciated.
0 Kudos