33 # if defined(__unix) || defined(__unix__) 41 static void CPL_STDCALL(*pGDALAllRegister) (void);
42 static void CPL_STDCALL(*pGDALClose) (GDALDatasetH);
43 static GDALRasterBandH CPL_STDCALL(*pGDALGetRasterBand) (GDALDatasetH, int);
44 static GDALDatasetH CPL_STDCALL(*pGDALOpen) (
const char *pszFilename,
46 static CPLErr CPL_STDCALL(*pGDALRasterIO) (GDALRasterBandH hRBand,
47 GDALRWFlag eRWFlag,
int nDSXOff,
48 int nDSYOff,
int nDSXSize,
49 int nDSYSize,
void *pBuffer,
50 int nBXSize,
int nBYSize,
51 GDALDataType eBDataType,
52 int nPixelSpace,
int nLineSpace);
53 static GDALDriverH CPL_STDCALL(*pGDALGetDriverByName) (
const char *);
54 static const char *CPL_STDCALL(*pGDALGetMetadataItem) (GDALMajorObjectH,
57 static GDALDatasetH CPL_STDCALL(*pGDALCreate) (GDALDriverH hDriver,
58 const char *, int, int, int,
59 GDALDataType,
char **);
60 static GDALDatasetH CPL_STDCALL(*pGDALCreateCopy) (GDALDriverH,
const char *,
61 GDALDatasetH, int,
char **,
62 GDALProgressFunc,
void *);
63 static CPLErr CPL_STDCALL(*pGDALSetRasterNoDataValue) (GDALRasterBandH,
65 static CPLErr CPL_STDCALL(*pGDALSetGeoTransform) (GDALDatasetH,
double *);
66 static CPLErr CPL_STDCALL(*pGDALSetProjection) (GDALDatasetH,
const char *);
67 static const char *CPL_STDCALL(*pGDALGetDriverShortName) (GDALDriverH);
68 static GDALDriverH CPL_STDCALL(*pGDALGetDatasetDriver) (GDALDatasetH);
71 # if defined(__unix) && !defined(__unix__) 72 # define __unix__ __unix 75 static void *library_h;
77 static void *get_symbol(
const char *
name)
82 sym = dlsym(library_h, name);
85 sym = GetProcAddress((HINSTANCE) library_h, name);
94 static void try_load_library(
const char *name)
97 library_h = dlopen(name, RTLD_NOW);
100 library_h = LoadLibrary(name);
104 static void load_library(
void)
106 static const char *
const candidates[] = {
141 for (i = 0; candidates[i]; i++) {
142 try_load_library(candidates[i]);
144 G_debug(3,
"found %s", candidates[i]);
152 static void init_gdal(
void)
156 # if defined(_WIN32) && !defined(_WIN64) 157 pGDALAllRegister = get_symbol(
"_GDALAllRegister@0");
158 pGDALOpen = get_symbol(
"_GDALOpen@8");
159 pGDALClose = get_symbol(
"_GDALClose@4");
160 pGDALGetRasterBand = get_symbol(
"_GDALGetRasterBand@8");
161 pGDALRasterIO = get_symbol(
"_GDALRasterIO@48");
162 pGDALGetDriverByName = get_symbol(
"_GDALGetDriverByName@4");
163 pGDALGetMetadataItem = get_symbol(
"_GDALGetMetadataItem@12");
164 pGDALCreate = get_symbol(
"_GDALCreate@28");
165 pGDALCreateCopy = get_symbol(
"_GDALCreateCopy@28");
166 pGDALSetRasterNoDataValue = get_symbol(
"_GDALSetRasterNoDataValue@12");
167 pGDALSetGeoTransform = get_symbol(
"_GDALSetGeoTransform@8");
168 pGDALSetProjection = get_symbol(
"_GDALSetProjection@8");
169 pGDALGetDriverShortName = get_symbol(
"_GDALGetDriverShortName@4");
170 pGDALGetDatasetDriver = get_symbol(
"_GDALGetDatasetDriver@4");
172 pGDALAllRegister = get_symbol(
"GDALAllRegister");
173 pGDALOpen = get_symbol(
"GDALOpen");
174 pGDALClose = get_symbol(
"GDALClose");
175 pGDALGetRasterBand = get_symbol(
"GDALGetRasterBand");
176 pGDALRasterIO = get_symbol(
"GDALRasterIO");
177 pGDALGetDriverByName = get_symbol(
"GDALGetDriverByName");
178 pGDALGetMetadataItem = get_symbol(
"GDALGetMetadataItem");
179 pGDALCreate = get_symbol(
"GDALCreate");
180 pGDALCreateCopy = get_symbol(
"GDALCreateCopy");
181 pGDALSetRasterNoDataValue = get_symbol(
"GDALSetRasterNoDataValue");
182 pGDALSetGeoTransform = get_symbol(
"GDALSetGeoTransform");
183 pGDALSetProjection = get_symbol(
"GDALSetProjection");
184 pGDALGetDriverShortName = get_symbol(
"GDALGetDriverShortName");
185 pGDALGetDatasetDriver = get_symbol(
"GDALGetDatasetDriver");
191 static void init_gdal(
void)
193 pGDALAllRegister = &GDALAllRegister;
194 pGDALOpen = &GDALOpen;
195 pGDALClose = &GDALClose;
196 pGDALGetRasterBand = &GDALGetRasterBand;
197 pGDALRasterIO = &GDALRasterIO;
198 pGDALGetDriverByName = &GDALGetDriverByName;
199 pGDALGetMetadataItem = &GDALGetMetadataItem;
200 pGDALCreate = &GDALCreate;
201 pGDALCreateCopy = &GDALCreateCopy;
202 pGDALSetRasterNoDataValue = &GDALSetRasterNoDataValue;
203 pGDALSetGeoTransform = &GDALSetGeoTransform;
204 pGDALSetProjection = &GDALSetProjection;
205 pGDALGetDriverShortName = &GDALGetDriverShortName;
206 pGDALGetDatasetDriver = &GDALGetDatasetDriver;
221 static int initialized;
227 (*pGDALAllRegister) ();
245 GDALRasterBandH
band;
290 if (strcmp(p,
"none") == 0 ||
324 if (req_type != map_type)
329 data = (*pGDALOpen) (filename, GA_ReadOnly);
333 band = (*pGDALGetRasterBand) (data, band_num);
335 (*pGDALClose) (data);
367 struct GDAL_Options opts;
368 struct Key_Value *projinfo, *projunits, *projepsg;
374 static void read_gdal_options(
void)
397 if (access(path, 0) != 0)
402 st->opts.ext =
G_store(p ? p :
"");
405 st->opts.format =
G_store(p ? p :
"GTiff");
443 if (st->projinfo && st->projunits)
451 sprintf(path,
"%s/%s%s", st->opts.dir, name, st->opts.ext);
461 gdal->
type = GDT_Byte;
465 gdal->
type = GDT_UInt16;
470 gdal->
type = GDT_Int32;
476 gdal->
type = GDT_Float32;
480 gdal->
type = GDT_Float64;
488 driver = (*pGDALGetDriverByName) (st->opts.format);
493 if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATE,
NULL)) {
497 1, gdal->
type, st->opts.options);
499 G_fatal_error(
_(
"Unable to create <%s> dataset using <%s> driver"),
500 name, st->opts.format);
504 else if ((*pGDALGetMetadataItem) (driver, GDAL_DCAP_CREATECOPY,
NULL)) {
505 GDALDriverH mem_driver;
507 G_message(
_(
"Driver <%s> does not support direct writing. " 508 "Using MEM driver for intermediate dataset."),
511 mem_driver = (*pGDALGetDriverByName) (
"MEM");
516 (*pGDALCreate)(mem_driver,
"",
518 1, gdal->
type, st->opts.options);
520 G_fatal_error(
_(
"Unable to create <%s> dataset using memory driver"),
529 (*pGDALSetRasterNoDataValue) (gdal->
band, gdal->
null_val);
539 if ((*pGDALSetGeoTransform) (gdal->
data, transform) >= CE_Failure)
543 if ((*pGDALSetProjection) (gdal->
data, st->srswkt) == CE_Failure)
548 G_fatal_error(
_(
"Unable to create cell_misc/%s/gdal file"), name);
557 sprintf(buf,
"%.22g", gdal->
null_val);
560 sprintf(buf,
"%d", gdal->
type);
584 (*pGDALClose) (gdal->
data);
603 GDALDriverH src_drv = (*pGDALGetDatasetDriver) (gdal->
data);
605 if (
G_strcasecmp((*pGDALGetDriverShortName) (src_drv),
"MEM") == 0) {
606 GDALDriverH dst_drv = (*pGDALGetDriverByName) (st->opts.format);
612 G_warning(
_(
"Unable to create output file <%s> using driver <%s>"),
619 (*pGDALClose) (gdal->
data);
635 int x_off,
int y_off,
int x_size,
int y_size,
636 void *buffer,
int buf_x_size,
int buf_y_size,
637 GDALDataType buf_type,
int pixel_size,
640 return (*pGDALRasterIO) (band, rw_flag, x_off, y_off, x_size, y_size,
641 buffer, buf_x_size, buf_y_size, buf_type,
642 pixel_size, line_size);
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
struct Key_Value * G_get_projinfo(void)
Gets projection information for location.
CPLErr Rast_gdal_raster_IO(GDALRasterBandH, GDALRWFlag, int, int, int, int, void *, int, int, GDALDataType, int, int)
int Rast_close_gdal_write_link(struct GDAL_link *gdal)
Close existing GDAL link and write out data.
double west
Extent coordinates (west)
struct Key_Value * G_fread_key_value(FILE *)
Read key/values pairs from file.
void G_free(void *)
Free allocated memory.
char ** G_tokenize(const char *, const char *)
Tokenize string.
void Rast__init_window(void)
void G_initialize_done(int *)
struct Cell_head wr_window
struct Key_Value * G_get_projunits(void)
Gets units information for location.
void G_message(const char *,...) __attribute__((format(printf
double north
Extent coordinates (north)
struct GDAL_link * Rast_create_gdal_link(const char *name, RASTER_MAP_TYPE map_type)
Create GDAL settings for given raster map.
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
char * GPJ_grass_to_wkt2(const struct Key_Value *, const struct Key_Value *, const struct Key_Value *, int, int)
Converts a GRASS co-ordinate system representation to WKT style. EPSG code is preferred if available...
FILE * G_fopen_old_misc(const char *, const char *, const char *, const char *)
open a database file for reading
const struct driver * driver
void G_set_key_value(const char *, const char *, struct Key_Value *)
Set value for given key.
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
int G_fwrite_key_value(FILE *, const struct Key_Value *)
Write key/value pairs to file.
int cols
Number of columns for 2D data.
int G_is_initialized(int *)
FILE * G_fopen_new_misc(const char *, const char *, const char *)
open a new database file
double ns_res
Resolution - north to south cell size for 2D data.
const char * G_mapset(void)
Get current mapset name.
void G_warning(const char *,...) __attribute__((format(printf
void Rast_init_gdal(void)
Initialization.
int G_make_mapset_element(const char *)
Create element in the current mapset.
const char * G_find_raster2(const char *, const char *)
Find a raster map (look but don't touch)
char * G_store(const char *)
Copy string to allocated memory.
void Rast_close_gdal_link(struct GDAL_link *gdal)
Close existing GDAL link.
struct Key_Value * G_create_key_value(void)
Allocate and initialize Key_Value structure.
double ew_res
Resolution - east to west cell size for 2D data.
int rows
Number of rows for 2D data.
int G_debug(int, const char *,...) __attribute__((format(printf
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
struct Key_Value * G_get_projepsg(void)
Gets EPSG information for the current location.
struct GDAL_link * Rast_get_gdal_link(const char *name, const char *mapset)
Get GDAL link settings for given raster map.
const char * G_find_key_value(const char *, const struct Key_Value *)
Find given key (case sensitive)
RASTER_MAP_TYPE Rast_map_type(const char *, const char *)
Determine raster data type.