Opened 8 weeks ago

Closed 6 weeks ago

Last modified 6 weeks ago

#5148 closed defect (fixed)

ST_Clip ERROR: rt_raster_from_two_rasters: The two rasters provided do not have the same alignment

Reported by: sergeish Owned by: robe
Priority: medium Milestone: PostGIS 3.3.0
Component: raster Version: master
Keywords: Cc:

Description

Called from RASTER_clip (ST_Clip), rt_raster_gdal_rasterize can produce mask raster that is not aligned with input raster.

In our case, the input geometry's (h3 index polygon) max. Y coordinate just happens to be within FLT_EPSILON distance from input raster Y coordinate. This causes alignment step in rt_raster_gdal_rasterize to be skipped rt_raster.c:3075

Later in RASTER_clip -> rt_raster_from_two_rasters -> rt_raster_same_alignment -> rt_raster_geopoint_to_cell rt_rpatial_relationship.c:91 the difference get magnified by 1/scale (scale = 8.3333333333338033e-05) and position of upper left corner relative to raster becomes (0, -1) rt_raster.c:831 so alignment check correctly fails.

# SELECT ST_Clip(
  ST_AsRaster(
    ST_GeomFromText('POLYGON((-83.50933333333333 29.978666666666665,-83.488 29.978666666666665,-83.488 29.95733333333333,-83.50933333333333 29.95733333333333,-83.50933333333333 29.978666666666665))'),
256, 256),
  ST_GeomFromText('POLYGON((-83.50907828753448 29.968993886572754,-83.50444815780213 29.971082725355338,-83.50415647331064 29.97591893624371,-83.50849527658693 29.978666590716937,-83.51312588106222 29.976577808850404,-83.51341720749126 29.97174131559702,-83.50907828753448 29.968993886572754))'));
ERROR:  rt_raster_from_two_rasters: The two rasters provided do not have the same alignment
CONTEXT:  PL/pgSQL function st_clip(raster,integer[],geometry,double precision[],boolean) line 8 at RETURN
SQL function "st_clip" statement 1

Possible solutions for fixing alignment check in rt_raster_gdal_rasterize rt_raster.c:3075:

  • take scale into account when checking if raster is already aligned, like (FLT_EQ(fabs(grid_yw - extent.UpperLestY) / scale, 0.0) for scale < 1.0);
  • replace FLT_EQ with DBL_EQ.

Is there a reason for using FLT_EQ for comparing doubles throughout raster code? This issue will be resolved as a special case if DBL_EQ is used instead.

Attachments (1)

clip.png (15.8 KB ) - added by sergeish 8 weeks ago.

Download all attachments as: .zip

Change History (5)

by sergeish, 8 weeks ago

Attachment: clip.png added

comment:1 by sergeish, 8 weeks ago

comment:2 by robe, 7 weeks ago

As I recall there was an issue a long time ago like pre GDAL 2.0 with using double precision compare in postgis raster. I forget what it was. I suspect it's not relevant anymore, so I'd go ahead and flip to DBL_EQ if it fixes this.

comment:3 by Darafei Praliaskouski <me@…>, 6 weeks ago

Resolution: fixed
Status: newclosed

In 368aeec/git:

NEWS entry

Closes #5148

comment:4 by sergei sh <sshoulbakov@…>, 6 weeks ago

In 7bff735/git:

raster alignment check fix, #5148

Note: See TracTickets for help on using tickets.