print.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/dbmi.h>
  5. #include <grass/glocale.h>
  6. #include "local_proto.h"
  7. #define printline(x) fprintf (stdout, " | %-74.74s |\n", x)
  8. #define divider(x) \
  9. fprintf (stdout, " %c", x); \
  10. for (i = 0; i < 76; i++ ) \
  11. fprintf ( stdout, "-" ); \
  12. fprintf (stdout, "%c\n", x)
  13. /* cloned from lib/gis/wind_format.c */
  14. void format_double(double value, char *buf)
  15. {
  16. sprintf(buf, "%.8f", value);
  17. G_trim_decimal(buf);
  18. }
  19. void print_region(const struct Map_info *Map)
  20. {
  21. char tmp1[100], tmp2[100];
  22. struct bound_box box;
  23. /*Print the spatial extent as double values*/
  24. Vect_get_map_box(Map, &box);
  25. G_format_northing(box.N, tmp1, -1);
  26. G_format_northing(box.S, tmp2, -1);
  27. fprintf(stdout, "north=%s\n", tmp1);
  28. fprintf(stdout, "south=%s\n", tmp2);
  29. G_format_easting(box.E, tmp1, -1);
  30. G_format_easting(box.W, tmp2, -1);
  31. fprintf(stdout, "east=%s\n", tmp1);
  32. fprintf(stdout, "west=%s\n", tmp2);
  33. fprintf(stdout, "top=%f\n", box.T);
  34. fprintf(stdout, "bottom=%f\n", box.B);
  35. }
  36. void print_topo(const struct Map_info *Map)
  37. {
  38. int with_z;
  39. long nprimitives;
  40. nprimitives = 0;
  41. with_z = Vect_is_3d(Map);
  42. nprimitives += Vect_get_num_primitives(Map, GV_POINT);
  43. nprimitives += Vect_get_num_primitives(Map, GV_LINE);
  44. nprimitives += Vect_get_num_primitives(Map, GV_BOUNDARY);
  45. nprimitives += Vect_get_num_primitives(Map, GV_CENTROID);
  46. if (with_z) {
  47. nprimitives += Vect_get_num_primitives(Map, GV_FACE);
  48. nprimitives += Vect_get_num_primitives(Map, GV_KERNEL);
  49. }
  50. fprintf(stdout, "nodes=%d\n",
  51. Vect_get_num_nodes(Map));
  52. fflush(stdout);
  53. fprintf(stdout, "points=%d\n",
  54. Vect_get_num_primitives(Map, GV_POINT));
  55. fflush(stdout);
  56. fprintf(stdout, "lines=%d\n",
  57. Vect_get_num_primitives(Map, GV_LINE));
  58. fflush(stdout);
  59. fprintf(stdout, "boundaries=%d\n",
  60. Vect_get_num_primitives(Map, GV_BOUNDARY));
  61. fflush(stdout);
  62. fprintf(stdout, "centroids=%d\n",
  63. Vect_get_num_primitives(Map, GV_CENTROID));
  64. fflush(stdout);
  65. fprintf(stdout, "areas=%d\n", Vect_get_num_areas(Map));
  66. fflush(stdout);
  67. fprintf(stdout, "islands=%d\n",
  68. Vect_get_num_islands(Map));
  69. fflush(stdout);
  70. if (with_z) {
  71. fprintf(stdout, "faces=%d\n",
  72. Vect_get_num_primitives(Map, GV_FACE));
  73. fflush(stdout);
  74. fprintf(stdout, "kernels=%d\n",
  75. Vect_get_num_primitives(Map, GV_KERNEL));
  76. fflush(stdout);
  77. fprintf(stdout, "volumes=%d\n",
  78. Vect_get_num_primitives(Map, GV_VOLUME));
  79. fflush(stdout);
  80. fprintf(stdout, "holes=%d\n",
  81. Vect_get_num_holes(Map));
  82. fflush(stdout);
  83. }
  84. fprintf(stdout, "primitives=%ld\n", nprimitives);
  85. fflush(stdout);
  86. fprintf(stdout, "map3d=%d\n",
  87. Vect_is_3d(Map) ? 1 : 0);
  88. fflush(stdout);
  89. }
  90. void print_columns(const struct Map_info *Map, const char *input_opt, const char *field_opt)
  91. {
  92. int num_dblinks, col, ncols;
  93. struct field_info *fi;
  94. dbDriver *driver = NULL;
  95. dbHandle handle;
  96. dbString table_name;
  97. dbTable *table;
  98. num_dblinks = Vect_get_num_dblinks(Map);
  99. if (num_dblinks <= 0) {
  100. G_fatal_error(_("Database connection for map <%s> is not defined in DB file"),
  101. input_opt);
  102. }
  103. G_message(_("Displaying column types/names for database connection of layer <%s>:"),
  104. field_opt);
  105. if ((fi = Vect_get_field2(Map, field_opt)) == NULL)
  106. G_fatal_error(_("Database connection not defined for layer <%s>"),
  107. field_opt);
  108. driver = db_start_driver(fi->driver);
  109. if (driver == NULL)
  110. G_fatal_error(_("Unable to open driver <%s>"),
  111. fi->driver);
  112. db_init_handle(&handle);
  113. db_set_handle(&handle, fi->database, NULL);
  114. if (db_open_database(driver, &handle) != DB_OK)
  115. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  116. fi->database, fi->driver);
  117. db_init_string(&table_name);
  118. db_set_string(&table_name, fi->table);
  119. if (db_describe_table(driver, &table_name, &table) != DB_OK)
  120. G_fatal_error(_("Unable to describe table <%s>"),
  121. fi->table);
  122. ncols = db_get_table_number_of_columns(table);
  123. for (col = 0; col < ncols; col++)
  124. fprintf(stdout, "%s|%s\n",
  125. db_sqltype_name(db_get_column_sqltype
  126. (db_get_table_column
  127. (table, col))),
  128. db_get_column_name(db_get_table_column
  129. (table, col)));
  130. db_close_database(driver);
  131. db_shutdown_driver(driver);
  132. }
  133. void print_shell(const struct Map_info *Map)
  134. {
  135. int map_type;
  136. map_type = Vect_maptype(Map);
  137. if (map_type == GV_FORMAT_OGR ||
  138. map_type == GV_FORMAT_OGR_DIRECT) {
  139. fprintf(stdout, "ogr_layer=%s\n",
  140. Vect_get_ogr_layer_name(Map));
  141. fprintf(stdout, "ogr_dsn=%s\n",
  142. Vect_get_ogr_dsn_name(Map));
  143. }
  144. else {
  145. fprintf(stdout, "name=%s\n",
  146. Vect_get_name(Map));
  147. fprintf(stdout, "mapset=%s\n",
  148. Vect_get_mapset(Map));
  149. }
  150. fprintf(stdout, "location=%s\n",
  151. G_location());
  152. fprintf(stdout, "database=%s\n",
  153. G_gisdbase());
  154. fprintf(stdout, "title=%s\n",
  155. Vect_get_map_name(Map));
  156. fprintf(stdout, "scale=1:%d\n",
  157. Vect_get_scale(Map));
  158. if (map_type == GV_FORMAT_OGR ||
  159. map_type == GV_FORMAT_OGR_DIRECT) {
  160. fprintf(stdout, "format=%s,%s\n",
  161. Vect_maptype_info(Map), Vect_get_ogr_format_info(Map));
  162. }
  163. else {
  164. fprintf(stdout, "format=%s\n",
  165. Vect_maptype_info(Map));
  166. }
  167. fprintf(stdout, "creator=%s\n",
  168. Vect_get_person(Map));
  169. fprintf(stdout, "organization=%s\n",
  170. Vect_get_organization(Map));
  171. fprintf(stdout, "source_date=%s\n",
  172. Vect_get_map_date(Map));
  173. fprintf(stdout, "level=%d\n",
  174. Vect_level(Map));
  175. if (Vect_level(Map) > 0) {
  176. fprintf(stdout, "num_dblinks=%d\n",
  177. Vect_get_num_dblinks(Map));
  178. }
  179. fprintf(stdout, "projection=%s\n",
  180. Vect_get_proj_name(Map));
  181. if (G_projection() == PROJECTION_UTM) {
  182. fprintf(stdout, "zone=%d\n",
  183. Vect_get_zone(Map));
  184. }
  185. fprintf(stdout, "digitization_threshold=%f\n",
  186. Vect_get_thresh(Map));
  187. fprintf(stdout, "comment=%s\n",
  188. Vect_get_comment(Map));
  189. }
  190. void print_info(const struct Map_info *Map)
  191. {
  192. int i, map_type;
  193. char line[100];
  194. char tmp1[100], tmp2[100];
  195. char timebuff[256];
  196. struct TimeStamp ts;
  197. int time_ok = FALSE, first_time_ok = FALSE, second_time_ok = FALSE;
  198. struct bound_box box;
  199. int utm_zone = -1;
  200. map_type = Vect_maptype(Map);
  201. /* Check the Timestamp */
  202. time_ok = G_read_vector_timestamp(Vect_get_name(Map), NULL, "", &ts);
  203. /* Check for valid entries, show none if no timestamp available */
  204. if (time_ok == TRUE) {
  205. if (ts.count > 0)
  206. first_time_ok = TRUE;
  207. if (ts.count > 1)
  208. second_time_ok = TRUE;
  209. }
  210. divider('+');
  211. if (map_type == GV_FORMAT_OGR ||
  212. map_type == GV_FORMAT_OGR_DIRECT) {
  213. /* for OGR format print also datasource and layer */
  214. sprintf(line, "%-17s%s", _("OGR layer:"),
  215. Vect_get_ogr_layer_name(Map));
  216. printline(line);
  217. sprintf(line, "%-17s%s", _("OGR datasource:"),
  218. Vect_get_ogr_dsn_name(Map));
  219. printline(line);
  220. }
  221. else {
  222. sprintf(line, "%-17s%s", _("Name:"),
  223. Vect_get_name(Map));
  224. printline(line);
  225. sprintf(line, "%-17s%s", _("Mapset:"),
  226. Vect_get_mapset(Map));
  227. printline(line);
  228. }
  229. sprintf(line, "%-17s%s", _("Location:"),
  230. G_location());
  231. printline(line);
  232. sprintf(line, "%-17s%s", _("Database:"),
  233. G_gisdbase());
  234. printline(line);
  235. sprintf(line, "%-17s%s", _("Title:"),
  236. Vect_get_map_name(Map));
  237. printline(line);
  238. sprintf(line, "%-17s1:%d", _("Map scale:"),
  239. Vect_get_scale(Map));
  240. printline(line);
  241. if (map_type == GV_FORMAT_OGR ||
  242. map_type == GV_FORMAT_OGR_DIRECT) {
  243. sprintf(line, "%-17s%s (%s)", _("Map format:"),
  244. Vect_maptype_info(Map), Vect_get_ogr_format_info(Map));
  245. }
  246. else {
  247. sprintf(line, "%-17s%s", _("Map format:"),
  248. Vect_maptype_info(Map));
  249. }
  250. printline(line);
  251. sprintf(line, "%-17s%s", _("Name of creator:"),
  252. Vect_get_person(Map));
  253. printline(line);
  254. sprintf(line, "%-17s%s", _("Organization:"),
  255. Vect_get_organization(Map));
  256. printline(line);
  257. sprintf(line, "%-17s%s", _("Source date:"),
  258. Vect_get_map_date(Map));
  259. printline(line);
  260. /* This shows the TimeStamp (if present) */
  261. if (time_ok == TRUE && (first_time_ok || second_time_ok)) {
  262. G_format_timestamp(&ts, timebuff);
  263. sprintf(line, "%-17s%s", _("Timestamp (first layer): "), timebuff);
  264. printline(line);
  265. }
  266. /* else
  267. strcpy(line, _("Timestamp (first layer): none"));
  268. printline(line); */
  269. divider('|');
  270. sprintf(line, " %s: %s (%s: %i)",
  271. _("Type of map"), _("vector"), _("level"), Vect_level(Map));
  272. printline(line);
  273. if (Vect_level(Map) > 0) {
  274. printline("");
  275. sprintf(line,
  276. " %-24s%-9d %-22s%-9d",
  277. _("Number of points:"),
  278. Vect_get_num_primitives(Map, GV_POINT),
  279. _("Number of centroids:"),
  280. Vect_get_num_primitives(Map, GV_CENTROID));
  281. printline(line);
  282. sprintf(line,
  283. " %-24s%-9d %-22s%-9d",
  284. _("Number of lines:"),
  285. Vect_get_num_primitives(Map, GV_LINE),
  286. _("Number of boundaries:"),
  287. Vect_get_num_primitives(Map, GV_BOUNDARY));
  288. printline(line);
  289. sprintf(line,
  290. " %-24s%-9d %-22s%-9d",
  291. _("Number of areas:"),
  292. Vect_get_num_areas(Map),
  293. _("Number of islands:"),
  294. Vect_get_num_islands(Map));
  295. printline(line);
  296. if (Vect_is_3d(Map)) {
  297. sprintf(line,
  298. " %-24s%-9d %-22s%-9d",
  299. _("Number of faces:"),
  300. Vect_get_num_primitives(Map, GV_FACE),
  301. _("Number of kernels:"),
  302. Vect_get_num_primitives(Map, GV_KERNEL));
  303. printline(line);
  304. sprintf(line,
  305. " %-24s%-9d %-22s%-9d",
  306. _("Number of volumes:"),
  307. Vect_get_num_volumes(Map),
  308. _("Number of holes:"),
  309. Vect_get_num_holes(Map));
  310. printline(line);
  311. }
  312. printline("");
  313. sprintf(line, " %-24s%s",
  314. _("Map is 3D:"),
  315. Vect_is_3d(Map) ? _("Yes") : _("No"));
  316. printline(line);
  317. sprintf(line, " %-24s%-9d",
  318. _("Number of dblinks:"),
  319. Vect_get_num_dblinks(Map));
  320. printline(line);
  321. }
  322. printline("");
  323. /* this differs from r.info in that proj info IS taken from the map here, not the location settings */
  324. /* Vect_get_proj_name() and _zone() are typically unset?! */
  325. if (G_projection() == PROJECTION_UTM) {
  326. utm_zone = Vect_get_zone(Map);
  327. if (utm_zone < 0 || utm_zone > 60)
  328. strcpy(tmp1, _("invalid"));
  329. else if (utm_zone == 0)
  330. strcpy(tmp1, _("unspecified"));
  331. else
  332. sprintf(tmp1, "%d", utm_zone);
  333. sprintf(line, " %s: %s (%s %s)",
  334. _("Projection"), Vect_get_proj_name(Map),
  335. _("zone"), tmp1);
  336. }
  337. else
  338. sprintf(line, " %s: %s",
  339. _("Projection"), Vect_get_proj_name(Map));
  340. printline(line);
  341. printline("");
  342. Vect_get_map_box(Map, &box);
  343. G_format_northing(box.N, tmp1, G_projection());
  344. G_format_northing(box.S, tmp2, G_projection());
  345. sprintf(line, " %c: %17s %c: %17s",
  346. 'N', tmp1, 'S', tmp2);
  347. printline(line);
  348. G_format_easting(box.E, tmp1, G_projection());
  349. G_format_easting(box.W, tmp2, G_projection());
  350. sprintf(line, " %c: %17s %c: %17s",
  351. 'E', tmp1, 'W', tmp2);
  352. printline(line);
  353. if (Vect_is_3d(Map)) {
  354. format_double(box.B, tmp1);
  355. format_double(box.T, tmp2);
  356. sprintf(line, " %c: %17s %c: %17s",
  357. 'B', tmp1, 'T', tmp2);
  358. printline(line);
  359. }
  360. printline("");
  361. format_double(Vect_get_thresh(Map), tmp1);
  362. sprintf(line, " %s: %s", _("Digitization threshold"), tmp1);
  363. printline(line);
  364. sprintf(line, " %s:", _("Comment"));
  365. printline(line);
  366. sprintf(line, " %s", Vect_get_comment(Map));
  367. printline(line);
  368. divider('+');
  369. fprintf(stdout, "\n");
  370. }