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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
/* ****************************************************************************
*
* MODULE: v.select - select features from one map by features in another map.
*
*
* AUTHOR(S): Radim Blazek
*
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "gis.h"
#include "dbmi.h"
#include "Vect.h"
#define OP_OVERLAP 0
/* Add all elements of area A to the list */
void add_aarea ( struct Map_info *In, int aarea, int *ALines )
{
int i, j, aline, naisles, aisle, acentroid;
static struct ilist *BoundList = NULL;
if ( !BoundList )
BoundList = Vect_new_list ();
acentroid = Vect_get_area_centroid ( In, aarea );
ALines[acentroid] = 1;
Vect_get_area_boundaries ( In, aarea, BoundList );
for ( i = 0; i < BoundList->n_values; i++ ) {
aline = abs(BoundList->value[i]);
ALines[aline] = 1;
}
naisles = Vect_get_area_num_isles ( In, aarea );
for ( j = 0; j < naisles; j++ ) {
aisle = Vect_get_area_isle ( In, aarea, j );
Vect_get_isle_boundaries ( In, aisle, BoundList );
for ( i = 0; i < BoundList->n_values; i++ ) {
aline = abs(BoundList->value[i]);
ALines[aline] = 1;
}
}
}
/* Returns 1 if line1 from Map1 overlaps area2 from Map2,
* 0 otherwise */
int line_overlap_area ( struct Map_info *LMap, int line, struct Map_info *AMap, int area )
{
int i, nisles, isle;
static struct line_pnts *LPoints = NULL;
static struct line_pnts *APoints = NULL;
G_debug (4, "line_overlap_area line = %d area = %d", line, area);
if ( !LPoints ) {
LPoints = Vect_new_line_struct();
APoints = Vect_new_line_struct();
}
/* Read line coordinates */
Vect_read_line (LMap, LPoints, NULL, line);
/* Try if any of line vertices is within area */
for (i = 0; i < LPoints->n_points; i++ ) {
if ( Vect_point_in_area(AMap, area, LPoints->x[i], LPoints->y[i]) ) {
G_debug (4, " -> line vertex inside area");
return 1;
}
}
/* Try intersections of line with area/isles boundary */
/* Outer boundary */
Vect_get_area_points ( AMap, area, APoints );
if ( Vect_line_check_intersection(LPoints, APoints, 0) ) {
G_debug (4, " -> line intersects outer area boundary");
return 1;
}
nisles = Vect_get_area_num_isles ( AMap, area );
for ( i = 0; i < nisles; i++ ) {
isle = Vect_get_area_isle ( AMap, area, i );
Vect_get_isle_points ( AMap, isle, APoints );
if ( Vect_line_check_intersection(LPoints, APoints, 0) ) {
G_debug (4, " -> line intersects area island boundary");
return 1;
}
}
return 0;
}
int
main (int argc, char *argv[])
{
int i;
int input, operator;
int aline, nalines;
int type[2], field[2];
int **cats, *ncats, nfields, *fields;
char *mapset[2], *pre[2];
struct GModule *module;
struct Option *in_opt[2], *out_opt, *type_opt[2], *field_opt[2], *operator_opt;
struct Flag *table_flag;
struct Map_info In[2], Out;
struct field_info *IFi, *OFi;
struct line_pnts *APoints, *BPoints;
struct line_cats *ACats, *BCats;
int *ALines; /* List of lines: 0 do not output, 1 - write to output */
struct ilist *List, *TmpList, *BoundList;
G_gisinit (argv[0]);
pre[0] ="a";
pre[1] ="b";
module = G_define_module();
module->description = "Select features from ainput by features from binput.";
in_opt[0] = G_define_standard_option(G_OPT_V_INPUT);
in_opt[0]->key = "ainput";
type_opt[0] = G_define_standard_option(G_OPT_V_TYPE) ;
type_opt[0]->key = "atype";
field_opt[0] = G_define_standard_option(G_OPT_V_FIELD);
field_opt[0]->key = "alayer";
in_opt[1] = G_define_standard_option(G_OPT_V_INPUT);
in_opt[1]->key = "binput";
type_opt[1] = G_define_standard_option(G_OPT_V_TYPE) ;
type_opt[1]->key = "btype";
field_opt[1] = G_define_standard_option(G_OPT_V_FIELD);
field_opt[1]->key = "blayer";
out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
operator_opt = G_define_option();
operator_opt->key = "operator";
operator_opt->type = TYPE_STRING;
operator_opt->required = NO;
operator_opt->multiple = NO;
operator_opt->options = "overlap";
operator_opt->answer = "overlap";
operator_opt->description = "Operator defines required relation between features. "
"A feature is written to output if the result of operation 'ainput operator binput' is true. "
"An input feature is considered to be true, if category of given layer is defined.\n"
"\t overlap : features partialy or completely overlap\n";
table_flag = G_define_flag ();
table_flag->key = 't';
table_flag->description = "Do not create attribute table.";
if (G_parser (argc, argv)) exit(-1);
if ( operator_opt->answer[0] == 'o' ) operator = OP_OVERLAP;
for ( input = 0; input < 2; input++ ) {
type[input] = Vect_option_to_types (type_opt[input]);
field[input] = atoi(field_opt[input]->answer);
Vect_check_input_output_name ( in_opt[input]->answer, out_opt->answer, GV_FATAL_EXIT );
if ((mapset[input] = G_find_vector2 (in_opt[input]->answer, NULL)) == NULL) {
G_fatal_error ("Could not find vector '%s'\n", in_opt[input]->answer);
}
Vect_set_open_level (2);
Vect_open_old (&(In[input]), in_opt[input]->answer, mapset[input] );
}
/* Read field info */
IFi = Vect_get_field(&(In[0]), field[0]);
APoints = Vect_new_line_struct();
BPoints = Vect_new_line_struct();
ACats = Vect_new_cats_struct();
BCats = Vect_new_cats_struct();
List = Vect_new_list ();
TmpList = Vect_new_list ();
BoundList = Vect_new_list ();
/* Open output */
Vect_open_new (&Out, out_opt->answer, 0);
Vect_set_map_name ( &Out, "Output from v.select");
Vect_set_person ( &Out, G_whoami ());
Vect_copy_head_data (&(In[0]), &Out);
Vect_hist_copy (&(In[0]), &Out);
Vect_hist_command ( &Out );
nalines = Vect_get_num_lines ( &(In[0]) );
/* Alloc space for input lines array */
ALines = (int *) G_calloc ( nalines+1, sizeof(int) );
/* Lines in A. Go through all lines and mark those that meets condition */
if ( type[0] & (GV_POINTS | GV_LINES) ) {
fprintf ( stderr, "Processing ainput lines ... " );
for ( aline = 1; aline <= nalines; aline++ ) {
BOUND_BOX abox;
G_debug (3, "aline = %d", aline);
G_percent ( aline, nalines, 1 ); /* must be before any continue */
/* Check category */
if ( Vect_get_line_cat(&(In[0]), aline, field[0]) < 0 ) continue;
/* Read line and check type */
if ( !(Vect_read_line(&(In[0]), APoints, NULL, aline) & type[0]) ) continue;
Vect_get_line_box ( &(In[0]), aline, &abox );
/* Check if this line overlaps any feature in B */
/* x Lines in B */
if ( type[1] & (GV_POINTS | GV_LINES) ) {
int i;
int found = 0;
/* Lines */
Vect_select_lines_by_box ( &(In[1]), &abox, type[1], List);
for (i = 0; i < List->n_values; i++) {
int bline;
bline = List->value[i];
G_debug (3, " bline = %d", bline);
/* Check category */
if ( !Vect_get_line_cat(&(In[1]), bline, field[1]) < 0 ) continue;
Vect_read_line ( &(In[1]), BPoints, NULL, bline);
if ( Vect_line_check_intersection ( APoints, BPoints, 0 ) ) {
found = 1;
break;
}
}
if ( found ) {
ALines[aline] = 1;
continue; /* Go to next A line */
}
}
/* x Areas in B. */
if ( type[1] & GV_AREA ) {
int i;
Vect_select_areas_by_box ( &(In[1]), &abox, List);
for (i = 0; i < List->n_values; i++) {
int barea;
barea = List->value[i];
G_debug (3, " barea = %d", barea);
if ( Vect_get_area_cat(&(In[1]), barea, field[1]) < 0 ) continue;
if ( line_overlap_area(&(In[0]), aline, &(In[1]), barea) ) {
ALines[aline] = 1;
break;
}
}
}
}
}
/* Areas in A. */
if ( type[0] & GV_AREA ) {
int aarea, naareas;
fprintf ( stderr, "Processing ainput areas ... " );
naareas = Vect_get_num_areas ( &(In[0]) );
for ( aarea = 1; aarea <= naareas; aarea++ ) {
int i;
BOUND_BOX abox;
G_percent ( aarea, naareas, 1 ); /* must be before any continue */
if ( Vect_get_area_cat(&(In[0]), aarea, field[0]) < 0 ) continue;
Vect_get_area_box ( &(In[0]), aarea, &abox );
/* x Lines in B */
if ( type[1] & (GV_POINTS | GV_LINES) ) {
Vect_select_lines_by_box ( &(In[1]), &abox, type[1], List);
for (i = 0; i < List->n_values; i++) {
int bline;
bline = List->value[i];
if ( Vect_get_line_cat(&(In[1]), bline, field[1]) < 0 ) continue;
if ( line_overlap_area(&(In[1]), bline, &(In[0]), aarea) ) {
add_aarea ( &(In[0]), aarea, ALines );
continue;
}
}
}
/* x Areas in B */
if ( type[1] & GV_AREA ) {
int naisles;
int found = 0;
/* List of areas B */
/* Make a list of features forming area A */
Vect_reset_list ( List );
Vect_get_area_boundaries ( &(In[0]), aarea, BoundList );
for ( i = 0; i < BoundList->n_values; i++ ) {
Vect_list_append ( List, abs(BoundList->value[i]) );
}
naisles = Vect_get_area_num_isles ( &(In[0]), aarea );
for ( i = 0; i < naisles; i++ ) {
int j, aisle;
aisle = Vect_get_area_isle ( &(In[0]), aarea, i );
Vect_get_isle_boundaries ( &(In[0]), aisle, BoundList );
for ( j = 0; j < BoundList->n_values; j++ ) {
Vect_list_append ( List, BoundList->value[j] );
}
}
Vect_select_areas_by_box ( &(In[1]), &abox, TmpList);
for (i = 0; i < List->n_values; i++) {
int j, aline;
aline = abs ( List->value[i] );
for (j = 0; j < TmpList->n_values; j++) {
int barea, bcentroid;
barea = TmpList->value[j];
G_debug (3, " barea = %d", barea);
if ( Vect_get_area_cat(&(In[1]), barea, field[1]) < 0 ) continue;
/* Check if any centroid of area B is in area A.
* This test is important in if area B is completely within area A */
bcentroid = Vect_get_area_centroid ( &(In[1]), barea );
Vect_read_line ( &(In[1]), BPoints, NULL, bcentroid);
if ( Vect_point_in_area ( &(In[0]), aarea, BPoints->x[0], BPoints->y[0]) ) {
found = 1;
break;
}
/* Check intersectin of lines from List with area B */
if ( line_overlap_area(&(In[0]), aline, &(In[1]), barea) ) {
found = 1;
break;
}
}
if ( found ) {
add_aarea ( &(In[0]), aarea, ALines );
break;
}
}
}
}
}
Vect_close ( &(In[1]) );
/* Write lines */
nfields = Vect_cidx_get_num_fields ( &(In[0]) );
cats = (int **) G_malloc ( nfields * sizeof(int *) );
ncats = (int *) G_malloc ( nfields * sizeof(int) );
fields = (int *) G_malloc ( nfields * sizeof(int) );
for ( i = 0; i < nfields; i++ ) {
ncats[i] = 0;
cats[i] = (int *) G_malloc ( Vect_cidx_get_num_cats_by_index(&(In[0]),i) * sizeof(int) );
fields[i] = Vect_cidx_get_field_number ( &(In[0]), i );
}
for ( aline = 1; aline <= nalines; aline++ ) {
int atype;
G_debug (4, "aline = %d ALines[aline] = %d", aline, ALines[aline]);
if ( !(ALines[aline]) ) continue;
atype = Vect_read_line ( &(In[0]), APoints, ACats, aline);
Vect_write_line ( &Out, atype, APoints, ACats );
if ( !(table_flag->answer) && (IFi != NULL) ) {
for ( i = 0; i < ACats->n_cats; i++ ) {
int f, j;
for ( j = 0; j < nfields; j++ ) { /* find field */
if ( fields[j] == ACats->field[i] ) {
f = j;
break;
}
}
cats[f][ncats[f]] = ACats->cat[i];
ncats[f]++;
}
}
}
/* Copy tables */
if ( !(table_flag->answer) ) {
int ttype, ntabs=0;
G_message ( "Writing attributes ...\n" );
/* Number of output tabs */
for ( i = 0; i < Vect_get_num_dblinks ( &(In[0]) ); i++ ) {
int f, j;
IFi = Vect_get_dblink ( &(In[0]), i );
for ( j = 0; j < nfields; j++ ) { /* find field */
if ( fields[j] == IFi->number ) {
f = j;
break;
}
}
if ( ncats[f] > 0 ) ntabs++;
}
if ( ntabs > 1 )
ttype = GV_MTABLE;
else
ttype = GV_1TABLE;
for ( i = 0; i < nfields; i++ ) {
int ret;
if ( fields[i] == 0 ) continue;
G_message ( "Layer %d", fields[i] );
/* Make a list of categories */
IFi = Vect_get_field ( &(In[0]), fields[i] );
if ( !IFi ) { /* no table */
G_message ( "No table." );
continue;
}
OFi = Vect_default_field_info ( &Out, IFi->number, IFi->name, ttype );
ret = db_copy_table_by_ints ( IFi->driver, IFi->database, IFi->table,
OFi->driver, Vect_subst_var(OFi->database,&Out), OFi->table,
IFi->key, cats[i], ncats[i] );
if ( ret == DB_FAILED ) {
G_warning ( "Cannot copy table" );
} else {
Vect_map_add_dblink ( &Out, OFi->number, OFi->name, OFi->table,
IFi->key, OFi->database, OFi->driver);
}
G_message ( "Done." );
}
}
Vect_close ( &(In[0]) );
Vect_build (&Out, stderr);
Vect_close (&Out);
exit (0);
}
|