header.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*!
  2. \file lib/vector/Vlib/header.c
  3. \brief Vector library - header manipulation
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2010 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author Update to GRASS 7 (OGR support) by Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <grass/vector.h>
  15. #include <grass/glocale.h>
  16. #include "local_proto.h"
  17. /*!
  18. \brief Print vector map header to stdout
  19. \param Map pointer to Map_info structure
  20. \return 0 on success
  21. */
  22. int Vect_print_header(const struct Map_info *Map)
  23. {
  24. fprintf(stdout, "\nSelected information from dig header\n");
  25. fprintf(stdout, " Organization: %s\n", Vect_get_organization(Map));
  26. fprintf(stdout, " Map Name: %s\n", Vect_get_map_name(Map));
  27. fprintf(stdout, " Source Date: %s\n", Vect_get_map_date(Map));
  28. fprintf(stdout, " Orig. Scale: %d\n", Vect_get_scale(Map));
  29. return 0;
  30. }
  31. /*!
  32. \brief Read vector map header from map head file
  33. \param Map pointrt to Map_info structure
  34. \return 0
  35. */
  36. int Vect_read_header(struct Map_info *Map)
  37. {
  38. Vect__read_head(Map);
  39. return 0;
  40. }
  41. /*!
  42. \brief Write vector map header to map head file
  43. \param Map pointer to Map_info structure
  44. \return 0
  45. */
  46. int Vect_write_header(const struct Map_info *Map)
  47. {
  48. /* do some sanity checking here */
  49. Vect__write_head(Map);
  50. return 0;
  51. }
  52. /*!
  53. \brief Writes head information to text file (GV_HEAD_ELEMENT)
  54. \param Map pointer to Map_info structure
  55. \return 0 on success
  56. \return -1 on error
  57. */
  58. int Vect__write_head(const struct Map_info *Map)
  59. {
  60. char *path;
  61. FILE *head_fp;
  62. path = Vect__get_path(Map);
  63. head_fp = G_fopen_new(path, GV_HEAD_ELEMENT);
  64. G_free(path);
  65. if (head_fp == NULL) {
  66. G_warning(_("Unable to create header file for vector map <%s>"),
  67. Vect_get_full_name(Map));
  68. return -1;
  69. }
  70. fprintf(head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map));
  71. fprintf(head_fp, "DIGIT DATE: %s\n", Vect_get_date(Map));
  72. fprintf(head_fp, "DIGIT NAME: %s\n", Vect_get_person(Map));
  73. fprintf(head_fp, "MAP NAME: %s\n", Vect_get_map_name(Map));
  74. fprintf(head_fp, "MAP DATE: %s\n", Vect_get_map_date(Map));
  75. fprintf(head_fp, "MAP SCALE: %d\n", Vect_get_scale(Map));
  76. fprintf(head_fp, "OTHER INFO: %s\n", Vect_get_comment(Map));
  77. if (Vect_get_proj(Map) > 0)
  78. fprintf(head_fp, "PROJ: %d\n", Vect_get_proj(Map));
  79. fprintf(head_fp, "ZONE: %d\n", Vect_get_zone(Map));
  80. fprintf(head_fp, "MAP THRESH: %f\n", Vect_get_thresh(Map));
  81. fclose(head_fp);
  82. return 0;
  83. }
  84. /*!
  85. \brief Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only
  86. \param Map pointer to Map_info structure
  87. \return 0 on success
  88. \return -1 on error
  89. */
  90. int Vect__read_head(struct Map_info *Map)
  91. {
  92. FILE *head_fp;
  93. char buff[2000];
  94. char *path, *ptr;
  95. /* Reset / init */
  96. Vect__init_head(Map);
  97. G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
  98. path = Vect__get_path(Map);
  99. head_fp = G_fopen_old(path, GV_HEAD_ELEMENT, Map->mapset);
  100. G_free(path);
  101. if (head_fp == NULL) {
  102. G_warning(_("Unable to open header file of vector <%s>"),
  103. Vect_get_full_name(Map));
  104. return -1;
  105. }
  106. while (G_getl2(buff, 2000, head_fp)) {
  107. if (!(ptr = strchr(buff, ':'))) {
  108. G_warning(_("Corrupted row in head: %s"), buff);
  109. continue;
  110. }
  111. ptr++; /* Search for the start of text */
  112. while (*ptr == ' ')
  113. ptr++;
  114. if (strncmp(buff, "ORGANIZATION:", sizeof(char) * 12) == 0)
  115. Vect_set_organization(Map, ptr);
  116. else if (strncmp(buff, "DIGIT DATE:", sizeof(char) * 11) == 0)
  117. Vect_set_date(Map, ptr);
  118. else if (strncmp(buff, "DIGIT NAME:", sizeof(char) * 11) == 0)
  119. Vect_set_person(Map, ptr);
  120. else if (strncmp(buff, "MAP NAME:", sizeof(char) * 9) == 0)
  121. Vect_set_map_name(Map, ptr);
  122. else if (strncmp(buff, "MAP DATE:", sizeof(char) * 9) == 0)
  123. Vect_set_map_date(Map, ptr);
  124. else if (strncmp(buff, "MAP SCALE:", sizeof(char) * 10) == 0)
  125. Vect_set_scale(Map, atoi(ptr));
  126. else if (strncmp(buff, "OTHER INFO:", sizeof(char) * 11) == 0)
  127. Vect_set_comment(Map, ptr);
  128. else if (strncmp(buff, "PROJ:", sizeof(char) * 5) == 0)
  129. Vect_set_proj(Map, atoi(ptr));
  130. else if (strncmp(buff, "ZONE:", sizeof(char) * 5) == 0 ||
  131. strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0)
  132. Vect_set_zone(Map, atoi(ptr));
  133. else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) {
  134. }
  135. else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) {
  136. }
  137. else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) {
  138. }
  139. else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) {
  140. }
  141. else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
  142. Vect_set_thresh(Map, atof(ptr));
  143. else
  144. G_warning(_("Unknown keyword '%s' in vector head"), buff);
  145. }
  146. fclose(head_fp);
  147. return 0;
  148. }
  149. /*!
  150. \brief Get name of vector map
  151. \param Map pointer to Map_info structure
  152. \return string containing name
  153. */
  154. const char *Vect_get_name(const struct Map_info *Map)
  155. {
  156. return Map->name;
  157. }
  158. /*!
  159. \brief Get name of mapset where vector map lives
  160. \param Map pointer to Map_info structure
  161. \return string containing mapset name
  162. */
  163. const char *Vect_get_mapset(const struct Map_info *Map)
  164. {
  165. return Map->mapset;
  166. }
  167. /*!
  168. \brief Get fully qualified name of vector map
  169. - for GV_FORMAT_NATIVE and GV_FORMAT_OGR returns "map@mapset"
  170. - for GV_FORMAT_OGR_DIRECT returns "layer@datasourse"
  171. Allocated string should be freed by G_free().
  172. \param Map pointer to Map_info structure
  173. \return allocated string "name@mapset"
  174. */
  175. const char *Vect_get_full_name(const struct Map_info *Map)
  176. {
  177. char *ptr;
  178. if (Map->format == GV_FORMAT_OGR_DIRECT &&
  179. Map->fInfo.ogr.dsn &&
  180. Map->fInfo.ogr.layer_name) {
  181. ptr = (char *) G_malloc(strlen(Map->fInfo.ogr.layer_name) +
  182. strlen(Map->fInfo.ogr.dsn) + 2);
  183. sprintf(ptr, "%s@%s", Map->fInfo.ogr.layer_name,
  184. Map->fInfo.ogr.dsn);
  185. return ptr;
  186. }
  187. ptr = (char *) G_malloc(strlen(Map->name) + strlen(Map->mapset) + 2);
  188. if (strlen(Map->mapset) > 0) {
  189. sprintf(ptr, "%s@%s", Map->name, Map->mapset);
  190. }
  191. else {
  192. sprintf(ptr, "%s", Map->name);
  193. }
  194. return ptr;
  195. }
  196. /*!
  197. \brief Check if vector map is 3D
  198. Check vector map header.
  199. \param Map pointer to Map_info structure
  200. \return TRUE vector map is 3D
  201. \return FALSE vector map is not 3D
  202. */
  203. int Vect_is_3d(const struct Map_info *Map)
  204. {
  205. return Map->head.with_z;
  206. }
  207. /*!
  208. \brief Set organization string in map header
  209. \param Map pointer to Map_info structure
  210. \param str organization name
  211. \return 0
  212. */
  213. int Vect_set_organization(struct Map_info *Map, const char *str)
  214. {
  215. G_free(Map->head.organization);
  216. Map->head.organization = G_store(str);
  217. return 0;
  218. }
  219. /*!
  220. \brief Get organization string from map header
  221. \param Map pointer to Map_info structure
  222. \return string containing organization name
  223. */
  224. const char *Vect_get_organization(const struct Map_info *Map)
  225. {
  226. return Map->head.organization;
  227. }
  228. /*!
  229. \brief Set date of digitization in map header
  230. \todo This should be coupled to DateTime functions to support
  231. time series
  232. \param Map pointer to Map_info structure
  233. \param str date given as string
  234. \return 0
  235. */
  236. int Vect_set_date(struct Map_info *Map, const char *str)
  237. {
  238. G_free(Map->head.date);
  239. Map->head.date = G_store(str);
  240. return 0;
  241. }
  242. /*!
  243. \brief Get date of digitization from map header
  244. \param Map pointer to Map_info structure
  245. \return date of digitization string
  246. */
  247. const char *Vect_get_date(const struct Map_info *Map)
  248. {
  249. return (Map->head.date);
  250. }
  251. /*!
  252. \brief Set name of user who digitized the map in map header
  253. \param Map pointer to Map_info structure
  254. \param str user name
  255. \return 0
  256. */
  257. int Vect_set_person(struct Map_info *Map, const char *str)
  258. {
  259. G_free(Map->head.user_name);
  260. Map->head.user_name = G_store(str);
  261. return 0;
  262. }
  263. /*!
  264. \brief Get user name string who digitized the map from map header
  265. \param Map pointer to Map_info structure
  266. \return string containing user name
  267. */
  268. const char *Vect_get_person(const struct Map_info *Map)
  269. {
  270. return (Map->head.user_name);
  271. }
  272. /*!
  273. \brief Set map name in map header
  274. \param Map pointer to Map_info structure
  275. \param str map name to be set
  276. \return 0
  277. */
  278. int Vect_set_map_name(struct Map_info *Map, const char *str)
  279. {
  280. G_free(Map->head.map_name);
  281. Map->head.map_name = G_store(str);
  282. return 0;
  283. }
  284. /*!
  285. \brief Get map name from map header
  286. \param Map pointer to Map_info structure
  287. \return string containing map name
  288. */
  289. const char *Vect_get_map_name(const struct Map_info *Map)
  290. {
  291. return Map->head.map_name;
  292. }
  293. /*!
  294. \brief Set date when the source map was originally produced in map header
  295. \param Map pointer to Map_info structure
  296. \param str date given as a string
  297. \return 0
  298. */
  299. int Vect_set_map_date(struct Map_info *Map, const char *str)
  300. {
  301. G_free(Map->head.source_date);
  302. Map->head.source_date = G_store(str);
  303. return 0;
  304. }
  305. /*!
  306. \brief Get date when the source map was originally produced from map header
  307. \param Map pointer to Map_info structure
  308. \return string containg a date
  309. */
  310. const char *Vect_get_map_date(const struct Map_info *Map)
  311. {
  312. return Map->head.source_date;
  313. }
  314. /*!
  315. \brief Set map scale in map header
  316. \param Map pointer to Map_info structure
  317. \param scale map scale
  318. \return 0
  319. */
  320. int Vect_set_scale(struct Map_info *Map, int scale)
  321. {
  322. Map->head.orig_scale = scale;
  323. return 0;
  324. }
  325. /*!
  326. \brief Get map scale from map header
  327. \param Map pointer to Map_info structure
  328. \return map scale
  329. */
  330. int Vect_get_scale(const struct Map_info *Map)
  331. {
  332. return (int) Map->head.orig_scale;
  333. }
  334. /*!
  335. \brief Set comment or other info string in map header
  336. \param Map pointer to Map_info structure
  337. \param str comment or other info string
  338. \return 0
  339. */
  340. int Vect_set_comment(struct Map_info *Map, const char *str)
  341. {
  342. G_free(Map->head.comment);
  343. Map->head.comment = G_store(str);
  344. return 0;
  345. }
  346. /*!
  347. \brief Get comment or other info string from map header
  348. \param Map pointer to Map_info structure
  349. \return comment or other info string
  350. */
  351. const char *Vect_get_comment(const struct Map_info *Map)
  352. {
  353. return (Map->head.comment);
  354. }
  355. /*!
  356. \brief Set projection zone in map header
  357. \param Map pointer to Map_info structure
  358. \param zone projection zone
  359. \return 0
  360. */
  361. int Vect_set_zone(struct Map_info *Map, int zone)
  362. {
  363. Map->head.plani_zone = zone;
  364. return 0;
  365. }
  366. /*!
  367. \brief Get projection zone from map header
  368. \param Map pointer to Map_info structure
  369. \return projection zone
  370. */
  371. int Vect_get_zone(const struct Map_info *Map)
  372. {
  373. return Map->head.plani_zone;
  374. }
  375. /*!
  376. \brief Set projection in map header
  377. Supported projections:
  378. - PROJECTION_XY 0 - x,y (Raw imagery),
  379. - PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
  380. - PROJECTION_LL 3 - Latitude-Longitude
  381. \param Map pointer to Map_info structure
  382. \param proj projection code
  383. \return 0
  384. */
  385. int Vect_set_proj(struct Map_info *Map, int proj)
  386. {
  387. Map->head.proj = proj;
  388. return 0;
  389. }
  390. /*!
  391. \brief Get projection from map header
  392. \param Map pointer to Map_info structure
  393. \return PROJECTION_XY 0 - x,y (Raw imagery),
  394. \return PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
  395. \return PROJECTION_LL 3 - Latitude-Longitude
  396. */
  397. int Vect_get_proj(const struct Map_info *Map)
  398. {
  399. return (Map->head.proj);
  400. }
  401. /*!
  402. \brief Query cartographic projection name of pointer to Map_info structure
  403. Returns a pointer to a string which is a printable name for
  404. projection code <em>proj</em> (as returned by Vect_get_proj()).
  405. \param Map pointer to Map_info structure
  406. \return allocated string containing projection name
  407. \return NULL if <em>proj</em> is not a valid projection
  408. */
  409. const char *Vect_get_proj_name(const struct Map_info *Map)
  410. {
  411. char name[256];
  412. int n;
  413. switch (n = Vect_get_proj(Map)) {
  414. case PROJECTION_XY:
  415. case PROJECTION_UTM:
  416. case PROJECTION_LL:
  417. return G__projection_name(n);
  418. case PROJECTION_OTHER:
  419. /* this won't protect against differing "other" projections, so
  420. better to just include P_OTHER in the above list so we return the
  421. strictly more correct, but less nice, string: "Other projection" ? */
  422. return G_database_projection_name();
  423. default:
  424. G_debug(1, "Vect_get_proj_name(): "
  425. "Vect_get_proj() returned an invalid result (%d)", n);
  426. break;
  427. }
  428. strcpy(name, _("Unknown projection"));
  429. return G_store(name);
  430. }
  431. /*!
  432. \brief Set threshold used for digitization in map header
  433. \param Map pointer to Map_info structure
  434. \param thresh threshold used for digitization
  435. \return 0
  436. */
  437. int Vect_set_thresh(struct Map_info *Map, double thresh)
  438. {
  439. G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
  440. Map->head.digit_thresh = thresh;
  441. return 0;
  442. }
  443. /*!
  444. \brief Get threshold used for digitization from map header
  445. \param Map pointer to Map_info structure
  446. \return threshold used for digitization
  447. */
  448. double Vect_get_thresh(const struct Map_info *Map)
  449. {
  450. return Map->head.digit_thresh;
  451. }