[mapserver-dev] count points in polygon

Stephen Woodbridge woodbri at swoodbridge.com
Mon Jan 19 23:24:09 EST 2009


Mathias Tobler wrote:
>   For a system showing dynamic species distributino maps I am looking 
> for a way to count the number of points in each polygon of a polygon 
> layer. I am using MapScript to dynamically add the points from data 
> stored in MySQL. The polygon layer is a regular shapefile that can 
> either be a regular grid or a shapefile of countries or regions. Due to 
> the larger application this will be part of I am limited to MySQL and 
> won't be able to use PostGIS or any other spatial database. I could 
> probably loop through all points and polygon in MapScript and use the 
> $status = $polygon->contains($point);  function, but considering that 
> there can be 100'000 or more points this can get really slow. Is there 
> any function that will do this?

I think I would recommend that you add a column to your points table and 
then cycle through each point, determine what polygon it is in and 
update the column with the polygon's uid. Then you can do a query like:

select polygon_uid, count(*) from points group by polygon_uid;

-Steve W


More information about the mapserver-dev mailing list