header.c 12 KB

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