header.c 14 KB

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