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