parse.c 8.5 KB

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