1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/******************************************************************************
* $Id: rasdamandataset.cpp 23362 2011-11-11 11:11:52Z rouault $
* Project: rasdaman Driver
* Purpose: Implement Rasdaman GDAL driver
* Author: Constantin Jucovschi, jucovschi@yahoo.com
*
******************************************************************************
* Copyright (c) 2010, Constantin Jucovschi
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include "gdal_pam.h"
#include "cpl_string.h"
#include "regex.h"
#include <string>
#include <memory>
#define __EXECUTABLE__
#define EARLY_TEMPLATE
#include "raslib/template_inst.hh"
#include "raslib/structuretype.hh"
#include "raslib/type.hh"
#include "rasodmg/database.hh"
CPL_CVSID("$Id: rasdamandataset.cpp 23362 2011-11-11 11:11:52Z rouault $");
CPL_C_START
void GDALRegister_RASDAMAN(void);
CPL_C_END
/************************************************************************/
/* ==================================================================== */
/* RasdamanDataset */
/* ==================================================================== */
/************************************************************************/
class RasdamanRasterBand;
static CPLString getQuery(const char *templateString, const char* x_lo, const char* x_hi, const char* y_lo, const char* y_hi);
class RasdamanDataset : public GDALPamDataset
{
friend class RasdamanRasterBand;
public:
~RasdamanDataset();
static GDALDataset *Open( GDALOpenInfo * );
private:
void getTypes(const r_Base_Type* baseType, int &counter, int pos);
void createBands(const char* queryString);
CPLString queryParam;
CPLString host;
int port;
CPLString username;
CPLString userpassword;
CPLString databasename;
int xPos;
int yPos;
int tileXSize;
int tileYSize;
};
/************************************************************************/
/* ==================================================================== */
/* RasdamanRasterBand */
/* ==================================================================== */
/************************************************************************/
class RasdamanRasterBand : public GDALPamRasterBand
{
friend class RasdamanDataset;
int nRecordSize;
int typeOffset;
int typeSize;
public:
RasdamanRasterBand( RasdamanDataset *, int, GDALDataType type, int offset, int size, int nBlockXSize, int nBlockYSize );
~RasdamanRasterBand();
virtual CPLErr IReadBlock( int, int, void * );
};
/************************************************************************/
/* RasdamanRasterBand() */
/************************************************************************/
RasdamanRasterBand::RasdamanRasterBand( RasdamanDataset *poDS, int nBand, GDALDataType type, int offset, int size, int nBlockXSize, int nBlockYSize )
{
this->poDS = poDS;
this->nBand = nBand;
eDataType = type;
typeSize = size;
typeOffset = offset;
this->nBlockXSize = nBlockXSize;
this->nBlockYSize = nBlockYSize;
nRecordSize = nBlockXSize * nBlockYSize * typeSize;
}
/************************************************************************/
/* ~RasdamanRasterBand() */
/************************************************************************/
RasdamanRasterBand::~RasdamanRasterBand()
{
}
/************************************************************************/
/* IReadBlock() */
/************************************************************************/
CPLErr RasdamanRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff,
void * pImage )
{
//cerr << "Read block " << nBlockXOff << " " << nBlockYOff << endl;
RasdamanDataset *poGDS = (RasdamanDataset *) poDS;
r_Database database;
r_Transaction transaction;
memset(pImage, 0, nRecordSize);
try {
database.set_servername(poGDS->host, poGDS->port);
database.set_useridentification(poGDS->username, poGDS->userpassword);
database.open(poGDS->databasename);
transaction.begin();
char x_lo[11], x_hi[11], y_lo[11], y_hi[11];
int xPos = poGDS->xPos;
int yPos = poGDS->yPos;
sprintf(x_lo, "%d", nBlockXOff * nBlockXSize);
sprintf(x_hi, "%d", (nBlockXOff+1) * nBlockXSize-1);
sprintf(y_lo, "%d", nBlockYOff * nBlockYSize);
sprintf(y_hi, "%d", (nBlockYOff+1) * nBlockYSize-1);
CPLString queryString = getQuery(poGDS->queryParam, x_lo, x_hi, y_lo, y_hi);
r_Set<r_Ref_Any> result_set;
r_OQL_Query query (queryString);
r_oql_execute (query, result_set);
if (result_set.get_element_type_schema()->type_id() == r_Type::MARRAYTYPE) {
r_Iterator<r_Ref_Any> iter = result_set.create_iterator();
r_Ref<r_GMarray> gmdd = r_Ref<r_GMarray>(*iter);
r_Minterval sp = gmdd->spatial_domain();
r_Point extent = sp.get_extent();
r_Point base = sp.get_origin();
int tileX = extent[xPos];
int tileY = extent[yPos];
r_Point access = base;
char *resultPtr;
for (int j=0; j<tileY; ++j) {
for (int i=0; i<tileX; ++i) {
resultPtr = (char*)pImage + (j*nBlockYSize+i)*typeSize;
access[xPos] = base[xPos]+i;
access[yPos] = base[yPos]+j;
const char *data = (*gmdd)[access] + typeOffset;
memcpy(resultPtr, data, typeSize);
}
}
}
transaction.commit();
database.close();
} catch (r_Error error) {
CPLError(CE_Failure, CPLE_AppDefined, "%s", error.what());
return CPLGetLastErrorType();
}
return CE_None;
}
/************************************************************************/
/* ==================================================================== */
/* RasdamanDataset */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* ~RasdamanDataset() */
/************************************************************************/
RasdamanDataset::~RasdamanDataset()
{
FlushCache();
}
/************************************************************************/
/* GetProjectionRef() */
/************************************************************************/
static CPLString getOption(const char *string, regmatch_t cMatch, const char* defaultValue) {
if (cMatch.rm_eo == -1 || cMatch.rm_so == -1)
return defaultValue;
char *result = new char[cMatch.rm_eo-cMatch.rm_so+1];
strncpy(result, string+cMatch.rm_so, cMatch.rm_eo-cMatch.rm_so);
result[cMatch.rm_eo-cMatch.rm_so]=0;
CPLString osResult = result;
delete[] result;
return osResult;
}
static int getOption(const char *string, regmatch_t cMatch, int defaultValue) {
if (cMatch.rm_eo == -1 || cMatch.rm_so == -1)
return defaultValue;
char *result = new char[cMatch.rm_eo-cMatch.rm_so+1];
strncpy(result, string+cMatch.rm_so, cMatch.rm_eo-cMatch.rm_so);
result[cMatch.rm_eo-cMatch.rm_so]=0;
int nRet = atoi(result);
delete[] result;
return nRet;
}
static CPLString getQuery(const char *templateString, const char* x_lo, const char* x_hi, const char* y_lo, const char* y_hi) {
static regex_t* replaceRegEx = NULL;
regmatch_t match[3];
if (replaceRegEx == NULL) {
replaceRegEx = new regex_t;
regcomp(replaceRegEx, "\\$(x|y)_(lo|hi)", REG_EXTENDED);
}
int pos = 0, resPos = 0;
char *result = new char[strlen(templateString)*2];
while (regexec(replaceRegEx, templateString+pos, 3, match, 0) == 0) {
strncpy(result+resPos, templateString+pos, match[1].rm_so-1);
resPos += match[1].rm_so-1;
if (templateString[pos+match[1].rm_so]=='x') {
if (templateString[pos+match[2].rm_so]=='h') {
strcpy(result+resPos, x_hi); resPos+=strlen(x_hi);
} else {
strcpy(result+resPos, x_lo); resPos+=strlen(x_lo);
}
} else {
if (templateString[pos+match[2].rm_so]=='h') {
strcpy(result+resPos, y_hi); resPos+=strlen(y_hi);
} else {
strcpy(result+resPos, y_lo); resPos+=strlen(y_lo);
}
}
pos += match[2].rm_eo;
}
int rest = strlen(templateString+pos);
strncpy(result+resPos, templateString+pos, rest);
resPos += rest;
result[resPos]='\0';
CPLString osResult = result;
delete[] result;
return osResult;
}
GDALDataType mapRasdamanTypesToGDAL(r_Type::r_Type_Id typeId) {
switch (typeId) {
case r_Type::ULONG:
return GDT_UInt32;
case r_Type::LONG:
return GDT_Int32;
case r_Type::SHORT:
return GDT_Int16;
case r_Type::USHORT:
return GDT_UInt16;
case r_Type::BOOL:
case r_Type::CHAR:
return GDT_Byte;
case r_Type::DOUBLE:
return GDT_Float64;
case r_Type::FLOAT:
return GDT_Float32;
case r_Type::COMPLEXTYPE1:
return GDT_CFloat32;
case r_Type::COMPLEXTYPE2:
return GDT_CFloat64;
default:
return GDT_Unknown;
}
}
void RasdamanDataset::getTypes(const r_Base_Type* baseType, int &counter, int pos) {
if (baseType->isStructType()) {
r_Structure_Type* tp = (r_Structure_Type*) baseType;
int elem = tp->count_elements();
for (int i=0; i<elem; ++i) {
r_Attribute attr = (*tp)[i];
getTypes(&attr.type_of(), counter, attr.global_offset());
}
}
if (baseType->isPrimitiveType()) {
r_Primitive_Type *primType = (r_Primitive_Type*)baseType;
r_Type::r_Type_Id typeId = primType->type_id();
SetBand(counter, new RasdamanRasterBand(this, counter, mapRasdamanTypesToGDAL(typeId), pos, primType->size(), this->tileXSize, this->tileYSize));
counter ++;
}
}
void RasdamanDataset::createBands(const char* queryString) {
r_Set<r_Ref_Any> result_set;
r_OQL_Query query (queryString);
r_oql_execute (query, result_set);
if (result_set.get_element_type_schema()->type_id() == r_Type::MARRAYTYPE) {
r_Iterator<r_Ref_Any> iter = result_set.create_iterator();
r_Ref<r_GMarray> gmdd = r_Ref<r_GMarray>(*iter);
const r_Base_Type* baseType = gmdd->get_base_type_schema();
int counter = 1;
getTypes(baseType, counter, 0);
}
}
static int getExtent(const char *queryString, int &pos) {
r_Set<r_Ref_Any> result_set;
r_OQL_Query query (queryString);
r_oql_execute (query, result_set);
if (result_set.get_element_type_schema()->type_id() == r_Type::MINTERVALTYPE) {
r_Iterator<r_Ref_Any> iter = result_set.create_iterator();
r_Ref<r_Minterval> interv = r_Ref<r_Minterval>(*iter);
r_Point extent = interv->get_extent();
int dim = extent.dimension();
int result = -1;
for (int i=0; i<dim; ++i) {
if (extent[i] == 1)
continue;
if (result != -1)
return -1;
result = extent[i];
pos = i;
}
if (result == -1)
return 1;
else
return result;
} else
return -1;
}
/************************************************************************/
/* Open() */
/************************************************************************/
GDALDataset *RasdamanDataset::Open( GDALOpenInfo * poOpenInfo )
{
// buffer to communicate errors
char errbuffer[4096];
// fast checks if current module should handle the request
// check 1: the request is not on a existing file in the file system
if (poOpenInfo->fp != NULL) {
return NULL;
}
// check 2: the request contains --collection
char* connString = poOpenInfo->pszFilename;
if (!EQUALN(connString, "rasdaman", 8)) {
return NULL;
}
// regex for parsing options
regex_t optionRegEx;
// regex for parsing query
regex_t queryRegEx;
// array to store matching subexpressions
regmatch_t matches[10];
#define QUERY_POSITION 2
#define SERVER_POSITION 3
#define PORT_POSITION 4
#define USERNAME_POSITION 5
#define USERPASSWORD_POSITION 6
#define DATABASE_POSITION 7
#define TILEXSIZE_POSITION 8
#define TILEYSIZE_POSITION 9
int result = regcomp(&optionRegEx, "^rasdaman:(query='([[:alnum:][:punct:] ]+)'|host='([[:alnum:]]+)'|port=([0-9]+)|user='([[:alnum:]]+)'|password='([[:alnum:]]+)'|database '([[:alnum:]]+)'|tileXSize=([0-9]+)|tileYSize=([0-9]+)| )*", REG_EXTENDED);
// should never happen
if (result != 0) {
regerror(result, &optionRegEx, errbuffer, 4096);
CPLError(CE_Failure, CPLE_AppDefined, "Internal error at compiling option parsing regex: %s", errbuffer);
return NULL;
}
result = regcomp(&queryRegEx, "^select ([[:alnum:][:punct:] ]*) from ([[:alnum:][:punct:] ]*)$", REG_EXTENDED);
// should never happen
if (result != 0) {
regerror(result, &queryRegEx, errbuffer, 4096);
CPLError(CE_Failure, CPLE_AppDefined, "Internal error at compiling option parsing regex: %s", errbuffer);
return NULL;
}
// executing option parsing regex on the connection string and checking if it succeeds
result = regexec(&optionRegEx, connString, 10, matches, 0);
if (result != 0) {
regerror(result, &optionRegEx, errbuffer, 4096);
CPLError(CE_Failure, CPLE_AppDefined, "Parsing opening parameters failed with error: %s", errbuffer);
regfree(&optionRegEx);
regfree(&queryRegEx);
return NULL;
}
regfree(&optionRegEx);
// checking if the whole expressions was matches, if not give an error where
// the matching stopped and exit
if (size_t(matches[0].rm_eo) < strlen(connString)) {
CPLError(CE_Failure, CPLE_AppDefined, "Parsing opening parameters failed with error: %s", connString+matches[0].rm_eo);
regfree(&queryRegEx);
return NULL;
}
CPLString queryParam = getOption(connString, matches[QUERY_POSITION], (const char*)NULL);
CPLString host = getOption(connString, matches[SERVER_POSITION], "localhost");
int port = getOption(connString, matches[PORT_POSITION], 7001);
CPLString username = getOption(connString, matches[USERNAME_POSITION], "rasguest");
CPLString userpassword = getOption(connString, matches[USERPASSWORD_POSITION], "rasguest");
CPLString databasename = getOption(connString, matches[DATABASE_POSITION], "RASBASE");
int tileXSize = getOption(connString, matches[TILEXSIZE_POSITION], 1024);
int tileYSize = getOption(connString, matches[TILEYSIZE_POSITION], 1024);
result = regexec(&queryRegEx, queryParam, 10, matches, 0);
if (result != 0) {
regerror(result, &queryRegEx, errbuffer, 4096);
CPLError(CE_Failure, CPLE_AppDefined, "Parsing query parameter failed with error: %s", errbuffer);
regfree(&queryRegEx);
return NULL;
}
regfree(&queryRegEx);
CPLString osQueryString = "select sdom(";
osQueryString += getOption(queryParam, matches[1], "");
osQueryString += ") from ";
osQueryString += getOption(queryParam, matches[2], "");
CPLString queryX = getQuery(osQueryString, "*", "*", "0", "0");
CPLString queryY = getQuery(osQueryString, "0", "0", "*", "*");
CPLString queryUnit = getQuery(queryParam, "0", "0", "0", "0");
stringstream queryStream;
r_Transaction transaction;
RasdamanDataset *rasDataset = new RasdamanDataset();
r_Database database;
try {
database.set_servername(host, port);
database.set_useridentification(username, userpassword);
database.open(databasename);
transaction.begin();
int dimX = getExtent(queryX, rasDataset->xPos);
int dimY = getExtent(queryY, rasDataset->yPos);
rasDataset->nRasterXSize = dimX;
rasDataset->nRasterYSize = dimY;
rasDataset->tileXSize = tileXSize;
rasDataset->tileYSize = tileYSize;
rasDataset->createBands(queryUnit);
transaction.commit();
database.close();
} catch (r_Error error) {
CPLError(CE_Failure, CPLE_AppDefined, "%s", error.what());
delete rasDataset;
return NULL;
}
rasDataset->queryParam = queryParam;
rasDataset->host = host;
rasDataset->port = port;
rasDataset->username = username;
rasDataset->userpassword = userpassword;
rasDataset->databasename = databasename;
return rasDataset;
}
/************************************************************************/
/* GDALRegister_RASDAMAN() */
/************************************************************************/
extern void GDALRegister_RASDAMAN()
{
GDALDriver *poDriver;
if( GDALGetDriverByName( "RASDAMAN" ) == NULL )
{
poDriver = new GDALDriver();
poDriver->SetDescription( "RASDAMAN" );
poDriver->SetMetadataItem( GDAL_DMD_LONGNAME,
"RASDAMAN" );
poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC,
"frmt_rasdaman.html" );
poDriver->pfnOpen = RasdamanDataset::Open;
GetGDALDriverManager()->RegisterDriver( poDriver );
}
}
|