1 : /******************************************************************************
2 : * $Id: ogrshape.h 23972 2012-02-13 21:19:46Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Private definitions within the Shapefile driver to implement
6 : * integration with OGR.
7 : * Author: Frank Warmerdam, warmerdam@pobox.com
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 1999, Les Technologies SoftMap Inc.
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : #ifndef _OGRSHAPE_H_INCLUDED
32 : #define _OGRSHAPE_H_INCLUDED
33 :
34 : #include "ogrsf_frmts.h"
35 : #include "shapefil.h"
36 : #include <vector>
37 :
38 : /* ==================================================================== */
39 : /* Functions from Shape2ogr.cpp. */
40 : /* ==================================================================== */
41 : OGRFeature *SHPReadOGRFeature( SHPHandle hSHP, DBFHandle hDBF,
42 : OGRFeatureDefn * poDefn, int iShape,
43 : SHPObject *psShape, const char *pszSHPEncoding );
44 : OGRGeometry *SHPReadOGRObject( SHPHandle hSHP, int iShape, SHPObject *psShape );
45 : OGRFeatureDefn *SHPReadOGRFeatureDefn( const char * pszName,
46 : SHPHandle hSHP, DBFHandle hDBF,
47 : const char *pszSHPEncoding );
48 : OGRErr SHPWriteOGRFeature( SHPHandle hSHP, DBFHandle hDBF,
49 : OGRFeatureDefn *poFeatureDefn,
50 : OGRFeature *poFeature, const char *pszSHPEncoding,
51 : int* pbTruncationWarningEmitted );
52 :
53 : /************************************************************************/
54 : /* OGRShapeLayer */
55 : /************************************************************************/
56 :
57 : class OGRShapeDataSource;
58 :
59 : class OGRShapeLayer : public OGRLayer
60 : {
61 : OGRShapeDataSource *poDS;
62 : OGRSpatialReference *poSRS; /* lazy loaded --> use GetSpatialRef() */
63 : int bSRSSet;
64 : OGRFeatureDefn *poFeatureDefn;
65 : int iNextShapeId;
66 : int nTotalShapeCount;
67 :
68 : char *pszFullName;
69 :
70 : SHPHandle hSHP;
71 : DBFHandle hDBF;
72 :
73 : int bUpdateAccess;
74 :
75 : OGRwkbGeometryType eRequestedGeomType;
76 : int ResetGeomType( int nNewType );
77 :
78 : int ScanIndices();
79 :
80 : long *panMatchingFIDs;
81 : int iMatchingFID;
82 : void ClearMatchingFIDs();
83 :
84 : OGRGeometry *m_poFilterGeomLastValid;
85 : int nSpatialFIDCount;
86 : int *panSpatialFIDs;
87 : void ClearSpatialFIDs();
88 :
89 : int bHeaderDirty;
90 :
91 : int bCheckedForQIX;
92 : SHPTreeDiskHandle hQIX;
93 :
94 : int CheckForQIX();
95 :
96 : int bSbnSbxDeleted;
97 :
98 : CPLString ConvertCodePage( const char * );
99 : CPLString osEncoding;
100 :
101 : int bTruncationWarningEmitted;
102 :
103 : int bHSHPWasNonNULL; /* to know if we must try to reopen a .shp */
104 : int bHDBFWasNonNULL; /* to know if we must try to reopen a .dbf */
105 : int eFileDescriptorsState; /* current state of opening of file descriptor to .shp and .dbf */
106 : int TouchLayer();
107 : int ReopenFileDescriptors();
108 :
109 : int bResizeAtClose;
110 :
111 : void TruncateDBF();
112 :
113 : /* WARNING: each of the below public methods should start with a call to */
114 : /* TouchLayer() and test its return value, so as to make sure that */
115 : /* the layer is properly re-opened if necessary */
116 :
117 : public:
118 : OGRErr CreateSpatialIndex( int nMaxDepth );
119 : OGRErr DropSpatialIndex();
120 : OGRErr Repack();
121 : OGRErr RecomputeExtent();
122 : OGRErr ResizeDBF();
123 :
124 2382 : void SetResizeAtClose( int bFlag ) { bResizeAtClose = bFlag; }
125 :
126 1036 : const char *GetFullName() { return pszFullName; }
127 :
128 : void CloseFileDescriptors();
129 :
130 : /* The 2 following members should not be used by OGRShapeLayer, except */
131 : /* in its constructor */
132 : OGRShapeLayer *poPrevLayer; /* Chain to a layer that was used more recently */
133 : OGRShapeLayer *poNextLayer; /* Chain to a layer that was used less recently */
134 :
135 : OGRFeature * FetchShape(int iShapeId);
136 : int GetFeatureCountWithSpatialFilterOnly();
137 :
138 : public:
139 : OGRShapeLayer( OGRShapeDataSource* poDSIn,
140 : const char * pszName,
141 : SHPHandle hSHP, DBFHandle hDBF,
142 : OGRSpatialReference *poSRS, int bSRSSet,
143 : int bUpdate,
144 : OGRwkbGeometryType eReqType );
145 : ~OGRShapeLayer();
146 :
147 : void ResetReading();
148 : OGRFeature * GetNextFeature();
149 : virtual OGRErr SetNextByIndex( long nIndex );
150 :
151 : OGRFeature *GetFeature( long nFeatureId );
152 : OGRErr SetFeature( OGRFeature *poFeature );
153 : OGRErr DeleteFeature( long nFID );
154 : OGRErr CreateFeature( OGRFeature *poFeature );
155 : OGRErr SyncToDisk();
156 :
157 2177502 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
158 :
159 : int GetFeatureCount( int );
160 : OGRErr GetExtent(OGREnvelope *psExtent, int bForce);
161 :
162 : virtual OGRErr CreateField( OGRFieldDefn *poField,
163 : int bApproxOK = TRUE );
164 : virtual OGRErr DeleteField( int iField );
165 : virtual OGRErr ReorderFields( int* panMap );
166 : virtual OGRErr AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags );
167 :
168 : virtual OGRSpatialReference *GetSpatialRef();
169 :
170 : virtual int TestCapability( const char * );
171 : virtual void SetSpatialFilter( OGRGeometry * );
172 : virtual OGRErr SetAttributeFilter( const char * );
173 : };
174 :
175 : /************************************************************************/
176 : /* OGRShapeDataSource */
177 : /************************************************************************/
178 :
179 : class OGRShapeDataSource : public OGRDataSource
180 : {
181 : OGRShapeLayer **papoLayers;
182 : int nLayers;
183 :
184 : char *pszName;
185 :
186 : int bDSUpdate;
187 :
188 : int bSingleFileDataSource;
189 :
190 : OGRShapeLayer *poMRULayer; /* the most recently used layer */
191 : OGRShapeLayer *poLRULayer; /* the least recently used layer (still opened) */
192 : int nMRUListSize; /* the size of the list */
193 :
194 : void AddLayer(OGRShapeLayer* poLayer);
195 :
196 : std::vector<CPLString> oVectorLayerName;
197 :
198 : public:
199 : OGRShapeDataSource();
200 : ~OGRShapeDataSource();
201 :
202 : int Open( const char *, int bUpdate, int bTestOpen,
203 : int bForceSingleFileDataSource = FALSE );
204 : int OpenFile( const char *, int bUpdate, int bTestOpen );
205 :
206 898 : virtual const char *GetName() { return pszName; }
207 :
208 : virtual int GetLayerCount();
209 : virtual OGRLayer *GetLayer( int );
210 : virtual OGRLayer *GetLayerByName(const char *);
211 :
212 : virtual OGRLayer *CreateLayer( const char *,
213 : OGRSpatialReference * = NULL,
214 : OGRwkbGeometryType = wkbUnknown,
215 : char ** = NULL );
216 :
217 : virtual OGRLayer *ExecuteSQL( const char *pszStatement,
218 : OGRGeometry *poSpatialFilter,
219 : const char *pszDialect );
220 :
221 : virtual int TestCapability( const char * );
222 : virtual OGRErr DeleteLayer( int iLayer );
223 :
224 : void SetLastUsedLayer( OGRShapeLayer* poLayer );
225 : void UnchainLayer( OGRShapeLayer* poLayer );
226 : };
227 :
228 : /************************************************************************/
229 : /* OGRShapeDriver */
230 : /************************************************************************/
231 :
232 : class OGRShapeDriver : public OGRSFDriver
233 389 : {
234 : public:
235 : ~OGRShapeDriver();
236 :
237 : const char *GetName();
238 : OGRDataSource *Open( const char *, int );
239 :
240 : virtual OGRDataSource *CreateDataSource( const char *pszName,
241 : char ** = NULL );
242 : OGRErr DeleteDataSource( const char *pszDataSource );
243 :
244 : int TestCapability( const char * );
245 : };
246 :
247 :
248 : #endif /* ndef _OGRSHAPE_H_INCLUDED */
|