Friday, April 10, 2009

Subquery of subquery PostGIS SQL

Had to create some polygons that represented the dissolved and buffered outlines of a region, that were then clipped to an artificial extent. Came up with the following SQL, which still makes me scratch my head when I look at it.

SELECT ST_Intersection(c1.diss_buff_geom, m.the_geom) AS intersection_geom, 
        c1.name AS name
    FROM (SELECT geomunion(the_geom) AS diss_buff_geom, name
        FROM (SELECT name, ST_Buffer((ST_Dump(the_geom)).geom, .12) AS the_geom 
            FROM coastlines) c
        WHERE c.name LIKE 'India' GROUP BY c.name) c1, 
            global_land_mask_v5 m
        WHERE ST_Intersects(c1.diss_buff_geom, m.the_geom) 
            AND c1.name = 'India';

Honestly... still hurts to look at this now.

No comments: