main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /****************************************************************************
  2. *
  3. * MODULE: v.select
  4. * AUTHOR(S): Radim Blazek <radim.blazek gmail.com> (original contributor)
  5. * Glynn Clements <glynn gclements.plus.com>
  6. * Markus Neteler <neteler itc.it>
  7. * Martin Landa <landa.martin gmail.com> (GEOS support)
  8. * PURPOSE: Select features from one map by features in another map.
  9. * COPYRIGHT: (C) 2003-2011 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General
  12. * Public License (>=v2). Read the file COPYING that
  13. * comes with GRASS for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <grass/gis.h>
  20. #include <grass/vector.h>
  21. #include <grass/glocale.h>
  22. #include "proto.h"
  23. int main(int argc, char *argv[])
  24. {
  25. int iopt;
  26. int operator;
  27. int nskipped, native;
  28. int itype[2], ifield[2];
  29. int *ALines; /* List of lines: 0 do not output, 1 - write to output */
  30. int **cats, *ncats, *fields, nfields;
  31. struct GModule *module;
  32. struct GParm parm;
  33. struct GFlag flag;
  34. struct Map_info In[2], Out;
  35. struct field_info *IFi;
  36. G_gisinit(argv[0]);
  37. module = G_define_module();
  38. G_add_keyword(_("vector"));
  39. G_add_keyword(_("spatial query"));
  40. module->description =
  41. _("Selects features from vector map (A) by features from other vector map (B).");
  42. parse_options(&parm, &flag);
  43. if (G_parser(argc, argv))
  44. exit(EXIT_FAILURE);
  45. if (parm.operator->answer[0] == 'e')
  46. operator = OP_EQUALS;
  47. else if (parm.operator->answer[0] == 'd') {
  48. /* operator = OP_DISJOINT; */
  49. operator = OP_INTERSECTS;
  50. flag.reverse->answer = YES;
  51. }
  52. else if (parm.operator->answer[0] == 'i')
  53. operator = OP_INTERSECTS;
  54. else if (parm.operator->answer[0] == 't')
  55. operator = OP_TOUCHES;
  56. else if (parm.operator->answer[0] == 'c' && parm.operator->answer[1] == 'r')
  57. operator = OP_CROSSES;
  58. else if (parm.operator->answer[0] == 'w')
  59. operator = OP_WITHIN;
  60. else if (parm.operator->answer[0] == 'c' && parm.operator->answer[1] == 'o')
  61. operator = OP_CONTAINS;
  62. else if (parm.operator->answer[0] == 'o') {
  63. if (strcmp(parm.operator->answer, "overlaps") == 0)
  64. operator = OP_OVERLAPS;
  65. else
  66. operator = OP_OVERLAP;
  67. }
  68. else if (parm.operator->answer[0] == 'r')
  69. operator = OP_RELATE;
  70. else
  71. G_fatal_error(_("Unknown operator '%s'"), parm.operator->answer);
  72. if (operator == OP_RELATE && !parm.relate->answer) {
  73. G_fatal_error(_("Required parameter <%s> not set"),
  74. parm.relate->key);
  75. }
  76. for (iopt = 0; iopt < 2; iopt++) {
  77. itype[iopt] = Vect_option_to_types(parm.type[iopt]);
  78. Vect_check_input_output_name(parm.input[iopt]->answer, parm.output->answer,
  79. GV_FATAL_EXIT);
  80. Vect_set_open_level(2);
  81. Vect_open_old2(&(In[iopt]), parm.input[iopt]->answer, "", parm.field[iopt]->answer);
  82. ifield[iopt] = Vect_get_field_number(&(In[iopt]), parm.field[iopt]->answer);
  83. }
  84. /* Alloc space for input lines array */
  85. ALines = (int *)G_calloc(Vect_get_num_lines(&(In[0])) + 1, sizeof(int));
  86. /* Read field info */
  87. IFi = Vect_get_field(&(In[0]), ifield[0]);
  88. /* Open output */
  89. Vect_open_new(&Out, parm.output->answer, Vect_is_3d(&(In[0])));
  90. Vect_set_map_name(&Out, _("Output from v.select"));
  91. Vect_set_person(&Out, G_whoami());
  92. Vect_copy_head_data(&(In[0]), &Out);
  93. Vect_hist_copy(&(In[0]), &Out);
  94. Vect_hist_command(&Out);
  95. /* Select features */
  96. nskipped = select_lines(&(In[0]), itype[0], ifield[0],
  97. &(In[1]), itype[1], ifield[1],
  98. flag.cat->answer ? 1 : 0, operator,
  99. parm.relate->answer,
  100. ALines);
  101. Vect_close(&(In[1]));
  102. #ifdef HAVE_GEOS
  103. finishGEOS();
  104. #endif
  105. native = Vect_maptype(&Out) == GV_FORMAT_NATIVE;
  106. nfields = Vect_cidx_get_num_fields(&(In[0]));
  107. cats = (int **)G_malloc(nfields * sizeof(int *));
  108. ncats = (int *)G_malloc(nfields * sizeof(int));
  109. fields = (int *)G_malloc(nfields * sizeof(int));
  110. /* Write lines */
  111. if (!flag.table->answer && !native) {
  112. /* Copy attributes for OGR output */
  113. Vect_copy_map_dblinks(&(In[0]), &Out, TRUE);
  114. }
  115. write_lines(&(In[0]), IFi, ALines,
  116. &Out, flag.table->answer ? 1 : 0, flag.reverse->answer ? 1 : 0,
  117. nfields, fields, ncats, cats);
  118. /* Copy tables */
  119. if (!flag.table->answer && native) {
  120. copy_tabs(&(In[0]), &Out,
  121. nfields, fields, ncats, cats);
  122. }
  123. Vect_close(&(In[0]));
  124. Vect_build(&Out);
  125. Vect_close(&Out);
  126. if (nskipped > 0) {
  127. G_warning(_("%d features without category skipped"), nskipped);
  128. }
  129. G_done_msg(_("%d features written to output."), Vect_get_num_lines(&Out));
  130. exit(EXIT_SUCCESS);
  131. }