main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /***************************************************************************
  2. *
  3. * MODULE: r.proj
  4. *
  5. * AUTHOR(S): Martin Schroeder
  6. * University of Heidelberg
  7. * Dept. of Geography
  8. * emes@geo0.geog.uni-heidelberg.de
  9. *
  10. * (With the help of a lot of existing GRASS sources, in
  11. * particular v.proj)
  12. *
  13. * PURPOSE: r.proj converts a map to a new geographic projection. It reads a
  14. * map from a different location, projects it and write it out
  15. * to the current location. The projected data is resampled with
  16. * one of three different methods: nearest neighbor, bilinear and
  17. * cubic convolution.
  18. *
  19. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  20. *
  21. * This program is free software under the GNU General Public
  22. * License (>=v2). Read the file COPYING that comes with GRASS
  23. * for details.
  24. *
  25. * Changes
  26. * Morten Hulden <morten@ngb.se>, Aug 2000:
  27. * - aborts if input map is outside current location.
  28. * - can handle projections (conic, azimuthal etc) where
  29. * part of the map may fall into areas where south is
  30. * upward and east is leftward.
  31. * - avoids passing location edge coordinates to PROJ
  32. * (they may be invalid in some projections).
  33. * - output map will be clipped to borders of the current region.
  34. * - output map cell edges and centers will coinside with those
  35. * of the current region.
  36. * - output map resolution (unless changed explicitly) will
  37. * match (exactly) the resolution of the current region.
  38. * - if the input map is smaller than the current region, the
  39. * output map will only cover the overlapping area.
  40. * - if the input map is larger than the current region, only the
  41. * needed amount of memory will be allocated for the projection
  42. *
  43. * Bugfixes 20050328: added floor() before (int) typecasts to in avoid
  44. * assymetrical rounding errors. Added missing offset outcellhd.ew_res/2
  45. * to initial xcoord for each row in main projection loop (we want to project
  46. * center of cell, not border).
  47. *****************************************************************************/
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <unistd.h>
  52. #include <grass/gis.h>
  53. #include <grass/gprojects.h>
  54. #include <grass/glocale.h>
  55. #include "r.proj.h"
  56. /* modify this table to add new methods */
  57. struct menu menu[] = {
  58. {p_nearest, "nearest", "nearest neighbor"},
  59. {p_bilinear, "bilinear", "bilinear"},
  60. {p_cubic, "cubic", "cubic convolution"},
  61. {NULL, NULL, NULL}
  62. };
  63. static char *make_ipol_list(void);
  64. int main(int argc, char **argv)
  65. {
  66. char *mapname, /* ptr to name of output layer */
  67. *setname, /* ptr to name of input mapset */
  68. *ipolname; /* name of interpolation method */
  69. int fdi, /* input map file descriptor */
  70. fdo, /* output map file descriptor */
  71. method, /* position of method in table */
  72. permissions, /* mapset permissions */
  73. cell_type, /* output celltype */
  74. cell_size, /* size of a cell in bytes */
  75. row, col, /* counters */
  76. irows, icols, /* original rows, cols */
  77. orows, ocols, have_colors, /* Input map has a colour table */
  78. overwrite; /* overwrite output map */
  79. void *obuffer, /* buffer that holds one output row */
  80. *obufptr; /* column ptr in output buffer */
  81. FCELL **ibuffer; /* buffer that holds the input map */
  82. func interpolate; /* interpolation routine */
  83. double xcoord1, xcoord2, /* temporary x coordinates */
  84. ycoord1, ycoord2, /* temporary y coordinates */
  85. col_idx, /* column index in input matrix */
  86. row_idx, /* row index in input matrix */
  87. onorth, osouth, /* save original border coords */
  88. oeast, owest, inorth, isouth, ieast, iwest;
  89. struct Colors colr; /* Input map colour table */
  90. struct History history;
  91. struct pj_info iproj, /* input map proj parameters */
  92. oproj; /* output map proj parameters */
  93. struct Key_Value *in_proj_info, /* projection information of */
  94. *in_unit_info, /* input and output mapsets */
  95. *out_proj_info, *out_unit_info;
  96. struct GModule *module;
  97. struct Flag *list, /* list files in source location */
  98. *nocrop; /* don't crop output map */
  99. struct Option *imapset, /* name of input mapset */
  100. *inmap, /* name of input layer */
  101. *inlocation, /* name of input location */
  102. *outmap, /* name of output layer */
  103. *indbase, /* name of input database */
  104. *interpol, /* interpolation method:
  105. nearest neighbor, bilinear, cubic */
  106. *res; /* resolution of target map */
  107. struct Cell_head incellhd, /* cell header of input map */
  108. outcellhd; /* and output map */
  109. G_gisinit(argv[0]);
  110. module = G_define_module();
  111. module->keywords = _("raster, projection");
  112. module->description =
  113. _("Re-projects a raster map from one location to the current location.");
  114. inmap = G_define_standard_option(G_OPT_R_INPUT);
  115. inmap->description = _("Name of input raster map to re-project");
  116. inmap->required = NO;
  117. inlocation = G_define_option();
  118. inlocation->key = "location";
  119. inlocation->type = TYPE_STRING;
  120. inlocation->required = YES;
  121. inlocation->description = _("Location of input raster map");
  122. imapset = G_define_option();
  123. imapset->key = "mapset";
  124. imapset->type = TYPE_STRING;
  125. imapset->required = NO;
  126. imapset->description = _("Mapset of input raster map");
  127. indbase = G_define_option();
  128. indbase->key = "dbase";
  129. indbase->type = TYPE_STRING;
  130. indbase->required = NO;
  131. indbase->description = _("Path to GRASS database of input location");
  132. outmap = G_define_standard_option(G_OPT_R_OUTPUT);
  133. outmap->required = NO;
  134. outmap->description = _("Name for output raster map (default: input)");
  135. ipolname = make_ipol_list();
  136. interpol = G_define_option();
  137. interpol->key = "method";
  138. interpol->type = TYPE_STRING;
  139. interpol->required = NO;
  140. interpol->answer = "nearest";
  141. interpol->options = ipolname;
  142. interpol->description = _("Interpolation method to use");
  143. res = G_define_option();
  144. res->key = "resolution";
  145. res->type = TYPE_DOUBLE;
  146. res->required = NO;
  147. res->description = _("Resolution of output map");
  148. list = G_define_flag();
  149. list->key = 'l';
  150. list->description = _("List raster maps in input location and exit");
  151. nocrop = G_define_flag();
  152. nocrop->key = 'n';
  153. nocrop->description = _("Do not perform region cropping optimization");
  154. /* The parser checks if the map already exists in current mapset,
  155. we switch out the check and do it
  156. * in the module after the parser */
  157. overwrite = G_check_overwrite(argc, argv);
  158. if (G_parser(argc, argv))
  159. exit(EXIT_FAILURE);
  160. /* get the method */
  161. for (method = 0; (ipolname = menu[method].name); method++)
  162. if (strcmp(ipolname, interpol->answer) == 0)
  163. break;
  164. if (!ipolname)
  165. G_fatal_error(_("<%s=%s> unknown %s"),
  166. interpol->key, interpol->answer, interpol->key);
  167. interpolate = menu[method].method;
  168. mapname = outmap->answer ? outmap->answer : inmap->answer;
  169. if (mapname && !list->answer && !overwrite &&
  170. G_find_cell(mapname, G_mapset()))
  171. G_fatal_error(_("option <%s>: <%s> exists."), "output", mapname);
  172. setname = imapset->answer ? imapset->answer : G_store(G_mapset());
  173. if (!indbase->answer && strcmp(inlocation->answer, G_location()) == 0)
  174. G_fatal_error(_("Input and output locations can not be the same"));
  175. G_get_window(&outcellhd);
  176. /* Get projection info for output mapset */
  177. if ((out_proj_info = G_get_projinfo()) == NULL)
  178. G_fatal_error(_("Unable to get projection info of output raster map"));
  179. if ((out_unit_info = G_get_projunits()) == NULL)
  180. G_fatal_error(_("Unable to get projection units of output raster map"));
  181. if (pj_get_kv(&oproj, out_proj_info, out_unit_info) < 0)
  182. G_fatal_error(_("Unable to get projection key values of output raster map"));
  183. /* Change the location */
  184. G__create_alt_env();
  185. G__setenv("GISDBASE", indbase->answer ? indbase->answer : G_gisdbase());
  186. G__setenv("LOCATION_NAME", inlocation->answer);
  187. permissions = G__mapset_permissions(setname);
  188. if (permissions < 0) /* can't access mapset */
  189. G_fatal_error(_("Mapset <%s> in input location <%s> - %s"),
  190. setname, inlocation->answer,
  191. permissions == 0 ? _("permission denied")
  192. : _("not found"));
  193. /* if requested, list the raster maps in source location - MN 5/2001 */
  194. if (list->answer) {
  195. if (isatty(0)) /* check if on command line */
  196. G_message(_("Checking location <%s>, mapset <%s>..."),
  197. inlocation->answer, setname);
  198. G_list_element("cell", "raster", setname, 0);
  199. exit(EXIT_SUCCESS); /* leave r.proj after listing */
  200. }
  201. if (!inmap->answer)
  202. G_fatal_error(_("Required parameter <%s> not set"), inmap->key);
  203. if (!G_find_cell(inmap->answer, setname))
  204. G_fatal_error(_("Raster map <%s> in location <%s> in mapset <%s> not found"),
  205. inmap->answer, inlocation->answer, setname);
  206. /* Read input map colour table */
  207. have_colors = G_read_colors(inmap->answer, setname, &colr);
  208. /* Get projection info for input mapset */
  209. if ((in_proj_info = G_get_projinfo()) == NULL)
  210. G_fatal_error(_("Unable to get projection info of input map"));
  211. if ((in_unit_info = G_get_projunits()) == NULL)
  212. G_fatal_error(_("Unable to get projection units of input map"));
  213. if (pj_get_kv(&iproj, in_proj_info, in_unit_info) < 0)
  214. G_fatal_error(_("Unable to get projection key values of input map"));
  215. G_free_key_value(in_proj_info);
  216. G_free_key_value(in_unit_info);
  217. G_free_key_value(out_proj_info);
  218. G_free_key_value(out_unit_info);
  219. pj_print_proj_params(&iproj, &oproj);
  220. /* this call causes r.proj to read the entire map into memeory */
  221. G_get_cellhd(inmap->answer, setname, &incellhd);
  222. G_set_window(&incellhd);
  223. if (G_projection() == PROJECTION_XY)
  224. G_fatal_error(_("Unable to work with unprojected data (xy location)"));
  225. /* Save default borders so we can show them later */
  226. inorth = incellhd.north;
  227. isouth = incellhd.south;
  228. ieast = incellhd.east;
  229. iwest = incellhd.west;
  230. irows = incellhd.rows;
  231. icols = incellhd.cols;
  232. onorth = outcellhd.north;
  233. osouth = outcellhd.south;
  234. oeast = outcellhd.east;
  235. owest = outcellhd.west;
  236. orows = outcellhd.rows;
  237. ocols = outcellhd.cols;
  238. /* Cut non-overlapping parts of input map */
  239. if (!nocrop->answer)
  240. bordwalk(&outcellhd, &incellhd, &oproj, &iproj);
  241. /* Add 2 cells on each side for bilinear/cubic & future interpolation methods */
  242. /* (should probably be a factor based on input and output resolution) */
  243. incellhd.north += 2 * incellhd.ns_res;
  244. incellhd.east += 2 * incellhd.ew_res;
  245. incellhd.south -= 2 * incellhd.ns_res;
  246. incellhd.west -= 2 * incellhd.ew_res;
  247. if (incellhd.north > inorth)
  248. incellhd.north = inorth;
  249. if (incellhd.east > ieast)
  250. incellhd.east = ieast;
  251. if (incellhd.south < isouth)
  252. incellhd.south = isouth;
  253. if (incellhd.west < iwest)
  254. incellhd.west = iwest;
  255. G_set_window(&incellhd);
  256. /* And switch back to original location */
  257. G__switch_env();
  258. /* Adjust borders of output map */
  259. if (!nocrop->answer)
  260. bordwalk(&incellhd, &outcellhd, &iproj, &oproj);
  261. #if 0
  262. outcellhd.west = outcellhd.south = HUGE_VAL;
  263. outcellhd.east = outcellhd.north = -HUGE_VAL;
  264. for (row = 0; row < incellhd.rows; row++) {
  265. ycoord1 = G_row_to_northing((double)(row + 0.5), &incellhd);
  266. for (col = 0; col < incellhd.cols; col++) {
  267. xcoord1 = G_col_to_easting((double)(col + 0.5), &incellhd);
  268. pj_do_proj(&xcoord1, &ycoord1, &iproj, &oproj);
  269. if (xcoord1 > outcellhd.east)
  270. outcellhd.east = xcoord1;
  271. if (ycoord1 > outcellhd.north)
  272. outcellhd.north = ycoord1;
  273. if (xcoord1 < outcellhd.west)
  274. outcellhd.west = xcoord1;
  275. if (ycoord1 < outcellhd.south)
  276. outcellhd.south = ycoord1;
  277. }
  278. }
  279. #endif
  280. if (res->answer != NULL) /* set user defined resolution */
  281. outcellhd.ns_res = outcellhd.ew_res = atof(res->answer);
  282. G_adjust_Cell_head(&outcellhd, 0, 0);
  283. G_set_window(&outcellhd);
  284. G_message(NULL);
  285. G_message(_("Input:"));
  286. G_message(_("Cols: %d (%d)"), incellhd.cols, icols);
  287. G_message(_("Rows: %d (%d)"), incellhd.rows, irows);
  288. G_message(_("North: %f (%f)"), incellhd.north, inorth);
  289. G_message(_("South: %f (%f)"), incellhd.south, isouth);
  290. G_message(_("West: %f (%f)"), incellhd.west, iwest);
  291. G_message(_("East: %f (%f)"), incellhd.east, ieast);
  292. G_message(_("EW-res: %f"), incellhd.ew_res);
  293. G_message(_("NS-res: %f"), incellhd.ns_res);
  294. G_message(NULL);
  295. G_message(_("Output:"));
  296. G_message(_("Cols: %d (%d)"), outcellhd.cols, ocols);
  297. G_message(_("Rows: %d (%d)"), outcellhd.rows, orows);
  298. G_message(_("North: %f (%f)"), outcellhd.north, onorth);
  299. G_message(_("South: %f (%f)"), outcellhd.south, osouth);
  300. G_message(_("West: %f (%f)"), outcellhd.west, owest);
  301. G_message(_("East: %f (%f)"), outcellhd.east, oeast);
  302. G_message(_("EW-res: %f"), outcellhd.ew_res);
  303. G_message(_("NS-res: %f"), outcellhd.ns_res);
  304. G_message(NULL);
  305. /* open and read the relevant parts of the input map and close it */
  306. G__switch_env();
  307. G_set_window(&incellhd);
  308. fdi = G_open_cell_old(inmap->answer, setname);
  309. cell_type = G_get_raster_map_type(fdi);
  310. ibuffer = (FCELL **) readcell(fdi);
  311. G_close_cell(fdi);
  312. G__switch_env();
  313. G_set_window(&outcellhd);
  314. if (strcmp(interpol->answer, "nearest") == 0) {
  315. fdo = G_open_raster_new(mapname, cell_type);
  316. obuffer = (CELL *) G_allocate_raster_buf(cell_type);
  317. }
  318. else {
  319. fdo = G_open_fp_cell_new(mapname);
  320. cell_type = FCELL_TYPE;
  321. obuffer = (FCELL *) G_allocate_raster_buf(cell_type);
  322. }
  323. cell_size = G_raster_size(cell_type);
  324. xcoord1 = xcoord2 = outcellhd.west + (outcellhd.ew_res / 2);
  325. /**/ ycoord1 = ycoord2 = outcellhd.north - (outcellhd.ns_res / 2);
  326. /**/ G_important_message(_("Projecting..."));
  327. G_percent(0, outcellhd.rows, 2);
  328. for (row = 0; row < outcellhd.rows; row++) {
  329. obufptr = obuffer;
  330. for (col = 0; col < outcellhd.cols; col++) {
  331. /* project coordinates in output matrix to */
  332. /* coordinates in input matrix */
  333. if (pj_do_proj(&xcoord1, &ycoord1, &oproj, &iproj) < 0)
  334. G_set_null_value(obufptr, 1, cell_type);
  335. else {
  336. /* convert to row/column indices of input matrix */
  337. col_idx = (xcoord1 - incellhd.west) / incellhd.ew_res;
  338. row_idx = (incellhd.north - ycoord1) / incellhd.ns_res;
  339. /* and resample data point */
  340. interpolate(ibuffer, obufptr, cell_type,
  341. &col_idx, &row_idx, &incellhd);
  342. }
  343. obufptr = G_incr_void_ptr(obufptr, cell_size);
  344. xcoord2 += outcellhd.ew_res;
  345. xcoord1 = xcoord2;
  346. ycoord1 = ycoord2;
  347. }
  348. if (G_put_raster_row(fdo, obuffer, cell_type) < 0)
  349. G_fatal_error(_("Failed writing raster map <%s> row %d"), mapname,
  350. row);
  351. xcoord1 = xcoord2 = outcellhd.west + (outcellhd.ew_res / 2);
  352. ycoord2 -= outcellhd.ns_res;
  353. ycoord1 = ycoord2;
  354. G_percent(row, outcellhd.rows - 1, 2);
  355. }
  356. G_close_cell(fdo);
  357. if (have_colors > 0) {
  358. G_write_colors(mapname, G_mapset(), &colr);
  359. G_free_colors(&colr);
  360. }
  361. G_short_history(mapname, "raster", &history);
  362. G_command_history(&history);
  363. G_write_history(mapname, &history);
  364. G_done_msg(" ");
  365. exit(EXIT_SUCCESS);
  366. }
  367. static char *make_ipol_list(void)
  368. {
  369. int size = 0;
  370. int i;
  371. char *buf;
  372. for (i = 0; menu[i].name; i++)
  373. size += strlen(menu[i].name) + 1;
  374. buf = G_malloc(size);
  375. *buf = '\0';
  376. for (i = 0; menu[i].name; i++) {
  377. if (i)
  378. strcat(buf, ",");
  379. strcat(buf, menu[i].name);
  380. }
  381. return buf;
  382. }