header.c 12 KB

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