Count points in polygon

From GRASS-Wiki
Revision as of 06:33, 27 January 2009 by ⚠️Craigleat (talk | contribs)
Jump to navigation Jump to search

Q: How can I count the number of points which fall into polygons?

A: Carry over an attribute from the polygons to the points, then count the number of equal attributes by SQL grouping: Example: How many archaeological sites does each parcel owner have?

 # Spearfish area
 # (Note: DBF driver unsupported, please use SQLite, MySQL or PostgreSQL)
 g.copy vect=archsites,myarchsites
 v.db.addcol myarchsites col="owner varchar(25)"

We now transfer the owner names to the archaeological sites (points):

 v.what.vect myarchsites qvect=fields column=owner qcolumn=label
 # verification:
 v.db.select myarchsites

 d.mon x0
 d.vect -c fields
 d.vect myarchsites icon=basic/diamond size=10

 # test run
 echo "SELECT *,count(owner) FROM myarchsites GROUP BY owner" | db.select

 # upload to polygons map
 g.copy vect=fields,myfields
 v.db.addcol myfields col="numpoints integer"
 v.db.update myfields column=numpoints value="(SELECT count(owner) FROM myarchsites WHERE \
 myarchsites.owner=myfields.label GROUP BY owner);"
 # verification:
 v.db.select myfields

The resulting polygon table contains the number of points falling into each polygon.