I have 2 tables in postgis - one of type polygons and one of type points.

I want the color of the polygons to be related to the number of points within each polygon, say white when no points are inside a polygon, pink when 'some' points are inside and red when 'a lot' of points are inside (a full range of colors based on percentage of points/polygon is what I'm really after).

I am using mapserver, and in the expression section of CLASS it doesn't mention any way of taking information from another table. Only attributes from within the same table used for the DATA tag.

2 Answers 2


You'll want to do a spatial join in the DATA statemet

DATA "geom from (SELECT poly.gid, poly.geom, Count(gid) AS ptcount 
                 FROM poly JOIN pts ON ST_Intersects(poly.geom, pts.com)
                 GROUP BY poly.gid, poly.geom) using unique gid"

If your data aren't changing very often, doing this for every map draw is probably not a good performance idea, and you might be better served by creating a materialized view of the result, mapping from that, and updating the view regularly.

  • hi! thanks allot! i just found the same thing after struggling with it :) ill delete my answer as yours is more elaborated. again - thanks!
    – dowi
    Jun 22, 2014 at 15:09
  • well there is a tiny difference: you wrote st_intersects and in the post i attached he used st_contains. is there a difference?
    – dowi
    Jun 22, 2014 at 15:14
  • btw, your lectures rock :-)
    – dowi
    Jun 22, 2014 at 15:15
  • For point-in-polygon intersects and contains return identical results. Thanks! Jun 22, 2014 at 15:17

ok i have a solution, but if someone has a better one i'd like to hear!

i need to add the count information at the data stage via JOIN as said in this post

then i can use regular classes and styles based on that attribute

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.