header.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 GRASS_OK on success
  57. \return GRASS_ERR 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", GRASS_VECT_DIRECTORY, Map->name);
  64. head_fp = G_fopen_new(buf, GRASS_VECT_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 (GRASS_ERR);
  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 (GRASS_OK);
  81. }
  82. /*!
  83. \brief Reads head information from text file (GRASS_VECT_HEAD_ELEMENT).
  84. \param Map vector map
  85. \return GRASS_OK on success
  86. \return GRASS_ERR 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, 0);
  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", GRASS_VECT_DIRECTORY, Map->name);
  105. head_fp = G_fopen_old(buff, GRASS_VECT_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 (GRASS_ERR);
  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 (GRASS_OK);
  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 map name
  172. Allocated string should be freed by G_free().
  173. \param Map vector map
  174. \return poiter 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. sprintf(ptr, "%s@%s", Map->name, Map->mapset);
  181. return (ptr);
  182. }
  183. /*!
  184. \brief Check if vector map is 3D (with z)
  185. \param Map vector map
  186. \return 1 map is 3D
  187. \return 0 map is not 3D
  188. */
  189. int Vect_is_3d(const struct Map_info *Map)
  190. {
  191. return (Map->head.with_z);
  192. }
  193. /*!
  194. \brief Set organization string in map header
  195. \param Map vector map
  196. \param str organization name
  197. \return 0
  198. */
  199. int Vect_set_organization(struct Map_info *Map, const char *str)
  200. {
  201. G_free(Map->head.organization);
  202. Map->head.organization = G_store(str);
  203. return 0;
  204. }
  205. /*!
  206. \brief Get organization string from map header
  207. \param Map vector map
  208. \return organization string
  209. */
  210. const char *Vect_get_organization(const struct Map_info *Map)
  211. {
  212. return (Map->head.organization);
  213. }
  214. /*!
  215. \brief Set date of digitization string in map header
  216. SUGGESTION: this should be coupled to DateTime functions to support
  217. time series
  218. \param Map vector map
  219. \param str data string
  220. \return 0
  221. */
  222. int Vect_set_date(struct Map_info *Map, const char *str)
  223. {
  224. G_free(Map->head.date);
  225. Map->head.date = G_store(str);
  226. return 0;
  227. }
  228. /*!
  229. \brief Get date of digitization string from map header
  230. SUGGESTION: this should be coupled to DateTime functions to support
  231. time series
  232. \param Map vector map
  233. \return date of digitization string
  234. */
  235. const char *Vect_get_date(const struct Map_info *Map)
  236. {
  237. return (Map->head.date);
  238. }
  239. /*!
  240. \brief Set user name string who digitized the map in map header
  241. \param Map vector map
  242. \param str user name string
  243. \return 0 on success
  244. */
  245. int Vect_set_person(struct Map_info *Map, const char *str)
  246. {
  247. G_free(Map->head.your_name);
  248. Map->head.your_name = G_store(str);
  249. return 0;
  250. }
  251. /*!
  252. \brief Get user name string who digitized the map from map header
  253. \param Map vector map
  254. \return user name string
  255. */
  256. const char *Vect_get_person(const struct Map_info *Map)
  257. {
  258. return (Map->head.your_name);
  259. }
  260. /*!
  261. \brief Set map name string in map header
  262. \param Map vector map
  263. \param str map name string
  264. \return 0 on success
  265. */
  266. int Vect_set_map_name(struct Map_info *Map, const char *str)
  267. {
  268. G_free(Map->head.map_name);
  269. Map->head.map_name = G_store(str);
  270. return 0;
  271. }
  272. /*!
  273. \brief Get map name string in map header
  274. \param Map vector map
  275. \return map name string
  276. */
  277. const char *Vect_get_map_name(const struct Map_info *Map)
  278. {
  279. return (Map->head.map_name);
  280. }
  281. /*!
  282. \brief Set date string when the source map was originally produced in map header
  283. \param Map vector map
  284. \param str date when the source map was originally produced string
  285. \return 0 on success
  286. */
  287. int Vect_set_map_date(struct Map_info *Map, const char *str)
  288. {
  289. G_free(Map->head.source_date);
  290. Map->head.source_date = G_store(str);
  291. return 0;
  292. }
  293. /*!
  294. \brief Get date string when the source map was originally produced in map header
  295. \param Map vector map
  296. \return date when the source map was originally produced string
  297. */
  298. const char *Vect_get_map_date(const struct Map_info *Map)
  299. {
  300. return (Map->head.source_date);
  301. }
  302. /*!
  303. \brief Set map scale in map header
  304. \param Map vector map
  305. \param map scale
  306. \return 0 on success
  307. */
  308. int Vect_set_scale(struct Map_info *Map, int scale)
  309. {
  310. Map->head.orig_scale = scale;
  311. return 0;
  312. }
  313. /*!
  314. \brief Get map scale from map header
  315. \param Map vector map
  316. \return map scale
  317. */
  318. int Vect_get_scale(const struct Map_info *Map)
  319. {
  320. return ((int) Map->head.orig_scale);
  321. }
  322. /*!
  323. \brief Set comment or other info string in map header
  324. \param Map vector map
  325. \param str comment or other info string
  326. \return 0 on success
  327. */
  328. int Vect_set_comment(struct Map_info *Map, const char *str)
  329. {
  330. G_free(Map->head.line_3);
  331. Map->head.line_3 = G_store(str);
  332. return 0;
  333. }
  334. /*!
  335. \brief Get comment or other info string from map header
  336. \param Map vector map
  337. \return comment or other info string
  338. */
  339. const char *Vect_get_comment(const struct Map_info *Map)
  340. {
  341. return (Map->head.line_3);
  342. }
  343. /*!
  344. \brief Set projection zone in map header
  345. \param Map vector map
  346. \param zone projection zone
  347. \return 0 on success
  348. */
  349. int Vect_set_zone(struct Map_info *Map, int zone)
  350. {
  351. Map->head.plani_zone = zone;
  352. return 0;
  353. }
  354. /*!
  355. \brief Get projection zone from map header
  356. \param Map vector map
  357. \return projection zone
  358. */
  359. int Vect_get_zone(const struct Map_info *Map)
  360. {
  361. return (Map->head.plani_zone);
  362. }
  363. /*!
  364. \brief Get projection from map header
  365. \param Map vector map
  366. \return PROJECTION_XY - x,y (Raw imagery),
  367. \return PROJECTION_UTM - UTM Universal Transverse Mercator,
  368. \return PROJECTION_SP - State Plane (in feet),
  369. \return PROJECTION_LL - Latitude-Longitude
  370. */
  371. int Vect_get_proj(const struct Map_info *Map)
  372. {
  373. return (Map->proj);
  374. }
  375. /*!
  376. \brief Query cartographic projection name of vector map
  377. Returns a pointer to a string which is a printable name for
  378. projection code <em>proj</em> (as returned by
  379. Vect_get_proj()).
  380. \param Map vector map
  381. \return poiter to projection name
  382. \return NULL if <em>proj</em> is not a valid projection
  383. */
  384. const char *Vect_get_proj_name(const 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(const 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. }