2

I would like to iterate a list of raster objects and apply Con() to each raster. The rasters are in a specific order in the list. That's important. All I want to do is to write the value of the current raster in the cell if there is no value in the base_raster cell already! Only 1 raster will ever write to a cell. The first raster (following order in the list) that has a value > 0 can write to the cell, and that cell must not be overwritten by the following raster.

base_raster = list_raster_obj[0]
    for raster in list_raster_obj[1:]:
        #if base_raster has already a value in that cell, do nothing       
        raster_out = Con(base_raster < 1, raster, base_raster)
        base_raster = raster_out        
        
    raster_out.save(path)

This kind of works for 3,4 rasters but takes ages if you get a list with like 10 rasters. If you know the amount of raster you want to compare and "hardcode" the Con() commands, it is blazing fast - like in ArcGIS Pro. It is only extremely slow when you use arcpy with a list.

What am I doing wrong or missing here?

Edit: I need also to have an attribute table in the final raster.

10
  • 1
    When you say "no value in the base_raster" are you saying this is a nodata value in the cell? Subsequent rasters in your list could have no data too, this would be the scenario when raster 2 "passes" through raster 1 and into your base_raster?
    – Hornbydd
    Apr 2 at 0:27
  • Your condition has nothing to handle Nodata cell values
    – FelixIP
    Apr 2 at 1:04
  • 3
    If you are really after replacing empty cells, mosaic to new raster (first) will do.
    – FelixIP
    Apr 2 at 1:20
  • I am not quite sure about NoData but it doesn't seem to be an issue. The area surrounding my "area of interest" seems to be NoData. The "area of my interest" has 0 in the cells and I would like to overwrite those if any raster in the list has >0 value to offer. My code kinda works, but it seems to scale extremely poor (not even linear but exponentially) when you have more than few raster files in the list. The same raster files used in modelbuilder with Con() and hardcoded names works extremely fast. I glanced at arcpy.MosaicToNewRaster and the area seems to be ok, but used cell have value=255
    – Gero
    Apr 2 at 6:34
  • I didn't mention it, but I also need to have/keep an attribute table. The input raster have also one. The output of "arcpy.MosaicToNewRaster" somehow just have 255 as value to overwritten cells and has no attribute table.
    – Gero
    Apr 2 at 6:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.