Thursday, 9 March 2017

arcgis desktop - Setting NoData for a multiband raster?


I have an RGB orthoimage that has black area instead of NoData, as pictured here: enter image description here



When I use the SetNull tool, it does remove the black area, however it then turns it into a grayscale orthoimage, which is not what I want.


enter image description here


I know that there is a way around this by using the Clip (Data Management) tool to clip the raster to itself and in there you can set the NoData value to be the value of the black cells (0). However, I am creating a workshop for students and I would like to use a less counterintuitive method.


There is of course the option of running the NoData tool on each band individually and then combining the three bands together, however I need to have a condition where band1, band2, and band3 all equal 0 to set that as NoData. Being able to do this in ModelBuilder would be ideal, but scripting it using ArcPy would also be good.


I am using ArcGIS 10.2 Desktop with Advanced License.



Answer



What you want to do is Set Raster Properties in a script or change it manually in ArcCatalog. This will not create a new raster or even take very long.


In python it's a bit tricky:


import sys, os, arcpy


InFolder = sys.argv[1]
arcpy.env.workspace = InFolder

for Ras in arcpy.ListRasters():
arcpy.AddMessage("Processing " + Ras)
arcpy.SetRasterProperties_management(Ras,nodata="1 0;2 0;3 0")

Because the nodata is way down the list I find it easier to specify that; the parameters are Band Value;Band Value;... until all the bands are addressed. If you are likely to have more bands (or less) in the same folder then you will have to use arcpy.Describe and the bandCount property to set the null for the correct number of bands:


import sys, os, arcpy


InFolder = sys.argv[1]
arcpy.env.workspace = InFolder

for Ras in arcpy.ListRasters():
arcpy.AddMessage("Processing " + Ras)
desc = arcpy.Describe(Ras)
if desc.bandCount == 3:
arcpy.SetRasterProperties_management(Ras,nodata="1 0;2 0;3 0")
elif desc.bandCount == 4:
arcpy.SetRasterProperties_management(Ras,nodata="1 0;2 0;3 0;4 0")

elif desc.bandCount == 1:
arcpy.SetRasterProperties_management(Ras,nodata="1 0")

In ArcCatalog, right click on the layer and go to properties: enter image description here


hit the edit button:


enter image description here


Enter the values then hit OK to dismiss the NoData editor and OK to enforce the change.


Now the raster will display nothing in ArcMap where the cell value is 0,0,0.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...