gdal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <grass/config.h>
  4. #include <grass/gis.h>
  5. #include <grass/glocale.h>
  6. #include "G.h"
  7. #ifndef HAVE_GDAL
  8. #undef GDAL_LINK
  9. #endif
  10. #ifdef GDAL_LINK
  11. #ifdef GDAL_DYNAMIC
  12. # if defined(__unix) || defined(__unix__)
  13. # include <dlfcn.h>
  14. # endif
  15. # ifdef _WIN32
  16. # include <windows.h>
  17. # endif
  18. #endif
  19. static void CPL_STDCALL (*pGDALAllRegister)(void);
  20. static void CPL_STDCALL (*pGDALClose)(GDALDatasetH);
  21. static GDALRasterBandH CPL_STDCALL (*pGDALGetRasterBand)(GDALDatasetH, int);
  22. static GDALDatasetH CPL_STDCALL (*pGDALOpen)(
  23. const char *pszFilename, GDALAccess eAccess);
  24. static CPLErr CPL_STDCALL (*pGDALRasterIO)(
  25. GDALRasterBandH hRBand, GDALRWFlag eRWFlag,
  26. int nDSXOff, int nDSYOff, int nDSXSize, int nDSYSize,
  27. void * pBuffer, int nBXSize, int nBYSize,GDALDataType eBDataType,
  28. int nPixelSpace, int nLineSpace);
  29. #if GDAL_DYNAMIC
  30. # if defined(__unix) && !defined(__unix__)
  31. # define __unix__ __unix
  32. # endif
  33. static void *library_h;
  34. static void *get_symbol(const char *name)
  35. {
  36. void *sym;
  37. # ifdef __unix__
  38. sym = dlsym(library_h, name);
  39. # endif
  40. # ifdef _WIN32
  41. sym = GetProcAddress((HINSTANCE) library_h, name);
  42. # endif
  43. if (!sym)
  44. G_fatal_error(_("Unable to locate symbol <%s>"), name);
  45. return sym;
  46. }
  47. static void try_load_library(const char *name)
  48. {
  49. # ifdef __unix__
  50. library_h = dlopen(name, RTLD_NOW);
  51. # endif
  52. # ifdef _WIN32
  53. library_h = LoadLibrary(name);
  54. # endif
  55. }
  56. static void load_library(void)
  57. {
  58. static const char * const candidates[] = {
  59. # ifdef __unix__
  60. "libgdal.1.1.so",
  61. "gdal.1.0.so",
  62. "gdal.so.1.0",
  63. "libgdal.so.1",
  64. "libgdal.so",
  65. # endif
  66. # ifdef _WIN32
  67. "gdal11.dll",
  68. "gdal.1.0.dll",
  69. "gdal.dll",
  70. # endif
  71. NULL
  72. };
  73. int i;
  74. for (i = 0; candidates[i]; i++) {
  75. try_load_library(candidates[i]);
  76. if (library_h)
  77. return;
  78. }
  79. G_fatal_error(_("Unable to load GDAL library"));
  80. }
  81. static void init_gdal(void)
  82. {
  83. load_library();
  84. pGDALAllRegister = get_symbol("GDALAllRegister");
  85. pGDALOpen = get_symbol("GDALOpen");
  86. pGDALClose = get_symbol("GDALClose");
  87. pGDALGetRasterBand = get_symbol("GDALGetRasterBand");
  88. pGDALRasterIO = get_symbol("GDALRasterIO");
  89. }
  90. #else /* GDAL_DYNAMIC */
  91. static void init_gdal(void)
  92. {
  93. pGDALAllRegister = &GDALAllRegister;
  94. pGDALOpen = &GDALOpen;
  95. pGDALClose = &GDALClose;
  96. pGDALGetRasterBand = &GDALGetRasterBand;
  97. pGDALRasterIO = &GDALRasterIO;
  98. }
  99. #endif /* GDAL_DYNAMIC */
  100. #endif /* GDAL_LINK */
  101. void G_init_gdal(void)
  102. {
  103. #ifdef GDAL_LINK
  104. static int initialized;
  105. if (initialized)
  106. return;
  107. init_gdal();
  108. (*pGDALAllRegister)();
  109. initialized = 1;
  110. #endif
  111. }
  112. struct GDAL_link *G_get_gdal_link(const char *name, const char *mapset)
  113. {
  114. #ifdef GDAL_LINK
  115. GDALDatasetH data;
  116. GDALRasterBandH band;
  117. GDALDataType type;
  118. RASTER_MAP_TYPE req_type;
  119. #endif
  120. const char *filename;
  121. int band_num;
  122. struct GDAL_link *gdal;
  123. RASTER_MAP_TYPE map_type;
  124. FILE *fp;
  125. struct Key_Value *key_val;
  126. const char *p;
  127. DCELL null_val;
  128. if (!G_find_cell2(name, mapset))
  129. return NULL;
  130. map_type = G_raster_map_type(name, mapset);
  131. if (map_type < 0)
  132. return NULL;
  133. fp = G_fopen_old_misc("cell_misc", "gdal", name, mapset);
  134. if (!fp)
  135. return NULL;
  136. key_val = G_fread_key_value(fp);
  137. fclose(fp);
  138. if (!key_val)
  139. return NULL;
  140. filename = G_find_key_value("file", key_val);
  141. if (!filename)
  142. return NULL;
  143. p = G_find_key_value("band", key_val);
  144. if (!p)
  145. return NULL;
  146. band_num = atoi(p);
  147. if (!band_num)
  148. return NULL;
  149. p = G_find_key_value("null", key_val);
  150. if (!p)
  151. return NULL;
  152. if (strcmp(p, "none") == 0)
  153. G_set_d_null_value(&null_val, 1);
  154. else
  155. null_val = atof(p);
  156. #ifdef GDAL_LINK
  157. p = G_find_key_value("type", key_val);
  158. if (!p)
  159. return NULL;
  160. type = atoi(p);
  161. switch (type) {
  162. case GDT_Byte:
  163. case GDT_Int16:
  164. case GDT_UInt16:
  165. case GDT_Int32:
  166. case GDT_UInt32:
  167. req_type = CELL_TYPE;
  168. break;
  169. case GDT_Float32:
  170. req_type = FCELL_TYPE;
  171. break;
  172. case GDT_Float64:
  173. req_type = DCELL_TYPE;
  174. break;
  175. default:
  176. return NULL;
  177. }
  178. if (req_type != map_type)
  179. return NULL;
  180. G_init_gdal();
  181. data = (*pGDALOpen)(filename, GA_ReadOnly);
  182. if (!data)
  183. return NULL;
  184. band = (*pGDALGetRasterBand)(data, band_num);
  185. if (!band) {
  186. (*pGDALClose)(data);
  187. return NULL;
  188. }
  189. #endif
  190. gdal = G_calloc(1, sizeof(struct GDAL_link));
  191. gdal->filename = G_store(filename);
  192. gdal->band_num = band_num;
  193. gdal->null_val = null_val;
  194. #ifdef GDAL_LINK
  195. gdal->data = data;
  196. gdal->band = band;
  197. gdal->type = type;
  198. #endif
  199. return gdal;
  200. }
  201. void G_close_gdal_link(struct GDAL_link *gdal)
  202. {
  203. #ifdef GDAL_LINK
  204. (*pGDALClose)(gdal->data);
  205. #endif
  206. G_free(gdal->filename);
  207. G_free(gdal);
  208. }
  209. #ifdef GDAL_LINK
  210. CPLErr G_gdal_raster_IO(
  211. GDALRasterBandH band, GDALRWFlag rw_flag,
  212. int x_off, int y_off, int x_size, int y_size,
  213. void *buffer, int buf_x_size, int buf_y_size, GDALDataType buf_type,
  214. int pixel_size, int line_size)
  215. {
  216. return (*pGDALRasterIO)(
  217. band, rw_flag, x_off, y_off, x_size, y_size,
  218. buffer, buf_x_size, buf_y_size, buf_type,
  219. pixel_size, line_size);
  220. }
  221. #endif