# How does ST_SnapToGrid work using nearest neighbor interpolation?

I have rasters in a PostGIS database that are not necessarily aligned (some rasters offset from the grid). I want to useST_SnapToGridto 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.

## Assessment and visualization of spatial interpolation of soil pH values in farmland

Site-specific farming entails fine-scale detection of field parameters that affect yield coupled with directing appropriate management inputs to select areas that improve field-scale cropping system profitability. Currently, limited technologies are available to evaluate spatial variability in soil properties on a fine scale (submeter resolution). Therefore, information is typically generated by collecting discrete samples and utilizing spatial interpolation to estimate data for the unsampled locations. In this study, soil pH samples were collected from a 12.15 ha agricultural field in northwest Missouri using two grid-sampling regimes: 0.11 ha with 110 samples and 0.98 ha with 12 samples. Three spatial interpolation methods (inverse distance weighted, spline and kriging) were tested to evaluate the effects of interpolation on unsampled locations. In addition to quantitative validation evaluations, results were also assessed by 2D visualization and 3D visualization. Although each assessment approach provided useful information, the inverse distance weighted technique overall better-estimated soil pH values as determined by a combination of all three approaches.

This is a preview of subscription content, access via your institution.