1 /******************************************************************************
2  * $Id: pcidskdataset2.h fa752ad6eabafaf630a704e1892a9d837d683cb3 2021-03-06 17:04:38 +0100 Even Rouault $
3  *
4  * Project:  PCIDSK Database File
5  * Purpose:  Read/write PCIDSK Database File used by the PCI software, using
6  *           the external PCIDSK library.
7  * Author:   Frank Warmerdam, warmerdam@pobox.com
8  *
9  ******************************************************************************
10  * Copyright (c) 2009, Frank Warmerdam <warmerdam@pobox.com>
11  * Copyright (c) 2009-2013, Even Rouault <even dot rouault at spatialys.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef PCIDSKDATASET2_H_INCLUDED
33 #define PCIDSKDATASET2_H_INCLUDED
34 
35 #define GDAL_PCIDSK_DRIVER
36 
37 #include "cpl_string.h"
38 #include "gdal_pam.h"
39 #include "ogrsf_frmts.h"
40 #include "ogr_spatialref.h"
41 #include "pcidsk.h"
42 #include "pcidsk_pct.h"
43 #include "pcidsk_vectorsegment.h"
44 
45 #include <unordered_map>
46 
47 using namespace PCIDSK;
48 
49 class OGRPCIDSKLayer;
50 
51 /************************************************************************/
52 /*                              PCIDSK2Dataset                           */
53 /************************************************************************/
54 
55 class PCIDSK2Dataset final: public GDALPamDataset
56 {
57     friend class PCIDSK2Band;
58 
59     mutable OGRSpatialReference* m_poSRS = nullptr;
60 
61     std::unordered_map<std::string, std::string> m_oCacheMetadataItem{};
62     char      **papszLastMDListValue;
63 
64     PCIDSK::PCIDSKFile  *poFile;
65 
66     std::vector<OGRPCIDSKLayer*> apoLayers;
67 
68     static GDALDataType  PCIDSKTypeToGDAL( PCIDSK::eChanType eType );
69     void                 ProcessRPC();
70 
71   public:
72                 PCIDSK2Dataset();
73     virtual ~PCIDSK2Dataset();
74 
75     static int           Identify( GDALOpenInfo * );
76     static GDALDataset  *Open( GDALOpenInfo * );
77     static GDALDataset  *LLOpen( const char *pszFilename, PCIDSK::PCIDSKFile *,
78                                  GDALAccess eAccess,
79                                  char** papszSiblingFiles = nullptr );
80     static GDALDataset  *Create( const char * pszFilename,
81                                  int nXSize, int nYSize, int nBands,
82                                  GDALDataType eType,
83                                  char **papszParamList );
84 
85     char              **GetFileList() override;
86     CPLErr              GetGeoTransform( double * padfTransform ) override;
87     CPLErr              SetGeoTransform( double * ) override;
88 
89     const OGRSpatialReference* GetSpatialRef() const override;
90     CPLErr SetSpatialRef(const OGRSpatialReference* poSRS) override;
91 
92     virtual char      **GetMetadataDomainList() override;
93     CPLErr              SetMetadata( char **, const char * ) override;
94     char              **GetMetadata( const char* ) override;
95     CPLErr              SetMetadataItem(const char*,const char*,const char*) override;
96     const char         *GetMetadataItem( const char*, const char*) override;
97 
98     virtual void FlushCache() override;
99 
100     virtual CPLErr IBuildOverviews( const char *, int, int *,
101                                     int, int *, GDALProgressFunc, void * ) override;
102 
GetLayerCount()103     virtual int                 GetLayerCount() override { return (int) apoLayers.size(); }
104     virtual OGRLayer            *GetLayer( int ) override;
105 
106     virtual int                 TestCapability( const char * ) override;
107 
108     virtual OGRLayer           *ICreateLayer( const char *, OGRSpatialReference *,
109                                      OGRwkbGeometryType, char ** ) override;
110 };
111 
112 /************************************************************************/
113 /*                             PCIDSK2Band                              */
114 /************************************************************************/
115 
116 class PCIDSK2Band final: public GDALPamRasterBand
117 {
118     friend class PCIDSK2Dataset;
119 
120     PCIDSK::PCIDSKChannel *poChannel;
121     PCIDSK::PCIDSKFile *poFile;
122 
123     void        RefreshOverviewList();
124     std::vector<PCIDSK2Band*> apoOverviews;
125 
126     std::unordered_map<std::string, std::string> m_oCacheMetadataItem{};
127     char      **papszLastMDListValue;
128 
129     bool        CheckForColorTable();
130     GDALColorTable *poColorTable;
131     bool        bCheckedForColorTable;
132     int         nPCTSegNumber;
133 
134     char      **papszCategoryNames;
135 
136     void        Initialize();
137 
138   public:
139                 PCIDSK2Band( PCIDSK::PCIDSKFile *poFileIn,
140                              PCIDSK::PCIDSKChannel *poChannelIn );
141     explicit    PCIDSK2Band( PCIDSK::PCIDSKChannel * );
142     virtual ~PCIDSK2Band();
143 
144     virtual CPLErr IReadBlock( int, int, void * ) override;
145     virtual CPLErr IWriteBlock( int, int, void * ) override;
146 
147     virtual int        GetOverviewCount() override;
148     virtual GDALRasterBand *GetOverview(int) override;
149 
150     virtual GDALColorInterp GetColorInterpretation() override;
151     virtual GDALColorTable *GetColorTable() override;
152     virtual CPLErr SetColorTable( GDALColorTable * ) override;
153 
154     virtual void        SetDescription( const char * ) override;
155 
156     virtual char      **GetMetadataDomainList() override;
157     CPLErr              SetMetadata( char **, const char * ) override;
158     char              **GetMetadata( const char* ) override;
159     CPLErr              SetMetadataItem(const char*,const char*,const char*) override;
160     const char         *GetMetadataItem( const char*, const char*) override;
161 
162     virtual char      **GetCategoryNames() override;
163 };
164 
165 /************************************************************************/
166 /*                             OGRPCIDSKLayer                              */
167 /************************************************************************/
168 
169 class OGRPCIDSKLayer final: public OGRLayer, public OGRGetNextFeatureThroughRaw<OGRPCIDSKLayer>
170 {
171     PCIDSK::PCIDSKVectorSegment *poVecSeg;
172     PCIDSK::PCIDSKSegment       *poSeg;
173 
174     OGRFeatureDefn     *poFeatureDefn;
175 
176     OGRFeature *        GetNextRawFeature();
177 
178     int                 iRingStartField;
179     PCIDSK::ShapeId     hLastShapeId;
180 
181     bool                bUpdateAccess;
182 
183     OGRSpatialReference *poSRS;
184 
185     std::unordered_map<std::string, int> m_oMapFieldNameToIdx{};
186     bool                m_bEOF = false;
187 
188   public:
189     OGRPCIDSKLayer( PCIDSK::PCIDSKSegment*, PCIDSK::PCIDSKVectorSegment *, bool bUpdate );
190     virtual ~OGRPCIDSKLayer();
191 
192     void                ResetReading() override;
193     DEFINE_GET_NEXT_FEATURE_THROUGH_RAW(OGRPCIDSKLayer)
194 
195     OGRFeature         *GetFeature( GIntBig nFeatureId ) override;
196     virtual OGRErr      ISetFeature( OGRFeature *poFeature ) override;
197 
GetLayerDefn()198     OGRFeatureDefn *    GetLayerDefn() override { return poFeatureDefn; }
199 
200     int                 TestCapability( const char * ) override;
201 
202     OGRErr              DeleteFeature( GIntBig nFID ) override;
203     virtual OGRErr      ICreateFeature( OGRFeature *poFeature ) override;
204     virtual OGRErr      CreateField( OGRFieldDefn *poField,
205                                      int bApproxOK = TRUE ) override;
206 
207     GIntBig             GetFeatureCount( int ) override;
208     OGRErr              GetExtent( OGREnvelope *psExtent, int bForce ) override;
GetExtent(int iGeomField,OGREnvelope * psExtent,int bForce)209     virtual OGRErr      GetExtent(int iGeomField, OGREnvelope *psExtent, int bForce) override
210                 { return OGRLayer::GetExtent(iGeomField, psExtent, bForce); }
211 };
212 
213 #endif /*  PCIDSKDATASET2_H_INCLUDED */
214