I have rasters in a PostGIS database that are not necessarily aligned (some rasters offset from the grid). I want to use ST_SnapToGrid to align all rasters to the same grid but have some questions about how it works using nearest neighbors.

Which of these ways is how nearest neighbors works?

  1. If the pixels are aligned then it is just the value of the source pixel, but otherwise the target pixel takes on the value of the closest source pixel. This would be akin to "moving" the pixel into the correct place.

  2. The target pixel takes the average of the nearest neighbors, such that it might overlap 99% with source pixel A and 1% with source pixel B but take the averages of these pixels to be the value of the target pixel.

  3. Some other way?


Nearest neighbor is exactly what you describe by (1). (2) would be some form of interpolation, bilinear if you just multiply the color values by the percentage overlaps.

Docs are here: http://postgis.net/docs/RT_ST_SnapToGrid.html

Also consider ST_Resample: http://postgis.net/docs/RT_ST_Resample.html

I noticed that the docs don't really describe how the different resampling algorithms work, but they are all standard algorithms - look them up in wikipedia or wherever else.

  • Thank you. I haven't found the documentation to be very clear. For example from the documentation, "The default is NearestNeighbor which is the fastest but results in the worst interpolation." Notice that they even use the word interpolation! While all of the algorithms could be considered standard, they don't specify whether the use the closest neighbor (i.e. FIRST first order neighbor), ALL first order neighbors, kNN, etc. so it is unclear (to me) from the docs how PostGIS implements these algorithms – Ellis Valentiner Jun 16 '15 at 18:00
  • If you consider the names of the algorithms in terms of re-sampling algorithms, these terms are all very well-defined. Look at any image manipulation software (Gimp, Photoshop, etc) for more info and examples. – chodgson Jun 17 '15 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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