3

Let's say that we have two tables with polygons representing areas. Table A and Table B. Geometries from table A represents both the valid and invalid areas. And the geometries in table B represents the invalid ones.

For example, lets say that the geometries from table A represent, harvesting fields, and table B represents roads, shacks and generally areas that the harvesting is impossible for w/e reason (as polygons). Table B represents some kind of mask that's has being digitized differently.

The problem arises that the mask, can pass through many fields. So I want to clip that mask (Table B), with the field polygon (Table A) to create a new set (Table C) consisting with the geometries that represents the masks 'per field' along with the name of the field it was cut from.

This can be done by

create tableC as (select st_intersection(a.geom,b.geom) geom from tableA as a, tableB as b);

But I have troubles on how to select the tableA.name as well and transferring it to tableC.

the selection part of an ideal querry would be like this:

create tableC as (select st_intersection(a.geom,g.geom) geom, tableA.name as name from .... );

A sample defintion of table C is the following:

Column     |   Type      | 
gid        | int         |
geom       | geometry    |  <- st_intersetion(a.geom,b.geom)
name       |varchar(255) |  <- name of the feature of table A which took part in the st_intersection 

Table A is defined as following:

Column     |   Type      | 
gid        | int         |
name       | varchar(255)|
geom       | geometry    |

And Table B as follows:

Column     |   Type      | 
gid        | int         |
geom       | geometry    |

And for a visual representation :

enter image description here

4
  • st_intersects returns true/false (vs st_intersection) and your image is confusing, since there is none between A and B. Are their gids really not integers? What have you tried so far? Commented Sep 30, 2013 at 20:44
  • My mistake! I wanted to write st_intersection. st_intersection returns a geometrycoletion. I've updated the question.
    – nickves
    Commented Sep 30, 2013 at 20:59
  • Woah, where did the squares came from? What does the circle represent? Commented Oct 1, 2013 at 6:04
  • Hmm it seems that the question is not clear enough! i'll try to explain it a bit a more.
    – nickves
    Commented Oct 1, 2013 at 7:29

1 Answer 1

4

I think you're looking for this:

CREATE TABLE tableC AS 
  (SELECT ST_Intersection(a.geom, b.geom) AS geom, a.name AS name 
   FROM tableA AS a, tableB AS b
   WHERE ST_Intersects(a.geom, b.geom));

By looking at each intersection separately, you can access all the included data.

2
  • It should be b.geom instead of g.geom in your query Commented Oct 1, 2013 at 8:47
  • duh yes, I need to refresh my SQLs.
    – nickves
    Commented Oct 1, 2013 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.