1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*****************************************************************************
*
* MODULE: Vector library
*
* AUTHOR(S): Radim Blazek
*
* PURPOSE: Higher level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT: (C) 2001 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
#include <stdlib.h>
#include "gis.h"
#include "dbmi.h"
#include "Vect.h"
/*
* Create new VARRAY and allocate space for given number of items.
* Space allocated is 'size + 1' so that lines are accessed by line id.
* Array values are set to 0.
* Returns : pointer to new VARRAY
* NULL if failed
*/
VARRAY *
Vect_new_varray (int size)
{
VARRAY *p;
p = (VARRAY *) G_malloc (sizeof (VARRAY));
if ( p == NULL ) return NULL;
p->size = size;
p->c = (int *) G_calloc ( size + 1, sizeof(int) );
if ( p->c == NULL ) {
G_free (p);
return NULL;
}
return p;
}
/*
* Set values in 'varray' to 'value' if category of object of given type is in
* 'cstring' (string representing category list like: '1,3,5-7').
* 'type' may be either: GV_AREA
* or: GV_POINT | GV_LINE | GV_BOUNDARY | GV_CENTROID
*
* Array is not reset to zero before, but old values (if any > 0) are overwritten.
*
* Return: number of items set
* -1 on error
*/
int
Vect_set_varray_from_cat_string ( struct Map_info *Map, int field, char *cstring,
int type, int value, VARRAY *varray )
{
int ret;
struct cat_list *Clist;
G_debug (4, "Vect_set_varray_from_cat_string(): cstring = '%s'", cstring);
Clist = Vect_new_cat_list ();
ret = Vect_str_to_cat_list ( cstring, Clist);
if ( ret > 0 ) G_warning ( "%d errors in category string.", ret);
G_debug (4, " %d ranges in clist", Clist->n_ranges);
ret = Vect_set_varray_from_cat_list ( Map, field, Clist, type, value, varray );
Vect_destroy_cat_list (Clist);
return ret;
}
/*
* Set values in 'varray' to 'value' if category of object of given type is in 'clist' (category list).
* 'type' may be either: GV_AREA
* or: GV_POINT | GV_LINE | GV_BOUNDARY | GV_CENTROID
*
* Array is not reset to zero before, but old values (if any > 0) are overwritten.
*
* Return: number of items set
* -1 on error
*/
int
Vect_set_varray_from_cat_list ( struct Map_info *Map, int field, struct cat_list *clist,
int type, int value, VARRAY *varray )
{
int i, n, centr, cat;
int ni = 0; /* number of items set */
int ltype; /* line type */
struct line_cats *Cats;
G_debug (4, "Vect_set_varray_from_cat_list(): field = %d", field);
/* Check type */
if ( (type & GV_AREA) && (type & (GV_POINTS | GV_LINES)) ) {
G_warning ("Mixed area and other type requested for vector array.");
return 0;
}
Cats = Vect_new_cats_struct ();
if ( type & GV_AREA ) { /* Areas */
n = Vect_get_num_areas (Map);
if ( n > varray->size ) { /* not enough space */
G_warning ("Not enough space in vector array.");
return 0;
}
for ( i = 1; i <= n; i++ ) {
centr = Vect_get_area_centroid ( Map, i );
if ( centr <= 0 ) continue; /* No centroid */
Vect_read_line (Map, NULL, Cats, centr);
if ( !Vect_cat_get(Cats, field, &cat) ) continue; /* No such field */
if ( Vect_cat_in_cat_list (cat, clist) ) { /* cat is in list */
varray->c[i] = value;
ni++;
}
}
} else { /* Lines */
n = Vect_get_num_lines (Map);
if ( n > varray->size ) { /* not enough space */
G_warning ("Not enough space in vector array.");
return 0;
}
for ( i = 1; i <= n; i++ ) {
ltype = Vect_read_line (Map, NULL, Cats, i);
if ( !(ltype & type) ) continue; /* is not specified type */
if ( !Vect_cat_get(Cats, field, &cat) ) continue; /* No such field */
if ( Vect_cat_in_cat_list (cat, clist) ) { /* cat is in list */
varray->c[i] = value;
ni++;
}
}
}
Vect_destroy_cats_struct (Cats);
return ni;
}
/* compare 2 integers in array */
int cmp ( const void *pa, const void *pb)
{
int *p1 = (int *) pa;
int *p2 = (int *) pb;
if( *p1 < *p2 ) return -1;
if( *p1 > *p2 ) return 1;
return 0;
}
/* check if cat is in array */
int
in_array ( int *cats, int ncats, int cat )
{
int *p;
p = (int *) bsearch((void *) &cat, cats, ncats, sizeof(int), cmp);
if ( p == NULL ) return 0;
return 1;
}
/*
* Set values in 'varray' to 'value' if category of object of given type is in
* categories selected from DB based on where statement (given without where).
* 'type' may be either: GV_AREA
* or: GV_POINT | GV_LINE | GV_BOUNDARY | GV_CENTROID
*
* Array is not reset to zero before, but old values (if any > 0) are overwritten.
*
* Return: number of items set
* -1 on error
*/
int
Vect_set_varray_from_db ( struct Map_info *Map, int field, char *where,
int type, int value, VARRAY *varray )
{
int i, n, c, centr, cat, *cats, ncats;
int ni = 0; /* number of items set */
int ltype; /* line type */
struct line_cats *Cats;
struct field_info *Fi;
dbDriver *driver;
G_debug (4, "Vect_set_varray_from_db(): field = %d where = '%s'", field, where);
/* Note: use category index once available */
/* Check type */
if ( (type & GV_AREA) && (type & (GV_POINTS | GV_LINES)) ) {
G_warning ("Mixed area and other type requested for vector array.");
return 0;
}
Cats = Vect_new_cats_struct ();
/* Select categories from DB to array */
Fi = Vect_get_field ( Map, field);
if ( Fi == NULL ) {
G_warning ( "Cannot get field info" );
return -1;
}
driver = db_start_driver_open_database ( Fi->driver, Fi->database );
if ( driver == NULL ) {
G_warning ( "Cannot open database" );
return -1;
}
ncats = db_select_int( driver, Fi->table, Fi->key, where, &cats);
db_close_database_shutdown_driver ( driver );
if ( type & GV_AREA ) { /* Areas */
n = Vect_get_num_areas (Map);
if ( n > varray->size ) { /* not enough space */
G_warning ("Not enough space in vector array.");
return 0;
}
for ( i = 1; i <= n; i++ ) {
centr = Vect_get_area_centroid ( Map, i );
if ( centr <= 0 ) continue; /* No centroid */
Vect_read_line (Map, NULL, Cats, centr);
/*if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
for (c = 0; c < Cats->n_cats; c++) {
if (Cats->field[c] == field && in_array ( cats, ncats, Cats->cat[c] )) {
cat = Cats->cat[c];
varray->c[i] = value;
ni++;
break;
}
}
/*
if ( in_array ( cats, ncats, cat ) ) {
varray->c[i] = value;
ni++;
}
*/
}
} else { /* Lines */
n = Vect_get_num_lines (Map);
if ( n > varray->size ) { /* not enough space */
G_warning ("Not enough space in vector array.");
return 0;
}
for ( i = 1; i <= n; i++ ) {
ltype = Vect_read_line (Map, NULL, Cats, i);
if ( !(ltype & type) ) continue; /* is not specified type */
/* if ( !Vect_cat_get(Cats, field, &cat) ) continue; No such field */
for (c = 0; c < Cats->n_cats; c++) {
if (Cats->field[c] == field && in_array ( cats, ncats, Cats->cat[c] )) {
cat = Cats->cat[c];
varray->c[i] = value;
ni++;
break;
}
}
/*
if ( in_array ( cats, ncats, cat ) ) {
varray->c[i] = value;
ni++;
}
*/
}
}
G_free ( cats );
Vect_destroy_cats_struct (Cats);
return ni;
}
|