header.c 14 KB

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