Postgres 9.3.5, PostGIS 2.1.4.
I have tow tables (polygons
and points
) in a database.
I want to find out how many points
are in each polygon
. There will be 0 points per polygon or more than 200000. The little hick up is the following.
My point
table looks the following:
x y lan
10 11 en
10 11 fr
10 11 en
10 11 es
10 11 en
- #just for demonstration/clarification purposes
13 14 fr
13 14 fr
13 14 es
-
15 16 ar
15 16 ar
15 16 ps
I do not simply want to count the number of points per polygon. I want to know what is the most often occuring lan
in each polygon. So, assuming each -
indicates that the points are falling into a new polygon, my results would look the following:
Polygon
table:
polygon Count lan
1 3 en
2 2 fr
3 2 ar
This is what I got so far.
SELECT count(*), count.language AS language, hexagons.gid AS hexagonsWhere
FROM hexagonslan AS hexagons,
points_count AS france
WHERE ST_Within(count.geom, hexagons.geom)
GROUP BY language, hexagonsWhere
ORDER BY hexagons DESC;
It gives me the following:
Polygon Count language
1 3 en
1 1 fr
1 1 es
2 2 fr
2 1 es
3 2 ar
3 1 ps
Two things remain unclear.
- How to get only the max value?
- How will cases be treated where there are by any chance the max values identical?