parse.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <grass/glocale.h>
  5. #include "global.h"
  6. int parse_units();
  7. int parse_option();
  8. int match();
  9. int parse_command_line(int argc, char *argv[])
  10. {
  11. int ncols;
  12. struct
  13. {
  14. struct Option *vect;
  15. struct Option *option;
  16. struct Option *type;
  17. struct Option *field;
  18. struct Option *qfield;
  19. struct Option *col;
  20. struct Option *units;
  21. struct Option *qcol;
  22. } parms;
  23. struct
  24. {
  25. struct Flag *p, *s, *t;
  26. } flags;
  27. parms.vect = G_define_standard_option(G_OPT_V_MAP);
  28. parms.field = G_define_standard_option(G_OPT_V_FIELD);
  29. parms.field->label = _("Layer number or name (write to)");
  30. parms.type = G_define_standard_option(G_OPT_V_TYPE);
  31. parms.type->options = "point,line,boundary,centroid";
  32. parms.type->answer = "point,line,boundary,centroid";
  33. parms.type->label = _("Feature type");
  34. parms.type->description =
  35. _("For coor valid point/centroid, "
  36. "for length valid line/boundary");
  37. parms.qfield = G_define_standard_option(G_OPT_V_FIELD);
  38. parms.qfield->key = "qlayer";
  39. parms.qfield->label = _("Query layer number or name (read from)");
  40. parms.qfield->guisection = _("Query");
  41. parms.option = G_define_option();
  42. parms.option->key = "option";
  43. parms.option->type = TYPE_STRING;
  44. parms.option->required = YES;
  45. parms.option->multiple = NO;
  46. parms.option->options =
  47. "cat,area,compact,fd,perimeter,length,count,coor,start,end,sides,query,slope,sinuous,azimuth";
  48. parms.option->description = _("Value to upload");
  49. parms.option->descriptions =
  50. "cat;insert new row for each category if doesn't exist yet;"
  51. "area;area size;"
  52. "compact;compactness of an area, calculated as \n"
  53. " compactness = perimeter / (2 * sqrt(PI * area));"
  54. "fd;fractal dimension of boundary defining a polygon, calculated as \n"
  55. " fd = 2 * (log(perimeter) / log(area));"
  56. "perimeter;perimeter length of an area;"
  57. "length;line length;"
  58. "count;number of features for each category;"
  59. "coor;point coordinates, X,Y or X,Y,Z;"
  60. "start;line/boundary starting point coordinates, X,Y or X,Y,Z;"
  61. "end;line/boundary end point coordinates, X,Y or X,Y,Z;"
  62. "sides;categories of areas on the left and right side of the boundary, "
  63. "'qlayer' is used for area category;"
  64. "query;result of a database query for all records of the geometry"
  65. "(or geometries) from table specified by 'qlayer' option;"
  66. "slope;slope steepness of vector line or boundary;"
  67. "sinuous;line sinuousity, calculated as line length / distance between end points;"
  68. "azimuth;line azimuth, calculated as angle between North direction and endnode direction at startnode";
  69. parms.units = G_define_standard_option(G_OPT_M_UNITS);
  70. parms.units->options =
  71. "miles,feet,meters,kilometers,acres,hectares,radians,degrees";
  72. parms.col = G_define_standard_option(G_OPT_DB_COLUMNS);
  73. parms.qcol = G_define_standard_option(G_OPT_DB_COLUMN);
  74. parms.qcol->key = "qcolumn";
  75. parms.qcol->label = _("Name of attribute column used for 'query' option");
  76. parms.qcol->description = _("E.g. 'cat', 'count(*)', 'sum(val)'");
  77. parms.qcol->required = NO;
  78. parms.qcol->guisection = _("Query");
  79. flags.p = G_define_flag();
  80. flags.p->key = 'p';
  81. flags.p->description = _("Print only");
  82. flags.p->guisection = _("Print");
  83. flags.s = G_define_flag();
  84. flags.s->key = 's';
  85. flags.s->description = _("Only print SQL statements");
  86. flags.s->guisection = _("Print");
  87. flags.t = G_define_flag();
  88. flags.t->key = 'c';
  89. flags.t->description =
  90. _("In print mode prints totals for options: length,area,count");
  91. flags.t->guisection = _("Print");
  92. if (G_parser(argc, argv))
  93. exit(EXIT_FAILURE);
  94. options.print = flags.p->answer;
  95. options.sql = flags.s->answer;
  96. options.total = flags.t->answer;
  97. options.name = parms.vect->answer;
  98. options.type = Vect_option_to_types(parms.type);
  99. options.field = atoi(parms.field->answer);
  100. options.qfield = atoi(parms.qfield->answer);
  101. options.option = parse_option(parms.option->answer);
  102. options.units = parse_units(parms.units->answer);
  103. /* Check number of columns */
  104. ncols = 0;
  105. options.col[0] = NULL;
  106. options.col[1] = NULL;
  107. options.col[2] = NULL;
  108. while (parms.col->answers && parms.col->answers[ncols]) {
  109. options.col[ncols] = G_store(parms.col->answers[ncols]);
  110. ncols++;
  111. }
  112. if (!options.print) {
  113. if (options.option == O_AREA || options.option == O_LENGTH || options.option == O_COUNT ||
  114. options.option == O_QUERY || options.option == O_COMPACT || options.option == O_FD ||
  115. options.option == O_PERIMETER || options.option == O_SLOPE || options.option == O_SINUOUS ||
  116. options.option == O_AZIMUTH) { /* one column required */
  117. if (ncols != 1) {
  118. G_fatal_error(_("This option requires one column"));
  119. }
  120. }
  121. else if (options.option == O_SIDES) {
  122. if (ncols != 2) {
  123. G_fatal_error(_("This option requires two columns"));
  124. }
  125. }
  126. else if (options.option == O_COOR || options.option == O_START || options.option == O_END) {
  127. if (ncols < 2) {
  128. G_fatal_error(_("This option requires at least two columns"));
  129. }
  130. }
  131. }
  132. if (options.option == O_QUERY && !parms.qcol->answers)
  133. G_fatal_error(_("Parameter 'qcolumn' must be specified for 'option=query'"));
  134. options.qcol = parms.qcol->answer;
  135. if (options.option == O_SIDES && !(options.type | GV_BOUNDARY))
  136. G_fatal_error(_("The 'sides' option makes sense only for boundaries"));
  137. if (options.option == O_SINUOUS && !(options.type | GV_LINES))
  138. G_fatal_error(_("The 'sinuous' option makes sense only for lines"));
  139. if (options.option == O_AZIMUTH && !(options.type | GV_LINES))
  140. G_fatal_error(_("The 'azimuth' option makes sense only for lines"));
  141. return 0;
  142. }
  143. int parse_units(char *s)
  144. {
  145. int x = 0;
  146. if (match(s, "miles", 2))
  147. x = U_MILES;
  148. else if (match(s, "feet", 1))
  149. x = U_FEET;
  150. else if (match(s, "meters", 2))
  151. x = U_METERS;
  152. else if (match(s, "kilometers", 1))
  153. x = U_KILOMETERS;
  154. else if (match(s, "acres", 1))
  155. x = U_ACRES;
  156. else if (match(s, "hectares", 1))
  157. x = U_HECTARES;
  158. else if (match(s, "radians", 1))
  159. x = U_RADIANS;
  160. else if (match(s, "degrees", 1))
  161. x = U_DEGREES;
  162. return x;
  163. }
  164. int parse_option(char *s)
  165. {
  166. int x = 0;
  167. if (strcmp(s, "cat") == 0)
  168. x = O_CAT;
  169. else if (strcmp(s, "area") == 0)
  170. x = O_AREA;
  171. else if (strcmp(s, "length") == 0)
  172. x = O_LENGTH;
  173. else if (strcmp(s, "count") == 0)
  174. x = O_COUNT;
  175. else if (strcmp(s, "coor") == 0)
  176. x = O_COOR;
  177. else if (strcmp(s, "start") == 0)
  178. x = O_START;
  179. else if (strcmp(s, "end") == 0)
  180. x = O_END;
  181. else if (strcmp(s, "sides") == 0)
  182. x = O_SIDES;
  183. else if (strcmp(s, "query") == 0)
  184. x = O_QUERY;
  185. else if (strcmp(s, "compact") == 0)
  186. x = O_COMPACT;
  187. else if (strcmp(s, "fd") == 0)
  188. x = O_FD;
  189. else if (strcmp(s, "perimeter") == 0)
  190. x = O_PERIMETER;
  191. else if (strcmp(s, "slope") == 0)
  192. x = O_SLOPE;
  193. else if (strcmp(s, "sinuous") == 0)
  194. x = O_SINUOUS;
  195. else if (strcmp(s, "azimuth") == 0)
  196. x = O_AZIMUTH;
  197. return x;
  198. }
  199. int match(char *s, char *key, int min)
  200. {
  201. size_t len;
  202. if (!s)
  203. return 0;
  204. len = strlen(s);
  205. if (len < (size_t) min)
  206. return 0;
  207. return strncmp(s, key, len) == 0;
  208. }