Select to view content in your preferred language

Change pixel values using the nearest neighbor value

5979
8
07-14-2016 07:18 AM
AlexandreAssunção
Deactivated User

Hello Everyone. I am having some trouble when I convert the raster properties because I am using the resample tool to change the resolution and the number of columns/rows to match with another raster, so I can combine them after the conversion. However, after using the tool, the raster doesn't match, which was expected because some values were created to change its resolution.

I came up with the following idea, I can Extract by Mask using the original file as mask and those values I don't want in my output I can try to change them getting the nearest neighbor value; however, I didn't find one tool that could do that for me. In addition, I tried Majority Filter, but it changes values that I don't want to change and also do not eliminate all unwanted values.


The grid below is just an example of my whole grid and what I am trying to do is substitute the -1 values with the nearest value except for the -9999 (NoData value)

 0.48 1.16 1.9 
0.28 1.02 1.53 
1.82 2.08 1.86 
0.16 2.42 2.14 
 2.41 2.43 2.1 
 2.45 2.44 2.43
6 2.55 2.45 2.4
2.59 2.59 2.56 
2.61 -1 -1 -9999
67 2.66 2.63 -1
2.79 2.79 2.79 
 2.85 2.83 2.82
0.88 2.84 -1 -1 

Any help is welcome and I have some knowledge with python too, if that would be that only option.

Thank you.

0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

I think Con has been mentioned to you before, which can be done in the raster calculator of with the Con tool.  Just query the raster for values equal to -1, assign those locations/values some other value, then keep the remaining values intact.

The exact syntax depends on which approach you are using, in pseudo code,

'raster' == -1, 'value if true', 'value if 'false'

'raster' == -1, -88, 'raster'

0 Kudos
AlexandreAssunção
Deactivated User

But as you can see, there are different values that -1 has to be switched with. The way you stated all -1 values will be replaced to the true statement and it's not what I want here.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I am not sure what you want for the nearest value

1  2  3

4 -1 -9999

7  8   9

  1. nearest value by distance? (ie .... 4)
  2. average value in the NSEW axis exclusive of -9999? (not implemented in Arc*)
  3. average value in the 8 neighbours exclusive of -9999 (implemented in Arc
  4. weighted average by value and distance? (not implemented in Arc*
  5. average in the row progressive direction?  (coincidently gives the same value as distance)
  6. average in the column progressive direction (ie filter down columns, answer 2)

3 can be don't by producing a mean raster, then using its value in the Con statement

'raster' == -1, 'mean_raster', raster in pseudo code

1, 2, 5 and 6 can be done with numpy

0 Kudos
AlexandreAssunção
Deactivated User

Yes, I would say the number one. Or if it is possible do the same thing as the Majority Filter Tool does but just for the -1 values.

0 Kudos
DanPatterson_Retired
MVP Emeritus

use resampling with nearest neighbor.... there should be no new values introduced to the raster

if there are, stop

  1. start with resampled raster,  call this raster0
  2. query the resampled raster ( using con),  so that -1 is assigned nodata.... call this raster1
  3. run the majority filter on raster1 (no data and the former -1 will be ignored in the calculations... call this 'filtered'
  4. use a Con statement to replace the -1 values in raster0 with those in 'filtered'

you should be done.

As an alternative, arrays can be used to simplify this if you have too many classes to deal with in a simple con statement or multiple nested boolean or's and/or and's

0 Kudos
DanPatterson_Retired
MVP Emeritus

ps for resampling, use nearest neighbour or majority ... if you don't want your values to change, then you wouldn't have this problem Resample—Help | ArcGIS for Desktop

and if it is a float raster you are trying to resample, scale up by a factor of 10, take the integer of that, do the NN or majority resample, convert back to float

0 Kudos
AlexandreAssunção
Deactivated User

I used Resample Tool with nearest neighboor. The problem is I am trying to create new rows and columns to match the other raster. I have a 516 x 510 raster and I am trying to convert it to 1024x1024. Some values will be created and I have no control about it.

0 Kudos
AbdullahAnter
Honored Contributor

Is your raster is DEM?

0 Kudos