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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
|
/******************************************************************************
*
* File : pgchipdataset.cpp
* Project: PGCHIP Driver
* Purpose: GDALDataset code for POSTGIS CHIP/GDAL Driver
* Author: Benjamin Simon, noumayoss@gmail.com
*
******************************************************************************
* Copyright (c) 2005, Benjamin Simon, noumayoss@gmail.com
*
* 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.
******************************************************************************
*
* Revision 1.1 2005/08/29 bsimon
* New
*
*/
#include "pgchip.h"
/* Define to enable debugging info */
/*#define PGCHIP_DEBUG 1*/
CPL_C_START
void GDALRegister_PGCHIP(void);
CPL_C_END
/************************************************************************/
/* ==================================================================== */
/* PGCHIPDataset */
/* ==================================================================== */
/************************************************************************/
/************************************************************************/
/* PGCHIPDataset() */
/************************************************************************/
PGCHIPDataset::PGCHIPDataset(){
hPGConn = NULL;
pszTableName = NULL;
pszDSName = NULL;
PGCHIP = NULL;
bGeoTransformValid = FALSE;
adfGeoTransform[0] = 0.0;
adfGeoTransform[1] = 1.0;
adfGeoTransform[2] = 0.0;
adfGeoTransform[3] = 0.0;
adfGeoTransform[4] = 0.0;
adfGeoTransform[5] = 1.0;
SRID = -1;
pszProjection = CPLStrdup("");
bHaveNoData = FALSE;
dfNoDataValue = -1;
}
/************************************************************************/
/* ~PGCHIPDataset() */
/************************************************************************/
PGCHIPDataset::~PGCHIPDataset(){
if( hPGConn != NULL )
{
/* XXX - mloskot: After the connection is closed, valgrind still
* reports 36 bytes definitely lost, somewhere in the libpq.
*/
PQfinish( hPGConn );
hPGConn = NULL;
}
CPLFree(pszProjection);
CPLFree(pszTableName);
CPLFree(pszDSName);
CPLFree(PGCHIP);
}
/************************************************************************/
/* GetGeoTransform() */
/************************************************************************/
CPLErr PGCHIPDataset::GetGeoTransform( double * padfTransform ){
memcpy( padfTransform, adfGeoTransform, sizeof(adfGeoTransform[0]) * 6 );
if( bGeoTransformValid )
return CE_None;
else
return CE_Failure;
}
/************************************************************************/
/* GetProjectionRef() */
/************************************************************************/
const char *PGCHIPDataset::GetProjectionRef(){
char szCommand[1024];
PGresult *hResult;
if(SRID == -1)
{
return "";
}
/* -------------------------------------------------------------------- */
/* Reading proj */
/* -------------------------------------------------------------------- */
sprintf( szCommand,"SELECT srtext FROM spatial_ref_sys where SRID=%d",SRID);
hResult = PQexec(hPGConn,szCommand);
if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK
&& PQntuples(hResult) > 0 )
{
CPLFree(pszProjection);
pszProjection = CPLStrdup(PQgetvalue(hResult,0,0));
}
if( hResult )
PQclear( hResult );
return pszProjection;
}
/************************************************************************/
/* PGChipOpenConnection() */
/************************************************************************/
static
PGconn* PGChipOpenConnection(const char* pszFilename, char** ppszTableName, const char** ppszDSName,
int bExitOnMissingTable, int* pbExistTable, int *pbHasNameCol)
{
char *pszConnectionString;
PGconn *hPGConn;
PGresult *hResult = NULL;
int i=0;
int bHavePostGIS;
char *pszTableName;
char szCommand[1024];
if( pszFilename == NULL || !EQUALN(pszFilename,"PG:",3))
return NULL;
pszConnectionString = CPLStrdup(pszFilename);
/* -------------------------------------------------------------------- */
/* Try to establish connection. */
/* -------------------------------------------------------------------- */
while(pszConnectionString[i] != '\0')
{
if(pszConnectionString[i] == '#')
pszConnectionString[i] = ' ';
else if(pszConnectionString[i] == '%')
{
pszConnectionString[i] = '\0';
break;
}
i++;
}
hPGConn = PQconnectdb( pszConnectionString + 3 );
if( hPGConn == NULL || PQstatus(hPGConn) == CONNECTION_BAD )
{
CPLError( CE_Failure, CPLE_AppDefined,
"PGconnectcb failed.\n%s",
PQerrorMessage(hPGConn) );
PQfinish(hPGConn);
CPLFree(pszConnectionString);
return NULL;
}
/* -------------------------------------------------------------------- */
/* Test to see if this database instance has support for the */
/* PostGIS Geometry type. If so, disable sequential scanning */
/* so we will get the value of the gist indexes. */
/* -------------------------------------------------------------------- */
hResult = PQexec(hPGConn,
"SELECT oid FROM pg_type WHERE typname = 'geometry'" );
if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK
&& PQntuples(hResult) > 0 )
{
bHavePostGIS = TRUE;
}
if( hResult )
PQclear( hResult );
if(!bHavePostGIS){
CPLError( CE_Failure, CPLE_AppDefined,
"Can't find geometry type, is Postgis correctly installed ?\n");
PQfinish(hPGConn);
CPLFree(pszConnectionString);
return NULL;
}
/* -------------------------------------------------------------------- */
/* Try opening the layer */
/* -------------------------------------------------------------------- */
if( strstr(pszFilename, "layer=") != NULL )
{
pszTableName = CPLStrdup( strstr(pszFilename, "layer=") + 6 );
}
else
{
pszTableName = CPLStrdup("unknown_layer");
}
char* pszDSName = strstr(pszTableName, "%name=");
if (pszDSName)
{
*pszDSName = '\0';
pszDSName += 6;
}
else
pszDSName = "unknown_name";
sprintf( szCommand,
"select b.attname from pg_class a,pg_attribute b where a.oid=b.attrelid and a.relname='%s' and b.attname='raster';",
pszTableName);
hResult = PQexec(hPGConn,szCommand);
if( ! (hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK && PQntuples(hResult) > 0) )
{
if (pbExistTable)
*pbExistTable = FALSE;
if (bExitOnMissingTable)
{
if (hResult)
PQclear( hResult );
CPLFree(pszConnectionString);
CPLFree(pszTableName);
PQfinish(hPGConn);
return NULL;
}
}
else
{
if (pbExistTable)
*pbExistTable = TRUE;
sprintf( szCommand,
"select b.attname from pg_class a,pg_attribute b where a.oid=b.attrelid and a.relname='%s' and b.attname='name';",
pszTableName);
if (hResult)
PQclear( hResult );
hResult = PQexec(hPGConn,szCommand);
if (PQresultStatus(hResult) == PGRES_TUPLES_OK && PQntuples(hResult) > 0)
{
if (pbHasNameCol)
*pbHasNameCol = TRUE;
}
else
{
if (pbHasNameCol)
*pbHasNameCol = FALSE;
}
}
if (hResult)
PQclear( hResult );
if (ppszTableName)
*ppszTableName = pszTableName;
else
CPLFree(pszTableName);
if (ppszDSName)
*ppszDSName = pszDSName;
CPLFree(pszConnectionString);
return hPGConn;
}
/************************************************************************/
/* Open() */
/************************************************************************/
GDALDataset *PGCHIPDataset::Open( GDALOpenInfo * poOpenInfo ){
char szCommand[1024];
PGresult *hResult = NULL;
PGCHIPDataset *poDS = NULL;
char *chipStringHex;
unsigned char *chipdata;
int t;
char *pszTableName = NULL;
const char *pszDSName = NULL;
int bHasNameCol = FALSE;
PGconn* hPGConn = PGChipOpenConnection(poOpenInfo->pszFilename, &pszTableName, &pszDSName, TRUE, NULL, &bHasNameCol);
if( hPGConn == NULL)
return NULL;
poDS = new PGCHIPDataset();
poDS->hPGConn = hPGConn;
poDS->pszTableName = pszTableName;
poDS->pszDSName = CPLStrdup(pszDSName);
/* -------------------------------------------------------------------- */
/* Read the chip */
/* -------------------------------------------------------------------- */
hResult = PQexec(poDS->hPGConn, "BEGIN");
if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK )
{
PQclear( hResult );
if (bHasNameCol)
sprintf( szCommand,
"SELECT raster FROM %s WHERE name = '%s' LIMIT 1 ",
poDS->pszTableName, poDS->pszDSName);
else
sprintf( szCommand,
"SELECT raster FROM %s LIMIT 1",
poDS->pszTableName);
hResult = PQexec(poDS->hPGConn,szCommand);
}
if( !hResult || PQresultStatus(hResult) != PGRES_TUPLES_OK )
{
CPLError( CE_Failure, CPLE_AppDefined,
"%s", PQerrorMessage(poDS->hPGConn) );
if (hResult)
PQclear( hResult );
delete poDS;
return NULL;
}
if (PQntuples(hResult) == 0)
{
PQclear( hResult );
delete poDS;
return NULL;
}
chipStringHex = PQgetvalue(hResult, 0, 0);
if (chipStringHex == NULL)
{
PQclear( hResult );
delete poDS;
return NULL;
}
int stringlen = strlen((char *)chipStringHex);
// Allocating memory for chip
chipdata = (unsigned char *) CPLMalloc(stringlen/2);
for (t=0;t<stringlen/2;t++){
chipdata[t] = parse_hex( &chipStringHex[t*2]) ;
}
// Chip assigment
poDS->PGCHIP = (CHIP *)chipdata;
poDS->SRID = poDS->PGCHIP->SRID;
if (poDS->PGCHIP->bvol.xmin != 0 && poDS->PGCHIP->bvol.ymin != 0 &
poDS->PGCHIP->bvol.xmax != 0 && poDS->PGCHIP->bvol.ymax != 0)
{
poDS->bGeoTransformValid = TRUE;
poDS->adfGeoTransform[0] = poDS->PGCHIP->bvol.xmin;
poDS->adfGeoTransform[3] = poDS->PGCHIP->bvol.ymax;
poDS->adfGeoTransform[1] = (poDS->PGCHIP->bvol.xmax - poDS->PGCHIP->bvol.xmin) / poDS->PGCHIP->width;
poDS->adfGeoTransform[5] = - (poDS->PGCHIP->bvol.ymax - poDS->PGCHIP->bvol.ymin) / poDS->PGCHIP->height;
}
#ifdef PGCHIP_DEBUG
poDS->printChipInfo(*(poDS->PGCHIP));
#endif
PQclear( hResult );
hResult = PQexec(poDS->hPGConn, "COMMIT");
PQclear( hResult );
/* -------------------------------------------------------------------- */
/* Verify that there's no unknown field set. */
/* -------------------------------------------------------------------- */
if ( poDS->PGCHIP->future[0] != 0 ||
poDS->PGCHIP->future[1] != 0 ||
poDS->PGCHIP->future[2] != 0 ||
poDS->PGCHIP->future[3] != 0 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Unsupported CHIP format (future field bytes != 0)\n");
delete poDS;
return NULL;
}
if ( poDS->PGCHIP->compression != 0 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Compressed CHIP unsupported\n");
delete poDS;
return NULL;
}
/* -------------------------------------------------------------------- */
/* Set some information from the file that is of interest. */
/* -------------------------------------------------------------------- */
poDS->nRasterXSize = poDS->PGCHIP->width;
poDS->nRasterYSize = poDS->PGCHIP->height;
poDS->nBands = 1; // (int)poDS->PGCHIP->future[0];
//poDS->nBitDepth = (int)poDS->PGCHIP->future[1];
poDS->nColorType = PGCHIP_COLOR_TYPE_GRAY; //(int)poDS->PGCHIP->future[2];
switch (poDS->PGCHIP->datatype)
{
case 5: // 24bit integer
poDS->nBitDepth = 24;
break;
case 6: // 16bit integer
poDS->nBitDepth = 16;
break;
case 8:
poDS->nBitDepth = 8;
break;
case 1: // float32
case 7: // 16bit ???
case 101: // float32 (NDR)
case 105: // 24bit integer (NDR)
case 106: // 16bit integer (NDR)
case 107: // 16bit ??? (NDR)
case 108: // 8bit ??? (NDR) [ doesn't make sense ]
default :
CPLError( CE_Failure, CPLE_AppDefined,
"Under development : CHIP datatype %d unsupported.\n",
poDS->PGCHIP->datatype);
break;
}
/* -------------------------------------------------------------------- */
/* Create band information objects. */
/* -------------------------------------------------------------------- */
for( int iBand = 0; iBand < poDS->nBands; iBand++ )
poDS->SetBand( iBand+1, new PGCHIPRasterBand( poDS, iBand+1 ) );
/* -------------------------------------------------------------------- */
/* Is there a palette? Note: we should also read back and */
/* apply transparency values if available. */
/* -------------------------------------------------------------------- */
CPLAssert (poDS->nColorType != PGCHIP_COLOR_TYPE_PALETTE);
if( poDS->nColorType == PGCHIP_COLOR_TYPE_PALETTE )
{
unsigned char *pPalette;
int nColorCount = 0;
int sizePalette = 0;
int offsetColor = -1;
GDALColorEntry oEntry;
nColorCount = (int)poDS->PGCHIP->compression;
pPalette = (unsigned char *)chipdata + sizeof(CHIP);
sizePalette = nColorCount * sizeof(pgchip_color);
poDS->poColorTable = new GDALColorTable();
for( int iColor = 0; iColor < nColorCount; iColor++ )
{
oEntry.c1 = pPalette[offsetColor++];
oEntry.c2 = pPalette[offsetColor++];
oEntry.c3 = pPalette[offsetColor++];
oEntry.c4 = pPalette[offsetColor++];
poDS->poColorTable->SetColorEntry( iColor, &oEntry );
}
}
return( poDS );
}
/************************************************************************/
/* PGCHIPCreateCopy() */
/************************************************************************/
static GDALDataset * PGCHIPCreateCopy( const char * pszFilename, GDALDataset *poSrcDS,
int bStrict, char ** papszOptions,
GDALProgressFunc pfnProgress, void * pProgressData ){
PGconn *hPGConn;
char *pszTableName = NULL;
const char *pszDSName = NULL;
char* pszCommand;
PGresult *hResult;
char *pszProjection;
int SRID;
GDALColorTable *poCT= NULL;
int bTableExists = FALSE;
int bHasNameCol = FALSE;
int nXSize = poSrcDS->GetRasterXSize();
int nYSize = poSrcDS->GetRasterYSize();
int nBands = poSrcDS->GetRasterCount();
/* -------------------------------------------------------------------- */
/* Some some rudimentary checks */
/* -------------------------------------------------------------------- */
/* check number of bands */
if( nBands != 1 && nBands != 4)
{
CPLError( CE_Failure, CPLE_NotSupported,
"Under development : PGCHIP driver doesn't support %d bands. Must be 1 or 4\n", nBands );
return NULL;
}
if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_Byte
&& poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_UInt16)
{
CPLError( CE_Failure, CPLE_NotSupported,
"Under development : PGCHIP driver doesn't support data type %s. "
"Only eight bit (Byte) and sixteen bit (UInt16) bands supported.\n",
GDALGetDataTypeName(
poSrcDS->GetRasterBand(1)->GetRasterDataType()) );
return NULL;
}
hPGConn = PGChipOpenConnection(pszFilename, &pszTableName, &pszDSName, FALSE, &bTableExists, &bHasNameCol);
/* Check Postgis connection string */
if( hPGConn == NULL){
CPLError( CE_Failure, CPLE_NotSupported,
"Cannont connect to %s.\n", pszFilename);
return NULL;
}
/* -------------------------------------------------------------------- */
/* Setup some parameters. */
/* -------------------------------------------------------------------- */
int nBitDepth;
GDALDataType eType;
int storageChunk;
int nColorType=0;
if( nBands == 1 && poSrcDS->GetRasterBand(1)->GetColorTable() == NULL ){
nColorType = PGCHIP_COLOR_TYPE_GRAY;
}
else if( nBands == 1 ){
nColorType = PGCHIP_COLOR_TYPE_PALETTE;
}
else if( nBands == 4 ){
nColorType = PGCHIP_COLOR_TYPE_RGB_ALPHA;
}
if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_UInt16 )
{
eType = GDT_Byte;
nBitDepth = 8;
}
else
{
eType = GDT_UInt16;
nBitDepth = 16;
}
storageChunk = nBitDepth/8;
//printf("nBands = %d, nBitDepth = %d\n",nBands,nBitDepth);
pszCommand = (char*)CPLMalloc(1024);
if(!bTableExists){
sprintf( pszCommand,
"CREATE TABLE %s(raster chip, name varchar)",
pszTableName);
hResult = PQexec(hPGConn,pszCommand);
bHasNameCol = TRUE;
}
else
{
if (bHasNameCol)
sprintf( pszCommand,
"DELETE FROM %s WHERE name = '%s'", pszTableName, pszDSName);
else
sprintf( pszCommand,
"DELETE FROM %s", pszTableName);
hResult = PQexec(hPGConn,pszCommand);
}
if( hResult && (PQresultStatus(hResult) == PGRES_COMMAND_OK || PQresultStatus(hResult) == PGRES_TUPLES_OK)){
PQclear( hResult );
}
else {
CPLError( CE_Failure, CPLE_AppDefined,
"%s", PQerrorMessage(hPGConn) );
PQfinish( hPGConn );
hPGConn = NULL;
return NULL;
}
/* -------------------------------------------------------------------- */
/* Projection, finding SRID */
/* -------------------------------------------------------------------- */
pszProjection = (char *)poSrcDS->GetProjectionRef();
SRID = -1;
if( !EQUALN(pszProjection,"GEOGCS",6)
&& !EQUALN(pszProjection,"PROJCS",6)
&& !EQUALN(pszProjection,"+",6)
&& !EQUAL(pszProjection,"") )
{
CPLError( CE_Failure, CPLE_AppDefined,
"Only OGC WKT Projections supported for writing to Postgis.\n"
"%s not supported.",
pszProjection );
}
if( pszProjection[0]=='+')
sprintf( pszCommand,"SELECT SRID FROM spatial_ref_sys where proj4text=%s",pszProjection);
else
sprintf( pszCommand,"SELECT SRID FROM spatial_ref_sys where srtext=%s",pszProjection);
hResult = PQexec(hPGConn,pszCommand);
if( hResult && PQresultStatus(hResult) == PGRES_TUPLES_OK
&& PQntuples(hResult) > 0 ){
SRID = atoi(PQgetvalue(hResult,0,0));
}
// Try to find SRID via EPSG number
if (SRID == -1 && strcmp(pszProjection,"") != 0){
char *buf;
char epsg[16];
memset(epsg,0,16);
const char *workingproj = pszProjection;
while( (buf = strstr(workingproj,"EPSG")) != 0){
workingproj = buf+4;
}
int iChar = 0;
workingproj = workingproj + 3;
while(workingproj[iChar] != '"' && iChar < 15){
epsg[iChar] = workingproj[iChar];
iChar++;
}
if(epsg[0] != 0){
SRID = atoi(epsg);
}
}
else{
CPLError( CE_Failure, CPLE_AppDefined,
"Projection %s not found in spatial_ref_sys table. SRID will be set to -1.\n",
pszProjection );
SRID = -1;
}
if( hResult )
PQclear( hResult );
/* -------------------------------------------------------------------- */
/* Write palette if there is one. Technically, I think it is */
/* possible to write 16bit palettes for PNG, but we will omit */
/* this for now. */
/* -------------------------------------------------------------------- */
unsigned char *pPalette = NULL;
int bHaveNoData = FALSE;
double dfNoDataValue = -1;
int nbColors = 0,bFoundTrans = FALSE;
size_t sizePalette = 0;
if( nColorType == PGCHIP_COLOR_TYPE_PALETTE )
{
GDALColorEntry sEntry;
int iColor;
int offsetColor = -1;
poCT = poSrcDS->GetRasterBand(1)->GetColorTable();
nbColors = poCT->GetColorEntryCount();
sizePalette += sizeof(pgchip_color) * poCT->GetColorEntryCount();
pPalette = (unsigned char *) CPLMalloc(sizePalette);
for( iColor = 0; iColor < poCT->GetColorEntryCount(); iColor++ )
{
poCT->GetColorEntryAsRGB( iColor, &sEntry );
if( sEntry.c4 != 255 )
bFoundTrans = TRUE;
pPalette[offsetColor++] = (unsigned char) sEntry.c1;
pPalette[offsetColor++] = (unsigned char) sEntry.c2;
pPalette[offsetColor++] = (unsigned char) sEntry.c3;
if( bHaveNoData && iColor == (int) dfNoDataValue ){
pPalette[offsetColor++] = 0;
}
else{
pPalette[offsetColor++] = (unsigned char) sEntry.c4;
}
}
}
/* -------------------------------------------------------------------- */
/* Initialize CHIP Structure */
/* -------------------------------------------------------------------- */
CHIP PGCHIP;
memset(&PGCHIP,0,sizeof(PGCHIP));
PGCHIP.factor = 1.0;
PGCHIP.endian_hint = 1;
PGCHIP.compression = 0; // nbColors; // To cope with palette extra information : <header><palette><data>
PGCHIP.height = nYSize;
PGCHIP.width = nXSize;
PGCHIP.SRID = SRID;
PGCHIP.future[0] = 0; // nBands; //nBands is stored in future variable
PGCHIP.future[1] = 0; // nBitDepth; //nBitDepth is stored in future variable
PGCHIP.future[2] = 0; // nColorType; //nBitDepth is stored in future variable
PGCHIP.future[3] = 0; // nbColors; // Useless as we store nbColors in the "compression" integer
PGCHIP.data = NULL; // Serialized Form
double adfGeoTransform[6];
if (GDALGetGeoTransform(poSrcDS, adfGeoTransform) == CE_None)
{
if (adfGeoTransform[2] == 0 &&
adfGeoTransform[4] == 0 &&
adfGeoTransform[5] < 0)
{
PGCHIP.bvol.xmin = adfGeoTransform[0];
PGCHIP.bvol.ymax = adfGeoTransform[3];
PGCHIP.bvol.xmax = adfGeoTransform[0] + nXSize * adfGeoTransform[1];
PGCHIP.bvol.ymin = adfGeoTransform[3] + nYSize * adfGeoTransform[5];
}
else
{
CPLError( CE_Warning, CPLE_AppDefined,
"Could not write geotransform.\n" );
}
}
// PGCHIP.size changes if there is a palette.
// Is calculated by Postgis when inserting anyway
PGCHIP.size = sizeof(CHIP) - sizeof(void*) + (nYSize * nXSize * storageChunk * nBands) + sizePalette;
switch(nBitDepth)
{
case 8:
PGCHIP.datatype = 8; // NDR|XDR ?
break;
case 16:
PGCHIP.datatype = 6; // NDR|XDR ?
break;
case 24:
PGCHIP.datatype = 5; // NDR|XDR ?
break;
default:
CPLError( CE_Failure, CPLE_AppDefined,"Under development : ERROR STORAGE CHUNK SIZE NOT SUPPORTED\n");
break;
}
#ifdef PGCHIP_DEBUG
PGCHIPDataset::printChipInfo(PGCHIP);
#endif
/* -------------------------------------------------------------------- */
/* Loop over image */
/* -------------------------------------------------------------------- */
CPLErr eErr;
size_t lineSize = nXSize * storageChunk * nBands;
// allocating data buffer
GByte *data = (GByte *) CPLMalloc( nYSize * lineSize);
for( int iLine = 0; iLine < nYSize; iLine++ ){
for( int iBand = 0; iBand < nBands; iBand++ ){
GDALRasterBand * poBand = poSrcDS->GetRasterBand( iBand+1 );
eErr = poBand->RasterIO( GF_Read, 0, iLine, nXSize, 1,
data + (iBand*storageChunk) + iLine * lineSize,
nXSize, 1, eType,
nBands * storageChunk,
lineSize );
}
}
/* -------------------------------------------------------------------- */
/* Write Header, Palette and Data */
/* -------------------------------------------------------------------- */
char *result;
size_t j=0;
// Calculating result length (*2 -> Hex form, +1 -> end string)
size_t size_result = (PGCHIP.size * 2) + 1;
// memory allocation
result = (char *) CPLMalloc( size_result * sizeof(char));
// Assign chip
GByte *header = (GByte *)&PGCHIP;
// Copy header into result string
for(j=0; j<sizeof(PGCHIP)-sizeof(void*); j++) {
pgch_deparse_hex( header[j], (unsigned char*)&(result[j*2]));
}
// Copy Palette into result string if required
size_t offsetPalette = (sizeof(PGCHIP)-sizeof(void*)) * 2;
if(nColorType == PGCHIP_COLOR_TYPE_PALETTE && sizePalette>0){
for(j=0;j<sizePalette;j++){
pgch_deparse_hex( pPalette[j], (unsigned char *)&result[offsetPalette + (j*2)]);
}
}
// Copy data into result string
size_t offsetData = offsetPalette + sizePalette * 2;
for(j=0;j<(nYSize * lineSize);j++){
pgch_deparse_hex( data[j], (unsigned char *)&result[offsetData + (j*2)]);
}
// end string
result[offsetData + j*2] = '\0';
/* -------------------------------------------------------------------- */
/* Inserting Chip */
/* -------------------------------------------------------------------- */
// Second allocation to cope with data size
CPLFree(pszCommand);
pszCommand = (char *)CPLMalloc(PGCHIP.size*2 + 256);
hResult = PQexec(hPGConn, "BEGIN");
if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK )
{
PQclear( hResult );
if (bHasNameCol)
sprintf( pszCommand,
"INSERT INTO %s(raster, name) values('%s', '%s')",
pszTableName,result, pszDSName);
else
sprintf( pszCommand,
"INSERT INTO %s(raster) values('%s')",
pszTableName,result);
hResult = PQexec(hPGConn,pszCommand);
}
CPLFree(pszTableName); pszTableName = NULL;
if( hResult && PQresultStatus(hResult) == PGRES_COMMAND_OK ){
PQclear( hResult );
}
else {
CPLError( CE_Failure, CPLE_AppDefined,
"%s", PQerrorMessage(hPGConn) );
CPLFree(pszCommand); pszCommand = NULL;
PQfinish( hPGConn );
hPGConn = NULL;
return NULL;
}
hResult = PQexec(hPGConn, "COMMIT");
PQclear( hResult );
CPLFree( pszCommand ); pszCommand = NULL;
CPLFree( pPalette ); pPalette = NULL;
CPLFree( data ); data = NULL;
CPLFree( result ); result = NULL;
PQfinish( hPGConn );
hPGConn = NULL;
return (GDALDataset *)GDALOpen(pszFilename,GA_Update);
}
/************************************************************************/
/* Display CHIP information */
/************************************************************************/
void PGCHIPDataset::printChipInfo(const CHIP& c){
//if(this->PGCHIP != NULL){
printf("\n---< CHIP INFO >----\n");
printf("CHIP.datatype = %d\n", c.datatype);
printf("CHIP.compression = %d\n", c.compression);
printf("CHIP.size = %d\n", c.size);
printf("CHIP.factor = %f\n", c.factor);
printf("CHIP.width = %d\n", c.width);
printf("CHIP.height = %d\n", c.height);
//printf("CHIP.future[0] (nBands?) = %d\n", (int)c.future[0]);
//printf("CHIP.future[1] (nBitDepth?) = %d\n", (int)c.future[1]);
printf("--------------------\n");
//}
}
/************************************************************************/
/* PGCHIPDelete */
/************************************************************************/
static CPLErr PGCHIPDelete(const char* pszFilename)
{
return CE_None;
}
/************************************************************************/
/* GDALRegister_PGCHIP() */
/************************************************************************/
void GDALRegister_PGCHIP(){
GDALDriver *poDriver;
if( GDALGetDriverByName( "PGCHIP" ) == NULL )
{
poDriver = new GDALDriver();
poDriver->SetDescription( "PGCHIP" );
poDriver->SetMetadataItem( GDAL_DMD_LONGNAME,
"Postgis CHIP raster" );
poDriver->SetMetadataItem( GDAL_DMD_CREATIONDATATYPES,
"Byte UInt16" );
poDriver->pfnOpen = PGCHIPDataset::Open;
poDriver->pfnCreateCopy = PGCHIPCreateCopy;
poDriver->pfnDelete = PGCHIPDelete;
GetGDALDriverManager()->RegisterDriver( poDriver );
}
}
|