header.c 13 KB

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