1 : /******************************************************************************
2 : * $Id: ogr_mssqlspatial.h 24967 2012-09-24 21:51:46Z 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 : 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 *pszSchemaName;
203 :
204 : public:
205 : OGRMSSQLSpatialTableLayer( OGRMSSQLSpatialDataSource * );
206 : ~OGRMSSQLSpatialTableLayer();
207 :
208 : CPLErr Initialize( const char *pszSchema,
209 : const char *pszTableName,
210 : const char *pszGeomCol,
211 : int nCoordDimension,
212 : int nSRId,
213 : OGRwkbGeometryType eType);
214 :
215 : OGRErr CreateSpatialIndex();
216 : void DropSpatialIndex();
217 :
218 : virtual void ResetReading();
219 : virtual int GetFeatureCount( int );
220 :
221 : virtual OGRErr SetAttributeFilter( const char * );
222 :
223 : virtual OGRErr SetFeature( OGRFeature *poFeature );
224 : virtual OGRErr DeleteFeature( long nFID );
225 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
226 :
227 0 : const char* GetTableName() { return pszTableName; }
228 0 : const char* GetSchemaName() { return pszSchemaName; }
229 :
230 : virtual OGRErr CreateField( OGRFieldDefn *poField,
231 : int bApproxOK = TRUE );
232 :
233 : virtual OGRFeature *GetFeature( long nFeatureId );
234 :
235 : virtual int TestCapability( const char * );
236 :
237 0 : void SetLaunderFlag( int bFlag )
238 0 : { bLaunderColumnNames = bFlag; }
239 0 : void SetPrecisionFlag( int bFlag )
240 0 : { bPreservePrecision = bFlag; }
241 : void AppendFieldValue(CPLODBCStatement *poStatement,
242 : OGRFeature* poFeature, int i);
243 :
244 : int FetchSRSId();
245 : };
246 :
247 : /************************************************************************/
248 : /* OGRMSSQLSpatialSelectLayer */
249 : /************************************************************************/
250 :
251 : class OGRMSSQLSpatialSelectLayer : public OGRMSSQLSpatialLayer
252 : {
253 : char *pszBaseStatement;
254 :
255 : void ClearStatement();
256 : OGRErr ResetStatement();
257 :
258 : virtual CPLODBCStatement * GetStatement();
259 :
260 : public:
261 : OGRMSSQLSpatialSelectLayer( OGRMSSQLSpatialDataSource *,
262 : CPLODBCStatement * );
263 : ~OGRMSSQLSpatialSelectLayer();
264 :
265 : virtual void ResetReading();
266 : virtual int GetFeatureCount( int );
267 :
268 : virtual OGRFeature *GetFeature( long nFeatureId );
269 :
270 : virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE);
271 :
272 : virtual int TestCapability( const char * );
273 : };
274 :
275 : /************************************************************************/
276 : /* OGRODBCDataSource */
277 : /************************************************************************/
278 :
279 : class OGRMSSQLSpatialDataSource : public OGRDataSource
280 : {
281 : OGRMSSQLSpatialTableLayer **papoLayers;
282 : int nLayers;
283 :
284 : char *pszName;
285 :
286 : char *pszCatalog;
287 :
288 : int bDSUpdate;
289 : CPLODBCSession oSession;
290 :
291 : int nGeometryFormat;
292 :
293 : // We maintain a list of known SRID to reduce the number of trips to
294 : // the database to get SRSes.
295 : int nKnownSRID;
296 : int *panSRID;
297 : OGRSpatialReference **papoSRS;
298 :
299 : public:
300 : OGRMSSQLSpatialDataSource();
301 : ~OGRMSSQLSpatialDataSource();
302 :
303 0 : const char *GetCatalog() { return pszCatalog; }
304 :
305 : int ParseValue(char** pszValue, char* pszSource, const char* pszKey,
306 : int nStart, int nNext, int nTerm, int bRemove);
307 :
308 : int Open( const char *, int bUpdate, int bTestOpen );
309 : int OpenTable( const char *pszSchemaName, const char *pszTableName,
310 : const char *pszGeomCol,int nCoordDimension,
311 : int nSRID, OGRwkbGeometryType eType, int bUpdate );
312 :
313 0 : const char *GetName() { return pszName; }
314 : int GetLayerCount();
315 : OGRLayer *GetLayer( int );
316 :
317 0 : int GetGeometryFormat() { return nGeometryFormat; }
318 :
319 : virtual int DeleteLayer( int iLayer );
320 : virtual OGRLayer *CreateLayer( const char *,
321 : OGRSpatialReference * = NULL,
322 : OGRwkbGeometryType = wkbUnknown,
323 : char ** = NULL );
324 :
325 : int TestCapability( const char * );
326 :
327 : virtual OGRLayer * ExecuteSQL( const char *pszSQLCommand,
328 : OGRGeometry *poSpatialFilter,
329 : const char *pszDialect );
330 : virtual void ReleaseResultSet( OGRLayer * poLayer );
331 :
332 : char *LaunderName( const char *pszSrcName );
333 : OGRErr InitializeMetadataTables();
334 :
335 : OGRSpatialReference* FetchSRS( int nId );
336 : int FetchSRSId( OGRSpatialReference * poSRS );
337 :
338 : // Internal use
339 0 : CPLODBCSession *GetSession() { return &oSession; }
340 :
341 : };
342 :
343 : /************************************************************************/
344 : /* OGRODBCDriver */
345 : /************************************************************************/
346 :
347 : class OGRMSSQLSpatialDriver : public OGRSFDriver
348 226 : {
349 : public:
350 : ~OGRMSSQLSpatialDriver();
351 :
352 : const char *GetName();
353 : OGRDataSource *Open( const char *, int );
354 :
355 : virtual OGRDataSource *CreateDataSource( const char *pszName,
356 : char ** = NULL );
357 :
358 : int TestCapability( const char * );
359 : };
360 :
361 : #endif /* ndef _OGR_MSSQLSPATIAL_H_INCLUDED */
|