header.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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-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 Get OGR datasource name (relevant only for OGR format)
  190. \param Map pointer to Map_info structure
  191. \return poiter to string with OGR datasource name
  192. */
  193. const char *Vect_get_ogr_dsn_name(const struct Map_info *Map)
  194. {
  195. return (Map->fInfo.ogr.dsn);
  196. }
  197. /*!
  198. \brief Get OGR layer name (relevant only for OGR format)
  199. \param Map pointer to Map_info structure
  200. \return poiter to string with OGR layer name
  201. */
  202. const char *Vect_get_ogr_layer_name(const struct Map_info *Map)
  203. {
  204. return (Map->fInfo.ogr.layer_name);
  205. }
  206. /*!
  207. \brief Get OGR format info (relevant only for OGR format)
  208. \param Map pointer to Map_info structure
  209. \return poiter to string with OGR format info (allocated by G_store())
  210. \return NULL on error
  211. */
  212. const char *Vect_get_ogr_format_info(const struct Map_info *Map)
  213. {
  214. #ifdef HAVE_OGR
  215. if (!Map->fInfo.ogr.ds)
  216. return NULL;
  217. return G_store(OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds)));
  218. #endif
  219. return NULL;
  220. }
  221. /*!
  222. \brief Check if vector map is 3D (with z)
  223. \param Map vector map
  224. \return 1 map is 3D
  225. \return 0 map is not 3D
  226. */
  227. int Vect_is_3d(const struct Map_info *Map)
  228. {
  229. return (Map->head.with_z);
  230. }
  231. /*!
  232. \brief Set organization string in map header
  233. \param Map vector map
  234. \param str organization name
  235. \return 0
  236. */
  237. int Vect_set_organization(struct Map_info *Map, const char *str)
  238. {
  239. G_free(Map->head.organization);
  240. Map->head.organization = G_store(str);
  241. return 0;
  242. }
  243. /*!
  244. \brief Get organization string from map header
  245. \param Map vector map
  246. \return organization string
  247. */
  248. const char *Vect_get_organization(const struct Map_info *Map)
  249. {
  250. return (Map->head.organization);
  251. }
  252. /*!
  253. \brief Set date of digitization string in map header
  254. SUGGESTION: this should be coupled to DateTime functions to support
  255. time series
  256. \param Map vector map
  257. \param str data string
  258. \return 0
  259. */
  260. int Vect_set_date(struct Map_info *Map, const char *str)
  261. {
  262. G_free(Map->head.date);
  263. Map->head.date = G_store(str);
  264. return 0;
  265. }
  266. /*!
  267. \brief Get date of digitization string from map header
  268. SUGGESTION: this should be coupled to DateTime functions to support
  269. time series
  270. \param Map vector map
  271. \return date of digitization string
  272. */
  273. const char *Vect_get_date(const struct Map_info *Map)
  274. {
  275. return (Map->head.date);
  276. }
  277. /*!
  278. \brief Set user name string who digitized the map in map header
  279. \param Map vector map
  280. \param str user name string
  281. \return 0 on success
  282. */
  283. int Vect_set_person(struct Map_info *Map, const char *str)
  284. {
  285. G_free(Map->head.your_name);
  286. Map->head.your_name = G_store(str);
  287. return 0;
  288. }
  289. /*!
  290. \brief Get user name string who digitized the map from map header
  291. \param Map vector map
  292. \return user name string
  293. */
  294. const char *Vect_get_person(const struct Map_info *Map)
  295. {
  296. return (Map->head.your_name);
  297. }
  298. /*!
  299. \brief Set map name string in map header
  300. \param Map vector map
  301. \param str map name string
  302. \return 0 on success
  303. */
  304. int Vect_set_map_name(struct Map_info *Map, const char *str)
  305. {
  306. G_free(Map->head.map_name);
  307. Map->head.map_name = G_store(str);
  308. return 0;
  309. }
  310. /*!
  311. \brief Get map name string in map header
  312. \param Map vector map
  313. \return map name string
  314. */
  315. const char *Vect_get_map_name(const struct Map_info *Map)
  316. {
  317. return (Map->head.map_name);
  318. }
  319. /*!
  320. \brief Set date string when the source map was originally produced in map header
  321. \param Map vector map
  322. \param str date when the source map was originally produced string
  323. \return 0 on success
  324. */
  325. int Vect_set_map_date(struct Map_info *Map, const char *str)
  326. {
  327. G_free(Map->head.source_date);
  328. Map->head.source_date = G_store(str);
  329. return 0;
  330. }
  331. /*!
  332. \brief Get date string when the source map was originally produced in map header
  333. \param Map vector map
  334. \return date when the source map was originally produced string
  335. */
  336. const char *Vect_get_map_date(const struct Map_info *Map)
  337. {
  338. return (Map->head.source_date);
  339. }
  340. /*!
  341. \brief Set map scale in map header
  342. \param Map vector map
  343. \param map scale
  344. \return 0 on success
  345. */
  346. int Vect_set_scale(struct Map_info *Map, int scale)
  347. {
  348. Map->head.orig_scale = scale;
  349. return 0;
  350. }
  351. /*!
  352. \brief Get map scale from map header
  353. \param Map vector map
  354. \return map scale
  355. */
  356. int Vect_get_scale(const struct Map_info *Map)
  357. {
  358. return ((int) Map->head.orig_scale);
  359. }
  360. /*!
  361. \brief Set comment or other info string in map header
  362. \param Map vector map
  363. \param str comment or other info string
  364. \return 0 on success
  365. */
  366. int Vect_set_comment(struct Map_info *Map, const char *str)
  367. {
  368. G_free(Map->head.line_3);
  369. Map->head.line_3 = G_store(str);
  370. return 0;
  371. }
  372. /*!
  373. \brief Get comment or other info string from map header
  374. \param Map vector map
  375. \return comment or other info string
  376. */
  377. const char *Vect_get_comment(const struct Map_info *Map)
  378. {
  379. return (Map->head.line_3);
  380. }
  381. /*!
  382. \brief Set projection zone in map header
  383. \param Map vector map
  384. \param zone projection zone
  385. \return 0 on success
  386. */
  387. int Vect_set_zone(struct Map_info *Map, int zone)
  388. {
  389. Map->head.plani_zone = zone;
  390. return 0;
  391. }
  392. /*!
  393. \brief Get projection zone from map header
  394. \param Map vector map
  395. \return projection zone
  396. */
  397. int Vect_get_zone(const struct Map_info *Map)
  398. {
  399. return (Map->head.plani_zone);
  400. }
  401. /*!
  402. \brief Set projection in map header
  403. \param Map vector map
  404. PROJECTION_XY 0 - x,y (Raw imagery),
  405. PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
  406. PROJECTION_SP 2 - State Plane (in feet),
  407. PROJECTION_LL 3 - Latitude-Longitude
  408. \return 0 on success
  409. */
  410. int Vect_set_proj(struct Map_info *Map, int proj)
  411. {
  412. Map->proj = proj;
  413. return 0;
  414. }
  415. /*!
  416. \brief Get projection from map header
  417. \param Map vector map
  418. \return PROJECTION_XY 0 - x,y (Raw imagery),
  419. \return PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
  420. \return PROJECTION_SP 2 - State Plane (in feet),
  421. \return PROJECTION_LL 3 - Latitude-Longitude
  422. */
  423. int Vect_get_proj(const struct Map_info *Map)
  424. {
  425. return (Map->proj);
  426. }
  427. /*!
  428. \brief Query cartographic projection name of vector map
  429. Returns a pointer to a string which is a printable name for
  430. projection code <em>proj</em> (as returned by
  431. Vect_get_proj()).
  432. \param Map vector map
  433. \return poiter to projection name
  434. \return NULL if <em>proj</em> is not a valid projection
  435. */
  436. const char *Vect_get_proj_name(const struct Map_info *Map)
  437. {
  438. char name[256];
  439. int n;
  440. switch (n = Vect_get_proj(Map)) {
  441. case PROJECTION_XY:
  442. case PROJECTION_UTM:
  443. case PROJECTION_LL:
  444. case PROJECTION_SP:
  445. return G__projection_name(n);
  446. }
  447. if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
  448. strcpy(name, _("Unknown projection"));
  449. return G_store(name);
  450. }
  451. /*!
  452. \brief Set threshold used for digitization in map header
  453. \param Map vector map
  454. \param thresh threshold used for digitization
  455. \return 0 on success
  456. */
  457. int Vect_set_thresh(struct Map_info *Map, double thresh)
  458. {
  459. G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
  460. Map->head.digit_thresh = thresh;
  461. return (0);
  462. }
  463. /*!
  464. \brief Get threshold used for digitization from map header
  465. \param Map vector map
  466. \return threshold used for digitization
  467. */
  468. double Vect_get_thresh(const struct Map_info *Map)
  469. {
  470. G_debug(1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh);
  471. return (Map->head.digit_thresh);
  472. }
  473. /* from lib/gis/proj3.c */
  474. static int lookup(const char *file, const char *key, char *value, size_t len)
  475. {
  476. char path[GPATH_MAX];
  477. G__file_name(path, "", file, "PERMANENT");
  478. return G_lookup_key_value_from_file(path, key, value, (int)len) == 1;
  479. }