I am trying to compute certain statistics from an elevation (DEM) raster column but at certain points the raster tiles are not aligned, and continue to not be aligned even though ST_SnapToGrid() or ST_Resample() are called.

    buffer (buf) as 
        (values (ST_Transform(ST_Buffer(
          ST_SetSRID(ST_Point(-66.0005555554938, 18.0005555553954),
               4269)::geography, 30)::geometry, 4269))),
    raster as 
                -66.0005555554938, 18.0005555553954))
             as rast
         from raster_table, buffer where ST_Intersects(rast, buf))
    rast From raster;

with a similar approach using ST_Resample and another CTE to use the first raster returned from the intersection as a sort of reference. I have tried different resample methods and different values for maxerr, include 0, 1.0, and 100, but none of these prevent the error SQL Error [XX000]: ERROR: rt_raster_from_two_rasters: The two rasters provided do not have the same alignment from being raised. Is there something I'm missing or a better way to do this?

  • 1
    The way you use ST_SnapToGrid() only align pixel corners. You should provide pixel sizes as well. What was your query looking like with ST_Resample()? What if you round -66.0005555554938, 18.0005555553954 to zero decimal? Sep 4 '19 at 21:16
  • BTW, it's been a while but thanks for your comment. Rounding helps sidestep the problem sometimes, but not always.
    – Evan
    Dec 11 '19 at 12:20
  • I think I'm experiencing similar, but I'm explicitly setting the x & y pixel scale: ST_SnapToGrid(clipped_rast, columnx_offset, rowy_offset, ST_ScaleX(clipped_rast), ST_ScaleY(clipped_rast), 'Bilinear', 0) May 25 at 15:53
  • 1
    Hi, thanks for commenting. I haven't updated this question (because I forgot about it) but I am pretty sure there's an underlying issue in PostGIS (that possibly also goes back to GDAL or another library), as evidenced by the fact that ST_SameAlignment(...) has inconsistent results. I've opened a bug report here, unfortunately there hasn't been much activity on it: trac.osgeo.org/postgis/ticket/4502
    – Evan
    May 26 at 13:10

Your Answer

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

Browse other questions tagged or ask your own question.