1 : /******************************************************************************
2 : * $Id: ogr_mssqlspatial.h 24330 2012-04-28 11:46:49Z tamas $
3 : *
4 : * Project: MSSQL Spatial driver
5 : * Purpose: Definition of classes for OGR MSSQL Spatial driver.
6 : * Author: Tamas Szekeres, szekerest at gmail.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2010, Tamas Szekeres
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 _OGR_MSSQLSPATIAL_H_INCLUDED
31 : #define _OGR_MSSQLSPATIAL_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 : #include "cpl_odbc.h"
35 : #include "cpl_error.h"
36 :
37 : class OGRMSSQLSpatialDataSource;
38 :
39 : /* geometry format to transfer geometry column */
40 : #define MSSQLGEOMETRY_NATIVE 0
41 : #define MSSQLGEOMETRY_WKB 1
42 : #define MSSQLGEOMETRY_WKT 2
43 :
44 : /* geometry column types */
45 : #define MSSQLCOLTYPE_GEOMETRY 0
46 : #define MSSQLCOLTYPE_GEOGRAPHY 1
47 : #define MSSQLCOLTYPE_BINARY 2
48 : #define MSSQLCOLTYPE_TEXT 3
49 :
50 : /************************************************************************/
51 : /* OGRMSSQLAppendEscaped( ) */
52 : /************************************************************************/
53 :
54 : void OGRMSSQLAppendEscaped( CPLODBCStatement* poStatement, const char* pszStrValue);
55 :
56 : /************************************************************************/
57 : /* OGRMSSQLGeometryParser */
58 : /************************************************************************/
59 :
60 : class OGRMSSQLGeometryValidator
61 : {
62 : protected:
63 : int bIsValid;
64 : OGRGeometry* poValidGeometry;
65 : OGRGeometry* poOriginalGeometry;
66 :
67 : public:
68 : OGRMSSQLGeometryValidator(OGRGeometry* poGeom);
69 : ~OGRMSSQLGeometryValidator();
70 :
71 : int ValidatePoint(OGRPoint * poGeom);
72 : int ValidateMultiPoint(OGRMultiPoint * poGeom);
73 : int ValidateLineString(OGRLineString * poGeom);
74 : int ValidateLinearRing(OGRLinearRing * poGeom);
75 : int ValidateMultiLineString(OGRMultiLineString * poGeom);
76 : int ValidatePolygon(OGRPolygon* poGeom);
77 : int ValidateMultiPolygon(OGRMultiPolygon* poGeom);
78 : int ValidateGeometryCollection(OGRGeometryCollection* poGeom);
79 : int ValidateGeometry(OGRGeometry* poGeom);
80 :
81 : OGRGeometry* GetValidGeometryRef();
82 : int IsValid() { return bIsValid; };
83 : };
84 :
85 : /************************************************************************/
86 : /* OGRMSSQLGeometryParser */
87 : /************************************************************************/
88 :
89 : class OGRMSSQLGeometryParser
90 : {
91 : protected:
92 : unsigned char* pszData;
93 : /* serialization propeties */
94 : char chProps;
95 : /* point array */
96 : int nPointSize;
97 : int nPointPos;
98 : int nNumPoints;
99 : /* figure array */
100 : int nFigurePos;
101 : int nNumFigures;
102 : /* shape array */
103 : int nShapePos;
104 : int nNumShapes;
105 : int nSRSId;
106 : /* geometry or geography */
107 : int nColType;
108 :
109 : protected:
110 : OGRPoint* ReadPoint(int iShape);
111 : OGRMultiPoint* ReadMultiPoint(int iShape);
112 : OGRLineString* ReadLineString(int iShape);
113 : OGRMultiLineString* ReadMultiLineString(int iShape);
114 : OGRPolygon* ReadPolygon(int iShape);
115 : OGRMultiPolygon* ReadMultiPolygon(int iShape);
116 : OGRGeometryCollection* ReadGeometryCollection(int iShape);
117 :
118 : public:
119 : OGRMSSQLGeometryParser( int nGeomColumnType );
120 : OGRErr ParseSqlGeometry(unsigned char* pszInput, int nLen,
121 : OGRGeometry **poGeom);
122 0 : int GetSRSId() { return nSRSId; };
123 : };
124 :
125 :
126 : /************************************************************************/
127 : /* OGRMSSQLSpatialLayer */
128 : /************************************************************************/
129 :
130 : class OGRMSSQLSpatialLayer : public OGRLayer
131 : {
132 : protected:
133 : OGRFeatureDefn *poFeatureDefn;
134 :
135 : CPLODBCStatement *poStmt;
136 :
137 : // Layer spatial reference system, and srid.
138 : OGRSpatialReference *poSRS;
139 : int nSRSId;
140 :
141 : int iNextShapeId;
142 :
143 : OGRMSSQLSpatialDataSource *poDS;
144 :
145 : int nGeomColumnType;
146 : char *pszGeomColumn;
147 : char *pszFIDColumn;
148 :
149 : int *panFieldOrdinals;
150 :
151 : CPLErr BuildFeatureDefn( const char *pszLayerName,
152 : CPLODBCStatement *poStmt );
153 :
154 0 : virtual CPLODBCStatement * GetStatement() { return poStmt; }
155 :
156 : public:
157 : OGRMSSQLSpatialLayer();
158 : virtual ~OGRMSSQLSpatialLayer();
159 :
160 : virtual void ResetReading();
161 : virtual OGRFeature *GetNextRawFeature();
162 : virtual OGRFeature *GetNextFeature();
163 :
164 : virtual OGRFeature *GetFeature( long nFeatureId );
165 :
166 0 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
167 :
168 : virtual OGRSpatialReference *GetSpatialRef();
169 :
170 : virtual OGRErr StartTransaction();
171 : virtual OGRErr CommitTransaction();
172 : virtual OGRErr RollbackTransaction();
173 :
174 : virtual const char *GetFIDColumn();
175 : virtual const char *GetGeometryColumn();
176 :
177 : virtual int TestCapability( const char * );
178 : char* GByteArrayToHexString( const GByte* pabyData, int nLen);
179 : };
180 :
181 : /************************************************************************/
182 : /* OGRMSSQLSpatialTableLayer */
183 : /************************************************************************/
184 :
185 : class OGRMSSQLSpatialTableLayer : public OGRMSSQLSpatialLayer
186 : {
187 : int bUpdateAccess;
188 : int bLaunderColumnNames;
189 : int bPreservePrecision;
190 :
191 : char *pszQuery;
192 :
193 : void ClearStatement();
194 : CPLODBCStatement* BuildStatement(const char* pszColumns);
195 :
196 : CPLString BuildFields();
197 :
198 : virtual CPLODBCStatement * GetStatement();
199 :
200 : char *pszTableName;
201 : char *pszSchemaName;
202 :
203 : public:
204 : OGRMSSQLSpatialTableLayer( OGRMSSQLSpatialDataSource * );
205 : ~OGRMSSQLSpatialTableLayer();
206 :
207 : CPLErr Initialize( const char *pszSchema,
208 : const char *pszTableName,
209 : const char *pszGeomCol,
210 : int nCoordDimension,
211 : int nSRId,
212 : OGRwkbGeometryType eType);
213 :
214 : OGRErr CreateSpatialIndex();
215 : void DropSpatialIndex();
216 :
217 : virtual void ResetReading();
218 : virtual int GetFeatureCount( int );
219 :
220 : virtual OGRErr SetAttributeFilter( const char * );
221 :
222 : virtual OGRErr SetFeature( OGRFeature *poFeature );
223 : virtual OGRErr DeleteFeature( long nFID );
224 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
225 :
226 0 : const char* GetTableName() { return pszTableName; }
227 0 : const char* GetSchemaName() { return pszSchemaName; }
228 :
229 : virtual OGRErr CreateField( OGRFieldDefn *poField,
230 : int bApproxOK = TRUE );
231 :
232 : virtual OGRFeature *GetFeature( long nFeatureId );
233 :
234 : virtual int TestCapability( const char * );
235 :
236 0 : void SetLaunderFlag( int bFlag )
237 0 : { bLaunderColumnNames = bFlag; }
238 0 : void SetPrecisionFlag( int bFlag )
239 0 : { bPreservePrecision = bFlag; }
240 : void AppendFieldValue(CPLODBCStatement *poStatement,
241 : OGRFeature* poFeature, int i);
242 :
243 : int FetchSRSId();
244 : };
245 :
246 : /************************************************************************/
247 : /* OGRMSSQLSpatialSelectLayer */
248 : /************************************************************************/
249 :
250 : class OGRMSSQLSpatialSelectLayer : public OGRMSSQLSpatialLayer
251 : {
252 : char *pszBaseStatement;
253 :
254 : void ClearStatement();
255 : OGRErr ResetStatement();
256 :
257 : virtual CPLODBCStatement * GetStatement();
258 :
259 : public:
260 : OGRMSSQLSpatialSelectLayer( OGRMSSQLSpatialDataSource *,
261 : CPLODBCStatement * );
262 : ~OGRMSSQLSpatialSelectLayer();
263 :
264 : virtual void ResetReading();
265 : virtual int GetFeatureCount( int );
266 :
267 : virtual OGRFeature *GetFeature( long nFeatureId );
268 :
269 : virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE);
270 :
271 : virtual int TestCapability( const char * );
272 : };
273 :
274 : /************************************************************************/
275 : /* OGRODBCDataSource */
276 : /************************************************************************/
277 :
278 : class OGRMSSQLSpatialDataSource : public OGRDataSource
279 : {
280 : OGRMSSQLSpatialTableLayer **papoLayers;
281 : int nLayers;
282 :
283 : char *pszName;
284 :
285 : char *pszCatalog;
286 :
287 : int bDSUpdate;
288 : CPLODBCSession oSession;
289 :
290 : int nGeometryFormat;
291 :
292 : // We maintain a list of known SRID to reduce the number of trips to
293 : // the database to get SRSes.
294 : int nKnownSRID;
295 : int *panSRID;
296 : OGRSpatialReference **papoSRS;
297 :
298 : public:
299 : OGRMSSQLSpatialDataSource();
300 : ~OGRMSSQLSpatialDataSource();
301 :
302 0 : const char *GetCatalog() { return pszCatalog; }
303 :
304 : int ParseValue(char** pszValue, char* pszSource, const char* pszKey,
305 : int nStart, int nNext, int nTerm, int bRemove);
306 :
307 : int Open( const char *, int bUpdate, int bTestOpen );
308 : int OpenTable( const char *pszSchemaName, const char *pszTableName,
309 : const char *pszGeomCol,int nCoordDimension,
310 : int nSRID, OGRwkbGeometryType eType, int bUpdate );
311 :
312 0 : const char *GetName() { return pszName; }
313 : int GetLayerCount();
314 : OGRLayer *GetLayer( int );
315 :
316 0 : int GetGeometryFormat() { return nGeometryFormat; }
317 :
318 : virtual int DeleteLayer( int iLayer );
319 : virtual OGRLayer *CreateLayer( const char *,
320 : OGRSpatialReference * = NULL,
321 : OGRwkbGeometryType = wkbUnknown,
322 : char ** = NULL );
323 :
324 : int TestCapability( const char * );
325 :
326 : virtual OGRLayer * ExecuteSQL( const char *pszSQLCommand,
327 : OGRGeometry *poSpatialFilter,
328 : const char *pszDialect );
329 : virtual void ReleaseResultSet( OGRLayer * poLayer );
330 :
331 : char *LaunderName( const char *pszSrcName );
332 : OGRErr InitializeMetadataTables();
333 :
334 : OGRSpatialReference* FetchSRS( int nId );
335 : int FetchSRSId( OGRSpatialReference * poSRS );
336 :
337 : // Internal use
338 0 : CPLODBCSession *GetSession() { return &oSession; }
339 :
340 : };
341 :
342 : /************************************************************************/
343 : /* OGRODBCDriver */
344 : /************************************************************************/
345 :
346 : class OGRMSSQLSpatialDriver : public OGRSFDriver
347 389 : {
348 : public:
349 : ~OGRMSSQLSpatialDriver();
350 :
351 : const char *GetName();
352 : OGRDataSource *Open( const char *, int );
353 :
354 : virtual OGRDataSource *CreateDataSource( const char *pszName,
355 : char ** = NULL );
356 :
357 : int TestCapability( const char * );
358 : };
359 :
360 : #endif /* ndef _OGR_MSSQLSPATIAL_H_INCLUDED */
|