output.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: g.proj
  5. * AUTHOR(S): Paul Kelly - paul-grass@stjohnspoint.co.uk
  6. * PURPOSE: Provides a means of reporting the contents of GRASS
  7. * projection information files and creating
  8. * new projection information files.
  9. * COPYRIGHT: (C) 2003-2007 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <proj_api.h>
  20. #include <grass/gis.h>
  21. #include <grass/gprojects.h>
  22. #include <grass/glocale.h>
  23. #include <grass/config.h>
  24. #include "local_proto.h"
  25. static int check_xy(int shell);
  26. void print_projinfo(int shell)
  27. {
  28. int i;
  29. char path[GPATH_MAX];
  30. if (check_xy(shell))
  31. return;
  32. if (!shell)
  33. fprintf(stdout,
  34. "-PROJ_INFO-------------------------------------------------\n");
  35. for (i = 0; i < projinfo->nitems; i++) {
  36. if (shell)
  37. fprintf(stdout, "%s=%s\n", projinfo->key[i], projinfo->value[i]);
  38. else
  39. fprintf(stdout, "%-11s: %s\n", projinfo->key[i], projinfo->value[i]);
  40. }
  41. /* EPSG code is preserved for historical metadata interest only:
  42. the contents of this file are not used by pj_*() routines at all */
  43. G_file_name(path, "", "PROJ_EPSG", "PERMANENT");
  44. if (access(path, F_OK) == 0) {
  45. struct Key_Value *in_epsg_key;
  46. in_epsg_key = G_read_key_value_file(path);
  47. if (!shell) {
  48. fprintf(stdout,
  49. "-PROJ_EPSG-------------------------------------------------\n");
  50. fprintf(stdout, "%-11s: %s\n", in_epsg_key->key[0],
  51. in_epsg_key->value[0]);
  52. }
  53. else
  54. fprintf(stdout, "%s=%s\n", in_epsg_key->key[0],
  55. in_epsg_key->value[0]);
  56. if (in_epsg_key != NULL)
  57. G_free_key_value(in_epsg_key);
  58. }
  59. if (!shell)
  60. fprintf(stdout,
  61. "-PROJ_UNITS------------------------------------------------\n");
  62. for (i = 0; i < projunits->nitems; i++) {
  63. if (shell)
  64. fprintf(stdout, "%s=%s\n",
  65. projunits->key[i], projunits->value[i]);
  66. else
  67. fprintf(stdout, "%-11s: %s\n",
  68. projunits->key[i], projunits->value[i]);
  69. }
  70. return;
  71. }
  72. void print_datuminfo(void)
  73. {
  74. char *datum, *params;
  75. struct gpj_datum dstruct;
  76. int validdatum = 0;
  77. if (check_xy(FALSE))
  78. return;
  79. GPJ__get_datum_params(projinfo, &datum, &params);
  80. if (datum)
  81. validdatum = GPJ_get_datum_by_name(datum, &dstruct);
  82. if (validdatum > 0)
  83. fprintf(stdout, "GRASS datum code: %s\nWKT Name: %s\n",
  84. dstruct.name, dstruct.longname);
  85. else if (datum)
  86. fprintf(stdout, "Invalid datum code: %s\n", datum);
  87. else
  88. fprintf(stdout, "Datum name not present\n");
  89. if (params)
  90. fprintf(stdout,
  91. "Datum transformation parameters (PROJ.4 format):\n"
  92. "\t%s\n", params);
  93. else if (validdatum > 0) {
  94. char *defparams;
  95. GPJ_get_default_datum_params_by_name(dstruct.name, &defparams);
  96. fprintf(stdout,
  97. "Datum parameters not present; default for %s is:\n"
  98. "\t%s\n", dstruct.name, defparams);
  99. G_free(defparams);
  100. }
  101. else
  102. fprintf(stdout, "Datum parameters not present\n");
  103. if (validdatum > 0)
  104. GPJ_free_datum(&dstruct);
  105. return;
  106. }
  107. void print_proj4(int dontprettify)
  108. {
  109. struct pj_info pjinfo;
  110. char *proj4, *proj4mod, *i;
  111. const char *unfact;
  112. if (check_xy(FALSE))
  113. return;
  114. pj_get_kv(&pjinfo, projinfo, projunits);
  115. proj4 = pj_get_def(pjinfo.pj, 0);
  116. pj_free(pjinfo.pj);
  117. /* GRASS-style PROJ.4 strings don't include a unit factor as this is
  118. * handled separately in GRASS - must include it here though */
  119. unfact = G_find_key_value("meters", projunits);
  120. if (unfact != NULL && (strcmp(pjinfo.proj, "ll") != 0))
  121. G_asprintf(&proj4mod, "%s +to_meter=%s", proj4, unfact);
  122. else
  123. proj4mod = G_store(proj4);
  124. pj_dalloc(proj4);
  125. for (i = proj4mod; *i; i++) {
  126. /* Don't print the first space */
  127. if (i == proj4mod && *i == ' ')
  128. continue;
  129. if (*i == ' ' && *(i+1) == '+' && !(dontprettify))
  130. fputc('\n', stdout);
  131. else
  132. fputc(*i, stdout);
  133. }
  134. fputc('\n', stdout);
  135. G_free(proj4mod);
  136. return;
  137. }
  138. #ifdef HAVE_OGR
  139. void print_wkt(int esristyle, int dontprettify)
  140. {
  141. char *outwkt;
  142. if (check_xy(FALSE))
  143. return;
  144. outwkt = GPJ_grass_to_wkt(projinfo, projunits, esristyle,
  145. !(dontprettify));
  146. if (outwkt != NULL) {
  147. fprintf(stdout, "%s\n", outwkt);
  148. G_free(outwkt);
  149. }
  150. else
  151. G_warning(_("%s: Unable to convert to WKT"), G_program_name());
  152. return;
  153. }
  154. #endif
  155. static int check_xy(int shell)
  156. {
  157. if (cellhd.proj == PROJECTION_XY) {
  158. if (shell)
  159. fprintf(stdout, "name=");
  160. fprintf(stdout, "XY location (unprojected)\n");
  161. return 1;
  162. }
  163. else
  164. return 0;
  165. }