input.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: g.proj
  5. * AUTHOR(S): Paul Kelly - paul-grass@stjohnspoint.co.uk
  6. * Frank Warmerdam
  7. * Radim Blazek
  8. * PURPOSE: Provides a means of reporting the contents of GRASS
  9. * projection information files and creating
  10. * new projection information files.
  11. * COPYRIGHT: (C) 2001-2007 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <math.h>
  21. #include <grass/gis.h>
  22. #include <grass/gprojects.h>
  23. #include <grass/glocale.h>
  24. #include <grass/config.h>
  25. #ifdef HAVE_OGR
  26. # include <gdal.h>
  27. # include <ogr_api.h>
  28. # include <cpl_csv.h>
  29. #endif
  30. #include "local_proto.h"
  31. static void set_default_region(void);
  32. #ifdef HAVE_OGR
  33. static void set_gdal_region(GDALDatasetH);
  34. static void set_ogr_region(OGRLayerH);
  35. #endif
  36. /**
  37. * \brief Read projection and region information from current location
  38. *
  39. * Reads projection and region information from current location and
  40. * stores in global structs cellhd, projinfo and projunits.
  41. **/
  42. void input_currloc(void)
  43. {
  44. G_get_default_window(&cellhd);
  45. if (cellhd.proj != PROJECTION_XY) {
  46. projinfo = G_get_projinfo();
  47. projunits = G_get_projunits();
  48. }
  49. return;
  50. }
  51. #ifdef HAVE_OGR
  52. /**
  53. * \brief Read projection information in WKT format from stdin or a file
  54. *
  55. * Reads projection information from a file or stdin and stores in global
  56. * structs projinfo and projunits.
  57. * Populates global cellhd with default region information.
  58. *
  59. * \param wktfile File to read WKT co-ordinate system description from; -
  60. * for stdin
  61. *
  62. * \return 2 if a projected or lat/long co-ordinate system has been
  63. * defined; 1 if an unreferenced XY co-ordinate system has
  64. * been defined
  65. **/
  66. int input_wkt(char *wktfile)
  67. {
  68. FILE *infd;
  69. char buff[8000];
  70. int ret;
  71. if (strcmp(wktfile, "-") == 0)
  72. infd = stdin;
  73. else
  74. infd = fopen(wktfile, "r");
  75. if (infd) {
  76. fread(buff, sizeof(buff), 1, infd);
  77. if (ferror(infd))
  78. G_fatal_error(_("Error reading WKT projection description"));
  79. else
  80. fclose(infd);
  81. /* Get rid of newlines */
  82. G_squeeze(buff);
  83. }
  84. else
  85. G_fatal_error(_("Unable to open file '%s' for reading"), wktfile);
  86. ret = GPJ_wkt_to_grass(&cellhd, &projinfo, &projunits, buff, 0);
  87. set_default_region();
  88. return ret;
  89. }
  90. /**
  91. * \brief Read projection information in PROJ.4 format from a string
  92. * or stdin
  93. *
  94. * Reads projection information from a string or stdin and stores in global
  95. * structs projinfo and projunits.
  96. * Populates global cellhd with default region information.
  97. *
  98. * \param proj4params String representation of PROJ.4 co-ordinate system
  99. * description, or - to read it from stdin
  100. *
  101. * \return 2 if a projected or lat/long co-ordinate system has been
  102. * defined; 1 if an unreferenced XY co-ordinate system has
  103. * been defined
  104. **/
  105. int input_proj4(char *proj4params)
  106. {
  107. FILE *infd;
  108. char buff[8000];
  109. char *proj4string;
  110. OGRSpatialReferenceH hSRS;
  111. int ret = 0;
  112. if (strcmp(proj4params, "-") == 0) {
  113. infd = stdin;
  114. fgets(buff, sizeof(buff), infd);
  115. G_asprintf(&proj4string, "%s +no_defs", buff);
  116. }
  117. else
  118. G_asprintf(&proj4string, "%s +no_defs", proj4params);
  119. /* Set finder function for locating OGR csv co-ordinate system tables */
  120. SetCSVFilenameHook(GPJ_set_csv_loc);
  121. hSRS = OSRNewSpatialReference(NULL);
  122. if (OSRImportFromProj4(hSRS, proj4string) != OGRERR_NONE)
  123. G_fatal_error(_("Can't parse PROJ.4-style parameter string"));
  124. G_free(proj4string);
  125. ret = GPJ_osr_to_grass(&cellhd, &projinfo, &projunits, hSRS, 0);
  126. OSRDestroySpatialReference(hSRS);
  127. set_default_region();
  128. return ret;
  129. }
  130. /**
  131. * \brief Read projection information corresponding to an EPSG co-ordinate
  132. * system number
  133. *
  134. * Determines projection information corresponding to an EPSG co-ordinate
  135. * system number and stores in global structs projinfo and projunits.
  136. * Populates global cellhd with default region information.
  137. *
  138. * \param epsg_num EPSG number for co-ordinate system
  139. *
  140. * \return 2 if a projected or lat/long co-ordinate system has been
  141. * defined; 1 if an unreferenced XY co-ordinate system has
  142. * been defined
  143. **/
  144. int input_epsg(int epsg_num)
  145. {
  146. OGRSpatialReferenceH hSRS;
  147. int ret = 0;
  148. /* Set finder function for locating OGR csv co-ordinate system tables */
  149. SetCSVFilenameHook(GPJ_set_csv_loc);
  150. hSRS = OSRNewSpatialReference(NULL);
  151. if (OSRImportFromEPSG(hSRS, epsg_num) != OGRERR_NONE)
  152. G_fatal_error(_("Unable to translate EPSG code"));
  153. ret = GPJ_osr_to_grass(&cellhd, &projinfo, &projunits, hSRS, 0);
  154. OSRDestroySpatialReference(hSRS);
  155. set_default_region();
  156. return ret;
  157. }
  158. /**
  159. * \brief Read projection and region information associated with a
  160. * georeferenced file
  161. *
  162. * Reads projection information associated with a georeferenced file, if
  163. * available. Attempts are made to open the file with OGR and GDAL in turn.
  164. * (GDAL conventionally supports raster formats, and OGR vector formats.)
  165. *
  166. * If successful, projection and region information are read from the file
  167. * using GDAL/OGR functions and stored in global structs cellhd, projinfo
  168. * and projunits.
  169. *
  170. * \param geofile Path to georeferenced file
  171. *
  172. * \return 2 if a projected or lat/long co-ordinate system has been
  173. * defined; 1 if an unreferenced XY co-ordinate system has
  174. * been defined
  175. **/
  176. int input_georef(char *geofile)
  177. {
  178. OGRDataSourceH ogr_ds;
  179. int ret = 0;
  180. /* Try opening file with OGR first because it doesn't output a
  181. * (potentially confusing) error message if it can't open the file */
  182. G_message(_("Trying to open with OGR..."));
  183. OGRRegisterAll();
  184. if ((ogr_ds = OGROpen(geofile, FALSE, NULL))
  185. && (OGR_DS_GetLayerCount(ogr_ds) > 0)) {
  186. OGRLayerH ogr_layer;
  187. OGRSpatialReferenceH ogr_srs;
  188. G_message(_("...succeeded."));
  189. /* Get the first layer */
  190. ogr_layer = OGR_DS_GetLayer(ogr_ds, 0);
  191. ogr_srs = OGR_L_GetSpatialRef(ogr_layer);
  192. ret = GPJ_osr_to_grass(&cellhd, &projinfo, &projunits, ogr_srs, 0);
  193. set_ogr_region(ogr_layer);
  194. OGR_DS_Destroy(ogr_ds);
  195. }
  196. else {
  197. /* Try opening with GDAL */
  198. GDALDatasetH gdal_ds;
  199. G_message(_("Trying to open with GDAL..."));
  200. GDALAllRegister();
  201. if ((gdal_ds = GDALOpen(geofile, GA_ReadOnly))) {
  202. char *wktstring;
  203. G_message(_("...succeeded."));
  204. wktstring = (char *)GDALGetProjectionRef(gdal_ds);
  205. ret =
  206. GPJ_wkt_to_grass(&cellhd, &projinfo, &projunits, wktstring,
  207. 0);
  208. set_gdal_region(gdal_ds);
  209. }
  210. else
  211. G_fatal_error(_("Could not read georeferenced file %s using "
  212. "either OGR nor GDAL"), geofile);
  213. }
  214. if (cellhd.proj == PROJECTION_XY)
  215. G_warning(_("Read of file %s was successful, but it did not contain "
  216. "projection information. 'XY (unprojected)' will be used"),
  217. geofile);
  218. return ret;
  219. }
  220. #endif /* HAVE_OGR */
  221. /**
  222. * \brief Populates global cellhd with "default" region settings
  223. **/
  224. static void set_default_region(void)
  225. {
  226. /* If importing projection there will be no region information, so
  227. * set some default values */
  228. cellhd.rows = 1;
  229. cellhd.rows3 = 1;
  230. cellhd.cols = 1;
  231. cellhd.cols3 = 1;
  232. cellhd.depths = 1.;
  233. cellhd.north = 1.;
  234. cellhd.ns_res = 1.;
  235. cellhd.ns_res3 = 1.;
  236. cellhd.south = 0.;
  237. cellhd.west = 0.;
  238. cellhd.ew_res = 1.;
  239. cellhd.ew_res3 = 1.;
  240. cellhd.east = 1.;
  241. cellhd.top = 1.;
  242. cellhd.tb_res = 1.;
  243. cellhd.bottom = 0.;
  244. return;
  245. }
  246. #ifdef HAVE_OGR
  247. /**
  248. * \brief Populates global cellhd with region settings based on
  249. * georeferencing information in a GDAL dataset
  250. *
  251. * \param hDS GDAL dataset object to retrieve georeferencing information from
  252. **/
  253. static void set_gdal_region(GDALDatasetH hDS)
  254. {
  255. double adfGeoTransform[6];
  256. /* Populate with initial values in case we can't set everything */
  257. set_default_region();
  258. /* Code below originally from r.in.gdal */
  259. cellhd.rows = GDALGetRasterYSize(hDS);
  260. cellhd.cols = GDALGetRasterXSize(hDS);
  261. cellhd.rows3 = cellhd.rows;
  262. cellhd.cols3 = cellhd.cols;
  263. if (GDALGetGeoTransform(hDS, adfGeoTransform) == CE_None
  264. && adfGeoTransform[5] < 0.0) {
  265. if (adfGeoTransform[2] != 0.0 || adfGeoTransform[4] != 0.0) {
  266. /* Map is rotated. Calculation of north/south extents and
  267. * resolution more complicated.
  268. * TODO: do it anyway */
  269. return;
  270. }
  271. cellhd.north = adfGeoTransform[3];
  272. cellhd.ns_res = fabs(adfGeoTransform[5]);
  273. cellhd.south = cellhd.north - cellhd.ns_res * cellhd.rows;
  274. cellhd.west = adfGeoTransform[0];
  275. cellhd.ew_res = adfGeoTransform[1];
  276. cellhd.east = cellhd.west + cellhd.cols * cellhd.ew_res;
  277. cellhd.ns_res3 = cellhd.ns_res;
  278. cellhd.ew_res3 = cellhd.ew_res;
  279. }
  280. else {
  281. cellhd.north = cellhd.rows;
  282. cellhd.east = cellhd.cols;
  283. }
  284. return;
  285. }
  286. /**
  287. * \brief Populates global cellhd with region settings based on
  288. * georeferencing information in an OGR layer
  289. *
  290. * \param Ogr_layer OGR layer to retrieve georeferencing information from
  291. **/
  292. static void set_ogr_region(OGRLayerH Ogr_layer)
  293. {
  294. OGREnvelope oExt;
  295. /* Populate with initial values in case we can't set everything */
  296. set_default_region();
  297. /* Code below originally from v.in.ogr */
  298. if ((OGR_L_GetExtent(Ogr_layer, &oExt, 1)) == OGRERR_NONE) {
  299. cellhd.north = oExt.MaxY;
  300. cellhd.south = oExt.MinY;
  301. cellhd.west = oExt.MinX;
  302. cellhd.east = oExt.MaxX;
  303. cellhd.rows = 20; /* TODO - calculate useful values */
  304. cellhd.cols = 20;
  305. cellhd.ns_res = (cellhd.north - cellhd.south) / cellhd.rows;
  306. cellhd.ew_res = (cellhd.east - cellhd.west) / cellhd.cols;
  307. cellhd.rows3 = cellhd.rows;
  308. cellhd.cols3 = cellhd.cols;
  309. cellhd.ns_res3 = cellhd.ns_res;
  310. cellhd.ew_res3 = cellhd.ew_res;
  311. }
  312. return;
  313. }
  314. #endif /* HAVE_OGR */