<div dir="ltr"><div>Hello,</div><div><br></div><div>I have a question regarding the use of joins when clipping multiple raster datasets in a single query. I have two raster datasets with the same spatial resolution in separate tables. I perform the clips on them separately in their own CTEs and then in the last step I would like to do to use the map algebra to multiply the rasters together. What I am finding is that the LEFT join is giving the correct answer and the INNER join is not. I find this strange as both sets should be equal.</div><div><br></div><div>Are these assumptions correct?</div><div>The clip function should return the same number of tiles, with the same spatial extents if the same boundary and raster datasets (spatial extent and resolution) are used? To perform a join on raster tiles one needs to determine if they have the same spatial extent. Currently I am using the id fields and the ST_Within(a,b) and ST_Within(b,a) functions. (<a href="http://postgis.net/docs/manual-1.4/ST_Equals.html">http://postgis.net/docs/manual-1.4/ST_Equals.html</a>)</div><div><br></div><div>Consider the toy queries below.</div><div>With r1 as</div><div>(</div><div>SELECT <a href="http://p.id">p.id</a>, <a href="http://p.name">p.name</a>, ST_Clip(r.rast, p.geom) as rast</div><div>FROM polygon p inner join raster1 r on ST_Intersects(r.rast, p.geom)</div><div>), </div><div>r2 as</div><div>(</div><div>SELECT <a href="http://p.id">p.id</a>, <a href="http://p.name">p.name</a>, ST_Clip(r.rast, p.geom) as rast</div><div>FROM polygon p inner join raster2 r on ST_Intersects(r.rast, p.geom)</div><div>),</div><div>Select <a href="http://r1.id">r1.id</a>, <a href="http://r1.name">r1.name</a>, (ST_SummaryStatsAgg(ST_MapAlgebra(r1.rast, 1, r2.rast, 1, '[rast1]*[rast2]', '32BF'),1, True)).*</div><div>FROM r1 left join r2 on <a href="http://r1.id">r1.id</a> = <a href="http://r2.id">r2.id</a> and ST_Within(r1.rast, r2.rast) and ST_Within(r2.rast, r1.rast)</div><div><br></div><div><br></div><div>With r1 as</div><div>(</div><div>SELECT <a href="http://p.id">p.id</a>, <a href="http://p.name">p.name</a>, ST_Clip(r.rast, p.geom) as rast</div><div>FROM polygon p inner join raster1 r on ST_Intersects(r.rast, p.geom)</div><div>), </div><div>r2 as</div><div>(</div><div>SELECT <a href="http://p.id">p.id</a>, <a href="http://p.name">p.name</a>, ST_Clip(r.rast, p.geom) as rast</div><div>FROM polygon p inner join raster2 r on ST_Intersects(r.rast, p.geom)</div><div>),</div><div>Select <a href="http://r1.id">r1.id</a>, <a href="http://r1.name">r1.name</a>, (ST_SummaryStatsAgg(ST_MapAlgebra(r1.rast, 1, r2.rast, 1, '[rast1]*[rast2]', '32BF'),1, True)).*</div><div>FROM r1 inner join r2 on <a href="http://r1.id">r1.id</a> = <a href="http://r2.id">r2.id</a> and ST_Within(r1.rast, r2.rast) and ST_Within(r2.rast, r1.rast)</div></div>