1 : /******************************************************************************
2 : * $Id: ogr_mssqlspatial.h 21939 2011-03-11 21:55: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 :
107 : protected:
108 : OGRPoint* ReadPoint(int iShape);
109 : OGRMultiPoint* ReadMultiPoint(int iShape);
110 : OGRLineString* ReadLineString(int iShape);
111 : OGRMultiLineString* ReadMultiLineString(int iShape);
112 : OGRPolygon* ReadPolygon(int iShape);
113 : OGRMultiPolygon* ReadMultiPolygon(int iShape);
114 : OGRGeometryCollection* ReadGeometryCollection(int iShape);
115 :
116 : public:
117 : OGRMSSQLGeometryParser();
118 : OGRErr ParseSqlGeometry(unsigned char* pszInput, int nLen,
119 : OGRGeometry **poGeom);
120 0 : int GetSRSId() { return nSRSId; };
121 : };
122 :
123 :
124 : /************************************************************************/
125 : /* OGRMSSQLSpatialLayer */
126 : /************************************************************************/
127 :
128 : class OGRMSSQLSpatialLayer : public OGRLayer
129 : {
130 : protected:
131 : OGRFeatureDefn *poFeatureDefn;
132 :
133 : CPLODBCStatement *poStmt;
134 :
135 : // Layer spatial reference system, and srid.
136 : OGRSpatialReference *poSRS;
137 : int nSRSId;
138 :
139 : int iNextShapeId;
140 :
141 : OGRMSSQLSpatialDataSource *poDS;
142 :
143 : int nGeomColumnType;
144 : char *pszGeomColumn;
145 : char *pszFIDColumn;
146 :
147 : int *panFieldOrdinals;
148 :
149 : CPLErr BuildFeatureDefn( const char *pszLayerName,
150 : CPLODBCStatement *poStmt );
151 :
152 0 : virtual CPLODBCStatement * GetStatement() { return poStmt; }
153 :
154 : public:
155 : OGRMSSQLSpatialLayer();
156 : virtual ~OGRMSSQLSpatialLayer();
157 :
158 : virtual void ResetReading();
159 : virtual OGRFeature *GetNextRawFeature();
160 : virtual OGRFeature *GetNextFeature();
161 :
162 : virtual OGRFeature *GetFeature( long nFeatureId );
163 :
164 0 : OGRFeatureDefn * GetLayerDefn() { return poFeatureDefn; }
165 :
166 : virtual OGRSpatialReference *GetSpatialRef();
167 :
168 : virtual OGRErr StartTransaction();
169 : virtual OGRErr CommitTransaction();
170 : virtual OGRErr RollbackTransaction();
171 :
172 : virtual const char *GetFIDColumn();
173 : virtual const char *GetGeometryColumn();
174 :
175 : virtual int TestCapability( const char * );
176 : char* GByteArrayToHexString( const GByte* pabyData, int nLen);
177 : };
178 :
179 : /************************************************************************/
180 : /* OGRMSSQLSpatialTableLayer */
181 : /************************************************************************/
182 :
183 : class OGRMSSQLSpatialTableLayer : public OGRMSSQLSpatialLayer
184 : {
185 : int bUpdateAccess;
186 : int bLaunderColumnNames;
187 : int bPreservePrecision;
188 :
189 : char *pszQuery;
190 :
191 : void ClearStatement();
192 : CPLODBCStatement* BuildStatement(const char* pszColumns);
193 :
194 : CPLString BuildFields();
195 :
196 : virtual CPLODBCStatement * GetStatement();
197 :
198 : char *pszTableName;
199 : char *pszSchemaName;
200 :
201 : public:
202 : OGRMSSQLSpatialTableLayer( OGRMSSQLSpatialDataSource * );
203 : ~OGRMSSQLSpatialTableLayer();
204 :
205 : CPLErr Initialize( const char *pszSchema,
206 : const char *pszTableName,
207 : const char *pszGeomCol,
208 : int nCoordDimension,
209 : int nSRId,
210 : OGRwkbGeometryType eType);
211 :
212 : OGRErr CreateSpatialIndex();
213 : void DropSpatialIndex();
214 :
215 : virtual void ResetReading();
216 : virtual int GetFeatureCount( int );
217 :
218 : virtual OGRErr SetAttributeFilter( const char * );
219 :
220 : virtual OGRErr SetFeature( OGRFeature *poFeature );
221 : virtual OGRErr DeleteFeature( long nFID );
222 : virtual OGRErr CreateFeature( OGRFeature *poFeature );
223 :
224 0 : const char* GetTableName() { return pszTableName; }
225 0 : const char* GetSchemaName() { return pszSchemaName; }
226 :
227 : virtual OGRErr CreateField( OGRFieldDefn *poField,
228 : int bApproxOK = TRUE );
229 :
230 : virtual OGRFeature *GetFeature( long nFeatureId );
231 :
232 : virtual int TestCapability( const char * );
233 :
234 0 : void SetLaunderFlag( int bFlag )
235 0 : { bLaunderColumnNames = bFlag; }
236 0 : void SetPrecisionFlag( int bFlag )
237 0 : { bPreservePrecision = bFlag; }
238 : void AppendFieldValue(CPLODBCStatement *poStatement,
239 : OGRFeature* poFeature, int i);
240 :
241 : int FetchSRSId();
242 : };
243 :
244 : /************************************************************************/
245 : /* OGRMSSQLSpatialSelectLayer */
246 : /************************************************************************/
247 :
248 : class OGRMSSQLSpatialSelectLayer : public OGRMSSQLSpatialLayer
249 : {
250 : char *pszBaseStatement;
251 :
252 : void ClearStatement();
253 : OGRErr ResetStatement();
254 :
255 : virtual CPLODBCStatement * GetStatement();
256 :
257 : public:
258 : OGRMSSQLSpatialSelectLayer( OGRMSSQLSpatialDataSource *,
259 : CPLODBCStatement * );
260 : ~OGRMSSQLSpatialSelectLayer();
261 :
262 : virtual void ResetReading();
263 : virtual int GetFeatureCount( int );
264 :
265 : virtual OGRFeature *GetFeature( long nFeatureId );
266 :
267 : virtual OGRErr GetExtent(OGREnvelope *psExtent, int bForce = TRUE);
268 :
269 : virtual int TestCapability( const char * );
270 : };
271 :
272 : /************************************************************************/
273 : /* OGRODBCDataSource */
274 : /************************************************************************/
275 :
276 : class OGRMSSQLSpatialDataSource : public OGRDataSource
277 : {
278 : OGRMSSQLSpatialTableLayer **papoLayers;
279 : int nLayers;
280 :
281 : char *pszName;
282 :
283 : char *pszCatalog;
284 :
285 : int bDSUpdate;
286 : CPLODBCSession oSession;
287 :
288 : int nGeometryFormat;
289 :
290 : // We maintain a list of known SRID to reduce the number of trips to
291 : // the database to get SRSes.
292 : int nKnownSRID;
293 : int *panSRID;
294 : OGRSpatialReference **papoSRS;
295 :
296 : public:
297 : OGRMSSQLSpatialDataSource();
298 : ~OGRMSSQLSpatialDataSource();
299 :
300 0 : const char *GetCatalog() { return pszCatalog; }
301 :
302 : int ParseValue(char** pszValue, char* pszSource, const char* pszKey,
303 : int nStart, int nNext, int nTerm, int bRemove);
304 :
305 : int Open( const char *, int bUpdate, int bTestOpen );
306 : int OpenTable( const char *pszSchemaName, const char *pszTableName,
307 : const char *pszGeomCol,int nCoordDimension,
308 : int nSRID, OGRwkbGeometryType eType, int bUpdate );
309 :
310 0 : const char *GetName() { return pszName; }
311 : int GetLayerCount();
312 : OGRLayer *GetLayer( int );
313 :
314 0 : int GetGeometryFormat() { return nGeometryFormat; }
315 :
316 : virtual int DeleteLayer( int iLayer );
317 : virtual OGRLayer *CreateLayer( const char *,
318 : OGRSpatialReference * = NULL,
319 : OGRwkbGeometryType = wkbUnknown,
320 : char ** = NULL );
321 :
322 : int TestCapability( const char * );
323 :
324 : virtual OGRLayer * ExecuteSQL( const char *pszSQLCommand,
325 : OGRGeometry *poSpatialFilter,
326 : const char *pszDialect );
327 : virtual void ReleaseResultSet( OGRLayer * poLayer );
328 :
329 : char *LaunderName( const char *pszSrcName );
330 : OGRErr InitializeMetadataTables();
331 :
332 : OGRSpatialReference* FetchSRS( int nId );
333 : int FetchSRSId( OGRSpatialReference * poSRS );
334 :
335 : // Internal use
336 0 : CPLODBCSession *GetSession() { return &oSession; }
337 :
338 : };
339 :
340 : /************************************************************************/
341 : /* OGRODBCDriver */
342 : /************************************************************************/
343 :
344 : class OGRMSSQLSpatialDriver : public OGRSFDriver
345 178 : {
346 : public:
347 : ~OGRMSSQLSpatialDriver();
348 :
349 : const char *GetName();
350 : OGRDataSource *Open( const char *, int );
351 :
352 : virtual OGRDataSource *CreateDataSource( const char *pszName,
353 : char ** = NULL );
354 :
355 : int TestCapability( const char * );
356 : };
357 :
358 : #endif /* ndef _OGR_MSSQLSPATIAL_H_INCLUDED */
|