0
votes

I have imported two shape-files using QGIS into pgAdmin 4 (PostgreSQL), I use the PostGIS extension to be able to access spatial commands.

I want to check all the POIS (points) that are included in the Gemeinden (multipolygon) table. To accomplish that I use the spatial command "ST_Contains" ([postgis.net documentation regarding the command])1. Contrary to the fact that the points are actually in the polygons, the returned query is empty (see QGIS and output screenshot). What could be the issue? Any help would be appreciated. Thank you in advance!

Output - pgAdmin enter image description here

The command I use:

SELECT * FROM public."POIS" AS pois INNER JOIN public."Gemeinden" AS gem
ON (1 = 1)
WHERE ST_Contains(gem.geom, pois.geom) = true;

My tables:

  1. POIS table content

[POIS 3

  1. Gemeinden table content

[Gemeinden4

QGIS Screenshots:

  1. Both shape-files together:

[enter image description here5

  1. Only the POIS shape-file:

[enter image description here6

  1. Only the Gemeinden shape-file:

[enter image description here7

Update:

I have created a polygon table out of the multi-polygons using the command

CREATE TABLE polygon_table AS 
SELECT id, public."Gemeinden".kg_nr, public."Gemeinden".kg, (ST_DUMP(geom)).geom::geometry(Polygon,4326) AS geom FROM public."Gemeinden";

Afterwards I've updated the SRID of both the POIS table and the newly created one using :

SELECT UpdateGeometrySRID('polygon_table','geom',4326);

and

SELECT UpdateGeometrySRID('POIS','geom',4326);

Sadly,

SELECT pois.* FROM public."POIS" AS pois JOIN public."polygon_table" AS 
gem
ON ST_intersects(gem.geom, pois.geom);

still returns a empty query.

Any ideas? Thank you in advance!

2

2 Answers

3
votes

First of all make sure the SRID of the both table must be same. If it is not same then spatial queries wont work. Secondly convert the multipolygons to single polygons. Following link may help

PostGIS - convert multipolygon to single polygon

Finally the following query would be enough if you just want to get the POIS that intersects the polygons

SELECT pois.* FROM public."POIS" AS pois JOIN public."Gemeinden" AS gem
ON  ST_intersects(gem.geom, pois.geom);
0
votes

I have resolved the problem by going back to QGIS and saving both previously imported shape-files as the same SRID. Apparently, my code that converted the SRID in the database did not work.

Right click on the shape-file

Selecting the SRID

Note: I have done this for both shape-files, just to be sure.

Another factor that may have influenced the outcome was the selection of the "convert to single polygon" box while importing into the database from QGIS.

The box that I ticked for both shape-files while importing

PS: I use the German version of QGIS