Mosaic To New Raster creating nodata cells for 255

2664
7
03-09-2017 09:55 AM
curtvprice
MVP Esteemed Contributor

I am running into a problem that I cannot reproduce with small datasets. 

I am merging a massive number of rasters with the "MAXIMUM" option - most of the inputs are NoData at each cell and one value in one of the input rasters. I am specifying: 

arcpy.MosaicToNewRaster_management(huge_list, outFolder, outName", "32_BIT_SIGNED", "", 1, "MAXIMUM")

If my input cell is of value 255, (max of Nodata (x N), 255)  I am getting NoData out.  Looks like a bit depth / NoData handling issue to me.

I'd sent it to support but I can't easily reproduce with test data so I thought I'd ask around here first.

I will probably use the Cell Statistics tool to work around the problem, as it seems to have fewer bit depth issues.

0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

two salient readings... the first about nodata in general and the second about nodata in mosaic specifically

curtvprice
MVP Esteemed Contributor

These are good links to share for sure, but the second does not apply I am doing tiled raster processing and running the Mosaic To New Raster tool (which has nothing to do with Mosaic datasets). I did go back and look and the my output to Mosaic To New Raster has a NoData value of 255, this is incorrect as the values of the inputs are 32-bit signed and include a lot of the range. Both my data values "inside the raster" and edge values (outside study area) are both set to 255. The NoData value should be -2147483648 (min long: -2^(31) for 32-bit signed rasters.

Perhaps a fix here is to set the environment NoData (arcpy.env.nodata) to  PROMOTION before running the tool. Mosaic to New Raster is supposed to honor that env setting, according to the doc. Maybe that could work. 

Since my post, I did re-run test Mosaic To New Raster run with the SAME INPUT from the ArcMap python window and my 255 values came through unscathed, NoData was the value it should be. (The previous run was done from a "raw" python window.) The only difference I can think of is environment, but in both environments, arcpy.env.nodata is "NONE". This is pretty strange.

I think Cell Statistics may be the easiest workaround to my issue.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Curtis, I assume that if you are using a tool in arctoolbox, that you set the environments in the Environments tab/button in each tool.  I have come to learn that global settings are 'nice' but not to be trusted and if using a tool, I check everytime to make sure that the 'honored parameters/settings' are in deed kept.  As for script(s) ... set them explicitly is the only way

PS  I would use numpy anyway where you have complete numeric control, but I digress as usual

AbdullahAnter
Regular Contributor II

Are all raster that you will create mosaic for are same properties? (coordinate system / number of bands /pixel type /pixel depth ..)

make sure that the new mosaic raster similar to original raster .

0 Kudos
curtvprice
MVP Esteemed Contributor

There no new raster, Mosaic To New Raster writes to a new dataset.

Another fix is to to create an empty raster as 32-bit signed and run the Mosaic tool instead, specifying NoData explicitly.

out_raster = os.path.join(wk, "grid32")
arcpy.CreateRasterDataset_management(wk, "grid32", "100", "32_BIT_SIGNED", spat_ref, 1)
arcpy.Mosaic_management(inputs, out_raster, "MAXIMUM", 
                        nodata_value=-2147483647,
                        background_value=-2147483647)
0 Kudos
curtvprice
MVP Esteemed Contributor

I tried this and it is painfully slow, as each raster is loaded one at a time. Mosaic To New Raster is much faster. So I killed ArcMap and tried Cell Statistics, which gives me an error, I think it's way too many inputs. So, I tried this. It seems to work, the new NoData value that comes out is maxint (2147483647) and I can live with that.

env.nodata = "PROMOTION"
mergefac = arcpy.MosaicToNewRaster_management(tfacs, wkf, "mergefac",
    pixel_type="32_BIT_SIGNED",
    number_of_bands=1,
    mosaic_method="MAXIMUM")
0 Kudos
curtvprice
MVP Esteemed Contributor

I ran this in my script last night and I still got NoData 255 out. Here's my new method, since I know CellStatistics can handle 100 rasters:

# split list in to chunks of 100
chunk = 100
tchunks = [tfacs[i:i + chunk] for i in xrange(0, len(tfacs), chunk)]
tsums = []
for tchunk in tchunks:
    tsum = CellStatistics(tchunk, "MAXIMUM", "DATA")
    tsums.append(tsum)
mergefac = CellStatistics(tsums, "MAXIMUM", "DATA")
mergefac.save("mergefac")
0 Kudos