1 : /******************************************************************************
2 : * $Id: ogr_osm.h 24950 2012-09-22 13:54:36Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Private definitions for OGR/OpenStreeMap driver.
6 : * Author: Even Rouault, <even dot rouault at mines dash paris dot org>
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2010, Even Rouault
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_OSM_H_INCLUDED
31 : #define _OGR_OSM_H_INCLUDED
32 :
33 : #include "ogrsf_frmts.h"
34 : #include "cpl_string.h"
35 :
36 : #include <set>
37 : #include <map>
38 : #include <vector>
39 :
40 : #include "osm_parser.h"
41 :
42 : #include "ogr_sqlite.h"
43 :
44 : class ConstCharComp
45 : {
46 : public:
47 5920 : bool operator()(const char* a, const char* b) const
48 : {
49 5920 : return strcmp(a, b) < 0;
50 : }
51 : };
52 :
53 : /************************************************************************/
54 : /* OGROSMLayer */
55 : /************************************************************************/
56 :
57 : class OGROSMDataSource;
58 :
59 : class OGROSMLayer : public OGRLayer
60 : {
61 : friend class OGROSMDataSource;
62 :
63 : OGROSMDataSource *poDS;
64 : OGRFeatureDefn *poFeatureDefn;
65 : OGRSpatialReference *poSRS;
66 : long nFeatureCount;
67 :
68 : std::vector<char*> apszNames;
69 : std::map<const char*, int, ConstCharComp> oMapFieldNameToIndex;
70 :
71 : int bResetReadingAllowed;
72 :
73 : int nFeatureArraySize;
74 : int nFeatureArrayMaxSize;
75 : int nFeatureArrayIndex;
76 : OGRFeature** papoFeatures;
77 :
78 : int bHasOSMId;
79 : int nIndexOSMId;
80 : int nIndexOSMWayId;
81 : int bHasVersion;
82 : int bHasTimestamp;
83 : int bHasUID;
84 : int bHasUser;
85 : int bHasChangeset;
86 : int bHasOtherTags;
87 :
88 : int bHasWarnedTooManyFeatures;
89 :
90 : char *pszAllTags;
91 : int bHasWarnedAllTagsTruncated;
92 :
93 : int bUserInterested;
94 :
95 : int AddToArray(OGRFeature* poFeature);
96 :
97 : char szLaunderedFieldName[256];
98 : const char* GetLaunderedFieldName(const char* pszName);
99 :
100 : std::vector<char*> apszUnsignificantKeys;
101 : std::map<const char*, int, ConstCharComp> aoSetUnsignificantKeys;
102 :
103 : std::vector<char*> apszIgnoreKeys;
104 : std::map<const char*, int, ConstCharComp> aoSetIgnoreKeys;
105 :
106 : std::set<std::string> aoSetWarnKeys;
107 :
108 : public:
109 : OGROSMLayer( OGROSMDataSource* poDS,
110 : const char* pszName );
111 : virtual ~OGROSMLayer();
112 :
113 1614 : virtual OGRFeatureDefn *GetLayerDefn() {return poFeatureDefn;}
114 41 : virtual OGRSpatialReference * GetSpatialRef() { return poSRS; }
115 :
116 : virtual void ResetReading();
117 : virtual int TestCapability( const char * );
118 :
119 : virtual OGRFeature *GetNextFeature();
120 : virtual int GetFeatureCount( int bForce );
121 :
122 : virtual OGRErr GetExtent( OGREnvelope *psExtent, int bForce );
123 :
124 : const OGREnvelope* GetSpatialFilterEnvelope();
125 :
126 : int AddFeature(OGRFeature* poFeature,
127 : int bAttrFilterAlreadyEvaluated,
128 : int* pbFilteredOut = NULL);
129 : void ForceResetReading();
130 :
131 : void AddField(const char* pszName, OGRFieldType eFieldType);
132 : int GetFieldIndex(const char* pszName);
133 :
134 : int HasOSMId() const { return bHasOSMId; }
135 50 : void SetHasOSMId(int bIn) { bHasOSMId = bIn; }
136 :
137 10 : int HasVersion() const { return bHasVersion; }
138 50 : void SetHasVersion(int bIn) { bHasVersion = bIn; }
139 :
140 10 : int HasTimestamp() const { return bHasTimestamp; }
141 50 : void SetHasTimestamp(int bIn) { bHasTimestamp = bIn; }
142 :
143 10 : int HasUID() const { return bHasUID; }
144 50 : void SetHasUID(int bIn) { bHasUID = bIn; }
145 :
146 10 : int HasUser() const { return bHasUser; }
147 50 : void SetHasUser(int bIn) { bHasUser = bIn; }
148 :
149 10 : int HasChangeset() const { return bHasChangeset; }
150 50 : void SetHasChangeset(int bIn) { bHasChangeset = bIn; }
151 :
152 0 : void SetHasOtherTags(int bIn) { bHasOtherTags = bIn; }
153 50 : int HasOtherTags() const { return bHasOtherTags; }
154 : int AddInOtherTag(const char* pszK);
155 :
156 : void SetFieldsFromTags(OGRFeature* poFeature,
157 : GIntBig nID,
158 : int bIsWayID,
159 : unsigned int nTags, OSMTag* pasTags,
160 : OSMInfo* psInfo);
161 :
162 187 : void SetDeclareInterest(int bIn) { bUserInterested = bIn; }
163 509 : int IsUserInterested() const { return bUserInterested; }
164 :
165 42 : int HasAttributeFilter() const { return m_poAttrQuery != NULL; }
166 : int EvaluateAttributeFilter(OGRFeature* poFeature);
167 :
168 : void AddUnsignificantKey(const char* pszK);
169 53 : int IsSignificantKey(const char* pszK) const
170 53 : { return aoSetUnsignificantKeys.find(pszK) == aoSetUnsignificantKeys.end(); }
171 :
172 : void AddIgnoreKey(const char* pszK);
173 : void AddWarnKey(const char* pszK);
174 : };
175 :
176 : /************************************************************************/
177 : /* OGROSMDataSource */
178 : /************************************************************************/
179 :
180 : typedef struct
181 : {
182 : char* pszK;
183 : int nKeyIndex;
184 : int nOccurences;
185 : std::vector<char*> asValues;
186 : std::map<const char*, int, ConstCharComp> anMapV; /* map that is the reverse of asValues */
187 36 : } KeyDesc;
188 :
189 : typedef struct
190 : {
191 : short nKeyIndex; /* index of OGROSMDataSource.asKeys */
192 : short bVIsIndex; /* whether we should use nValueIndex or nOffsetInpabyNonRedundantValues */
193 : union
194 : {
195 : int nValueIndex; /* index of KeyDesc.asValues */
196 : int nOffsetInpabyNonRedundantValues; /* offset in OGROSMDataSource.pabyNonRedundantValues */
197 : } u;
198 : } IndexedKVP;
199 :
200 : typedef struct
201 : {
202 : GIntBig nOff;
203 : union
204 : {
205 : GByte *pabyBitmap;
206 : GByte *panSectorSize; /* (size in bytes - 8 ) / 2, minus 8. 252 means uncompressed */
207 : } u;
208 : } Bucket;
209 :
210 : typedef struct
211 : {
212 : int nLon;
213 : int nLat;
214 : } LonLat;
215 :
216 : typedef struct
217 : {
218 : GIntBig nWayID;
219 : GIntBig* panNodeRefs; /* point to a sub-array of OGROSMDataSource.anReqIds */
220 : unsigned int nRefs;
221 : unsigned int nTags;
222 : IndexedKVP* pasTags; /* point to a sub-array of OGROSMDataSource.pasAccumulatedTags */
223 : OSMInfo sInfo;
224 : OGRFeature *poFeature;
225 : int bIsArea : 1;
226 : int bAttrFilterAlreadyEvaluated : 1;
227 : } WayFeaturePair;
228 :
229 : class OGROSMDataSource : public OGRDataSource
230 : {
231 : friend class OGROSMLayer;
232 :
233 : int nLayers;
234 : OGROSMLayer** papoLayers;
235 : char* pszName;
236 :
237 : OGREnvelope sExtent;
238 : int bExtentValid;
239 :
240 : int bInterleavedReading;
241 : OGROSMLayer *poCurrentLayer;
242 :
243 : OSMContext *psParser;
244 : int bHasParsedFirstChunk;
245 : int bStopParsing;
246 :
247 : #ifdef HAVE_SQLITE_VFS
248 : sqlite3_vfs* pMyVFS;
249 : #endif
250 :
251 : sqlite3 *hDB;
252 : sqlite3_stmt *hInsertNodeStmt;
253 : sqlite3_stmt *hInsertWayStmt;
254 : sqlite3_stmt *hSelectNodeBetweenStmt;
255 : sqlite3_stmt **pahSelectNodeStmt;
256 : sqlite3_stmt **pahSelectWayStmt;
257 : sqlite3_stmt *hInsertPolygonsStandaloneStmt;
258 : sqlite3_stmt *hDeletePolygonsStandaloneStmt;
259 : sqlite3_stmt *hSelectPolygonsStandaloneStmt;
260 : int bHasRowInPolygonsStandalone;
261 :
262 : int nMaxSizeForInMemoryDBInMB;
263 : int bInMemoryTmpDB;
264 : int bMustUnlink;
265 : CPLString osTmpDBName;
266 :
267 : int nNodesInTransaction;
268 :
269 : std::set<std::string> aoSetClosedWaysArePolygons;
270 :
271 : LonLat *pasLonLatCache;
272 :
273 : int bReportAllNodes;
274 : int bReportAllWays;
275 :
276 : int bFeatureAdded;
277 :
278 : int bInTransaction;
279 :
280 : int bIndexPoints;
281 : int bUsePointsIndex;
282 : int bIndexWays;
283 : int bUseWaysIndex;
284 :
285 : std::vector<int> abSavedDeclaredInterest;
286 : OGRLayer* poResultSetLayer;
287 : int bIndexPointsBackup;
288 : int bUsePointsIndexBackup;
289 : int bIndexWaysBackup;
290 : int bUseWaysIndexBackup;
291 :
292 : int bIsFeatureCountEnabled;
293 :
294 : int bAttributeNameLaundering;
295 :
296 : GByte *pabyWayBuffer;
297 :
298 : int nWaysProcessed;
299 : int nRelationsProcessed;
300 :
301 : int bCustomIndexing;
302 : int bCompressNodes;
303 :
304 : unsigned int nUnsortedReqIds;
305 : GIntBig *panUnsortedReqIds;
306 :
307 : unsigned int nReqIds;
308 : GIntBig *panReqIds;
309 : LonLat *pasLonLatArray;
310 :
311 : IndexedKVP *pasAccumulatedTags; /* points to content of pabyNonRedundantValues or aoMapIndexedKeys */
312 : int nAccumulatedTags;
313 : GByte *pabyNonRedundantValues;
314 : int nNonRedundantValuesLen;
315 : WayFeaturePair *pasWayFeaturePairs;
316 : int nWayFeaturePairs;
317 :
318 : int nNextKeyIndex;
319 : std::vector<KeyDesc*> asKeys;
320 : std::map<const char*, KeyDesc*, ConstCharComp> aoMapIndexedKeys; /* map that is the reverse of asKeys */
321 :
322 : CPLString osNodesFilename;
323 : int bInMemoryNodesFile;
324 : int bMustUnlinkNodesFile;
325 : GIntBig nNodesFileSize;
326 : VSILFILE *fpNodes;
327 :
328 : GIntBig nPrevNodeId;
329 : int nBucketOld;
330 : int nOffInBucketReducedOld;
331 : GByte *pabySector;
332 : Bucket *papsBuckets;
333 :
334 : int bNeedsToSaveWayInfo;
335 :
336 : int CompressWay (unsigned int nTags, IndexedKVP* pasTags,
337 : int nPoints, LonLat* pasLonLatPairs,
338 : OSMInfo* psInfo,
339 : GByte* pabyCompressedWay);
340 : int UncompressWay( int nBytes, GByte* pabyCompressedWay,
341 : LonLat* pasCoords,
342 : unsigned int* pnTags, OSMTag* pasTags,
343 : OSMInfo* psInfo );
344 :
345 : int ParseConf();
346 : int CreateTempDB();
347 : int SetDBOptions();
348 : int SetCacheSize();
349 : int CreatePreparedStatements();
350 : void CloseDB();
351 :
352 : int IndexPoint(OSMNode* psNode);
353 : int IndexPointSQLite(OSMNode* psNode);
354 : int FlushCurrentSector();
355 : int FlushCurrentSectorCompressedCase();
356 : int FlushCurrentSectorNonCompressedCase();
357 : int IndexPointCustom(OSMNode* psNode);
358 :
359 : void IndexWay(GIntBig nWayID,
360 : unsigned int nTags, IndexedKVP* pasTags,
361 : LonLat* pasLonLatPairs, int nPairs,
362 : OSMInfo* psInfo);
363 :
364 : int StartTransaction();
365 : int CommitTransaction();
366 :
367 : int FindNode(GIntBig nID);
368 : void ProcessWaysBatch();
369 :
370 : void ProcessPolygonsStandalone();
371 :
372 : void LookupNodes();
373 : void LookupNodesSQLite();
374 : void LookupNodesCustom();
375 : void LookupNodesCustomCompressedCase();
376 : void LookupNodesCustomNonCompressedCase();
377 :
378 : unsigned int LookupWays( std::map< GIntBig, std::pair<int,void*> >& aoMapWays,
379 : OSMRelation* psRelation );
380 :
381 : OGRGeometry* BuildMultiPolygon(OSMRelation* psRelation,
382 : unsigned int* pnTags,
383 : OSMTag* pasTags);
384 : OGRGeometry* BuildGeometryCollection(OSMRelation* psRelation, int bMultiLineString);
385 :
386 : int TransferToDiskIfNecesserary();
387 :
388 : public:
389 : OGROSMDataSource();
390 : ~OGROSMDataSource();
391 :
392 25 : virtual const char *GetName() { return pszName; }
393 584 : virtual int GetLayerCount() { return nLayers; }
394 : virtual OGRLayer *GetLayer( int );
395 :
396 : virtual int TestCapability( const char * );
397 :
398 : virtual OGRLayer * ExecuteSQL( const char *pszSQLCommand,
399 : OGRGeometry *poSpatialFilter,
400 : const char *pszDialect );
401 : virtual void ReleaseResultSet( OGRLayer * poLayer );
402 :
403 :
404 : int Open ( const char* pszFilename, int bUpdateIn );
405 :
406 : int ResetReading();
407 : int ParseNextChunk();
408 : OGRErr GetExtent( OGREnvelope *psExtent );
409 : int IsInterleavedReading();
410 :
411 : void NotifyNodes(unsigned int nNodes, OSMNode* pasNodes);
412 : void NotifyWay (OSMWay* psWay);
413 : void NotifyRelation (OSMRelation* psRelation);
414 : void NotifyBounds (double dfXMin, double dfYMin,
415 : double dfXMax, double dfYMax);
416 :
417 33 : OGROSMLayer* GetCurrentLayer() { return poCurrentLayer; }
418 51 : void SetCurrentLayer(OGROSMLayer* poLyr) { poCurrentLayer = poLyr; }
419 :
420 0 : int IsFeatureCountEnabled() const { return bIsFeatureCountEnabled; }
421 :
422 500 : int DoesAttributeNameLaundering() const { return bAttributeNameLaundering; }
423 : };
424 :
425 : /************************************************************************/
426 : /* OGROSMDriver */
427 : /************************************************************************/
428 :
429 : class OGROSMDriver : public OGRSFDriver
430 226 : {
431 : public:
432 : ~OGROSMDriver();
433 :
434 : virtual const char *GetName();
435 : virtual OGRDataSource *Open( const char *, int );
436 : virtual OGRDataSource *CreateDataSource( const char * pszName,
437 : char **papszOptions );
438 :
439 : virtual int TestCapability( const char * );
440 : };
441 :
442 : #endif /* ndef _OGR_OSM_H_INCLUDED */
443 :
|