[postgis-tickets] [PostGIS] #5148: ST_Clip ERROR: rt_raster_from_two_rasters: The two rasters provided do not have the same alignment
PostGIS
trac at osgeo.org
Thu May 5 06:20:18 PDT 2022
#5148: ST_Clip ERROR: rt_raster_from_two_rasters: The two rasters provided do not
have the same alignment
----------------------+---------------------------
Reporter: sergeish | Owner: robe
Type: defect | Status: new
Priority: medium | Milestone: PostGIS 3.3.0
Component: raster | Version: master
Keywords: |
----------------------+---------------------------
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
[https://github.com/postgis/postgis/blob/9c4a85d2aadacb008489630f14168958b770a065/raster/rt_core/rt_raster.c#L3075
rt_raster.c:3075]
Later in `RASTER_clip -> rt_raster_from_two_rasters ->
rt_raster_same_alignment -> rt_raster_geopoint_to_cell`
[https://github.com/postgis/postgis/blob/9c4a85d2aadacb008489630f14168958b770a065/raster/rt_core/rt_spatial_relationship.c#L91
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)
[https://github.com/postgis/postgis/blob/9c4a85d2aadacb008489630f14168958b770a065/raster/rt_core/rt_raster.c#L831
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`
[https://github.com/postgis/postgis/blob/9c4a85d2aadacb008489630f14168958b770a065/raster/rt_core/rt_raster.c#L3075
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.
--
Ticket URL: <https://trac.osgeo.org/postgis/ticket/5148>
PostGIS <http://trac.osgeo.org/postgis/>
The PostGIS Trac is used for bug, enhancement & task tracking, a user and developer wiki, and a view into the subversion code repository of PostGIS project.
More information about the postgis-tickets
mailing list