I have two tables in postgis (pgadmin). In one table there are points and in the other table there are polygons(provinces). The tables are both over the same location (Netherlands). I want to count the points who are in every province and I want to export them to a row in the polygon (maybe a new table). Summarized: I want from every province the points counted in a row.

1 Answer 1


Assuming the points and polygons are in the same projection (same SRID):

Add a column to the provinces table to store point counts:

ALTER TABLE provinces ADD COLUMN count_pts int;

Update the column with point counts:

UPDATE provinces SET count_pts = 
    (SELECT gid, count(gid) AS ct FROM
        (SELECT pts.pts_id AS id, provinces.gid AS gid FROM pts,provinces 
        WHERE st_intersects(pts.geom,provinces.geom)) a 
    GROUP BY gid) b
WHERE provinces.gid = b.gid);

Note that if a point fell in multiple polygons (this could be the case if polygons overlapped one another) if would be counted in both polygons.

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.