<div dir="ltr"><div><div><div><div><div><div>Hi Andy,<br><br></div>Thanks for your help, unfortunately, you solution doesn't seem to work.<br></div></div>From my point of view, it's normal perfectly that the intersection return null, this is why I've used a "LEFT OUTER JOIN" to return the objects from A that do not intersect B.<br></div>By using the  "LEFT OUTER JOIN" I should get the objects from A that does not share any any space with B.<br></div>Am I wrong ?<br><br></div>Can I perform in a single SQL query the same QGIS Geoprocessing Difference operation ? <br><div><div><div><br></div><div>Thanks<br><br>Arnaud<br></div><div><div><br><br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 29, 2014 at 7:06 PM, Andy Colson <span dir="ltr"><<a href="mailto:andy@squeakycode.net" target="_blank">andy@squeakycode.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 12/29/2014 8:46 AM, Arnaud Vandecasteele wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey all,<br>
<br>
It's been a couple of days that I'm trying to compute the difference<br>
(like the geoprocessing difference with QGIS) between two geometry<br>
(polygon) tables. These tables (let's call them A and B) contain<br>
polygons where :<br>
  - polygon from A can be intersected by one or more polygon from B<br>
  - polygon from A can have no spatial relationship with polygon from B<br>
(no intersection)<br>
<br>
To realize this difference I've joined the two tables with a "LEFT OUTER<br>
JOIN" so even if I don't have any intersection between A and B I should<br>
get the geometry from A.<br>
<br>
Below it's an example of the SQL query :<br>
******************************<u></u>***<br>
SELECT  A.id,<br>
         COALESCE(<br>
             ST_Difference(<br>
                 A.the_geom,<br>
                 ST_Union(B.the_geom)<br>
             ),<br>
             A.the_geom<br>
     ) As the_geom<br>
FROM A<br>
LEFT JOIN B ON A.the_geom && B.the_geom<br>
AND ST_Intersects(A.the_geom, B.the_geom)<br>
GROUP BY A.id;<br>
******************************<u></u>***<br>
<br>
The difference between objects from table A that are intersected by one<br>
or more objects from B is correct. But I don't get the objects from A<br>
that are not intersected by B. I don't understand why as I use a  "LEFT<br>
OUTER JOIN" and the COALESCE function.<br>
<br>
Could you please tell me what I'm doing wrong or what I don't understand ?<br>
<br>
Thanks for your help.<br>
<br>
Arnaud<br>
<br>
<br>
</blockquote>
<br></div></div>
I'm assuming ST_Intersects will return null if B.the_geom is null.  Try:<span class=""><br>
<br>
LEFT JOIN B ON A.the_geom && B.the_geom<br></span>
AND (B.the_geom is null or ST_Intersects(A.the_geom, B.the_geom))<br>
<br>
-Andy<br>
<br>
<br>
<br>
______________________________<u></u>_________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-<u></u>bin/mailman/listinfo/postgis-<u></u>users</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr">--------------------------------------------------------------------<br>Arnaud Vandecasteele<br>SIG - WebMapping - Spatial Ontology - GeoCollaboration<br><br>Web Site<br><a href="http://geotribu.net/" target="_blank">http://geotribu.net/</a><br><a href="http://about.me/arnaud_vandecasteele" target="_blank">http://about.me/arnaud_vandecasteele</a><br><br></div></div>
</div>