Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Here's a quick summary about what I'm trying to do: Let's say I got two layers. One (A) with 10 polygons and one that is more granular with 100 polygons (B). Just to check if the behavior of PostGIS and ArcMap is the same, I would like to select all polygons in B that are under a polygon in A. Since the sum of all polygons of both layer got the same shape, all polygons of B should be selected. If I do that in ArcMap, everything works fine (100 of 100 polygons selected). If I use the following statement in PostGIS, some of the polygons are not in the resultset (about 10%):

SELECT layer_a.gid, layer_b.gid FROM layer_a, layer_b WHERE ST_Contains(layer_a.geom, layer_b.geom);

No I want to be more specific. Let's say I'd like to select all Polygons from B that are under a specific polygon from A with a polygon_id = 1. For this I use the following statement:

SELECT layer_a.gid, layer_b.gid FROM layer_a, layer_b WHERE ST_Contains(layer_a.geom, (SELECT layer_b.geom WHERE layer_by.polygon_id = '1');

But now, I get a empty resultset and I really don't know why. Could somebody explain me this behavior? Note: I just imported shape files without assigning a special SRID or something like this. I appreciate any help. :)

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Looks like ST_Contains is the correct function. Two things come to mind that could cause the results you are getting. First, invalid geometries can silently cause all kinds of trouble. Run a query similar to the following on both of your tables:

SELECT gid AS invalid_gid
  FROM layer_a
  WHERE NOT St_IsValid(geom);

Second issue could be that the polygons do not actually have the topological properties that you think they do. Replace St_Contains with St_Intersects and that should help to inform you. Let me know what that yields.

Your second query appears to have typos, but should look more like this:

SELECT layer_a.gid, layer_b.gid
  FROM layer_a, layer_b
  WHERE layer_b.polygon_id = 1
    AND St_Contains(layer_a.geom, layer_b.geom)
share|improve this answer
    
Hey Scro, I tried it with your statement and rechecked the results from ArcMap and now its looks good. Seems that I had it wrong. I thought ST_Contains would do this job for me, but now it know, that ST_Contains only selects Polygons that are completly in/under the other polygon. And ST_Intersects selects all polygons that intersects with the bigger one. Looks good now. Thank you very much! :] –  Chronnie Mar 14 '13 at 12:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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