main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /****************************************************************************
  2. *
  3. * MODULE: r.latlong
  4. * AUTHOR(S): Yann Chemin - yann.chemin@gmail.com
  5. * PURPOSE: Calculates the longitude of the pixels in the map.
  6. *
  7. * COPYRIGHT: (C) 2002-2008, 2012 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General
  10. * Public License (>=v2). Read the file COPYING that
  11. * comes with GRASS for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <grass/gis.h>
  18. #include <grass/raster.h>
  19. #include <grass/gprojects.h>
  20. #include <grass/glocale.h>
  21. int main(int argc, char *argv[])
  22. {
  23. struct Cell_head cellhd; /*region+header info */
  24. int nrows, ncols;
  25. int row, col;
  26. int not_ll = 0; /*if proj is not lat/long, it will be 1. */
  27. struct GModule *module;
  28. struct Option *input1, *output1;
  29. struct Flag *flag1;
  30. struct History history; /*metadata */
  31. struct pj_info iproj;
  32. struct pj_info oproj;
  33. struct Key_Value *in_proj_info, *in_unit_info;
  34. /************************************/
  35. char *result1; /*output raster name */
  36. int infd;
  37. int outfd1;
  38. char *in;
  39. double xmin, ymin;
  40. double xmax, ymax;
  41. double stepx, stepy;
  42. double latitude, longitude;
  43. void *inrast;
  44. DCELL * outrast1;
  45. DCELL d;
  46. /************************************/
  47. G_gisinit(argv[0]);
  48. module = G_define_module();
  49. G_add_keyword(_("raster"));
  50. G_add_keyword(_("latitude"));
  51. G_add_keyword(_("longitude"));
  52. G_add_keyword(_("projection"));
  53. module->description = _("Creates a latitude/longitude raster map.");
  54. /* Define the different options */
  55. input1 = G_define_standard_option(G_OPT_R_INPUT);
  56. output1 = G_define_standard_option(G_OPT_R_OUTPUT);
  57. output1->description = _("Name for output latitude or longitude raster map");
  58. flag1 = G_define_flag();
  59. flag1->key = 'l';
  60. flag1->description = _("Longitude output");
  61. /********************/
  62. if (G_parser(argc, argv))
  63. exit(EXIT_FAILURE);
  64. in = input1->answer;
  65. result1 = output1->answer;
  66. /***************************************************/
  67. infd = Rast_open_old(in, "");
  68. Rast_get_cellhd(in, "", &cellhd);
  69. inrast = Rast_allocate_d_buf();
  70. /***************************************************/
  71. xmin = cellhd.west;
  72. xmax = cellhd.east;
  73. ymin = cellhd.south;
  74. ymax = cellhd.north;
  75. nrows = Rast_window_rows();
  76. ncols = Rast_window_cols();
  77. stepx = abs(xmax-xmin)/(double)ncols;
  78. stepy = abs(ymax-ymin)/(double)nrows;
  79. /*Stolen from r.sun */
  80. /* Set up parameters for projection to lat/long if necessary */
  81. if ((G_projection() != PROJECTION_LL))
  82. {
  83. not_ll = 1;
  84. if ((in_proj_info = G_get_projinfo()) == NULL)
  85. G_fatal_error(_("Unable to get projection info of current location"));
  86. if ((in_unit_info = G_get_projunits()) == NULL)
  87. G_fatal_error(_("Unable to get projection units of current location"));
  88. if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
  89. G_fatal_error(_("Unable to get projection key values of current location"));
  90. G_free_key_value(in_proj_info);
  91. G_free_key_value(in_unit_info);
  92. /* Set output projection to latlong w/ same ellipsoid */
  93. oproj.zone = 0;
  94. oproj.meters = 1.;
  95. sprintf(oproj.proj, "ll");
  96. if ((oproj.pj = pj_latlong_from_proj(iproj.pj)) == NULL)
  97. G_fatal_error(_("Unable to set up lat/long projection parameters"));
  98. } /* End of stolen from r.sun */
  99. outrast1 = Rast_allocate_d_buf();
  100. outfd1 = Rast_open_new(result1, DCELL_TYPE);
  101. for (row = 0; row < nrows; row++)
  102. {
  103. G_percent(row, nrows, 2);
  104. Rast_get_d_row(infd, inrast, row);
  105. for (col = 0; col < ncols; col++)
  106. {
  107. latitude = ymax - ((double)row * stepy);
  108. longitude = xmin + ((double)col * stepx);
  109. if (not_ll)
  110. if (pj_do_proj(&longitude, &latitude, &iproj, &oproj) < 0)
  111. G_fatal_error(_("Error in pj_do_proj"));
  112. if(flag1->answer)
  113. d = longitude;
  114. else
  115. d = latitude;
  116. outrast1[col] = d;
  117. }
  118. Rast_put_d_row(outfd1, outrast1);
  119. }
  120. G_free(inrast);
  121. Rast_close(infd);
  122. G_free(outrast1);
  123. Rast_close(outfd1);
  124. Rast_short_history(result1, "raster", &history);
  125. Rast_command_history(&history);
  126. Rast_write_history(result1, &history);
  127. exit(EXIT_SUCCESS);
  128. }