1 /*
2  * Copyright (C) 1994-1995. James Darrell McCauley. (darrell@mccauley-usa.com)
3  *                                http://mccauley-usa.com/
4  *
5  * This program is free software under the GPL (>=v2)
6  * Read the file GPL.TXT coming with GRASS for details.
7  */
8 
9 #include <stdio.h>
10 #include <math.h>
11 #include <stdlib.h>
12 #include <grass/gis.h>
13 #include <grass/vector.h>
14 #include "quaddefs.h"
15 
count_sites(COOR * quads,int nquads,int * counts,double radius,struct Map_info * Map,int field)16 void count_sites(COOR * quads, int nquads, int *counts, double radius,
17 		 struct Map_info *Map, int field)
18 /*
19  * counts the number of sites in the Map that fall within nquads quads of a certain radius
20  */
21 {
22 
23     int i, line, nlines;
24     struct line_pnts *Points;
25     struct line_cats *Cats;
26 
27     Points = Vect_new_line_struct();
28     Cats   = Vect_new_cats_struct();
29 
30     nlines = Vect_get_num_lines(Map);
31 
32     for (line = 1; line <= nlines; line++) {
33 	int type;
34 
35 	type = Vect_read_line(Map, Points, Cats, line);
36 
37 	if (field != -1 && !Vect_cat_get(Cats, field, NULL))
38 	    continue;
39 
40 	if (!(type & GV_POINT))
41 	    continue;
42 
43 	for (i = 0; i < nquads; ++i) {
44 	    if (hypot(Points->x[0] - quads[i].x, Points->y[0] - quads[i].y) <=
45 		radius) {
46 		counts[i]++;
47 		break;		/* next point, quads don't overlap */
48 	    }
49 	}
50     }
51 
52     Vect_destroy_line_struct(Points);
53     Vect_destroy_cats_struct(Cats);
54 }
55