Count points in polygon: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(New FAQ)
 
mNo edit summary
Line 25: Line 25:
   g.copy vect=fields,myfields
   g.copy vect=fields,myfields
   v.db.addcol myfields col="numpoints integer"
   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);"
   v.db.update myfields column=numpoints value="(SELECT count(owner) FROM myarchsites WHERE \
  myarchsites.owner=myfields.label GROUP BY owner);"
   # verification:
   # verification:
   v.db.select myfields
   v.db.select myfields


The resulting polygon table contains the number of points falling into each polygon.
The resulting polygon table contains the number of points falling into each polygon.
[[Category:FAQ]]

Revision as of 06:33, 27 January 2009

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.