[GRASS-SVN] r33559 - grass/trunk/lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 26 11:55:46 EDT 2008


Author: glynn
Date: 2008-09-26 11:55:45 -0400 (Fri, 26 Sep 2008)
New Revision: 33559

Modified:
   grass/trunk/lib/gis/G.h
   grass/trunk/lib/gis/Makefile
   grass/trunk/lib/gis/gdal.c
   grass/trunk/lib/gis/get_row.c
Log:
Allow GDAL to be loaded dynamically


Modified: grass/trunk/lib/gis/G.h
===================================================================
--- grass/trunk/lib/gis/G.h	2008-09-26 14:54:34 UTC (rev 33558)
+++ grass/trunk/lib/gis/G.h	2008-09-26 15:55:45 UTC (rev 33559)
@@ -30,6 +30,14 @@
 #endif
 };
 
+#ifdef HAVE_GDAL
+extern CPLErr G_gdal_raster_IO(
+    GDALRasterBandH, GDALRWFlag,
+    int, int, int, int,
+    void *, int, int, GDALDataType,
+    int, int);
+#endif
+
 struct fileinfo			/* Information for opened cell files */
 {
     int open_mode;		/* see defines below            */

Modified: grass/trunk/lib/gis/Makefile
===================================================================
--- grass/trunk/lib/gis/Makefile	2008-09-26 14:54:34 UTC (rev 33558)
+++ grass/trunk/lib/gis/Makefile	2008-09-26 15:55:45 UTC (rev 33559)
@@ -1,5 +1,8 @@
 MODULE_TOPDIR = ../..
 
+GDAL_LINK = $(USE_GDAL)
+GDAL_DYNAMIC = 1
+
 LIB_NAME = $(GIS_LIBNAME)
 EXTRA_LIBS = $(XDRLIB) $(SOCKLIB) $(DATETIMELIB) $(INTLLIB) $(MATHLIB)
 DATASRC = ellipse.table datum.table datumtransform.table FIPS.code state27 state83 projections gui.tcl
@@ -17,12 +20,23 @@
 	EXTRA_CFLAGS = -D_FILE_OFFSET_BITS=64
 endif
 
-ifneq ($(USE_GDAL),)
-EXTRA_CFLAGS += -DGDAL_LINK
+ifneq ($(GDAL_LINK),)
+EXTRA_CFLAGS += -DGDAL_LINK=1
 EXTRA_INC += $(PROJINC) $(GDALCFLAGS)
+
+ifneq ($(GDAL_DYNAMIC),)
+EXTRA_CFLAGS += -DGDAL_DYNAMIC=1
+ifneq ($(MINGW),)
+EXTRA_LIBS += -lkernel32
+else
+EXTRA_LIBS += $(DLLIB)
+endif
+else
 EXTRA_LIBS += $(GDALLIBS)
 endif
 
+endif
+
 default: lib $(FMODE_OBJ) $(DATAFILES) $(COLORFILES) $(ETC)/colors.desc $(ETC)/element_list
 
 $(FMODE_OBJ): fmode.dat

Modified: grass/trunk/lib/gis/gdal.c
===================================================================
--- grass/trunk/lib/gis/gdal.c	2008-09-26 14:54:34 UTC (rev 33558)
+++ grass/trunk/lib/gis/gdal.c	2008-09-26 15:55:45 UTC (rev 33559)
@@ -3,11 +3,126 @@
 #include <string.h>
 #include <grass/config.h>
 #include <grass/gis.h>
+#include <grass/glocale.h>
 #include "G.h"
 
+#ifndef HAVE_GDAL
+#undef GDAL_LINK
+#endif
+
+#ifdef GDAL_LINK
+
+#ifdef GDAL_DYNAMIC
+# if defined(__unix) || defined(__unix__)
+#  include <dlfcn.h>
+# endif
+# ifdef _WIN32
+#  include <windows.h>
+# endif
+#endif
+
+static void CPL_STDCALL (*pGDALAllRegister)(void);
+static void CPL_STDCALL (*pGDALClose)(GDALDatasetH);
+static GDALRasterBandH CPL_STDCALL (*pGDALGetRasterBand)(GDALDatasetH, int);
+static GDALDatasetH CPL_STDCALL (*pGDALOpen)(
+    const char *pszFilename, GDALAccess eAccess);
+static CPLErr CPL_STDCALL (*pGDALRasterIO)(
+    GDALRasterBandH hRBand, GDALRWFlag eRWFlag,
+    int nDSXOff, int nDSYOff, int nDSXSize, int nDSYSize,
+    void * pBuffer, int nBXSize, int nBYSize,GDALDataType eBDataType,
+    int nPixelSpace, int nLineSpace);
+
+#if GDAL_DYNAMIC
+# if defined(__unix) && !defined(__unix__)
+#  define __unix__ __unix
+# endif
+
+static void *library_h;
+
+static void *get_symbol(const char *name)
+{
+    void *sym;
+
+# ifdef __unix__
+    sym = dlsym(library_h, name);
+# endif
+# ifdef _WIN32
+    sym = GetProcAddress((HINSTANCE) library_h, name);
+# endif
+
+    if (!sym)
+	G_fatal_error(_("Unable to locate symbol <%s>"), name);
+
+    return sym;
+}
+
+static void try_load_library(const char *name)
+{
+# ifdef __unix__
+    library_h = dlopen(name, RTLD_NOW);
+# endif
+# ifdef _WIN32
+    library_h = LoadLibrary(name);
+# endif
+}
+
+static void load_library(void)
+{
+    static const char * const candidates[] = {
+# ifdef __unix__
+	"libgdal.1.1.so",
+	"gdal.1.0.so",
+	"gdal.so.1.0",
+	"libgdal.so.1",
+	"libgdal.so",
+# endif
+# ifdef _WIN32
+	"gdal11.dll",
+	"gdal.1.0.dll",
+	"gdal.dll",
+# endif
+	NULL
+    };
+    int i;
+
+    for (i = 0; candidates[i]; i++) {
+	try_load_library(candidates[i]);
+	if (library_h)
+	    return;
+    }
+
+    G_fatal_error(_("Unable to load GDAL library"));
+}
+
+static void init_gdal(void)
+{
+    load_library();
+
+    pGDALAllRegister   = get_symbol("GDALAllRegister");
+    pGDALOpen          = get_symbol("GDALOpen");
+    pGDALClose         = get_symbol("GDALClose");
+    pGDALGetRasterBand = get_symbol("GDALGetRasterBand");
+    pGDALRasterIO      = get_symbol("GDALRasterIO");
+}
+
+#else /* GDAL_DYNAMIC */
+
+static void init_gdal(void)
+{
+    pGDALAllRegister   = &GDALAllRegister;
+    pGDALOpen          = &GDALOpen;
+    pGDALClose         = &GDALClose;
+    pGDALGetRasterBand = &GDALGetRasterBand;
+    pGDALRasterIO      = &GDALRasterIO;
+}
+
+#endif /* GDAL_DYNAMIC */
+
+#endif /* GDAL_LINK */
+
 struct GDAL_link *G_get_gdal_link(const char *name, const char *mapset)
 {
-#ifdef HAVE_GDAL
+#ifdef GDAL_LINK
     static int initialized;
     GDALDatasetH data;
     GDALRasterBandH band;
@@ -58,7 +173,7 @@
     else
 	null_val = atof(p);
 
-#ifdef HAVE_GDAL
+#ifdef GDAL_LINK
     p = G_find_key_value("type", key_val);
     if (!p)
 	return NULL;
@@ -86,17 +201,18 @@
 	return NULL;
 
     if (!initialized) {
-	GDALAllRegister();
+	init_gdal();
+	(*pGDALAllRegister)();
 	initialized = 1;
     }
 
-    data = GDALOpen(filename, GA_ReadOnly);
+    data = (*pGDALOpen)(filename, GA_ReadOnly);
     if (!data)
 	return NULL;
 
-    band = GDALGetRasterBand(data, band_num);
+    band = (*pGDALGetRasterBand)(data, band_num);
     if (!band) {
-	GDALClose(data);
+	(*pGDALClose)(data);
 	return NULL;
     }
 #endif
@@ -106,7 +222,7 @@
     gdal->filename = G_store(filename);
     gdal->band_num = band_num;
     gdal->null_val = null_val;
-#ifdef HAVE_GDAL
+#ifdef GDAL_LINK
     gdal->data = data;
     gdal->band = band;
     gdal->type = type;
@@ -117,9 +233,23 @@
 
 void G_close_gdal_link(struct GDAL_link *gdal)
 {
-#ifdef HAVE_GDAL
-    GDALClose(gdal->data);
+#ifdef GDAL_LINK
+    (*pGDALClose)(gdal->data);
 #endif
     G_free(gdal->filename);
     G_free(gdal);
 }
+
+#ifdef GDAL_LINK
+CPLErr G_gdal_raster_IO(
+    GDALRasterBandH band, GDALRWFlag rw_flag,
+    int x_off, int y_off, int x_size, int y_size,
+    void *buffer, int buf_x_size, int buf_y_size, GDALDataType buf_type,
+    int pixel_size, int line_size)
+{
+    return (*pGDALRasterIO)(
+	band, rw_flag, x_off, y_off, x_size, y_size,
+	buffer, buf_x_size, buf_y_size, buf_type,
+	pixel_size, line_size);
+}
+#endif

Modified: grass/trunk/lib/gis/get_row.c
===================================================================
--- grass/trunk/lib/gis/get_row.c	2008-09-26 14:54:34 UTC (rev 33558)
+++ grass/trunk/lib/gis/get_row.c	2008-09-26 15:55:45 UTC (rev 33559)
@@ -206,7 +206,7 @@
 
     *nbytes = fcb->nbytes;
 
-    err = GDALRasterIO(
+    err = G_gdal_raster_IO(
 	fcb->gdal->band, GF_Read, 0, row, fcb->cellhd.cols, 1, data_buf,
 	fcb->cellhd.cols, 1, fcb->gdal->type, 0, 0);
 



More information about the grass-commit mailing list