main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-2014 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[2], native;
  28. int itype[2], ifield[2];
  29. int *ALines; /* List of lines: 0 do not output, 1 - write to output */
  30. int *AAreas; /* List of areas: 0 do not output, 1 - write area boundaries to output */
  31. int **cats, *ncats, *fields, nfields;
  32. struct GModule *module;
  33. struct GParm parm;
  34. struct GFlag flag;
  35. struct Map_info In[2], Out;
  36. struct field_info *IFi;
  37. G_gisinit(argv[0]);
  38. module = G_define_module();
  39. G_add_keyword(_("vector"));
  40. G_add_keyword(_("geometry"));
  41. G_add_keyword(_("spatial query"));
  42. module->description =
  43. _("Selects features from vector map (A) by features from other vector map (B).");
  44. parse_options(&parm, &flag);
  45. if (G_parser(argc, argv))
  46. exit(EXIT_FAILURE);
  47. if (parm.operator->answer[0] == 'e')
  48. operator = OP_EQUALS;
  49. else if (parm.operator->answer[0] == 'd') {
  50. /* operator = OP_DISJOINT; */
  51. operator = OP_INTERSECTS;
  52. flag.reverse->answer = YES;
  53. }
  54. else if (parm.operator->answer[0] == 'i')
  55. operator = OP_INTERSECTS;
  56. else if (parm.operator->answer[0] == 't')
  57. operator = OP_TOUCHES;
  58. else if (parm.operator->answer[0] == 'c' && parm.operator->answer[1] == 'r')
  59. operator = OP_CROSSES;
  60. else if (parm.operator->answer[0] == 'w')
  61. operator = OP_WITHIN;
  62. else if (parm.operator->answer[0] == 'c' && parm.operator->answer[1] == 'o')
  63. operator = OP_CONTAINS;
  64. else if (parm.operator->answer[0] == 'o') {
  65. if (strcmp(parm.operator->answer, "overlaps") == 0)
  66. operator = OP_OVERLAPS;
  67. else
  68. operator = OP_OVERLAP;
  69. }
  70. else if (parm.operator->answer[0] == 'r')
  71. operator = OP_RELATE;
  72. else
  73. G_fatal_error(_("Unknown operator '%s'"), parm.operator->answer);
  74. #ifdef HAVE_GEOS
  75. if (operator == OP_RELATE && !parm.relate->answer) {
  76. G_fatal_error(_("Required parameter <%s> not set"),
  77. parm.relate->key);
  78. }
  79. #else
  80. if (operator != OP_OVERLAP) {
  81. G_warning(_("Operator can only be 'overlap'"));
  82. operator = OP_OVERLAP;
  83. }
  84. #endif
  85. for (iopt = 0; iopt < 2; iopt++) {
  86. itype[iopt] = Vect_option_to_types(parm.type[iopt]);
  87. Vect_check_input_output_name(parm.input[iopt]->answer, parm.output->answer,
  88. G_FATAL_EXIT);
  89. Vect_set_open_level(2);
  90. if (Vect_open_old2(&(In[iopt]), parm.input[iopt]->answer, "",
  91. parm.field[iopt]->answer) < 0)
  92. G_fatal_error(_("Unable to open vector map <%s>"),
  93. parm.input[iopt]->answer);
  94. ifield[iopt] = Vect_get_field_number(&(In[iopt]), parm.field[iopt]->answer);
  95. }
  96. /* Alloc space for input lines array */
  97. ALines = (int *)G_calloc(Vect_get_num_lines(&(In[0])) + 1, sizeof(int));
  98. AAreas = NULL;
  99. if (flag.reverse->answer)
  100. AAreas = (int *)G_calloc(Vect_get_num_areas(&(In[0])) + 1, sizeof(int));
  101. /* Read field info */
  102. IFi = Vect_get_field(&(In[0]), ifield[0]);
  103. /* Open output */
  104. if (Vect_open_new(&Out, parm.output->answer, Vect_is_3d(&(In[0]))) < 0)
  105. G_fatal_error(_("Unable to create vector map <%s>"),
  106. parm.output->answer);
  107. Vect_set_map_name(&Out, _("Output from v.select"));
  108. Vect_set_person(&Out, G_whoami());
  109. Vect_copy_head_data(&(In[0]), &Out);
  110. Vect_hist_copy(&(In[0]), &Out);
  111. Vect_hist_command(&Out);
  112. /* Select features */
  113. #ifdef HAVE_GEOS
  114. select_lines(&(In[0]), itype[0], ifield[0],
  115. &(In[1]), itype[1], ifield[1],
  116. flag.cat->answer ? 1 : 0, operator,
  117. parm.relate->answer,
  118. ALines, AAreas, nskipped);
  119. #else
  120. select_lines(&(In[0]), itype[0], ifield[0],
  121. &(In[1]), itype[1], ifield[1],
  122. flag.cat->answer ? 1 : 0, operator,
  123. NULL,
  124. ALines, AAreas, nskipped);
  125. #endif
  126. #ifdef HAVE_GEOS
  127. finishGEOS();
  128. #endif
  129. native = Vect_maptype(&Out) == GV_FORMAT_NATIVE;
  130. nfields = Vect_cidx_get_num_fields(&(In[0]));
  131. cats = (int **)G_malloc(nfields * sizeof(int *));
  132. ncats = (int *)G_malloc(nfields * sizeof(int));
  133. fields = (int *)G_malloc(nfields * sizeof(int));
  134. /* Write lines */
  135. if (!flag.table->answer && !native) {
  136. /* Copy attributes for OGR output */
  137. Vect_copy_map_dblinks(&(In[0]), &Out, TRUE);
  138. }
  139. write_lines(&(In[0]), IFi, ALines, AAreas,
  140. &Out, flag.table->answer ? 1 : 0, flag.reverse->answer ? 1 : 0,
  141. nfields, fields, ncats, cats);
  142. /* Copy tables */
  143. if (!flag.table->answer && native) {
  144. copy_tabs(&(In[0]), &Out,
  145. nfields, fields, ncats, cats);
  146. }
  147. /* print info about skipped features & close input maps */
  148. for (iopt = 0; iopt < 2; iopt++) {
  149. if (nskipped[iopt] > 0) {
  150. G_warning(_("%d features from <%s> without category skipped"),
  151. nskipped[iopt], Vect_get_full_name(&(In[iopt])));
  152. }
  153. Vect_close(&(In[iopt]));
  154. }
  155. Vect_build(&Out);
  156. Vect_close(&Out);
  157. G_done_msg(_("%d features written to output."), Vect_get_num_lines(&Out));
  158. exit(EXIT_SUCCESS);
  159. }