3.4.4. Operadores Conjunto

Considere os objetos geométricos apresentados na Figura 3.7.

Intersecção entre as geometrias A e B

Figura 3.7 - Exemplos de geometrias para uso nas operações de conjunto.

3.4.4.1. ST_Intersection

O operador ST_Intersection computa a geometria de intersecção entre duas geometrias.

Caso 1:

SELECT ST_AsText(
           ST_Intersection(
               ST_GeomFromText( 'POLYGON( (2 2, 2 4, 5 4, 5 2, 2 2) )' ),
               ST_GeomFromText( 'POLYGON( (4 1, 4 3, 7 3, 7 1, 4 1) )' )
           )
       );

A mesma consulta poderia ser construída da seguinte forma:

WITH
    red AS (
        SELECT ST_GeomFromText(
                   'POLYGON( (2 2, 2 4, 5 4, 5 2, 2 2) )') as geom
    ),
    blue AS (
        SELECT ST_GeomFromText(
                   'POLYGON( (4 1, 4 3, 7 3, 7 1, 4 1) )') as geom
    )
SELECT ST_Intersection((SELECT * FROM red), (SELECT * FROM blue));

Ou:

WITH
    my_geometries(red, blue) AS (
        values( ST_GeomFromText( 'POLYGON( (2 2, 2 4, 5 4, 5 2, 2 2) )' ),
                ST_GeomFromText( 'POLYGON( (4 1, 4 3, 7 3, 7 1, 4 1) )' ) )
    )
SELECT ST_Intersection(red, blue)
  FROM my_geometries;

Caso 2:

SELECT ST_AsText(
           ST_Intersection(
               ST_GeomFromText( 'POLYGON( (2 5, 2 7, 5 7, 5 5, 2 5) )' ),
               ST_GeomFromText( 'POLYGON( (5 5, 5 7, 8 7, 8 5, 5 5) )' )
           )
       );

Caso 3:

SELECT ST_AsText(
           ST_Intersection(
               ST_GeomFromText( 'POLYGON( (9 2, 9 4, 11 4, 11 2, 9 2) )' ),
               ST_GeomFromText( 'POLYGON( (12 1, 12 3, 15 3, 15 1, 12 1) )' )
           )
       );

3.4.4.2. ST_Union

O operador ST_Union computa a geometria formada pela união de outras duas geometrias.

Caso 1:

SELECT ST_AsText(
           ST_Union(
               ST_GeomFromText( 'POLYGON( (2 2, 2 4, 5 4, 5 2, 2 2) )' ),
               ST_GeomFromText( 'POLYGON( (4 1, 4 3, 7 3, 7 1, 4 1) )' )
           )
       );

Caso 2:

SELECT ST_AsText(
           ST_Union(
               ST_GeomFromText( 'POLYGON( (2 5, 2 7, 5 7, 5 5, 2 5) )' ),
               ST_GeomFromText( 'POLYGON( (5 5, 5 7, 8 7, 8 5, 5 5) )' )
           )
       );

Caso 3:

SELECT ST_AsText(
           ST_Union(
               ST_GeomFromText( 'POLYGON( (9 2, 9 4, 11 4, 11 2, 9 2) )' ),
               ST_GeomFromText( 'POLYGON( (12 1, 12 3, 15 3, 15 1, 12 1) )' )
           )
       );

3.4.4.2.1. ST_Difference

O operador ST_Difference computa a geometria formada pela diferença entre a geometria A e a geometria B.

Caso 1:

SELECT ST_AsText(
           ST_Difference(
               ST_GeomFromText( 'POLYGON( (2 2, 2 4, 5 4, 5 2, 2 2) )' ),
               ST_GeomFromText( 'POLYGON( (4 1, 4 3, 7 3, 7 1, 4 1) )' )
           )
       );

Caso 2:

SELECT ST_AsText(
           ST_Difference(
               ST_GeomFromText( 'POLYGON( (2 5, 2 7, 5 7, 5 5, 2 5) )' ),
               ST_GeomFromText( 'POLYGON( (5 5, 5 7, 8 7, 8 5, 5 5) )' )
           )
       );

Caso 3:

SELECT ST_AsText(
           ST_Difference(
               ST_GeomFromText( 'POLYGON( (9 2, 9 4, 11 4, 11 2, 9 2) )' ),
               ST_GeomFromText( 'POLYGON( (12 1, 12 3, 15 3, 15 1, 12 1) )' )
           )
       );

3.4.4.2.2. ST_SymDifference

O operador ST_SymDifference computa a geometria formada pela diferença simétrica entre duas geometrias.

Caso 1:

SELECT ST_AsText(
           ST_SymDifference(
               ST_GeomFromText( 'POLYGON( (2 2, 2 4, 5 4, 5 2, 2 2) )' ),
               ST_GeomFromText( 'POLYGON( (4 1, 4 3, 7 3, 7 1, 4 1) )' )
           )
       );

Caso 2:

SELECT ST_AsText(
           ST_SymDifference(
               ST_GeomFromText( 'POLYGON( (2 5, 2 7, 5 7, 5 5, 2 5) )' ),
               ST_GeomFromText( 'POLYGON( (5 5, 5 7, 8 7, 8 5, 5 5) )' )
           )
       );

Caso 3:

SELECT ST_AsText(
           ST_SymDifference(
               ST_GeomFromText( 'POLYGON( (9 2, 9 4, 11 4, 11 2, 9 2) )' ),
               ST_GeomFromText( 'POLYGON( (12 1, 12 3, 15 3, 15 1, 12 1) )' )
           )
       );