header.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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-2010 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. \author Update to GRASS 7 (OGR support) by Martin Landa <landa.martin gmail.com>
  11. */
  12. #include <grass/config.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. static int lookup(const char *file, const char *key, char *value, size_t len);
  19. /*!
  20. \brief Print vector map header to stdout
  21. \param Map pointer to Map_info structure
  22. \return 0 on success
  23. */
  24. int Vect_print_header(const struct Map_info *Map)
  25. {
  26. fprintf(stdout, "\nSelected information from dig header\n");
  27. fprintf(stdout, " Organization: %s\n", Vect_get_organization(Map));
  28. fprintf(stdout, " Map Name: %s\n", Vect_get_map_name(Map));
  29. fprintf(stdout, " Source Date: %s\n", Vect_get_map_date(Map));
  30. fprintf(stdout, " Orig. Scale: %d\n", Vect_get_scale(Map));
  31. return 0;
  32. }
  33. /*!
  34. \brief Read vector map header from map head file
  35. \param Map pointrt to Map_info structure
  36. \return 0
  37. */
  38. int Vect_read_header(struct Map_info *Map)
  39. {
  40. Vect__read_head(Map);
  41. return 0;
  42. }
  43. /*!
  44. \brief Write vector map header to map head file
  45. \param Map pointer to Map_info structure
  46. \return 0
  47. */
  48. int Vect_write_header(const struct Map_info *Map)
  49. {
  50. /* do some sanity checking here */
  51. Vect__write_head(Map);
  52. return 0;
  53. }
  54. /*!
  55. \brief Writes head information to text file (GV_HEAD_ELEMENT)
  56. \param Map pointer to Map_info structure
  57. \return 0 on success
  58. \return -1 on error
  59. */
  60. int Vect__write_head(const struct Map_info *Map)
  61. {
  62. char buf[GPATH_MAX];
  63. FILE *head_fp;
  64. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  65. head_fp = G_fopen_new(buf, GV_HEAD_ELEMENT);
  66. if (head_fp == NULL) {
  67. G_warning(_("Unable to open header file of vector <%s>"),
  68. Vect_get_full_name(Map));
  69. return -1;
  70. }
  71. fprintf(head_fp, "ORGANIZATION: %s\n", Vect_get_organization(Map));
  72. fprintf(head_fp, "DIGIT DATE: %s\n", Vect_get_date(Map));
  73. fprintf(head_fp, "DIGIT NAME: %s\n", Vect_get_person(Map));
  74. fprintf(head_fp, "MAP NAME: %s\n", Vect_get_map_name(Map));
  75. fprintf(head_fp, "MAP DATE: %s\n", Vect_get_map_date(Map));
  76. fprintf(head_fp, "MAP SCALE: %d\n", Vect_get_scale(Map));
  77. fprintf(head_fp, "OTHER INFO: %s\n", Vect_get_comment(Map));
  78. fprintf(head_fp, "ZONE: %d\n", Vect_get_zone(Map));
  79. fprintf(head_fp, "MAP THRESH: %f\n", Vect_get_thresh(Map));
  80. fclose(head_fp);
  81. return (0);
  82. }
  83. /*!
  84. \brief Reads head information from text file (GV_HEAD_ELEMENT) - for internal use only
  85. \param Map pointer to Map_info structure
  86. \return 0 on success
  87. \return -1 on error
  88. */
  89. int Vect__read_head(struct Map_info *Map)
  90. {
  91. FILE *head_fp;
  92. char buff[GPATH_MAX];
  93. char *ptr;
  94. /* Reset / init */
  95. Vect_set_organization(Map, "");
  96. Vect_set_date(Map, "");
  97. Vect_set_person(Map, "");
  98. Vect_set_map_name(Map, "");
  99. Vect_set_map_date(Map, "");
  100. Vect_set_scale(Map, 1);
  101. Vect_set_comment(Map, "");
  102. Vect_set_zone(Map, -1);
  103. Vect_set_thresh(Map, 0.);
  104. G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
  105. sprintf(buff, "%s/%s", GV_DIRECTORY, Map->name);
  106. head_fp = G_fopen_old(buff, GV_HEAD_ELEMENT, Map->mapset);
  107. if (head_fp == NULL) {
  108. G_warning(_("Unable to open header file of vector <%s>"),
  109. Vect_get_full_name(Map));
  110. return -1;
  111. }
  112. while (G_getl2(buff, 2000, head_fp)) {
  113. if (!(ptr = strchr(buff, ':'))) {
  114. G_warning(_("Corrupted row in head: %s"), buff);
  115. continue;
  116. }
  117. ptr++; /* Search for the start of text */
  118. while (*ptr == ' ')
  119. ptr++;
  120. if (strncmp(buff, "ORGANIZATION:", sizeof(char) * 12) == 0)
  121. Vect_set_organization(Map, ptr);
  122. else if (strncmp(buff, "DIGIT DATE:", sizeof(char) * 11) == 0)
  123. Vect_set_date(Map, ptr);
  124. else if (strncmp(buff, "DIGIT NAME:", sizeof(char) * 11) == 0)
  125. Vect_set_person(Map, ptr);
  126. else if (strncmp(buff, "MAP NAME:", sizeof(char) * 9) == 0)
  127. Vect_set_map_name(Map, ptr);
  128. else if (strncmp(buff, "MAP DATE:", sizeof(char) * 9) == 0)
  129. Vect_set_map_date(Map, ptr);
  130. else if (strncmp(buff, "MAP SCALE:", sizeof(char) * 10) == 0)
  131. Vect_set_scale(Map, atoi(ptr));
  132. else if (strncmp(buff, "OTHER INFO:", sizeof(char) * 11) == 0)
  133. Vect_set_comment(Map, ptr);
  134. else if (strncmp(buff, "ZONE:", sizeof(char) * 5) == 0 ||
  135. strncmp(buff, "UTM ZONE:", sizeof(char) * 9) == 0)
  136. Vect_set_zone(Map, atoi(ptr));
  137. else if (strncmp(buff, "WEST EDGE:", sizeof(char) * 10) == 0) {
  138. }
  139. else if (strncmp(buff, "EAST EDGE:", sizeof(char) * 10) == 0) {
  140. }
  141. else if (strncmp(buff, "SOUTH EDGE:", sizeof(char) * 11) == 0) {
  142. }
  143. else if (strncmp(buff, "NORTH EDGE:", sizeof(char) * 11) == 0) {
  144. }
  145. else if (strncmp(buff, "MAP THRESH:", sizeof(char) * 11) == 0)
  146. Vect_set_thresh(Map, atof(ptr));
  147. else
  148. G_warning(_("Unknown keyword '%s' in vector head"), buff);
  149. }
  150. fclose(head_fp);
  151. return 0;
  152. }
  153. /*!
  154. \brief Get name of vector map
  155. \param Map pointer to Map_info structure
  156. \return string containing name
  157. */
  158. const char *Vect_get_name(const struct Map_info *Map)
  159. {
  160. return Map->name;
  161. }
  162. /*!
  163. \brief Get name of mapset where vector map lives
  164. \param Map pointer to Map_info structure
  165. \return string containing mapset name
  166. */
  167. const char *Vect_get_mapset(const struct Map_info *Map)
  168. {
  169. return Map->mapset;
  170. }
  171. /*!
  172. \brief Get fully qualified name of vector map
  173. - for GV_FORMAT_NATIVE and GV_FORMAT_OGR returns "map@mapset"
  174. - for GV_FORMAT_OGR_DIRECT returns "layer@datasourse"
  175. Allocated string should be freed by G_free().
  176. \param Map pointer to Map_info structure
  177. \return allocated string "name@mapset"
  178. */
  179. const char *Vect_get_full_name(const struct Map_info *Map)
  180. {
  181. char *ptr;
  182. if (Map->format == GV_FORMAT_OGR_DIRECT &&
  183. Map->fInfo.ogr.dsn &&
  184. Map->fInfo.ogr.layer_name) {
  185. ptr = (char *) G_malloc(strlen(Map->fInfo.ogr.layer_name) +
  186. strlen(Map->fInfo.ogr.dsn) + 2);
  187. sprintf(ptr, "%s@%s", Map->fInfo.ogr.layer_name,
  188. Map->fInfo.ogr.dsn);
  189. return ptr;
  190. }
  191. ptr = (char *) G_malloc(strlen(Map->name) + strlen(Map->mapset) + 2);
  192. if (strlen(Map->mapset) > 0) {
  193. sprintf(ptr, "%s@%s", Map->name, Map->mapset);
  194. }
  195. else {
  196. sprintf(ptr, "%s", Map->name);
  197. }
  198. return ptr;
  199. }
  200. /*!
  201. \brief Get OGR datasource name (relevant only for OGR format)
  202. \param Map pointer to Map_info structure
  203. \return string containing OGR datasource name
  204. */
  205. const char *Vect_get_ogr_dsn_name(const struct Map_info *Map)
  206. {
  207. return Map->fInfo.ogr.dsn;
  208. }
  209. /*!
  210. \brief Get OGR layer name (relevant only for OGR format)
  211. \param Map pointer to Map_info structure
  212. \return string containing OGR layer name
  213. */
  214. const char *Vect_get_ogr_layer_name(const struct Map_info *Map)
  215. {
  216. return Map->fInfo.ogr.layer_name;
  217. }
  218. /*!
  219. \brief Get OGR format info (relevant only for OGR format)
  220. Allocated space should be freed by G_free().
  221. \param Map pointer to Map_info structure
  222. \return string containing name of OGR format (allocated by G_store())
  223. \return NULL on error (or on missing OGR support)
  224. */
  225. const char *Vect_get_ogr_format_info(const struct Map_info *Map)
  226. {
  227. #ifdef HAVE_OGR
  228. if (!Map->fInfo.ogr.ds)
  229. return NULL;
  230. return G_store(OGR_Dr_GetName(OGR_DS_GetDriver(Map->fInfo.ogr.ds)));
  231. #endif
  232. return NULL;
  233. }
  234. /*!
  235. \brief Get OGR geometry type (relevant only for OGR format)
  236. \param Map pointer to Map_info structure
  237. \return allocated string containing geometry type info
  238. \return NULL on error (or on missing OGR support)
  239. */
  240. const char *Vect_get_ogr_geometry_type(const struct Map_info *Map)
  241. {
  242. #ifdef HAVE_OGR
  243. OGRwkbGeometryType Ogr_geom_type;
  244. OGRFeatureDefnH Ogr_feature_defn;
  245. if (!Map->fInfo.ogr.layer)
  246. return NULL;
  247. Ogr_feature_defn = OGR_L_GetLayerDefn(Map->fInfo.ogr.layer);
  248. Ogr_geom_type = OGR_FD_GetGeomType(Ogr_feature_defn);
  249. switch(Ogr_geom_type) {
  250. case wkbPoint:
  251. return G_store(_("point"));
  252. case wkbLineString:
  253. return G_store(_("linestring"));
  254. case wkbPolygon:
  255. return G_store(_("polygon"));
  256. default:
  257. return G_store(_("unknown"));
  258. }
  259. #endif
  260. return NULL;
  261. }
  262. /*!
  263. \brief Check if vector map is 3D
  264. \param Map pointer to Map_info structure
  265. \return TRUE vector map is 3D
  266. \return FALSE vector map is not 3D
  267. */
  268. int Vect_is_3d(const struct Map_info *Map)
  269. {
  270. return Map->head.with_z;
  271. }
  272. /*!
  273. \brief Set organization string in map header
  274. \param Map pointer to Map_info structure
  275. \param str organization name
  276. \return 0
  277. */
  278. int Vect_set_organization(struct Map_info *Map, const char *str)
  279. {
  280. G_free(Map->head.organization);
  281. Map->head.organization = G_store(str);
  282. return 0;
  283. }
  284. /*!
  285. \brief Get organization string from map header
  286. \param Map pointer to Map_info structure
  287. \return string containing organization name
  288. */
  289. const char *Vect_get_organization(const struct Map_info *Map)
  290. {
  291. return Map->head.organization;
  292. }
  293. /*!
  294. \brief Set date of digitization in map header
  295. \todo This should be coupled to DateTime functions to support
  296. time series
  297. \param Map pointer to Map_info structure
  298. \param str date given as string
  299. \return 0
  300. */
  301. int Vect_set_date(struct Map_info *Map, const char *str)
  302. {
  303. G_free(Map->head.date);
  304. Map->head.date = G_store(str);
  305. return 0;
  306. }
  307. /*!
  308. \brief Get date of digitization from map header
  309. \param Map pointer to Map_info structure
  310. \return date of digitization string
  311. */
  312. const char *Vect_get_date(const struct Map_info *Map)
  313. {
  314. return (Map->head.date);
  315. }
  316. /*!
  317. \brief Set name of user who digitized the map in map header
  318. \param Map pointer to Map_info structure
  319. \param str user name
  320. \return 0
  321. */
  322. int Vect_set_person(struct Map_info *Map, const char *str)
  323. {
  324. G_free(Map->head.your_name);
  325. Map->head.your_name = G_store(str);
  326. return 0;
  327. }
  328. /*!
  329. \brief Get user name string who digitized the map from map header
  330. \param Map pointer to Map_info structure
  331. \return string containing user name
  332. */
  333. const char *Vect_get_person(const struct Map_info *Map)
  334. {
  335. return Map->head.your_name;
  336. }
  337. /*!
  338. \brief Set map name in map header
  339. \param Map pointer to Map_info structure
  340. \param str map name to be set
  341. \return 0
  342. */
  343. int Vect_set_map_name(struct Map_info *Map, const char *str)
  344. {
  345. G_free(Map->head.map_name);
  346. Map->head.map_name = G_store(str);
  347. return 0;
  348. }
  349. /*!
  350. \brief Get map name from map header
  351. \param Map pointer to Map_info structure
  352. \return string containing map name
  353. */
  354. const char *Vect_get_map_name(const struct Map_info *Map)
  355. {
  356. return Map->head.map_name;
  357. }
  358. /*!
  359. \brief Set date when the source map was originally produced in map header
  360. \param Map pointer to Map_info structure
  361. \param str date given as a string
  362. \return 0
  363. */
  364. int Vect_set_map_date(struct Map_info *Map, const char *str)
  365. {
  366. G_free(Map->head.source_date);
  367. Map->head.source_date = G_store(str);
  368. return 0;
  369. }
  370. /*!
  371. \brief Get date when the source map was originally produced from map header
  372. \param Map pointer to Map_info structure
  373. \return string containg a date
  374. */
  375. const char *Vect_get_map_date(const struct Map_info *Map)
  376. {
  377. return Map->head.source_date;
  378. }
  379. /*!
  380. \brief Set map scale in map header
  381. \param Map pointer to Map_info structure
  382. \param scale map scale
  383. \return 0
  384. */
  385. int Vect_set_scale(struct Map_info *Map, int scale)
  386. {
  387. Map->head.orig_scale = scale;
  388. return 0;
  389. }
  390. /*!
  391. \brief Get map scale from map header
  392. \param Map pointer to Map_info structure
  393. \return map scale
  394. */
  395. int Vect_get_scale(const struct Map_info *Map)
  396. {
  397. return (int) Map->head.orig_scale;
  398. }
  399. /*!
  400. \brief Set comment or other info string in map header
  401. \param Map pointer to Map_info structure
  402. \param str comment or other info string
  403. \return 0
  404. */
  405. int Vect_set_comment(struct Map_info *Map, const char *str)
  406. {
  407. G_free(Map->head.line_3);
  408. Map->head.line_3 = G_store(str);
  409. return 0;
  410. }
  411. /*!
  412. \brief Get comment or other info string from map header
  413. \param Map pointer to Map_info structure
  414. \return comment or other info string
  415. */
  416. const char *Vect_get_comment(const struct Map_info *Map)
  417. {
  418. return Map->head.line_3;
  419. }
  420. /*!
  421. \brief Set projection zone in map header
  422. \param Map pointer to Map_info structure
  423. \param zone projection zone
  424. \return 0
  425. */
  426. int Vect_set_zone(struct Map_info *Map, int zone)
  427. {
  428. Map->head.plani_zone = zone;
  429. return 0;
  430. }
  431. /*!
  432. \brief Get projection zone from map header
  433. \param Map pointer to Map_info structure
  434. \return projection zone
  435. */
  436. int Vect_get_zone(const struct Map_info *Map)
  437. {
  438. return Map->head.plani_zone;
  439. }
  440. /*!
  441. \brief Set projection in map header
  442. Supported projections:
  443. - PROJECTION_XY 0 - x,y (Raw imagery),
  444. - PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
  445. - PROJECTION_SP 2 - State Plane (in feet),
  446. - PROJECTION_LL 3 - Latitude-Longitude
  447. \param Map pointer to Map_info structure
  448. \param proj projection code
  449. \return 0
  450. */
  451. int Vect_set_proj(struct Map_info *Map, int proj)
  452. {
  453. Map->proj = proj;
  454. return 0;
  455. }
  456. /*!
  457. \brief Get projection from map header
  458. \param Map pointer to Map_info structure
  459. \return PROJECTION_XY 0 - x,y (Raw imagery),
  460. \return PROJECTION_UTM 1 - UTM Universal Transverse Mercator,
  461. \return PROJECTION_SP 2 - State Plane (in feet),
  462. \return PROJECTION_LL 3 - Latitude-Longitude
  463. */
  464. int Vect_get_proj(const struct Map_info *Map)
  465. {
  466. return Map->proj;
  467. }
  468. /*!
  469. \brief Query cartographic projection name of pointer to Map_info structure
  470. Returns a pointer to a string which is a printable name for
  471. projection code <em>proj</em> (as returned by Vect_get_proj()).
  472. \param Map pointer to Map_info structure
  473. \return allocated string containing projection name
  474. \return NULL if <em>proj</em> is not a valid projection
  475. */
  476. const char *Vect_get_proj_name(const struct Map_info *Map)
  477. {
  478. char name[256];
  479. int n;
  480. switch (n = Vect_get_proj(Map)) {
  481. case PROJECTION_XY:
  482. case PROJECTION_UTM:
  483. case PROJECTION_LL:
  484. case PROJECTION_SP:
  485. return G__projection_name(n);
  486. }
  487. if (!lookup(PROJECTION_FILE, "name", name, sizeof(name)))
  488. strcpy(name, _("Unknown projection"));
  489. return G_store(name);
  490. }
  491. /*!
  492. \brief Set threshold used for digitization in map header
  493. \param Map pointer to Map_info structure
  494. \param thresh threshold used for digitization
  495. \return 0
  496. */
  497. int Vect_set_thresh(struct Map_info *Map, double thresh)
  498. {
  499. G_debug(1, "Vect_set_thresh(): thresh = %f", thresh);
  500. Map->head.digit_thresh = thresh;
  501. return 0;
  502. }
  503. /*!
  504. \brief Get threshold used for digitization from map header
  505. \param Map pointer to Map_info structure
  506. \return threshold used for digitization
  507. */
  508. double Vect_get_thresh(const struct Map_info *Map)
  509. {
  510. G_debug(1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh);
  511. return Map->head.digit_thresh;
  512. }
  513. /* from lib/gis/proj3.c */
  514. static int lookup(const char *file, const char *key, char *value, size_t len)
  515. {
  516. char path[GPATH_MAX];
  517. G__file_name(path, "", file, "PERMANENT");
  518. return G_lookup_key_value_from_file(path, key, value, (int) len) == 1;
  519. }