10

I have a raster with gaps in it. I want to fill these in with averages of surrounding cells using ArcGIS 10.2 for Desktop.

I have tried "Focal Statistics" but it averages EVERY cell and not just the gaps.

I have tried [Mosaic].IsNull.Con([Average], [Mosaic]) found here Patching but don't know how to implement it/it won't work for me

I have tried Map ALgebra but I think they were using versions before 10.2.

What I want. Make a fake raster with gaps filled in. Use the fake raster to fill in the gaps of the real raster without replacing the points with actual data.

10
  • Wrong syntax, use ([mosaic].isnull).Con([average],[mosaic])
    – FelixIP
    Commented Feb 19, 2015 at 21:22
  • 1
    Just observing that your 'Patching' link appears to be 15 years old (from 2000-2001).
    – phloem
    Commented Feb 19, 2015 at 21:25
  • 2
    A great deal has changed with rasters recently so a lot of older examples have the right intent but waaay wrong syntax to be used in the current version. @crmackey has the right translation though I'd use a circle and not a rectangle; for smaller dimensions there's little difference but for (relatively) large neighborhood areas there is significant difference between circle and rectangle. Commented Feb 19, 2015 at 21:43
  • 1
    Even if it is 15 yo, it is still very relevant. Huber knew and knows subject extremely well.
    – FelixIP
    Commented Feb 19, 2015 at 21:46
  • 1
    Exactly. The operation is the same only the syntax has changed. Commented Feb 19, 2015 at 21:47

4 Answers 4

10

This works:

filled = arcpy.sa.Con(arcpy.sa.IsNull(in_raster),arcpy.sa.FocalStatistics(in_raster,
                        arcpy.sa.NbrRectangle(w, h),'MEAN'), in_raster)

Where "w" and "h" are the search radius.

This only does focal stats on the NoData areas. I verified by erasing data from a DEM, and then finding the difference. You just have to make sure the search radius is adequate.

7
  • Do I just add all of this to raster calculator and change the "in_raster" and w and h? I for some reason can't get it to work, although the flow of it makes sense as it skips the manual creation of the focal raster and does it on the fly. Commented Feb 19, 2015 at 21:56
  • Got it to work! Thank you so much! was just adding "'s where I wasn;t supposed to. Thank you! Commented Feb 19, 2015 at 22:01
  • 1
    No problem. One disclaimer, as @MichaelMiles-Stimson pointed out, using NbrCircle() would be better.
    – crmackey
    Commented Feb 19, 2015 at 22:46
  • Are w h in map units of cells? Commented Jul 16, 2015 at 21:16
  • You would be better off using the arcpy.sa.NbrCircle(radius, units) where the units can either be 'MAP' or 'CELL'.
    – crmackey
    Commented Jul 16, 2015 at 21:26
4

You should use Raster Calculator and there is a useful and relevant line of code from ESRI's Support page HowTo: Remove and replace no data values within a raster using statistical information from the surrounding data values. From your question, it sounds like this would do exactly what you're trying to do, but let me know if I'm wrong. Here's my screenshot of Raster Calculator using a conditional statement to calculate an average value for cells with no data from surrounding cells: enter image description here

If you have hundreds of these to do, I recommend using the Raster Iterator in the Model Builder, which will feed all of your rasters into the Raster Calculator Tool, like this:

enter image description here

3

Use the focal statistics function, as you described to average all cells for the raster. Then, use the Con and IsNull functions in raster calculator to replace only the cells that are null.

Con(IsNull(<inputRaster>), <focalRaster>, <inputRaster>)

One possible drawback with this solution is that you may see some unwanted additional averaged cell values around the perimeter of your output raster

2
  • I can't upvote due to reputation or w/e, but this method works if I make a focal statistics raster first, and then apply the code above. I just wish I could do it all in one line like the comment above to save time. I have a couple hundred of these things... Commented Feb 19, 2015 at 21:44
  • It just depends on whether you want to keep your focal statistics raster or interpret on-the-fly... both methods are essentially the same (+1 to both). Commented Feb 19, 2015 at 21:46
2

I tried the Raster Calculator method LeonB used but ran into Out of Memory error, which is surprising given my system specs. Wrote a small stand-alone python script - modified from here - in IDLE that worked for me...

import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace="D:/Watershed/"

inRaster="D:/Watershed/tamtr14_1m"

arcpy.CheckOutExtension ("Spatial")

outCon=Con(IsNull(inRaster), FocalStatistics (inRaster, NbrRectangle (5,5, "CELL"), "MEAN"), inRaster)

outCon.save ("D:/Watershed/outCon")

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.