1 : /******************************************************************************
2 : * $Id: ogr_osm.h 25362 2012-12-27 17:02:54Z 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 14221 : bool operator()(const char* a, const char* b) const
48 : {
49 14221 : 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 2271 : 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 70 : void SetHasOSMId(int bIn) { bHasOSMId = bIn; }
136 :
137 14 : int HasVersion() const { return bHasVersion; }
138 70 : void SetHasVersion(int bIn) { bHasVersion = bIn; }
139 :
140 14 : int HasTimestamp() const { return bHasTimestamp; }
141 70 : void SetHasTimestamp(int bIn) { bHasTimestamp = bIn; }
142 :
143 14 : int HasUID() const { return bHasUID; }
144 70 : void SetHasUID(int bIn) { bHasUID = bIn; }
145 :
146 14 : int HasUser() const { return bHasUser; }
147 70 : void SetHasUser(int bIn) { bHasUser = bIn; }
148 :
149 14 : int HasChangeset() const { return bHasChangeset; }
150 70 : void SetHasChangeset(int bIn) { bHasChangeset = bIn; }
151 :
152 0 : void SetHasOtherTags(int bIn) { bHasOtherTags = bIn; }
153 384 : 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 4598 : int IsUserInterested() const { return bUserInterested; }
164 :
165 380 : int HasAttributeFilter() const { return m_poAttrQuery != NULL; }
166 : int EvaluateAttributeFilter(OGRFeature* poFeature);
167 :
168 : void AddUnsignificantKey(const char* pszK);
169 422 : int IsSignificantKey(const char* pszK) const
170 422 : { 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 100 : } 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 : /* Note: only one of nth bucket pabyBitmap or panSectorSize must be free'd */
204 : union
205 : {
206 : GByte *pabyBitmap; /* array of BUCKET_BITMAP_SIZE bytes */
207 : GByte *panSectorSize; /* array of BUCKET_SECTOR_SIZE_ARRAY_SIZE bytes. Each values means (size in bytes - 8 ) / 2, minus 8. 252 means uncompressed */
208 : } u;
209 : } Bucket;
210 :
211 : typedef struct
212 : {
213 : int nLon;
214 : int nLat;
215 : } LonLat;
216 :
217 : typedef struct
218 : {
219 : GIntBig nWayID;
220 : GIntBig* panNodeRefs; /* point to a sub-array of OGROSMDataSource.anReqIds */
221 : unsigned int nRefs;
222 : unsigned int nTags;
223 : IndexedKVP* pasTags; /* point to a sub-array of OGROSMDataSource.pasAccumulatedTags */
224 : OSMInfo sInfo;
225 : OGRFeature *poFeature;
226 : int bIsArea : 1;
227 : int bAttrFilterAlreadyEvaluated : 1;
228 : } WayFeaturePair;
229 :
230 : class OGROSMDataSource : public OGRDataSource
231 : {
232 : friend class OGROSMLayer;
233 :
234 : int nLayers;
235 : OGROSMLayer** papoLayers;
236 : char* pszName;
237 :
238 : OGREnvelope sExtent;
239 : int bExtentValid;
240 :
241 : int bInterleavedReading;
242 : OGROSMLayer *poCurrentLayer;
243 :
244 : OSMContext *psParser;
245 : int bHasParsedFirstChunk;
246 : int bStopParsing;
247 :
248 : #ifdef HAVE_SQLITE_VFS
249 : sqlite3_vfs* pMyVFS;
250 : #endif
251 :
252 : sqlite3 *hDB;
253 : sqlite3_stmt *hInsertNodeStmt;
254 : sqlite3_stmt *hInsertWayStmt;
255 : sqlite3_stmt *hSelectNodeBetweenStmt;
256 : sqlite3_stmt **pahSelectNodeStmt;
257 : sqlite3_stmt **pahSelectWayStmt;
258 : sqlite3_stmt *hInsertPolygonsStandaloneStmt;
259 : sqlite3_stmt *hDeletePolygonsStandaloneStmt;
260 : sqlite3_stmt *hSelectPolygonsStandaloneStmt;
261 : int bHasRowInPolygonsStandalone;
262 :
263 : int nMaxSizeForInMemoryDBInMB;
264 : int bInMemoryTmpDB;
265 : int bMustUnlink;
266 : CPLString osTmpDBName;
267 :
268 : int nNodesInTransaction;
269 :
270 : std::set<std::string> aoSetClosedWaysArePolygons;
271 :
272 : LonLat *pasLonLatCache;
273 :
274 : int bReportAllNodes;
275 : int bReportAllWays;
276 :
277 : int bFeatureAdded;
278 :
279 : int bInTransaction;
280 :
281 : int bIndexPoints;
282 : int bUsePointsIndex;
283 : int bIndexWays;
284 : int bUseWaysIndex;
285 :
286 : std::vector<int> abSavedDeclaredInterest;
287 : OGRLayer* poResultSetLayer;
288 : int bIndexPointsBackup;
289 : int bUsePointsIndexBackup;
290 : int bIndexWaysBackup;
291 : int bUseWaysIndexBackup;
292 :
293 : int bIsFeatureCountEnabled;
294 :
295 : int bAttributeNameLaundering;
296 :
297 : GByte *pabyWayBuffer;
298 :
299 : int nWaysProcessed;
300 : int nRelationsProcessed;
301 :
302 : int bCustomIndexing;
303 : int bCompressNodes;
304 :
305 : unsigned int nUnsortedReqIds;
306 : GIntBig *panUnsortedReqIds;
307 :
308 : unsigned int nReqIds;
309 : GIntBig *panReqIds;
310 : LonLat *pasLonLatArray;
311 :
312 : IndexedKVP *pasAccumulatedTags; /* points to content of pabyNonRedundantValues or aoMapIndexedKeys */
313 : int nAccumulatedTags;
314 : GByte *pabyNonRedundantValues;
315 : int nNonRedundantValuesLen;
316 : WayFeaturePair *pasWayFeaturePairs;
317 : int nWayFeaturePairs;
318 :
319 : int nNextKeyIndex;
320 : std::vector<KeyDesc*> asKeys;
321 : std::map<const char*, KeyDesc*, ConstCharComp> aoMapIndexedKeys; /* map that is the reverse of asKeys */
322 :
323 : CPLString osNodesFilename;
324 : int bInMemoryNodesFile;
325 : int bMustUnlinkNodesFile;
326 : GIntBig nNodesFileSize;
327 : VSILFILE *fpNodes;
328 :
329 : GIntBig nPrevNodeId;
330 : int nBucketOld;
331 : int nOffInBucketReducedOld;
332 : GByte *pabySector;
333 : Bucket *papsBuckets;
334 : int nBuckets;
335 :
336 : int bNeedsToSaveWayInfo;
337 :
338 : int CompressWay (unsigned int nTags, IndexedKVP* pasTags,
339 : int nPoints, LonLat* pasLonLatPairs,
340 : OSMInfo* psInfo,
341 : GByte* pabyCompressedWay);
342 : int UncompressWay( int nBytes, GByte* pabyCompressedWay,
343 : LonLat* pasCoords,
344 : unsigned int* pnTags, OSMTag* pasTags,
345 : OSMInfo* psInfo );
346 :
347 : int ParseConf();
348 : int CreateTempDB();
349 : int SetDBOptions();
350 : int SetCacheSize();
351 : int CreatePreparedStatements();
352 : void CloseDB();
353 :
354 : int IndexPoint(OSMNode* psNode);
355 : int IndexPointSQLite(OSMNode* psNode);
356 : int FlushCurrentSector();
357 : int FlushCurrentSectorCompressedCase();
358 : int FlushCurrentSectorNonCompressedCase();
359 : int IndexPointCustom(OSMNode* psNode);
360 :
361 : void IndexWay(GIntBig nWayID,
362 : unsigned int nTags, IndexedKVP* pasTags,
363 : LonLat* pasLonLatPairs, int nPairs,
364 : OSMInfo* psInfo);
365 :
366 : int StartTransaction();
367 : int CommitTransaction();
368 :
369 : int FindNode(GIntBig nID);
370 : void ProcessWaysBatch();
371 :
372 : void ProcessPolygonsStandalone();
373 :
374 : void LookupNodes();
375 : void LookupNodesSQLite();
376 : void LookupNodesCustom();
377 : void LookupNodesCustomCompressedCase();
378 : void LookupNodesCustomNonCompressedCase();
379 :
380 : unsigned int LookupWays( std::map< GIntBig, std::pair<int,void*> >& aoMapWays,
381 : OSMRelation* psRelation );
382 :
383 : OGRGeometry* BuildMultiPolygon(OSMRelation* psRelation,
384 : unsigned int* pnTags,
385 : OSMTag* pasTags);
386 : OGRGeometry* BuildGeometryCollection(OSMRelation* psRelation, int bMultiLineString);
387 :
388 : int TransferToDiskIfNecesserary();
389 :
390 : int AllocBucket(int iBucket);
391 : int AllocMoreBuckets(int nNewBucketIdx, int bAllocBucket = FALSE);
392 :
393 : public:
394 : OGROSMDataSource();
395 : ~OGROSMDataSource();
396 :
397 33 : virtual const char *GetName() { return pszName; }
398 594 : virtual int GetLayerCount() { return nLayers; }
399 : virtual OGRLayer *GetLayer( int );
400 :
401 : virtual int TestCapability( const char * );
402 :
403 : virtual OGRLayer * ExecuteSQL( const char *pszSQLCommand,
404 : OGRGeometry *poSpatialFilter,
405 : const char *pszDialect );
406 : virtual void ReleaseResultSet( OGRLayer * poLayer );
407 :
408 :
409 : int Open ( const char* pszFilename, int bUpdateIn );
410 :
411 : int ResetReading();
412 : int ParseNextChunk();
413 : OGRErr GetExtent( OGREnvelope *psExtent );
414 : int IsInterleavedReading();
415 :
416 : void NotifyNodes(unsigned int nNodes, OSMNode* pasNodes);
417 : void NotifyWay (OSMWay* psWay);
418 : void NotifyRelation (OSMRelation* psRelation);
419 : void NotifyBounds (double dfXMin, double dfYMin,
420 : double dfXMax, double dfYMax);
421 :
422 33 : OGROSMLayer* GetCurrentLayer() { return poCurrentLayer; }
423 51 : void SetCurrentLayer(OGROSMLayer* poLyr) { poCurrentLayer = poLyr; }
424 :
425 0 : int IsFeatureCountEnabled() const { return bIsFeatureCountEnabled; }
426 :
427 700 : int DoesAttributeNameLaundering() const { return bAttributeNameLaundering; }
428 : };
429 :
430 : /************************************************************************/
431 : /* OGROSMDriver */
432 : /************************************************************************/
433 :
434 : class OGROSMDriver : public OGRSFDriver
435 244 : {
436 : public:
437 : ~OGROSMDriver();
438 :
439 : virtual const char *GetName();
440 : virtual OGRDataSource *Open( const char *, int );
441 : virtual OGRDataSource *CreateDataSource( const char * pszName,
442 : char **papszOptions );
443 :
444 : virtual int TestCapability( const char * );
445 : };
446 :
447 : #endif /* ndef _OGR_OSM_H_INCLUDED */
448 :
|