main.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /****************************************************************************
  2. *
  3. * MODULE: v.proj
  4. * AUTHOR(S): Irina Kosinovsky, US ARMY CERL,
  5. * M.L. Holko, USDA, SCS, NHQ-CGIS,
  6. * R.L. Glenn, USDA, SCS, NHQ-CGIS (original contributors
  7. * Update to GRASS 6: Radim Blazek <radim.blazek gmail.com>
  8. * Huidae Cho <grass4u gmail.com>, Hamish Bowman <hamish_b yahoo.com>,
  9. * Jachym Cepicky <jachym les-ejk.cz>, Markus Neteler <neteler itc.it>,
  10. * Paul Kelly <paul-grass stjohnspoint.co.uk>
  11. * PURPOSE:
  12. * COPYRIGHT: (C) 1999-2008 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <ctype.h>
  22. #include <math.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <string.h>
  27. #include <grass/gis.h>
  28. #include <grass/vector.h>
  29. #include <grass/gprojects.h>
  30. #include <grass/glocale.h>
  31. #include "local_proto.h"
  32. int main(int argc, char *argv[])
  33. {
  34. int i, type, stat;
  35. int day, yr, Out_proj;
  36. int out_zone = 0;
  37. int overwrite; /* overwrite output map */
  38. const char *mapset;
  39. const char *omap_name, *map_name, *iset_name, *oset_name, *iloc_name;
  40. struct pj_info info_in;
  41. struct pj_info info_out;
  42. const char *gbase;
  43. char date[40], mon[4];
  44. struct GModule *module;
  45. struct Option *omapopt, *mapopt, *isetopt, *ilocopt, *ibaseopt;
  46. struct Key_Value *in_proj_keys, *in_unit_keys;
  47. struct Key_Value *out_proj_keys, *out_unit_keys;
  48. struct line_pnts *Points;
  49. struct line_cats *Cats;
  50. struct Map_info Map;
  51. struct Map_info Out_Map;
  52. struct
  53. {
  54. struct Flag *list; /* list files in source location */
  55. struct Flag *transformz; /* treat z as ellipsoidal height */
  56. } flag;
  57. G_gisinit(argv[0]);
  58. module = G_define_module();
  59. G_add_keyword(_("vector"));
  60. G_add_keyword(_("projection"));
  61. G_add_keyword(_("transformation"));
  62. module->description = _("Re-projects a vector map from one location to the current location.");
  63. /* set up the options and flags for the command line parser */
  64. mapopt = G_define_standard_option(G_OPT_V_INPUT);
  65. mapopt->required = NO;
  66. mapopt->guisection = _("Input");
  67. ilocopt = G_define_option();
  68. ilocopt->key = "location";
  69. ilocopt->type = TYPE_STRING;
  70. ilocopt->required = YES;
  71. ilocopt->description = _("Location containing input vector map");
  72. ilocopt->gisprompt = "old,location,location";
  73. ilocopt->key_desc = "name";
  74. isetopt = G_define_option();
  75. isetopt->key = "mapset";
  76. isetopt->type = TYPE_STRING;
  77. isetopt->required = NO;
  78. isetopt->description = _("Mapset containing input vector map");
  79. isetopt->gisprompt = "old,mapset,mapset";
  80. isetopt->key_desc = "name";
  81. isetopt->guisection = _("Input");
  82. ibaseopt = G_define_option();
  83. ibaseopt->key = "dbase";
  84. ibaseopt->type = TYPE_STRING;
  85. ibaseopt->required = NO;
  86. ibaseopt->description = _("Path to GRASS database of input location");
  87. ibaseopt->gisprompt = "old,dbase,dbase";
  88. ibaseopt->key_desc = "path";
  89. ibaseopt->guisection = _("Input");
  90. omapopt = G_define_standard_option(G_OPT_V_OUTPUT);
  91. omapopt->required = NO;
  92. omapopt->description = _("Name for output vector map (default: input)");
  93. omapopt->guisection = _("Output");
  94. flag.list = G_define_flag();
  95. flag.list->key = 'l';
  96. flag.list->description = _("List vector maps in input location and exit");
  97. flag.transformz = G_define_flag();
  98. flag.transformz->key = 'z';
  99. flag.transformz->description = _("3D vector maps only");
  100. flag.transformz->label =
  101. _("Assume z co-ordinate is ellipsoidal height and "
  102. "transform if possible");
  103. flag.transformz->guisection = _("Output");
  104. /* The parser checks if the map already exists in current mapset,
  105. we switch out the check and do it
  106. in the module after the parser */
  107. overwrite = G_check_overwrite(argc, argv);
  108. if (G_parser(argc, argv))
  109. exit(EXIT_FAILURE);
  110. /* start checking options and flags */
  111. /* set input vector map name and mapset */
  112. map_name = mapopt->answer;
  113. if (omapopt->answer)
  114. omap_name = omapopt->answer;
  115. else
  116. omap_name = map_name;
  117. if (omap_name && !flag.list->answer && !overwrite &&
  118. G_find_vector2(omap_name, G_mapset()))
  119. G_fatal_error(_("option <%s>: <%s> exists."), omapopt->key,
  120. omap_name);
  121. if (isetopt->answer)
  122. iset_name = isetopt->answer;
  123. else
  124. iset_name = G_store(G_mapset());
  125. oset_name = G_store(G_mapset());
  126. iloc_name = ilocopt->answer;
  127. if (ibaseopt->answer)
  128. gbase = ibaseopt->answer;
  129. else
  130. gbase = G_store(G_gisdbase());
  131. if (!ibaseopt->answer && strcmp(iloc_name, G_location()) == 0)
  132. G_fatal_error(_("Input and output locations can not be the same"));
  133. /* Change the location here and then come back */
  134. select_target_env();
  135. G__setenv("GISDBASE", gbase);
  136. G__setenv("LOCATION_NAME", iloc_name);
  137. stat = G__mapset_permissions(iset_name);
  138. /*DEBUG*/ {
  139. char path[256];
  140. G__file_name(path, "", "", iset_name);
  141. }
  142. if (stat >= 0) { /* yes, we can access the mapset */
  143. /* if requested, list the vector maps in source location - MN 5/2001 */
  144. if (flag.list->answer) {
  145. int i;
  146. char **list;
  147. G_verbose_message(_("Checking location <%s> mapset <%s>"),
  148. iloc_name, iset_name);
  149. list = G_list(G_ELEMENT_VECTOR, G__getenv("GISDBASE"),
  150. G__getenv("LOCATION_NAME"), iset_name);
  151. for (i = 0; list[i]; i++) {
  152. fprintf(stdout, "%s\n", list[i]);
  153. }
  154. fflush(stdout);
  155. exit(EXIT_SUCCESS); /* leave v.proj after listing */
  156. }
  157. if (mapopt->answer == NULL) {
  158. G_fatal_error(_("Required parameter <%s> not set"), mapopt->key);
  159. }
  160. G__setenv("MAPSET", iset_name);
  161. /* Make sure map is available */
  162. mapset = G_find_vector2(map_name, iset_name);
  163. if (mapset == NULL)
  164. G_fatal_error(_("Vector map <%s> in location <%s> mapset <%s> not found"),
  165. map_name, iloc_name, iset_name);
  166. /*** Get projection info for input mapset ***/
  167. in_proj_keys = G_get_projinfo();
  168. if (in_proj_keys == NULL)
  169. exit(EXIT_FAILURE);
  170. in_unit_keys = G_get_projunits();
  171. if (in_unit_keys == NULL)
  172. exit(EXIT_FAILURE);
  173. if (pj_get_kv(&info_in, in_proj_keys, in_unit_keys) < 0)
  174. exit(EXIT_FAILURE);
  175. Vect_set_open_level(1);
  176. G_debug(1, "Open old: location: %s mapset : %s", G__location_path(),
  177. G_mapset());
  178. Vect_open_old(&Map, map_name, mapset);
  179. }
  180. else if (stat < 0)
  181. { /* allow 0 (i.e. denied permission) */
  182. /* need to be able to read from others */
  183. if (stat == 0)
  184. G_fatal_error(_("Mapset <%s> in input location <%s> - permission denied"),
  185. iset_name, iloc_name);
  186. else
  187. G_fatal_error(_("Mapset <%s> in input location <%s> not found"),
  188. iset_name, iloc_name);
  189. }
  190. select_current_env();
  191. /****** get the output projection parameters ******/
  192. Out_proj = G_projection();
  193. out_proj_keys = G_get_projinfo();
  194. if (out_proj_keys == NULL)
  195. exit(EXIT_FAILURE);
  196. out_unit_keys = G_get_projunits();
  197. if (out_unit_keys == NULL)
  198. exit(EXIT_FAILURE);
  199. if (pj_get_kv(&info_out, out_proj_keys, out_unit_keys) < 0)
  200. exit(EXIT_FAILURE);
  201. G_free_key_value(in_proj_keys);
  202. G_free_key_value(in_unit_keys);
  203. G_free_key_value(out_proj_keys);
  204. G_free_key_value(out_unit_keys);
  205. if (G_verbose() == G_verbose_max()) {
  206. pj_print_proj_params(&info_in, &info_out);
  207. }
  208. G_debug(1, "Open new: location: %s mapset : %s", G__location_path(),
  209. G_mapset());
  210. Vect_open_new(&Out_Map, omap_name, Vect_is_3d(&Map));
  211. Vect_copy_head_data(&Map, &Out_Map);
  212. Vect_hist_copy(&Map, &Out_Map);
  213. Vect_hist_command(&Out_Map);
  214. out_zone = info_out.zone;
  215. Vect_set_zone(&Out_Map, out_zone);
  216. /* Read and write header info */
  217. sprintf(date, "%s", G_date());
  218. sscanf(date, "%*s%s%d%*s%d", mon, &day, &yr);
  219. if (yr < 2000)
  220. yr = yr - 1900;
  221. else
  222. yr = yr - 2000;
  223. sprintf(date, "%s %d %d", mon, day, yr);
  224. Vect_set_date(&Out_Map, date);
  225. /* Initialize the Point / Cat structure */
  226. Points = Vect_new_line_struct();
  227. Cats = Vect_new_cats_struct();
  228. /* Cycle through all lines */
  229. Vect_rewind(&Map);
  230. i = 0;
  231. fprintf(stderr, _("Reprojecting primitives: "));
  232. while (1) {
  233. ++i;
  234. if (i % 1000 == 0) {
  235. fprintf(stderr, "%7d\b\b\b\b\b\b\b", i);
  236. }
  237. type = Vect_read_next_line(&Map, Points, Cats); /* read line */
  238. if (type == 0)
  239. continue; /* Dead */
  240. if (type == -1)
  241. G_fatal_error(_("Reading input vector map"));
  242. if (type == -2)
  243. break;
  244. if (pj_do_transform(Points->n_points, Points->x, Points->y,
  245. flag.transformz->answer ? Points->z : NULL,
  246. &info_in, &info_out) < 0) {
  247. G_fatal_error(_("Error in pj_do_transform"));
  248. }
  249. Vect_write_line(&Out_Map, type, Points, Cats); /* write line */
  250. } /* end lines section */
  251. fprintf(stderr, "\r");
  252. /* Copy tables */
  253. if (Vect_copy_tables(&Map, &Out_Map, 0))
  254. G_warning(_("Failed to copy attribute table to output map"));
  255. Vect_close(&Map);
  256. Vect_build(&Out_Map);
  257. Vect_close(&Out_Map);
  258. exit(EXIT_SUCCESS);
  259. }