GRASS Programmer's Manual
6.4.2(2012)
|
00001 00014 #include <stdlib.h> 00015 #include <string.h> 00016 #include <grass/config.h> 00017 #include <grass/gis.h> 00018 #include <grass/glocale.h> 00019 #include "G.h" 00020 00021 #ifndef HAVE_GDAL 00022 #undef GDAL_LINK 00023 #endif 00024 00025 #ifdef GDAL_LINK 00026 00027 #ifdef GDAL_DYNAMIC 00028 # if defined(__unix) || defined(__unix__) 00029 # include <dlfcn.h> 00030 # endif 00031 # ifdef _WIN32 00032 # include <windows.h> 00033 # endif 00034 #endif 00035 00036 static void CPL_STDCALL (*pGDALAllRegister)(void); 00037 static void CPL_STDCALL (*pGDALClose)(GDALDatasetH); 00038 static GDALRasterBandH CPL_STDCALL (*pGDALGetRasterBand)(GDALDatasetH, int); 00039 static GDALDatasetH CPL_STDCALL (*pGDALOpen)( 00040 const char *pszFilename, GDALAccess eAccess); 00041 static CPLErr CPL_STDCALL (*pGDALRasterIO)( 00042 GDALRasterBandH hRBand, GDALRWFlag eRWFlag, 00043 int nDSXOff, int nDSYOff, int nDSXSize, int nDSYSize, 00044 void * pBuffer, int nBXSize, int nBYSize,GDALDataType eBDataType, 00045 int nPixelSpace, int nLineSpace); 00046 00047 #if GDAL_DYNAMIC 00048 # if defined(__unix) && !defined(__unix__) 00049 # define __unix__ __unix 00050 # endif 00051 00052 static void *library_h; 00053 00054 static void *get_symbol(const char *name) 00055 { 00056 void *sym; 00057 00058 # ifdef __unix__ 00059 sym = dlsym(library_h, name); 00060 # endif 00061 # ifdef _WIN32 00062 sym = GetProcAddress((HINSTANCE) library_h, name); 00063 # endif 00064 00065 if (!sym) 00066 G_fatal_error(_("Unable to locate symbol <%s>"), name); 00067 00068 return sym; 00069 } 00070 00071 static void try_load_library(const char *name) 00072 { 00073 # ifdef __unix__ 00074 library_h = dlopen(name, RTLD_NOW); 00075 # endif 00076 # ifdef _WIN32 00077 library_h = LoadLibrary(name); 00078 # endif 00079 } 00080 00081 static void load_library(void) 00082 { 00083 static const char * const candidates[] = { 00084 # ifdef __unix__ 00085 "libgdal.1.1.so", 00086 "gdal.1.0.so", 00087 "gdal.so.1.0", 00088 "libgdal.so.1", 00089 "libgdal.so", 00090 "libgdal1.6.0.so", 00091 "libgdal1.7.0.so", 00092 # endif 00093 # ifdef _WIN32 00094 "gdal19.dll", 00095 "gdal18.dll", 00096 "gdal17.dll", 00097 "gdal16.dll", 00098 "gdal15.dll", 00099 "gdal11.dll", 00100 "gdal.1.0.dll", 00101 "libgdal-1.dll", 00102 "gdal.dll", 00103 # endif 00104 NULL 00105 }; 00106 int i; 00107 00108 for (i = 0; candidates[i]; i++) { 00109 try_load_library(candidates[i]); 00110 if (library_h) 00111 return; 00112 } 00113 00114 G_fatal_error(_("Unable to load GDAL library")); 00115 } 00116 00117 static void init_gdal(void) 00118 { 00119 load_library(); 00120 00121 # ifdef _WIN32 00122 pGDALAllRegister = get_symbol("_GDALAllRegister@0"); 00123 pGDALOpen = get_symbol("_GDALOpen@8"); 00124 pGDALClose = get_symbol("_GDALClose@4"); 00125 pGDALGetRasterBand = get_symbol("_GDALGetRasterBand@8"); 00126 pGDALRasterIO = get_symbol("_GDALRasterIO@48"); 00127 #else 00128 pGDALAllRegister = get_symbol("GDALAllRegister"); 00129 pGDALOpen = get_symbol("GDALOpen"); 00130 pGDALClose = get_symbol("GDALClose"); 00131 pGDALGetRasterBand = get_symbol("GDALGetRasterBand"); 00132 pGDALRasterIO = get_symbol("GDALRasterIO"); 00133 #endif 00134 } 00135 00136 #else /* GDAL_DYNAMIC */ 00137 00138 static void init_gdal(void) 00139 { 00140 pGDALAllRegister = &GDALAllRegister; 00141 pGDALOpen = &GDALOpen; 00142 pGDALClose = &GDALClose; 00143 pGDALGetRasterBand = &GDALGetRasterBand; 00144 pGDALRasterIO = &GDALRasterIO; 00145 } 00146 00147 #endif /* GDAL_DYNAMIC */ 00148 00149 #endif /* GDAL_LINK */ 00150 00151 struct GDAL_link *G_get_gdal_link(const char *name, const char *mapset) 00152 { 00153 #ifdef GDAL_LINK 00154 static int initialized; 00155 GDALDatasetH data; 00156 GDALRasterBandH band; 00157 GDALDataType type; 00158 RASTER_MAP_TYPE req_type; 00159 #endif 00160 const char *filename; 00161 int band_num; 00162 struct GDAL_link *gdal; 00163 RASTER_MAP_TYPE map_type; 00164 FILE *fp; 00165 struct Key_Value *key_val; 00166 const char *p; 00167 DCELL null_val; 00168 00169 if (!G_find_cell2(name, mapset)) 00170 return NULL; 00171 00172 map_type = G_raster_map_type(name, mapset); 00173 if (map_type < 0) 00174 return NULL; 00175 00176 fp = G_fopen_old_misc("cell_misc", "gdal", name, mapset); 00177 if (!fp) 00178 return NULL; 00179 key_val = G_fread_key_value(fp); 00180 fclose(fp); 00181 00182 if (!key_val) 00183 return NULL; 00184 00185 filename = G_find_key_value("file", key_val); 00186 if (!filename) 00187 return NULL; 00188 00189 p = G_find_key_value("band", key_val); 00190 if (!p) 00191 return NULL; 00192 band_num = atoi(p); 00193 if (!band_num) 00194 return NULL; 00195 00196 p = G_find_key_value("null", key_val); 00197 if (!p) 00198 return NULL; 00199 if (strcmp(p, "none") == 0) 00200 G_set_d_null_value(&null_val, 1); 00201 else 00202 null_val = atof(p); 00203 00204 #ifdef GDAL_LINK 00205 p = G_find_key_value("type", key_val); 00206 if (!p) 00207 return NULL; 00208 type = atoi(p); 00209 00210 switch (type) { 00211 case GDT_Byte: 00212 case GDT_Int16: 00213 case GDT_UInt16: 00214 case GDT_Int32: 00215 case GDT_UInt32: 00216 req_type = CELL_TYPE; 00217 break; 00218 case GDT_Float32: 00219 req_type = FCELL_TYPE; 00220 break; 00221 case GDT_Float64: 00222 req_type = DCELL_TYPE; 00223 break; 00224 default: 00225 return NULL; 00226 } 00227 00228 if (req_type != map_type) 00229 return NULL; 00230 00231 if (!initialized) { 00232 init_gdal(); 00233 (*pGDALAllRegister)(); 00234 initialized = 1; 00235 } 00236 00237 data = (*pGDALOpen)(filename, GA_ReadOnly); 00238 if (!data) 00239 return NULL; 00240 00241 band = (*pGDALGetRasterBand)(data, band_num); 00242 if (!band) { 00243 (*pGDALClose)(data); 00244 return NULL; 00245 } 00246 #endif 00247 00248 gdal = G_calloc(1, sizeof(struct GDAL_link)); 00249 00250 gdal->filename = G_store(filename); 00251 gdal->band_num = band_num; 00252 gdal->null_val = null_val; 00253 #ifdef GDAL_LINK 00254 gdal->data = data; 00255 gdal->band = band; 00256 gdal->type = type; 00257 #endif 00258 00259 return gdal; 00260 } 00261 00262 void G_close_gdal_link(struct GDAL_link *gdal) 00263 { 00264 #ifdef GDAL_LINK 00265 (*pGDALClose)(gdal->data); 00266 #endif 00267 G_free(gdal->filename); 00268 G_free(gdal); 00269 } 00270 00271 #ifdef GDAL_LINK 00272 CPLErr G_gdal_raster_IO( 00273 GDALRasterBandH band, GDALRWFlag rw_flag, 00274 int x_off, int y_off, int x_size, int y_size, 00275 void *buffer, int buf_x_size, int buf_y_size, GDALDataType buf_type, 00276 int pixel_size, int line_size) 00277 { 00278 return (*pGDALRasterIO)( 00279 band, rw_flag, x_off, y_off, x_size, y_size, 00280 buffer, buf_x_size, buf_y_size, buf_type, 00281 pixel_size, line_size); 00282 } 00283 #endif