GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gdal.c
Go to the documentation of this file.
1 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <grass/config.h>
17 #include <grass/gis.h>
18 #include <grass/glocale.h>
19 #include "G.h"
20 
21 #ifndef HAVE_GDAL
22 #undef GDAL_LINK
23 #endif
24 
25 #ifdef GDAL_LINK
26 
27 #ifdef GDAL_DYNAMIC
28 # if defined(__unix) || defined(__unix__)
29 # include <dlfcn.h>
30 # endif
31 # ifdef _WIN32
32 # include <windows.h>
33 # endif
34 #endif
35 
36 static void CPL_STDCALL (*pGDALAllRegister)(void);
37 static void CPL_STDCALL (*pGDALClose)(GDALDatasetH);
38 static GDALRasterBandH CPL_STDCALL (*pGDALGetRasterBand)(GDALDatasetH, int);
39 static GDALDatasetH CPL_STDCALL (*pGDALOpen)(
40  const char *pszFilename, GDALAccess eAccess);
41 static CPLErr CPL_STDCALL (*pGDALRasterIO)(
42  GDALRasterBandH hRBand, GDALRWFlag eRWFlag,
43  int nDSXOff, int nDSYOff, int nDSXSize, int nDSYSize,
44  void * pBuffer, int nBXSize, int nBYSize,GDALDataType eBDataType,
45  int nPixelSpace, int nLineSpace);
46 
47 #if GDAL_DYNAMIC
48 # if defined(__unix) && !defined(__unix__)
49 # define __unix__ __unix
50 # endif
51 
52 static void *library_h;
53 
54 static void *get_symbol(const char *name)
55 {
56  void *sym;
57 
58 # ifdef __unix__
59  sym = dlsym(library_h, name);
60 # endif
61 # ifdef _WIN32
62  sym = GetProcAddress((HINSTANCE) library_h, name);
63 # endif
64 
65  if (!sym)
66  G_fatal_error(_("Unable to locate symbol <%s>"), name);
67 
68  return sym;
69 }
70 
71 static void try_load_library(const char *name)
72 {
73 # ifdef __unix__
74  library_h = dlopen(name, RTLD_NOW);
75 # endif
76 # ifdef _WIN32
77  library_h = LoadLibrary(name);
78 # endif
79 }
80 
81 static void load_library(void)
82 {
83  static const char * const candidates[] = {
84 # ifdef __unix__
85  "libgdal.1.1.so",
86  "gdal.1.0.so",
87  "gdal.so.1.0",
88  "libgdal.so.1",
89  "libgdal.so",
90  "libgdal1.6.0.so",
91  "libgdal1.7.0.so",
92 # endif
93 # ifdef _WIN32
94  "gdal111.dll",
95  "gdal110.dll",
96  "gdal19.dll",
97  "gdal18.dll",
98  "gdal17.dll",
99  "gdal16.dll",
100  "gdal15.dll",
101  "gdal11.dll",
102  "gdal.1.0.dll",
103  "libgdal-1.dll",
104  "gdal.dll",
105 # endif
106  NULL
107  };
108  int i;
109 
110  for (i = 0; candidates[i]; i++) {
111  try_load_library(candidates[i]);
112  if (library_h) {
113  G_debug(3, "found %s", candidates[i]);
114  return;
115  }
116  }
117 
118  G_fatal_error(_("Unable to load GDAL library"));
119 }
120 
121 static void init_gdal(void)
122 {
123  load_library();
124 
125 # if defined(_WIN32) && !defined(_WIN64)
126  pGDALAllRegister = get_symbol("_GDALAllRegister@0");
127  pGDALOpen = get_symbol("_GDALOpen@8");
128  pGDALClose = get_symbol("_GDALClose@4");
129  pGDALGetRasterBand = get_symbol("_GDALGetRasterBand@8");
130  pGDALRasterIO = get_symbol("_GDALRasterIO@48");
131 #else
132  pGDALAllRegister = get_symbol("GDALAllRegister");
133  pGDALOpen = get_symbol("GDALOpen");
134  pGDALClose = get_symbol("GDALClose");
135  pGDALGetRasterBand = get_symbol("GDALGetRasterBand");
136  pGDALRasterIO = get_symbol("GDALRasterIO");
137 #endif
138 }
139 
140 #else /* GDAL_DYNAMIC */
141 
142 static void init_gdal(void)
143 {
144  pGDALAllRegister = &GDALAllRegister;
145  pGDALOpen = &GDALOpen;
146  pGDALClose = &GDALClose;
147  pGDALGetRasterBand = &GDALGetRasterBand;
148  pGDALRasterIO = &GDALRasterIO;
149 }
150 
151 #endif /* GDAL_DYNAMIC */
152 
153 #endif /* GDAL_LINK */
154 
155 struct GDAL_link *G_get_gdal_link(const char *name, const char *mapset)
156 {
157 #ifdef GDAL_LINK
158  static int initialized;
159  GDALDatasetH data;
160  GDALRasterBandH band;
161  GDALDataType type;
162  RASTER_MAP_TYPE req_type;
163 #endif
164  const char *filename;
165  int band_num;
166  struct GDAL_link *gdal;
167  RASTER_MAP_TYPE map_type;
168  FILE *fp;
169  struct Key_Value *key_val;
170  const char *p;
171  DCELL null_val;
172 
173  if (!G_find_cell2(name, mapset))
174  return NULL;
175 
176  map_type = G_raster_map_type(name, mapset);
177  if (map_type < 0)
178  return NULL;
179 
180  fp = G_fopen_old_misc("cell_misc", "gdal", name, mapset);
181  if (!fp)
182  return NULL;
183  key_val = G_fread_key_value(fp);
184  fclose(fp);
185 
186  if (!key_val)
187  return NULL;
188 
189  filename = G_find_key_value("file", key_val);
190  if (!filename)
191  return NULL;
192 
193  p = G_find_key_value("band", key_val);
194  if (!p)
195  return NULL;
196  band_num = atoi(p);
197  if (!band_num)
198  return NULL;
199 
200  p = G_find_key_value("null", key_val);
201  if (!p)
202  return NULL;
203  if (strcmp(p, "none") == 0)
204  G_set_d_null_value(&null_val, 1);
205  else
206  null_val = atof(p);
207 
208 #ifdef GDAL_LINK
209  p = G_find_key_value("type", key_val);
210  if (!p)
211  return NULL;
212  type = atoi(p);
213 
214  switch (type) {
215  case GDT_Byte:
216  case GDT_Int16:
217  case GDT_UInt16:
218  case GDT_Int32:
219  case GDT_UInt32:
220  req_type = CELL_TYPE;
221  break;
222  case GDT_Float32:
223  req_type = FCELL_TYPE;
224  break;
225  case GDT_Float64:
226  req_type = DCELL_TYPE;
227  break;
228  default:
229  return NULL;
230  }
231 
232  if (req_type != map_type)
233  return NULL;
234 
235  if (!initialized) {
236  init_gdal();
237  (*pGDALAllRegister)();
238  initialized = 1;
239  }
240 
241  data = (*pGDALOpen)(filename, GA_ReadOnly);
242  if (!data)
243  return NULL;
244 
245  band = (*pGDALGetRasterBand)(data, band_num);
246  if (!band) {
247  (*pGDALClose)(data);
248  return NULL;
249  }
250 #endif
251 
252  gdal = G_calloc(1, sizeof(struct GDAL_link));
253 
254  gdal->filename = G_store(filename);
255  gdal->band_num = band_num;
256  gdal->null_val = null_val;
257 #ifdef GDAL_LINK
258  gdal->data = data;
259  gdal->band = band;
260  gdal->type = type;
261 #endif
262 
263  return gdal;
264 }
265 
266 void G_close_gdal_link(struct GDAL_link *gdal)
267 {
268 #ifdef GDAL_LINK
269  (*pGDALClose)(gdal->data);
270 #endif
271  G_free(gdal->filename);
272  G_free(gdal);
273 }
274 
275 #ifdef GDAL_LINK
276 CPLErr G_gdal_raster_IO(
277  GDALRasterBandH band, GDALRWFlag rw_flag,
278  int x_off, int y_off, int x_size, int y_size,
279  void *buffer, int buf_x_size, int buf_y_size, GDALDataType buf_type,
280  int pixel_size, int line_size)
281 {
282  return (*pGDALRasterIO)(
283  band, rw_flag, x_off, y_off, x_size, y_size,
284  buffer, buf_x_size, buf_y_size, buf_type,
285  pixel_size, line_size);
286 }
287 #endif
char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key.
Definition: key_value1.c:128
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
char * G_store(const char *s)
Copy string to allocated memory.
Definition: store.c:32
struct Key_Value * G_fread_key_value(FILE *fd)
Read key/values pairs from file.
Definition: key_value2.c:51
string name
Definition: render.py:1314
void G_set_d_null_value(DCELL *dcellVals, int numVals)
Definition: null_val.c:176
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
Definition: open_misc.c:200
tuple data
char * G_find_cell2(const char *name, const char *mapset)
find a raster map (look but don&#39;t touch)
Definition: find_cell.c:83
void G_close_gdal_link(struct GDAL_link *gdal)
Definition: gdal.c:266
int
Definition: g3dcolor.c:48
return NULL
Definition: dbfopen.c:1394
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
fclose(fd)
struct GDAL_link * G_get_gdal_link(const char *name, const char *mapset)
Definition: gdal.c:155
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
RASTER_MAP_TYPE G_raster_map_type(const char *name, const char *mapset)
Determine raster data type.
Definition: opencell.c:1001