main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /****************************************************************
  2. *
  3. * MODULE: v.random (based on s.rand)
  4. *
  5. * AUTHOR(S): James Darrell McCauley darrell@mccauley-usa.com
  6. * http://mccauley-usa.com/
  7. * OGR support by Martin Landa <landa.martin gmail.com>
  8. * Area support by Markus Metz
  9. *
  10. * PURPOSE: Randomly generate a 2D/3D GRASS vector points map.
  11. *
  12. * Modification History:
  13. *
  14. * s.rand v 0.5B <25 Jun 1995> Copyright (c) 1993-1995. James Darrell McCauley
  15. * <?? ??? 1993> - began coding and released test version (jdm)
  16. * <10 Jan 1994> - changed RAND_MAX for rand(), since it is different for
  17. * SunOS 4.1.x and Solaris 2.3. stdlib.h in the latter defines
  18. * RAND_MAX, but it doesn't in the former. v0.2B (jdm)
  19. * <02 Jan 1995> - clean Gmakefile, man page. added html v0.3B (jdm)
  20. * <25 Feb 1995> - cleaned 'gcc -Wall' warnings (jdm)
  21. * <25 Jun 1995> - new site API (jdm)
  22. * <13 Sep 2000> - released under GPL
  23. *
  24. * COPYRIGHT: (C) 2003-2018 by the GRASS Development Team
  25. *
  26. * This program is free software under the GNU General
  27. * Public License (>=v2). Read the file COPYING that
  28. * comes with GRASS for details.
  29. *
  30. **************************************************************/
  31. #include <stdlib.h>
  32. #include <math.h>
  33. #include <string.h>
  34. #include <grass/gis.h>
  35. #include <grass/vector.h>
  36. #include <grass/dbmi.h>
  37. #include <grass/glocale.h>
  38. /* for qsort */
  39. typedef struct {
  40. int i;
  41. double size;
  42. struct bound_box box;
  43. } BOX_SIZE;
  44. static int sort_by_size(const void *a, const void *b)
  45. {
  46. BOX_SIZE *as = (BOX_SIZE *)a;
  47. BOX_SIZE *bs = (BOX_SIZE *)b;
  48. if (as->size < bs->size)
  49. return -1;
  50. return (as->size > bs->size);
  51. }
  52. int main(int argc, char *argv[])
  53. {
  54. char *output, buf[DB_SQL_MAX];
  55. double (*rng)(void) = G_drand48;
  56. double zmin, zmax;
  57. int seed;
  58. unsigned long i, n, total_n;
  59. int j, k, type, usefloat;
  60. int area, nareas, field, cat_area;
  61. int cat, icol, ncols;
  62. struct boxlist *List = NULL;
  63. BOX_SIZE *size_list = NULL;
  64. int alloc_size_list = 0;
  65. struct Map_info In, Out;
  66. struct line_pnts *Points;
  67. struct line_cats *Cats;
  68. struct cat_list *cat_list;
  69. struct bound_box box;
  70. struct Cell_head window;
  71. struct GModule *module;
  72. struct
  73. {
  74. struct Option *input, *field, *cats, *where, *output, *nsites,
  75. *zmin, *zmax, *zcol, *ztype, *seed;
  76. } parm;
  77. struct
  78. {
  79. struct Flag *z, *notopo, *a;
  80. } flag;
  81. int notable;
  82. struct field_info *Fi, *Fi_input;
  83. dbDriver *driver, *driver_input;
  84. dbString sql;
  85. dbTable *table;
  86. dbCatValI *cats_array = NULL;
  87. G_gisinit(argv[0]);
  88. module = G_define_module();
  89. G_add_keyword(_("vector"));
  90. G_add_keyword(_("sampling"));
  91. G_add_keyword(_("statistics"));
  92. G_add_keyword(_("random"));
  93. G_add_keyword(_("point pattern"));
  94. G_add_keyword(_("stratified random sampling"));
  95. G_add_keyword(_("level1"));
  96. module->description = _("Generates random 2D/3D vector points.");
  97. parm.output = G_define_standard_option(G_OPT_V_OUTPUT);
  98. parm.nsites = G_define_option();
  99. parm.nsites->key = "npoints";
  100. parm.nsites->type = TYPE_INTEGER;
  101. parm.nsites->required = YES;
  102. parm.nsites->description = _("Number of points to be created");
  103. parm.input = G_define_standard_option(G_OPT_V_INPUT);
  104. parm.input->key = "restrict";
  105. parm.input->required = NO;
  106. parm.input->description = _("Restrict points to areas in input vector");
  107. parm.input->guisection = _("Selection");
  108. parm.input->guisection = _("Restrict");
  109. parm.field = G_define_standard_option(G_OPT_V_FIELD_ALL);
  110. parm.field->guisection = _("Selection");
  111. parm.cats = G_define_standard_option(G_OPT_V_CATS);
  112. parm.cats->guisection = _("Selection");
  113. parm.where = G_define_standard_option(G_OPT_DB_WHERE);
  114. parm.where->guisection = _("Selection");
  115. parm.zmin = G_define_option();
  116. parm.zmin->key = "zmin";
  117. parm.zmin->type = TYPE_DOUBLE;
  118. parm.zmin->required = NO;
  119. parm.zmin->description =
  120. _("Minimum z height (needs -z flag or column name)");
  121. parm.zmin->answer = "0.0";
  122. parm.zmin->guisection = _("3D output");
  123. parm.zmax = G_define_option();
  124. parm.zmax->key = "zmax";
  125. parm.zmax->type = TYPE_DOUBLE;
  126. parm.zmax->required = NO;
  127. parm.zmax->description =
  128. _("Maximum z height (needs -z flag or column name)");
  129. parm.zmax->answer = "0.0";
  130. parm.zmax->guisection = _("3D output");
  131. parm.seed = G_define_option();
  132. parm.seed->key = "seed";
  133. parm.seed->type = TYPE_INTEGER;
  134. parm.seed->required = NO;
  135. parm.seed->description =
  136. _("The seed to initialize the random generator. If not set the process ID is used");
  137. parm.zcol = G_define_standard_option(G_OPT_DB_COLUMN);
  138. parm.zcol->label = _("Name of column for z values");
  139. parm.zcol->description =
  140. _("Writes z values to column");
  141. parm.zcol->guisection = _("3D output");
  142. parm.ztype = G_define_option();
  143. parm.ztype->key = "column_type";
  144. parm.ztype->type = TYPE_STRING;
  145. parm.ztype->required = NO;
  146. parm.ztype->multiple = NO;
  147. parm.ztype->description = _("Type of column for z values");
  148. parm.ztype->options = "integer,double precision";
  149. parm.ztype->answer = "double precision";
  150. parm.ztype->guisection = _("3D output");
  151. flag.z = G_define_flag();
  152. flag.z->key = 'z';
  153. flag.z->description = _("Create 3D output");
  154. flag.z->guisection = _("3D output");
  155. flag.a = G_define_flag();
  156. flag.a->key = 'a';
  157. flag.a->description = _("Generate n points for each individual area (requires restrict parameter)");
  158. flag.a->guisection = _("Restrict");
  159. flag.notopo = G_define_standard_flag(G_FLG_V_TOPO);
  160. G_option_requires(flag.a, parm.input, NULL);
  161. if (G_parser(argc, argv))
  162. exit(EXIT_FAILURE);
  163. output = parm.output->answer;
  164. n = strtoul(parm.nsites->answer, NULL, 10);
  165. if(parm.seed->answer)
  166. seed = atoi(parm.seed->answer);
  167. if (n <= 0) {
  168. G_fatal_error(_("Number of points must be > 0 (%ld given)"), n);
  169. }
  170. nareas = 0;
  171. cat_list = NULL;
  172. field = -1;
  173. if (parm.input->answer) {
  174. Vect_set_open_level(2); /* topology required */
  175. if (2 > Vect_open_old2(&In, parm.input->answer, "", parm.field->answer))
  176. G_fatal_error(_("Unable to open vector map <%s>"),
  177. parm.input->answer);
  178. if (parm.field->answer)
  179. field = Vect_get_field_number(&In, parm.field->answer);
  180. if ((parm.cats->answer || parm.where->answer) && field == -1) {
  181. G_warning(_("Invalid layer number (%d). Parameter '%s' or '%s' specified, assuming layer '1'."),
  182. field, parm.cats->key, parm.where->key);
  183. field = 1;
  184. }
  185. if (field > 0)
  186. cat_list = Vect_cats_set_constraint(&In, field, parm.where->answer,
  187. parm.cats->answer);
  188. nareas = Vect_get_num_areas(&In);
  189. if (nareas == 0) {
  190. Vect_close(&In);
  191. G_fatal_error(_("No areas in vector map <%s>"), parm.input->answer);
  192. }
  193. }
  194. /* create new vector map */
  195. if (-1 == Vect_open_new(&Out, output, flag.z->answer ? WITH_Z : WITHOUT_Z))
  196. G_fatal_error(_("Unable to create vector map <%s>"), output);
  197. Vect_set_error_handler_io(NULL, &Out);
  198. /* Do we need to write random values into attribute table? */
  199. usefloat = -1;
  200. notable = !(parm.zcol->answer || (parm.input -> answer && field > 0));
  201. if (!notable) {
  202. Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
  203. driver =
  204. db_start_driver_open_database(Fi->driver,
  205. Vect_subst_var(Fi->database, &Out));
  206. if (driver == NULL) {
  207. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  208. Vect_subst_var(Fi->database, &Out), Fi->driver);
  209. }
  210. db_set_error_handler_driver(driver);
  211. db_begin_transaction(driver);
  212. db_init_string(&sql);
  213. /* Create table */
  214. sprintf(buf, "create table %s (%s integer", Fi->table, GV_KEY_COLUMN);
  215. db_set_string(&sql, buf);
  216. if (parm.zcol->answer) {
  217. sprintf(buf, ", %s %s", parm.zcol->answer, parm.ztype->answer);
  218. db_append_string(&sql, buf);
  219. }
  220. if (parm.input->answer && field > 0) {
  221. dbString table_name;
  222. dbColumn *col;
  223. Fi_input = Vect_get_field2(&In, parm.field->answer);
  224. if (Fi_input == NULL)
  225. G_fatal_error(_("Database connection not defined for layer <%s>"),
  226. parm.field->answer);
  227. driver_input = db_start_driver_open_database(
  228. Fi_input->driver,
  229. Vect_subst_var(Fi_input->database, &In));
  230. if (driver_input == NULL) {
  231. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  232. Vect_subst_var(Fi_input->database, &In), Fi_input->driver);
  233. }
  234. db_set_error_handler_driver(driver_input);
  235. db_init_string(&table_name);
  236. db_set_string(&table_name, Fi_input->table);
  237. if (db_describe_table(driver_input, &table_name, &table) != DB_OK)
  238. G_fatal_error(_("Unable to describe table <%s>"),
  239. Fi_input->table);
  240. ncols = db_get_table_number_of_columns(table);
  241. for (icol = 0; icol < ncols; icol++) {
  242. col = db_get_table_column(table, icol);
  243. sprintf(buf, ",%s_%s %s", parm.input->answer,
  244. db_get_column_name(col),
  245. db_sqltype_name(db_get_column_sqltype(col)));
  246. db_append_string(&sql, buf);
  247. }
  248. }
  249. db_append_string(&sql, ")");
  250. if (db_execute_immediate(driver, &sql) != DB_OK) {
  251. G_fatal_error(_("Unable to create table: %s"),
  252. db_get_string(&sql));
  253. }
  254. /* Create index */
  255. if (db_create_index2(driver, Fi->table, Fi->key) != DB_OK)
  256. G_warning(_("Unable to create index"));
  257. /* Grant */
  258. if (db_grant_on_table
  259. (driver, Fi->table, DB_PRIV_SELECT,
  260. DB_GROUP | DB_PUBLIC) != DB_OK) {
  261. G_fatal_error(_("Unable to grant privileges on table <%s>"),
  262. Fi->table);
  263. }
  264. /* OK. Let's check what type of column user has created */
  265. db_set_string(&sql, Fi->table);
  266. if (db_describe_table(driver, &sql, &table) != DB_OK) {
  267. G_fatal_error(_("Unable to describe table <%s>"), Fi->table);
  268. }
  269. if (parm.zcol->answer) {
  270. type = db_get_column_sqltype(db_get_table_column(table, 1));
  271. if (type == DB_SQL_TYPE_SMALLINT || type == DB_SQL_TYPE_INTEGER)
  272. usefloat = 0;
  273. if (type == DB_SQL_TYPE_REAL || type == DB_SQL_TYPE_DOUBLE_PRECISION)
  274. usefloat = 1;
  275. if (usefloat < 0) {
  276. G_fatal_error(_("You have created unsupported column type. This module supports only INTEGER"
  277. " and DOUBLE PRECISION column types."));
  278. }
  279. }
  280. Vect_map_add_dblink(&Out, 1, NULL, Fi->table, GV_KEY_COLUMN, Fi->database,
  281. Fi->driver);
  282. }
  283. Vect_hist_command(&Out);
  284. /* Init the random seed */
  285. if(parm.seed->answer)
  286. G_srand48(seed);
  287. else
  288. G_srand48_auto();
  289. G_get_window(&window);
  290. Points = Vect_new_line_struct();
  291. Cats = Vect_new_cats_struct();
  292. if (nareas > 0) {
  293. int first = 1, count;
  294. struct bound_box abox, bbox;
  295. box.W = window.west;
  296. box.E = window.east;
  297. box.S = window.south;
  298. box.N = window.north;
  299. box.B = -PORT_DOUBLE_MAX;
  300. box.T = PORT_DOUBLE_MAX;
  301. count = 0;
  302. for (i = 1; i <= nareas; i++) {
  303. if (!Vect_get_area_centroid(&In, i))
  304. continue;
  305. if (field > 0) {
  306. if (Vect_get_area_cats(&In, i, Cats))
  307. continue;
  308. if (!Vect_cats_in_constraint(Cats, field, cat_list))
  309. continue;
  310. }
  311. Vect_get_area_box(&In, i, &abox);
  312. if (!Vect_box_overlap(&abox, &box))
  313. continue;
  314. if (first) {
  315. Vect_box_copy(&bbox, &abox);
  316. first = 0;
  317. }
  318. else
  319. Vect_box_extend(&bbox, &abox);
  320. count++;
  321. }
  322. if (count == 0) {
  323. Vect_close(&In);
  324. Vect_close(&Out);
  325. Vect_delete(output);
  326. G_fatal_error(_("Selected areas in input vector <%s> do not overlap with the current region"),
  327. parm.input->answer);
  328. }
  329. Vect_box_copy(&box, &bbox);
  330. /* does the vector overlap with the current region ? */
  331. if (box.W >= window.east || box.E <= window.west ||
  332. box.S >= window.north || box.N <= window.south) {
  333. Vect_close(&In);
  334. Vect_close(&Out);
  335. Vect_delete(output);
  336. G_fatal_error(_("Input vector <%s> does not overlap with the current region"),
  337. parm.input->answer);
  338. }
  339. /* try to reduce the current region */
  340. if (window.east > box.E)
  341. window.east = box.E;
  342. if (window.west < box.W)
  343. window.west = box.W;
  344. if (window.north > box.N)
  345. window.north = box.N;
  346. if (window.south < box.S)
  347. window.south = box.S;
  348. List = Vect_new_boxlist(1);
  349. alloc_size_list = 10;
  350. size_list = G_malloc(alloc_size_list * sizeof(BOX_SIZE));
  351. }
  352. zmin = zmax = 0;
  353. if (flag.z->answer || parm.zcol->answer) {
  354. zmax = atof(parm.zmax->answer);
  355. zmin = atof(parm.zmin->answer);
  356. }
  357. G_message(_("Generating points..."));
  358. if (flag.a->answer && nareas > 0) {
  359. struct bound_box abox, bbox;
  360. cat = 1;
  361. /* n points for each area */
  362. nareas = Vect_get_num_areas(&In);
  363. /* init cat/cat_area array */
  364. total_n = n * nareas;
  365. cats_array = G_malloc(total_n * sizeof(dbCatValI));
  366. G_percent(0, nareas, 1);
  367. for (area = 1; area <= nareas; area++) {
  368. G_percent(area, nareas, 1);
  369. if (!Vect_get_area_centroid(&In, area))
  370. continue;
  371. if (field > 0) {
  372. if (Vect_get_area_cats(&In, area, Cats))
  373. continue;
  374. if (!Vect_cats_in_constraint(Cats, field, cat_list)) {
  375. continue;
  376. }
  377. }
  378. box.W = window.west;
  379. box.E = window.east;
  380. box.S = window.south;
  381. box.N = window.north;
  382. box.B = -PORT_DOUBLE_MAX;
  383. box.T = PORT_DOUBLE_MAX;
  384. Vect_get_area_box(&In, area, &abox);
  385. if (!Vect_box_overlap(&box, &abox))
  386. continue;
  387. bbox = abox;
  388. if (bbox.W < box.W)
  389. bbox.W = box.W;
  390. if (bbox.E > box.E)
  391. bbox.E = box.E;
  392. if (bbox.S < box.S)
  393. bbox.S = box.S;
  394. if (bbox.N > box.N)
  395. bbox.N = box.N;
  396. if (field > 0)
  397. Vect_cat_get(Cats, field, &cat_area);
  398. for (i = 0; i < n; ++i) {
  399. double x, y, z;
  400. int outside = 1;
  401. int ret;
  402. if (field > 0) {
  403. cats_array[cat-1].cat = cat;
  404. cats_array[cat-1].val = cat_area;
  405. }
  406. Vect_reset_line(Points);
  407. Vect_reset_cats(Cats);
  408. while (outside) {
  409. x = rng() * (bbox.W - bbox.E) + bbox.E;
  410. y = rng() * (bbox.N - bbox.S) + bbox.S;
  411. z = rng() * (zmax - zmin) + zmin;
  412. ret = Vect_point_in_area(x, y, &In, area, &abox);
  413. G_debug(3, " area = %d Vect_point_in_area() = %d", area, ret);
  414. if (ret >= 1) {
  415. outside = 0;
  416. }
  417. }
  418. if (flag.z->answer)
  419. Vect_append_point(Points, x, y, z);
  420. else
  421. Vect_append_point(Points, x, y, 0.0);
  422. if (!notable) {
  423. sprintf(buf, "insert into %s (%s", Fi->table, Fi->key);
  424. db_set_string(&sql, buf);
  425. if (parm.zcol->answer) {
  426. sprintf(buf, ", %s", parm.zcol->answer);
  427. db_append_string(&sql, buf);
  428. }
  429. sprintf(buf, ") values ( %d", cat);
  430. db_append_string(&sql, buf);
  431. if (parm.zcol->answer) {
  432. /* Round random value if column is integer type */
  433. if (usefloat)
  434. sprintf(buf, ", %f", z);
  435. else
  436. sprintf(buf, ", %.0f", z);
  437. db_append_string(&sql, buf);
  438. }
  439. db_append_string(&sql, ")");
  440. G_debug(3, "%s", db_get_string(&sql));
  441. if (db_execute_immediate(driver, &sql) != DB_OK) {
  442. G_fatal_error(_("Unable to insert new row: %s"),
  443. db_get_string(&sql));
  444. }
  445. }
  446. Vect_cat_set(Cats, 1, cat++);
  447. Vect_write_line(&Out, GV_POINT, Points, Cats);
  448. }
  449. }
  450. }
  451. else {
  452. total_n = n;
  453. if (parm.input->answer && field > 0)
  454. cats_array = G_malloc(n * sizeof(dbCatValI));
  455. /* n points in total */
  456. for (i = 0; i < n; ++i) {
  457. double x, y, z;
  458. G_percent(i, n, 4);
  459. Vect_reset_line(Points);
  460. Vect_reset_cats(Cats);
  461. x = rng() * (window.west - window.east) + window.east;
  462. y = rng() * (window.north - window.south) + window.south;
  463. z = rng() * (zmax - zmin) + zmin;
  464. if (nareas) {
  465. int outside = 1;
  466. do {
  467. /* select areas by box */
  468. box.E = x;
  469. box.W = x;
  470. box.N = y;
  471. box.S = y;
  472. box.T = PORT_DOUBLE_MAX;
  473. box.B = -PORT_DOUBLE_MAX;
  474. Vect_select_areas_by_box(&In, &box, List);
  475. G_debug(3, " %d areas selected by box", List->n_values);
  476. /* sort areas by size, the smallest is likely to be the nearest */
  477. if (alloc_size_list < List->n_values) {
  478. alloc_size_list = List->n_values;
  479. size_list = G_realloc(size_list, alloc_size_list * sizeof(BOX_SIZE));
  480. }
  481. k = 0;
  482. for (j = 0; j < List->n_values; j++) {
  483. area = List->id[j];
  484. if (!Vect_get_area_centroid(&In, area))
  485. continue;
  486. if (field > 0) {
  487. if (Vect_get_area_cats(&In, area, Cats))
  488. continue;
  489. if (!Vect_cats_in_constraint(Cats, field, cat_list)) {
  490. continue;
  491. }
  492. }
  493. List->id[k] = List->id[j];
  494. List->box[k] = List->box[j];
  495. size_list[k].i = List->id[k];
  496. box = List->box[k];
  497. size_list[k].box = List->box[k];
  498. size_list[k].size = (box.N - box.S) * (box.E - box.W);
  499. k++;
  500. }
  501. List->n_values = k;
  502. if (List->n_values == 2) {
  503. /* simple swap */
  504. if (size_list[1].size < size_list[0].size) {
  505. size_list[0].i = List->id[1];
  506. size_list[1].i = List->id[0];
  507. size_list[0].box = List->box[1];
  508. size_list[1].box = List->box[0];
  509. }
  510. }
  511. else if (List->n_values > 2)
  512. qsort(size_list, List->n_values, sizeof(BOX_SIZE), sort_by_size);
  513. for (j = 0; j < List->n_values; j++) {
  514. int ret;
  515. area = size_list[j].i;
  516. ret = Vect_point_in_area(x, y, &In, area, &size_list[j].box);
  517. G_debug(3, " area = %d Vect_point_in_area() = %d", area, ret);
  518. if (ret >= 1) {
  519. if (field > 0) {
  520. /* read categories for matched area */
  521. Vect_get_area_cats(&In, area, Cats);
  522. }
  523. outside = 0;
  524. break;
  525. }
  526. }
  527. if (outside) {
  528. x = rng() * (window.west - window.east) + window.east;
  529. y = rng() * (window.north - window.south) + window.south;
  530. z = rng() * (zmax - zmin) + zmin;
  531. }
  532. } while (outside);
  533. }
  534. if (flag.z->answer)
  535. Vect_append_point(Points, x, y, z);
  536. else
  537. Vect_append_point(Points, x, y, 0.0);
  538. cat = i + 1;
  539. if (!notable) {
  540. if (parm.input->answer) {
  541. Vect_cat_get(Cats, field, &cat_area);
  542. cats_array[i].cat = cat;
  543. cats_array[i].val = cat_area;
  544. }
  545. sprintf(buf, "insert into %s (%s", Fi->table, Fi->key);
  546. db_set_string(&sql, buf);
  547. if (parm.zcol->answer) {
  548. sprintf(buf, ", %s", parm.zcol->answer);
  549. db_append_string(&sql, buf);
  550. }
  551. sprintf(buf, ") values ( %ld", i + 1);
  552. db_append_string(&sql, buf);
  553. if (parm.zcol->answer) {
  554. /* Round random value if column is integer type */
  555. if (usefloat)
  556. sprintf(buf, ", %f", z);
  557. else
  558. sprintf(buf, ", %.0f", z);
  559. db_append_string(&sql, buf);
  560. }
  561. db_append_string(&sql, ")");
  562. G_debug(3, "%s", db_get_string(&sql));
  563. if (db_execute_immediate(driver, &sql) != DB_OK) {
  564. G_fatal_error(_("Unable to insert new row: %s"),
  565. db_get_string(&sql));
  566. }
  567. }
  568. Vect_cat_set(Cats, 1, cat);
  569. Vect_write_line(&Out, GV_POINT, Points, Cats);
  570. }
  571. G_percent(1, 1, 1);
  572. }
  573. if (parm.input->answer && field > 0) {
  574. int more, ctype;
  575. const char *column_name;
  576. dbColumn *column;
  577. dbValue *value;
  578. dbString value_str, update_str;
  579. dbCursor cursor;
  580. db_init_string(&value_str);
  581. db_init_string(&update_str);
  582. sprintf(buf, "select * from %s", Fi_input->table);
  583. db_set_string(&sql, buf);
  584. if (db_open_select_cursor(driver_input, &sql,
  585. &cursor, DB_SEQUENTIAL) != DB_OK)
  586. G_fatal_error(_("Unable to open select cursor"));
  587. table = db_get_cursor_table(&cursor);
  588. while (TRUE) {
  589. if (db_fetch(&cursor, DB_NEXT, &more) != DB_OK)
  590. G_fatal_error(_("Unable to fetch data from table <%s>"),
  591. Fi_input->table);
  592. if (!more) {
  593. break;
  594. }
  595. sprintf(buf, "update %s set ", Fi->table);
  596. db_set_string(&update_str, buf);
  597. for (icol = 0; icol < ncols; icol++) {
  598. column = db_get_table_column(table, icol);
  599. column_name = db_get_column_name(column);
  600. value = db_get_column_value(column);
  601. if (strcmp(column_name, Fi_input->key) == 0)
  602. cat_area = db_get_value_int(value);
  603. if (icol > 0)
  604. db_append_string(&update_str, ", ");
  605. sprintf(buf, "%s_%s = ", parm.input->answer, column_name);
  606. db_append_string(&update_str, buf);
  607. ctype = db_sqltype_to_Ctype(db_get_column_sqltype(column));
  608. db_convert_value_to_string(value, ctype, &value_str);
  609. if (ctype == DB_C_TYPE_INT || ctype == DB_C_TYPE_DOUBLE)
  610. sprintf(buf, "%s", db_get_string(&value_str));
  611. else
  612. sprintf(buf, "'%s'", db_get_string(&value_str));
  613. db_append_string(&update_str, buf);
  614. }
  615. for (i = 0; i < total_n; i++) {
  616. if (cat_area == cats_array[i].val) {
  617. db_copy_string(&sql, &update_str);
  618. sprintf(buf, " where %s = %d", Fi->key, cats_array[i].cat);
  619. db_append_string(&sql, buf);
  620. G_debug(3, "%s", db_get_string(&sql));
  621. if (db_execute_immediate(driver, &sql) != DB_OK) {
  622. G_fatal_error(_("Unable to update row: %s"),
  623. db_get_string(&sql));
  624. }
  625. }
  626. }
  627. }
  628. G_free(cats_array);
  629. }
  630. if (!notable) {
  631. db_commit_transaction(driver);
  632. db_close_database_shutdown_driver(driver);
  633. }
  634. if (!flag.notopo->answer) {
  635. Vect_build(&Out);
  636. }
  637. Vect_close(&Out);
  638. exit(EXIT_SUCCESS);
  639. }