1 : /******************************************************************************
2 : * $Id: ogrshape.h 24645 2012-07-01 20:47:05Z 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 "ogrlayerpool.h"
37 : #include <vector>
38 :
39 : /* ==================================================================== */
40 : /* Functions from Shape2ogr.cpp. */
41 : /* ==================================================================== */
42 : OGRFeature *SHPReadOGRFeature( SHPHandle hSHP, DBFHandle hDBF,
43 : OGRFeatureDefn * poDefn, int iShape,
44 : SHPObject *psShape, const char *pszSHPEncoding );
45 : OGRGeometry *SHPReadOGRObject( SHPHandle hSHP, int iShape, SHPObject *psShape );
46 : OGRFeatureDefn *SHPReadOGRFeatureDefn( const char * pszName,
47 : SHPHandle hSHP, DBFHandle hDBF,
48 : const char *pszSHPEncoding );
49 : OGRErr SHPWriteOGRFeature( SHPHandle hSHP, DBFHandle hDBF,
50 : OGRFeatureDefn *poFeatureDefn,
51 : OGRFeature *poFeature, const char *pszSHPEncoding,
52 : int* pbTruncationWarningEmitted );
53 :
54 : /************************************************************************/
55 : /* OGRShapeLayer */
56 : /************************************************************************/
57 :
58 : class OGRShapeDataSource;
59 :
60 : class OGRShapeLayer : public OGRAbstractProxiedLayer
61 : {
62 : OGRShapeDataSource *poDS;
63 : OGRSpatialReference *poSRS; /* lazy loaded --> use GetSpatialRef() */
64 : int bSRSSet;
65 : OGRFeatureDefn *poFeatureDefn;
66 : int iNextShapeId;
67 : int nTotalShapeCount;
68 :
69 : char *pszFullName;
70 :
71 : SHPHandle hSHP;
72 : DBFHandle hDBF;
73 :
74 : int bUpdateAccess;
75 :
76 : OGRwkbGeometryType eRequestedGeomType;
77 : int ResetGeomType( int nNewType );
78 :
79 : int ScanIndices();
80 :
81 : long *panMatchingFIDs;
82 : int iMatchingFID;
83 : void ClearMatchingFIDs();
84 :
85 : OGRGeometry *m_poFilterGeomLastValid;
86 : int nSpatialFIDCount;
87 : int *panSpatialFIDs;
88 : void ClearSpatialFIDs();
89 :
90 : int bHeaderDirty;
91 :
92 : int bCheckedForQIX;
93 : SHPTreeDiskHandle hQIX;
94 : int CheckForQIX();
95 :
96 : int bCheckedForSBN;
97 : SBNSearchHandle hSBN;
98 : int CheckForSBN();
99 :
100 : int bSbnSbxDeleted;
101 :
102 : CPLString ConvertCodePage( const char * );
103 : CPLString osEncoding;
104 :
105 : int bTruncationWarningEmitted;
106 :
107 : int bHSHPWasNonNULL; /* to know if we must try to reopen a .shp */
108 : int bHDBFWasNonNULL; /* to know if we must try to reopen a .dbf */
109 : int eFileDescriptorsState; /* current state of opening of file descriptor to .shp and .dbf */
110 : int TouchLayer();
111 : int ReopenFileDescriptors();
112 :
113 : int bResizeAtClose;
114 :
115 : void TruncateDBF();
116 :
117 :
118 : protected:
119 :
120 : virtual void CloseUnderlyingLayer();
121 :
122 : /* WARNING: each of the below public methods should start with a call to */
123 : /* TouchLayer() and test its return value, so as to make sure that */
124 : /* the layer is properly re-opened if necessary */
125 :
126 : public:
127 : OGRErr CreateSpatialIndex( int nMaxDepth );
128 : OGRErr DropSpatialIndex();
129 : OGRErr Repack();
130 : OGRErr RecomputeExtent();
131 : OGRErr ResizeDBF();
132 :
133 1218 : void SetResizeAtClose( int bFlag ) { bResizeAtClose = bFlag; }
134 :
135 518 : const char *GetFullName() { return pszFullName; }
136 :
137 : OGRFeature * FetchShape(int iShapeId);
138 : int GetFeatureCountWithSpatialFilterOnly();
139 :
140 : public:
141 : OGRShapeLayer( OGRShapeDataSource* poDSIn,
142 : const char * pszName,
143 : SHPHandle hSHP, DBFHandle hDBF,
144 : OGRSpatialReference *poSRS, int bSRSSet,
145 : int bUpdate,
146 : OGRwkbGeometryType eReqType );
147 : ~OGRShapeLayer();
148 :
149 : void ResetReading();
150 : OGRFeature * GetNextFeature();
151 : virtual OGRErr SetNextByIndex( long nIndex );
152 :
153 : OGRFeature *GetFeature( long nFeatureId );
154 : OGRErr SetFeature( OGRFeature *poFeature );
155 : OGRErr DeleteFeature( long nFID );
156 : OGRErr CreateFeature( OGRFeature *poFeature );
157 : OGRErr SyncToDisk();
158 :
159 1091521 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
160 :
161 : int GetFeatureCount( int );
162 : OGRErr GetExtent(OGREnvelope *psExtent, int bForce);
163 :
164 : virtual OGRErr CreateField( OGRFieldDefn *poField,
165 : int bApproxOK = TRUE );
166 : virtual OGRErr DeleteField( int iField );
167 : virtual OGRErr ReorderFields( int* panMap );
168 : virtual OGRErr AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags );
169 :
170 : virtual OGRSpatialReference *GetSpatialRef();
171 :
172 : virtual int TestCapability( const char * );
173 : virtual void SetSpatialFilter( OGRGeometry * );
174 : virtual OGRErr SetAttributeFilter( const char * );
175 : };
176 :
177 : /************************************************************************/
178 : /* OGRShapeDataSource */
179 : /************************************************************************/
180 :
181 : class OGRShapeDataSource : public OGRDataSource
182 : {
183 : OGRShapeLayer **papoLayers;
184 : int nLayers;
185 :
186 : char *pszName;
187 :
188 : int bDSUpdate;
189 :
190 : int bSingleFileDataSource;
191 :
192 : OGRLayerPool* poPool;
193 :
194 : void AddLayer(OGRShapeLayer* poLayer);
195 :
196 : std::vector<CPLString> oVectorLayerName;
197 :
198 : public:
199 : OGRShapeDataSource();
200 : ~OGRShapeDataSource();
201 :
202 3908 : OGRLayerPool *GetPool() { return poPool; }
203 :
204 : int Open( const char *, int bUpdate, int bTestOpen,
205 : int bForceSingleFileDataSource = FALSE );
206 : int OpenFile( const char *, int bUpdate, int bTestOpen );
207 :
208 495 : virtual const char *GetName() { return pszName; }
209 :
210 : virtual int GetLayerCount();
211 : virtual OGRLayer *GetLayer( int );
212 : virtual OGRLayer *GetLayerByName(const char *);
213 :
214 : virtual OGRLayer *CreateLayer( const char *,
215 : OGRSpatialReference * = NULL,
216 : OGRwkbGeometryType = wkbUnknown,
217 : char ** = NULL );
218 :
219 : virtual OGRLayer *ExecuteSQL( const char *pszStatement,
220 : OGRGeometry *poSpatialFilter,
221 : const char *pszDialect );
222 :
223 : virtual int TestCapability( const char * );
224 : virtual OGRErr DeleteLayer( int iLayer );
225 :
226 : void SetLastUsedLayer( OGRShapeLayer* poLayer );
227 : void UnchainLayer( OGRShapeLayer* poLayer );
228 : };
229 :
230 : /************************************************************************/
231 : /* OGRShapeDriver */
232 : /************************************************************************/
233 :
234 : class OGRShapeDriver : public OGRSFDriver
235 226 : {
236 : public:
237 : ~OGRShapeDriver();
238 :
239 : const char *GetName();
240 : OGRDataSource *Open( const char *, int );
241 :
242 : virtual OGRDataSource *CreateDataSource( const char *pszName,
243 : char ** = NULL );
244 : OGRErr DeleteDataSource( const char *pszDataSource );
245 :
246 : int TestCapability( const char * );
247 : };
248 :
249 :
250 : #endif /* ndef _OGRSHAPE_H_INCLUDED */
|