1 /******************************************************************************
2  *
3  * Name:     georaster_priv.h
4  * Project:  Oracle Spatial GeoRaster Driver
5  * Purpose:  Define C++/Private declarations
6  * Author:   Ivan Lucena [ivan.lucena at oracle.com]
7  *
8  ******************************************************************************
9  * Copyright (c) 2008, Ivan Lucena
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files ( the "Software" ),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  *****************************************************************************/
29 
30 #ifndef GEORASTER_PRIV_H_INCLUDED
31 #define GEORASTER_PRIV_H_INCLUDED
32 
33 #include "gdal.h"
34 #include "gdal_priv.h"
35 #include "gdal_alg.h"
36 #include "gdal_rat.h"
37 #include "ogr_spatialref.h"
38 #include "cpl_minixml.h"
39 #include "cpl_list.h"
40 
41 //  ---------------------------------------------------------------------------
42 //  DEFLATE compression support
43 //  ---------------------------------------------------------------------------
44 
45 #include <zlib.h>
46 
47 //  ---------------------------------------------------------------------------
48 //  JPEG compression support
49 //  ---------------------------------------------------------------------------
50 
51 CPL_C_START
52 #include <jpeglib.h>
53 CPL_C_END
54 
55 void jpeg_vsiio_src (j_decompress_ptr cinfo, VSILFILE * infile);
56 void jpeg_vsiio_dest (j_compress_ptr cinfo, VSILFILE * outfile);
57 
58 //  ---------------------------------------------------------------------------
59 //  JPEG2000 support - Install the Virtual File System handler to OCI LOB
60 //  ---------------------------------------------------------------------------
61 
62 CPL_C_START
63 void CPL_DLL VSIInstallOCILobHandler(void);
64 CPL_C_END
65 
66 //  ---------------------------------------------------------------------------
67 //  System constants
68 //  ---------------------------------------------------------------------------
69 
70 //  VAT maximum string len
71 
72 #define MAXLEN_VATSTR 128
73 
74 //  Geographic system without EPSG parameters
75 
76 #define UNKNOWN_CRS     999999
77 #define NO_CRS          0
78 #define DEFAULT_CRS     NO_CRS
79 
80 //  Bitmap Mask for the whole dataset start with -99999
81 
82 #define DEFAULT_BMP_MASK -99999
83 
84 //  Default block size
85 
86 #define DEFAULT_BLOCK_ROWS 512
87 #define DEFAULT_BLOCK_COLUMNS 512
88 
89 #define DEFAULT_JP2_TILE_ROWS 512
90 #define DEFAULT_JP2_TILE_COLUMNS 512
91 
92 //  Default Model Coordinate Location (internal pixel geo-reference)
93 
94 #define MCL_CENTER      0
95 #define MCL_UPPERLEFT   1
96 #define MCL_DEFAULT     MCL_CENTER
97 
98 // MAX double string representation
99 
100 #define MAX_DOUBLE_STR_REP 20
101 
102 // Pyramid levels details
103 
104 struct hLevelDetails {
105     int             nColumnBlockSize;
106     int             nRowBlockSize;
107     int             nTotalColumnBlocks;
108     int             nTotalRowBlocks;
109     unsigned long   nBlockCount;
110     unsigned long   nBlockBytes;
111     unsigned long   nGDALBlockBytes;
112     unsigned long   nOffset;
113 };
114 
115 //  ---------------------------------------------------------------------------
116 //  Support for multi-values NoData support
117 //  ---------------------------------------------------------------------------
118 
119 struct hNoDataItem {
120     int             nBand;
121     double          dfLower;
122     double          dfUpper;
123 };
124 
125 //  ---------------------------------------------------------------------------
126 //  GeoRaster wrapper class definitions
127 //  ---------------------------------------------------------------------------
128 
129 #include "oci_wrapper.h"
130 
131 class GeoRasterDataset;
132 class GeoRasterRasterBand;
133 class GeoRasterWrapper;
134 
135 //  ---------------------------------------------------------------------------
136 //  GeoRasterDataset, extends GDALDataset to support GeoRaster Datasets
137 //  ---------------------------------------------------------------------------
138 
139 class GeoRasterDataset final: public GDALDataset
140 {
141     friend class GeoRasterRasterBand;
142 
143   public:
144     GeoRasterDataset();
145     ~GeoRasterDataset() override;
146 
147   private:
148     GeoRasterWrapper*   poGeoRaster;
149     bool                bGeoTransform;
150     bool                bForcedSRID;
151     char*               pszProjection;
152     char**              papszSubdatasets;
153     double              adfGeoTransform[6];
154     GeoRasterRasterBand*
155                         poMaskBand;
156     bool                bApplyNoDataArray;
157     void                JP2_Open( GDALAccess eAccess );
158     void                JP2_CreateCopy( GDALDataset* poJP2DS,
159                                         char** papszOptions,
160                                         int* pnResolutions,
161                                         GDALProgressFunc pfnProgress,
162                                         void* pProgressData );
163     boolean             JP2_CopyDirect( const char* pszJP2Filename,
164                                         int* pnResolutions,
165                                         GDALProgressFunc pfnProgress,
166                                         void* pProgressData );
167     boolean             JPEG_CopyDirect( const char* pszJPGFilename,
168                                          GDALProgressFunc pfnProgress,
169                                          void* pProgressData );
170 
171 public:
172 
173     GDALDataset*        poJP2Dataset;
174 
175     void                SetSubdatasets( GeoRasterWrapper* poGRW );
176 
177     static int          Identify( GDALOpenInfo* poOpenInfo );
178     static GDALDataset* Open( GDALOpenInfo* poOpenInfo );
179     static CPLErr       Delete( const char *pszFilename );
180     static GDALDataset* Create( const char* pszFilename,
181                             int nXSize,
182                             int nYSize,
183                             int nBands,
184                             GDALDataType eType,
185                             char** papszOptions );
186     static GDALDataset* CreateCopy( const char* pszFilename,
187                                     GDALDataset* poSrcDS,
188                                     int bStrict,
189                                     char** papszOptions,
190                                     GDALProgressFunc pfnProgress,
191                                     void* pProgressData );
192     CPLErr GetGeoTransform( double* padfTransform ) override;
193     CPLErr SetGeoTransform( double* padfTransform ) override;
194     const char *_GetProjectionRef() override;
195     CPLErr _SetProjection( const char* pszProjString ) override;
GetSpatialRef()196     const OGRSpatialReference* GetSpatialRef() const override {
197         return GetSpatialRefFromOldGetProjectionRef();
198     }
SetSpatialRef(const OGRSpatialReference * poSRS)199     CPLErr SetSpatialRef(const OGRSpatialReference* poSRS) override {
200         return OldSetProjectionFromSetSpatialRef(poSRS);
201     }
202 
203     char **GetMetadataDomainList() override;
204     char **GetMetadata( const char* pszDomain ) override;
205     void FlushCache() override;
206     CPLErr IRasterIO( GDALRWFlag eRWFlag,
207                       int nXOff, int nYOff, int nXSize, int nYSize,
208                       void *pData, int nBufXSize, int nBufYSize,
209                       GDALDataType eBufType,
210                       int nBandCount, int *panBandMap,
211                       GSpacing nPixelSpace, GSpacing nLineSpace,
212                       GSpacing nBandSpace,
213                       GDALRasterIOExtraArg* psExtraArg ) override;
214     int GetGCPCount() override;
215     const char* _GetGCPProjection() override;
GetGCPSpatialRef()216     const OGRSpatialReference* GetGCPSpatialRef() const override {
217         return GetGCPSpatialRefFromOldGetGCPProjection();
218     }
219     const GDAL_GCP*
220                 GetGCPs() override;
221     CPLErr _SetGCPs(
222                int nGCPCount,
223                const GDAL_GCP *pasGCPList,
224                const char *pszGCPProjection ) override;
225     using GDALDataset::SetGCPs;
SetGCPs(int nGCPCount,const GDAL_GCP * pasGCPList,const OGRSpatialReference * poSRS)226     CPLErr SetGCPs( int nGCPCount, const GDAL_GCP *pasGCPList,
227                     const OGRSpatialReference* poSRS ) override {
228         return OldSetGCPsFromNew(nGCPCount, pasGCPList, poSRS);
229     }
230 
231 
232     CPLErr IBuildOverviews(
233                const char* pszResampling,
234                int nOverviews,
235                int* panOverviewList,
236                int nListBandsover,
237                int* panBandList,
238                GDALProgressFunc pfnProgress,
239                void* pProgresoversData ) override;
240     CPLErr CreateMaskBand( int nFlags ) override;
241     // cppcheck-suppress functionStatic
242     OGRErr StartTransaction(int /* bForce */ =FALSE) override
243         { return CE_None; }
CommitTransaction()244     OGRErr CommitTransaction() override { return CE_None; }
RollbackTransaction()245     OGRErr RollbackTransaction() override { return CE_None; }
246 
247     char** GetFileList() override;
248 
249     void                AssignGeoRaster( GeoRasterWrapper* poGRW );
250 };
251 
252 //  ---------------------------------------------------------------------------
253 //  GeoRasterRasterBand, extends GDALRasterBand to support GeoRaster Band
254 //  ---------------------------------------------------------------------------
255 
256 class GeoRasterRasterBand final: public GDALRasterBand
257 {
258     friend class GeoRasterDataset;
259 
260   public:
261     GeoRasterRasterBand( GeoRasterDataset* poGDS,
262                          int nBand,
263                          int nLevel,
264                          GDALDataset* poJP2Dataset = nullptr );
265     ~GeoRasterRasterBand() override;
266 
267 private:
268     GeoRasterWrapper*   poGeoRaster;
269     GDALColorTable*     poColorTable;
270     GDALRasterAttributeTable*
271                         poDefaultRAT;
272     GDALDataset*        poJP2Dataset;
273     double              dfMin;
274     double              dfMax;
275     double              dfMean;
276     double              dfMedian;
277     double              dfMode;
278     double              dfStdDev;
279     bool                bValidStats;
280     double              dfNoData;
281     char*               pszVATName;
282     int                 nOverviewLevel;
283     GeoRasterRasterBand** papoOverviews;
284     int                 nOverviewCount;
285     hNoDataItem*        pahNoDataArray;
286     int                 nNoDataArraySz;
287     bool                bHasNoDataArray;
288 
289     void                ApplyNoDataArray( void* pBuffer ) const;
290 
291 public:
292     double GetNoDataValue( int *pbSuccess = nullptr ) override;
293     CPLErr SetNoDataValue( double dfNoDataValue ) override;
294     double GetMinimum( int* pbSuccess = nullptr ) override;
295     double GetMaximum( int* pbSuccess = nullptr ) override;
296     GDALColorTable *GetColorTable() override;
297     CPLErr SetColorTable( GDALColorTable *poInColorTable ) override;
298     GDALColorInterp GetColorInterpretation() override;
299     CPLErr IReadBlock( int nBlockXOff, int nBlockYOff, void *pImage ) override;
300     CPLErr IWriteBlock( int nBlockXOff, int nBlockYOff, void *pImage ) override;
301     CPLErr SetStatistics( double dfMin, double dfMax,
302                           double dfMean, double dfStdDev ) override;
303     CPLErr GetStatistics( int bApproxOK, int bForce,
304                           double* pdfMin, double* pdfMax,
305                           double* pdfMean, double* pdfStdDev ) override;
306     GDALRasterAttributeTable *GetDefaultRAT() override;
307     CPLErr      SetDefaultRAT( const GDALRasterAttributeTable *poRAT ) override;
308     int         GetOverviewCount() override;
309     GDALRasterBand*
310                 GetOverview( int ) override;
311     CPLErr      CreateMaskBand( int nFlags ) override;
312     GDALRasterBand *GetMaskBand() override;
313     int GetMaskFlags() override;
314 };
315 
316 //  ---------------------------------------------------------------------------
317 //  GeoRasterWrapper, an interface for Oracle Spatial SDO_GEORASTER objects
318 //  ---------------------------------------------------------------------------
319 
320 class GeoRasterWrapper
321 {
322   public:
323     GeoRasterWrapper();
324     virtual ~GeoRasterWrapper();
325 
326   private:
327     OCILobLocator**     pahLocator;
328     unsigned long       nBlockCount;
329     unsigned long       nBlockBytes;
330     unsigned long       nGDALBlockBytes;
331     GByte*              pabyBlockBuf;
332     GByte*              pabyCompressBuf;
333     OWStatement*        poBlockStmt;
334 
335     int                 nCurrentLevel;
336     long                nLevelOffset;
337 
338     long                nCacheBlockId;
339     bool                bFlushBlock;
340     unsigned long       nFlushBlockSize;
341 
342     bool                bWriteOnly;
343 
344     hLevelDetails*      pahLevels;
345 
346     int                 nCellSizeBits;
347     int                 nGDALCellBytes;
348 
349     bool                bUpdate;
350     bool                bInitializeIO;
351     bool                bFlushMetadata;
352 
353     void                InitializeLayersNode();
354     bool                InitializeIO();
355     void                InitializeLevel( int nLevel );
356 
357     void                LoadNoDataValues();
358 
359     void                UnpackNBits( GByte* pabyData );
360     void                PackNBits( GByte* pabyData ) const;
361     unsigned long       CompressJpeg();
362     unsigned long       CompressDeflate();
363     void                UncompressJpeg( unsigned long nBufferSize );
364     bool                UncompressDeflate( unsigned long nBufferSize );
365 
366     struct jpeg_decompress_struct sDInfo;
367     struct jpeg_compress_struct sCInfo;
368     struct jpeg_error_mgr sJErr;
369 
370     void                GetSpatialReference();
371 
372 public:
373 
374     int                 nGCPCount;
375     GDAL_GCP*           pasGCPList;
376     bool                bFlushGCP;
377     void                FlushGCP();
378 
379     bool                FlushMetadata();
380     static char**       ParseIdentificator( const char* pszStringID );
381     static GeoRasterWrapper*
382                         Open(
383                             const char* pszStringID,
384                             bool bUpdate );
385     bool                Create(
386                             char* pszDescription,
387                             char* pszInsert,
388                             bool bUpdate );
389     bool                Delete();
390     void                GetRasterInfo();
391     bool                GetStatistics( int nBand,
392                                        char* pszMin,
393                                        char* pszMax,
394                                        char* pszMean,
395                                        char* pszMedian,
396                                        char* pszMode,
397                                        char* pszStdDev,
398                                        char* pszSampling );
399     bool                SetStatistics( int nBand,
400                                        const char* pszMin,
401                                        const char* pszMax,
402                                        const char* pszMean,
403                                        const char* pszMedian,
404                                        const char* pszMode,
405                                        const char* pszStdDev,
406                                        const char* pszSampling );
407     bool                HasColorMap( int nBand );
408     void                GetColorMap( int nBand, GDALColorTable* poCT );
409     void                SetColorMap( int nBand, GDALColorTable* poCT );
410     void                SetGeoReference( long long nSRIDIn );
411     bool                GetDataBlock(
412                             int nBand,
413                             int nLevel,
414                             int nXOffset,
415                             int nYOffset,
416                             void* pData );
417     bool                SetDataBlock(
418                             int nBand,
419                             int nLevel,
420                             int nXOffset,
421                             int nYOffset,
422                             void* pData );
GetBlockNumber(int nB,int nX,int nY)423     long                GetBlockNumber( int nB, int nX, int nY ) const
424                         {
425                             return nLevelOffset +
426                                    (long) ( ( ceil( (double)
427                                    ( ( nB - 1 ) / nBandBlockSize ) ) *
428                                    nTotalColumnBlocks * nTotalRowBlocks ) +
429                                    ( nY * nTotalColumnBlocks ) + nX );
430                         }
431 
432     bool                FlushBlock( long nCacheBlock );
433     bool                GetNoData( int nLayer, double* pdfNoDataValue );
434     bool                SetNoData( int nLayer, const char* pszValue );
GetMetadata()435     CPLXMLNode*         GetMetadata() { return phMetadata; }
436     bool                SetVAT( int nBand, const char* pszName );
437     char*               GetVAT( int nBand );
438     bool                GeneratePyramid(
439                             int nLevels,
440                             const char* pszResampling,
441                             bool bInternal = false );
442     void                DeletePyramid();
443     void                PrepareToOverwrite();
444     bool                InitializeMask( int nLevel,
445                                                 int nBlockColumns,
446                                                 int nBlockRows,
447                                                 int nColumnBlocks,
448                                                 int nRowBlocks,
449                                                 int nBandBlocks );
SetWriteOnly(bool value)450     void                SetWriteOnly( bool value ) { bWriteOnly = value; }
451     void                SetRPC();
452     void                SetMaxLevel( int nMaxLevel );
453     void                GetRPC();
454     void                GetGCP();
455     void                SetGCP( int nGCPCountIn, const GDAL_GCP *pasGCPListIn );
456     void                QueryWKText();
457 
458 public:
459 
460     OWConnection*       poConnection;
461 
462     CPLString           sTable;
463     CPLString           sSchema;
464     CPLString           sOwner;
465     CPLString           sColumn;
466     CPLString           sDataTable;
467     long long           nRasterId;
468     CPLString           sWhere;
469     CPLString           sValueAttributeTab;
470 
471     long long           nSRID;
472     long long           nExtentSRID;
473     bool                bGenSpatialExtent;
474     bool                bCreateObjectTable;
475     CPLXMLNode*         phMetadata;
476     CPLString           sCellDepth;
477 
478     bool                bGenPyramid;
479     CPLString           sPyramidResampling;
480     int                 nPyramidLevels;
481 
482     CPLString           sCompressionType;
483     int                 nCompressQuality;
484     CPLString           sWKText;
485     CPLString           sAuthority;
486     CPLList*            psNoDataList;
487 
488     int                 nRasterColumns;
489     int                 nRasterRows;
490     int                 nRasterBands;
491 
492     CPLString           sInterleaving;
493     bool                bIsReferenced;
494 
495     bool                bBlocking;
496     bool                bAutoBlocking;
497 
498     double              dfXCoefficient[3];
499     double              dfYCoefficient[3];
500 
501     int                 nColumnBlockSize;
502     int                 nRowBlockSize;
503     int                 nBandBlockSize;
504 
505     int                 nTotalColumnBlocks;
506     int                 nTotalRowBlocks;
507     int                 nTotalBandBlocks;
508 
509     int                 iDefaultRedBand;
510     int                 iDefaultGreenBand;
511     int                 iDefaultBlueBand;
512 
513     int                 nPyramidMaxLevel;
514 
515     bool                bHasBitmapMask;
516     bool                bUniqueFound;
517 
518     int                 eModelCoordLocation;
519     unsigned int        anULTCoordinate[3];
520 
521     GDALRPCInfoV2*      phRPC;
522 };
523 
524 #endif /* ifndef GEORASTER_PRIV_H_INCLUDED */
525