Containing

In contrast to crossing and overlapping, containing means a geometry must be completely covered by the reference geometry, and no point may be located outside. To test this, two functions are provided: ST_Within and ST_Contains. The only difference is the order of arguments.

First, we will use the ST_Within function to determine how many water bodies are within the boundaries of Rybnik (which literally means fish pond in Czech, by the way):

SELECT name,wkb_geometry FROM multipolygons WHERE "natural"='water' AND ST_Within(wkb_geometry, 
(SELECT wkb_geometry FROM multipolygons WHERE name='Rybnik' AND admin_level='7'));
The result of an ST_Within query - a set of polygons fully contained within another polygon

Then we will deal with the second query - finding power towers in Rudnik using ST_Contains:

SELECT name,wkb_geometry FROM points WHERE other_tags LIKE '%power%' AND ST_Contains( 
(SELECT wkb_geometry FROM multipolygons WHERE name='gmina Rudnik' AND admin_level='7'),wkb_geometry);

Did you notice the difference? When using ST_Within, the geometries to be found are supplied as the first argument, and the boundary to search within as the second. For ST_Contains, the search boundary was first, and the geometries to be found, second. If we swapped the arguments, the query would return 0 rows: no polygon (at least, not a valid one) will fit into a point.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset