libosmscout 1.1.1
Loading...
Searching...
No Matches
GenLocationIndex.h
Go to the documentation of this file.
1#ifndef OSMSCOUT_IMPORT_GENLOCATIONINDEX_H
2#define OSMSCOUT_IMPORT_GENLOCATIONINDEX_H
3
4/*
5 This source is part of the libosmscout library
6 Copyright (C) 2009 Tim Teulings
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <map>
24#include <memory>
25#include <unordered_map>
26#include <unordered_set>
27
28#include <osmscout/Node.h>
29#include <osmscout/Area.h>
30#include <osmscout/Way.h>
31
32#include <osmscout/ObjectRef.h>
33
35
37
39
40namespace osmscout {
41
42 namespace locidx {
43
49 {
50 uint32_t minRegionChars=std::numeric_limits<uint32_t>::max();
51 uint32_t maxRegionChars=0;
52 uint32_t minRegionWords=std::numeric_limits<uint32_t>::max();
53 uint32_t maxRegionWords=0;
54 uint32_t maxPOIWords=0;
55 uint32_t minLocationChars=std::numeric_limits<uint32_t>::max();
56 uint32_t maxLocationChars=0;
57 uint32_t minLocationWords=std::numeric_limits<uint32_t>::max();
58 uint32_t maxLocationWords=0;
59 uint32_t maxAddressWords=0;
60
61 RegionMetrics() = default;
62 };
63
69 struct RegionAlias CLASS_FINAL
70 {
72 std::string name;
73 std::string altName;
74 };
75
79 struct RegionPOI CLASS_FINAL
80 {
81 std::string name;
82 ObjectFileRef object;
83
84 RegionPOI(const std::string& name,
85 const ObjectFileRef& object)
86 : name(name),
88 {
89 // no code
90 }
91
92 bool operator<(const RegionPOI& other) const
93 {
94 return object.GetFileOffset()<other.object.GetFileOffset();
95 }
96 };
97
101 struct RegionAddress CLASS_FINAL
102 {
103 std::string name;
104 ObjectFileRef object;
105
106 RegionAddress(const std::string& name,
107 const ObjectFileRef& object)
108 : name(name),
110 {
111 // no code
112 }
113
114 bool operator<(const RegionAddress& other) const
115 {
116 return object.GetFileOffset()<other.object.GetFileOffset();
117 }
118 };
119
124 struct RegionLocation CLASS_FINAL
125 {
126 std::unordered_map<std::string,
127 size_t> names;
129 std::list<ObjectFileRef> objects;
130 std::list<RegionAddress> addresses;
131
132 std::string GetName() const;
133 };
134
139 {
140 std::string name;
141 FileOffset dataOffsetOffset;
142 std::map<std::string,RegionLocation> locations;
143
144 explicit PostalArea(const std::string& name)
145 : name(name)
146 {
147 // no code
148 }
149
150 void AddLocationObject(const std::string& name,
151 const ObjectFileRef& objectRef);
152 };
153
154 class Region;
155
156 using RegionRef = std::shared_ptr<Region>;
157
174 class Region CLASS_FINAL
175 {
176 public:
177 using PostalAreaMap = std::unordered_map<std::string, PostalArea>;
178
179 private:
180 std::vector<GeoBox> boundingBoxes;
181 GeoBox boundingBox;
182 std::vector<GeoCoord> probePoints;
183
184 public:
187
188 ObjectFileRef reference;
189 std::string name;
190 std::string altName;
191 std::string isIn;
192 std::list<RegionAlias> aliases;
193 int8_t level{-1};
194
195 std::vector<std::vector<GeoCoord>> areas;
196 std::list<RegionPOI> pois;
198 PostalAreaMap::iterator defaultPostalArea;
199
200 std::list<RegionRef> regions;
201
202 public:
204
206 bool CouldContain(const GeoBox& boundingBox) const;
207 bool CouldContain(const Region& region, bool strict) const;
208
209 bool Contains(Region& child) const;
210
211 GeoBox GetBoundingBox() const
212 {
213 return boundingBox;
214 }
215
216 std::vector<GeoBox> GetAreaBoundingBoxes() const
217 {
218 return boundingBoxes;
219 }
220
221 void AddAlias(const RegionAlias& location,
222 const GeoCoord& node);
223
224 void AddPOINode(const FileOffset& fileOffset,
225 const std::string& name,
226 bool& added);
227
228 void AddPOIArea(const FileOffset& fileOffset,
229 const std::string& name,
230 const std::vector<Point>& nodes,
231 const GeoBox& boundingBox,
232 bool& added);
233
234 bool AddPOIWay(const FileOffset& fileOffset,
235 const std::string& name,
236 const std::vector<Point>& nodes,
237 const GeoBox& boundingBox,
238 bool& added);
239
240 void AddLocationObject(const std::string& name,
241 const std::string& postalCode,
242 const ObjectFileRef& objectRef);
243
244 bool AddLocationArea(const Area& area,
245 const std::vector<Point>& nodes,
246 const std::string& name,
247 const std::string& postalCode,
248 const GeoBox& boundingBox);
249
250 bool AddLocationWay(const Way& way,
251 const std::string& name,
252 const std::string& postalCode,
253 const GeoBox& boundingBox);
254
255 bool AddRegion(const RegionRef& region,
256 bool assume_contains=true);
257
258 private:
259 void CalculateProbePoints();
260 void CalculateProbePointsForArea(size_t areaIndex,
261 size_t refinement=0);
262 };
263
264 class RegionIndex CLASS_FINAL
265 {
266 private:
267 double cellWidth;
268 double cellHeight;
269 std::map<Pixel,std::list<RegionRef> > index;
270
271 public:
272 RegionIndex(double cellWidth,
273 double cellheight);
274
275 void IndexRegions(const std::vector<std::list<locidx::RegionRef> >& regionTree);
276
278 const GeoCoord& coord) const;
279 };
280
281 }
282
283 class LocationIndexGenerator CLASS_FINAL : public ImportModule
284 {
285 public:
286 static const char* const FILENAME_LOCATION_REGION_TXT;
287 static const char* const FILENAME_LOCATION_FULL_TXT;
288 static const char* const FILENAME_LOCATION_METRICS_TXT;
289
290 private:
291 uint8_t bytesForNodeFileOffset;
292 uint8_t bytesForAreaFileOffset;
293 uint8_t bytesForWayFileOffset;
294
295 ImportErrorReporterRef errorReporter;
296
297 private:
298 void Write(FileWriter& writer,
299 const ObjectFileRef& object) const;
300
301 void CalculateRegionMetrics(const locidx::Region& region,
302 locidx::RegionMetrics& metrics) const;
303
304 void DumpRegion(const locidx::Region& parent,
305 size_t indent,
306 std::ostream& out) const;
307
308 void DumpRegionAndData(const locidx::Region& parent,
309 size_t indent,
310 std::ostream& out) const;
311
312 bool DumpRegionTree(Progress& progress,
313 const locidx::Region& rootRegion,
314 const std::string& filename) const;
315
316 bool DumpLocationTree(Progress& progress,
317 const locidx::Region& rootRegion,
318 const std::string& filename) const;
319
320 bool DumpLocationMetrics(Progress& progress,
321 const std::string& filename,
322 const locidx::RegionMetrics& metrics,
323 const std::list<std::string>& regionIgnoreTokens,
324 const std::list<std::string>& poiIgnoreTokens,
325 const std::list<std::string>& locationIgnoreTokens) const;
326
327 bool GetBoundaryAreas(const ImportParameter& parameter,
328 Progress& progress,
329 const TypeConfigRef& typeConfig,
330 const TypeInfoSet& boundaryTypes,
331 std::vector<std::list<locidx::RegionRef>>& boundaryAreas) const;
332
333 void SortInBoundaries(Progress& progress,
334 locidx::Region& rootRegion,
335 const std::list<locidx::RegionRef>& boundaryAreas);
336
337 bool GetRegionAreas(const TypeConfig& typeConfig,
338 const ImportParameter& parameter,
339 Progress& progress,
340 std::list<locidx::RegionRef>& regionAreas) const;
341
342 bool SortInRegionAreas(Progress& progress,
343 locidx::Region& rootRegion,
344 std::list<locidx::RegionRef>& regionAreas);
345
346 void SortInRegion(const locidx::RegionRef& area,
347 std::vector<std::list<locidx::RegionRef> >& regionTree,
348 unsigned long level);
349
350 unsigned long GetRegionTreeDepth(const locidx::Region& rootRegion) const;
351
352 bool IndexRegionNodes(const TypeConfigRef& typeConfig,
353 const ImportParameter& parameter,
354 Progress& progress,
355 const locidx::RegionIndex& regionIndex,
356 locidx::RegionRef& rootRegion);
357
358 bool IndexLocationAreas(const TypeConfig& typeConfig,
359 const ImportParameter& parameter,
360 Progress& progress,
361 const locidx::RegionIndex& regionIndex,
362 locidx::RegionRef& rootRegion);
363
364 bool IndexLocationWays(const TypeConfig& typeConfig,
365 const ImportParameter& parameter,
366 Progress& progress,
367 const locidx::RegionIndex& regionIndex,
368 locidx::RegionRef& rootRegion);
369
370 void AddLocationAreaToRegion(locidx::RegionRef& rootRegion,
371 const Area& area,
372 const Area::Ring& ring,
373 const std::string& name,
374 const std::string& postalCode,
375 const locidx::RegionIndex& regionIndex);
376
377 void AddAddressToRegion(Progress& progress,
378 locidx::Region& region,
379 const ObjectFileRef& object,
380 const std::string& location,
381 const std::string& address,
382 const std::string &postalCode,
383 bool allowDuplicates,
384 bool& added);
385
386 void AddAddressAreaToRegion(Progress& progress,
387 locidx::Region& region,
388 const FileOffset& fileOffset,
389 const std::string& location,
390 const std::string& address,
391 const std::string &postalCode,
392 const std::vector<Point>& nodes,
393 const GeoBox& boundingBox,
394 bool& added);
395
396 bool IndexAddressAreas(const TypeConfig& typeConfig,
397 const ImportParameter& parameter,
398 Progress& progress,
399 locidx::RegionRef& rootRegion,
400 const locidx::RegionIndex& regionIndex);
401
402 bool AddAddressWayToRegion(Progress& progress,
403 locidx::Region& region,
404 const FileOffset& fileOffset,
405 const std::string& location,
406 const std::string& address,
407 const std::vector<Point>& nodes,
408 const GeoBox& boundingBox,
409 bool& added);
410
411 bool IndexAddressWays(const TypeConfig& typeConfig,
412 const ImportParameter& parameter,
413 Progress& progress,
414 const locidx::RegionRef& rootRegion,
415 const locidx::RegionIndex& regionIndex);
416
429 std::map<std::string,locidx::RegionLocation>::iterator FindLocation(Progress& progress,
430 const locidx::Region& region,
431 locidx::PostalArea& postalArea,
432 const std::string &locationName) const;
433
434 void AddAddressNodeToRegion(Progress& progress,
435 locidx::Region& region,
436 const FileOffset& fileOffset,
437 const std::string& location,
438 const std::string& address,
439 const std::string& postalCode,
440 bool& added);
441
442 bool IndexAddressNodes(const TypeConfig& typeConfig,
443 const ImportParameter& parameter,
444 Progress& progress,
445 const locidx::RegionRef& rootRegion,
446 const locidx::RegionIndex& regionIndex);
447
448 void CleanupPostalAreasAdresses(locidx::Region& region);
449 void CleanupPostalAreasLocations(locidx::Region& region);
450 void CleanupPostalAreas(locidx::Region& region);
451
452 void SortLocationTree(locidx::Region& region);
453
454 void ValidateIsIn(locidx::Region& region);
455
456 void WriteIgnoreTokens(FileWriter& writer,
457 const std::list<std::string>& regionIgnoreTokens,
458 const std::list<std::string>& poiIgnoreTokens,
459 const std::list<std::string>& locationIgnoreTokens) const;
460
461 void WriteRegionMetrics(FileWriter& writer,
462 const locidx::RegionMetrics& metrics) const;
463
464 void WriteRegionIndexEntry(FileWriter& writer,
465 const locidx::Region& parentRegion,
466 locidx::Region& region);
467
468 void WriteRegionIndex(FileWriter& writer,
469 locidx::Region& region);
470
471 void WriteRegionDataEntry(FileWriter& writer,
472 locidx::Region& region);
473
474 void WriteRegionData(FileWriter& writer,
475 locidx::Region& region);
476
477 void WritePostalArea(FileWriter& writer,
478 locidx::PostalArea& postalArea) const;
479
480 void WriteAddressDataEntry(FileWriter& writer,
481 const locidx::Region& region);
482
483 void WriteAddressData(FileWriter& writer,
484 const locidx::Region& region);
485
486 public:
487 void GetDescription(const ImportParameter& parameter,
488 ImportModuleDescription& description) const override;
489
490 bool Import(const TypeConfigRef& typeConfig,
491 const ImportParameter& parameter,
492 Progress& progress) override;
493 };
494}
495
496#endif
Definition Area.h:88
void GetDescription(const ImportParameter &parameter, ImportModuleDescription &description) const override
static const char *const FILENAME_LOCATION_REGION_TXT
Definition GenLocationIndex.h:286
std::vector< Point > nodes
List of nodes.
Definition Way.h:57
void Write(const TypeConfig &typeConfig, FileWriter &writer) const
static const char *const FILENAME_LOCATION_METRICS_TXT
Definition GenLocationIndex.h:288
bool Import(const TypeConfigRef &typeConfig, const ImportParameter &parameter, Progress &progress) override
@ out
Definition WaterIndexProcessor.h:270
static const char *const FILENAME_LOCATION_FULL_TXT
Definition GenLocationIndex.h:287
Definition ImportModule.h:101
Definition Location.h:39
uint64_t FileOffset
Definition OSMScoutTypes.h:46
std::shared_ptr< TypeConfig > TypeConfigRef
Definition TypeConfig.h:1396
Definition GenLocationIndex.h:42
std::shared_ptr< Region > RegionRef
Definition GenLocationIndex.h:156
Definition Area.h:39
std::shared_ptr< ImportErrorReporter > ImportErrorReporterRef
Definition ImportErrorReporter.h:137
Definition GenLocationIndex.h:49
uint32_t maxAddressWords
Definition GenLocationIndex.h:59
bool CouldContain(const Region &region, bool strict) const
bool AddPOIWay(const FileOffset &fileOffset, const std::string &name, const std::vector< Point > &nodes, const GeoBox &boundingBox, bool &added)
void AddPOINode(const FileOffset &fileOffset, const std::string &name, bool &added)
FileOffset reference
Reference to the node that is the alias.
Definition GenLocationIndex.h:71
bool AddRegion(const RegionRef &region, bool assume_contains=true)
bool AddLocationArea(const Area &area, const std::vector< Point > &nodes, const std::string &name, const std::string &postalCode, const GeoBox &boundingBox)
std::unordered_map< std::string, PostalArea > PostalAreaMap
Definition GenLocationIndex.h:177
ObjectFileRef object
Object.
Definition GenLocationIndex.h:82
std::vector< GeoBox > GetAreaBoundingBoxes() const
Definition GenLocationIndex.h:216
uint32_t minLocationChars
Definition GenLocationIndex.h:55
RegionAddress(const std::string &name, const ObjectFileRef &object)
Definition GenLocationIndex.h:106
void AddAlias(const RegionAlias &location, const GeoCoord &node)
FileOffset dataOffsetOffset
Offset of place where the address list offset is stored.
Definition GenLocationIndex.h:128
uint32_t minLocationWords
Definition GenLocationIndex.h:57
bool CouldContain(const GeoBox &boundingBox) const
std::list< RegionAlias > aliases
Location that are represented by this region.
Definition GenLocationIndex.h:192
bool operator<(const RegionAddress &other) const
Definition GenLocationIndex.h:114
FileOffset dataOffset
Offset into the index file.
Definition GenLocationIndex.h:186
bool Contains(Region &child) const
PostalArea(const std::string &name)
Definition GenLocationIndex.h:144
std::unordered_map< std::string, size_t > names
map of names in different case used for this location and their use count
Definition GenLocationIndex.h:127
bool operator<(const RegionPOI &other) const
Definition GenLocationIndex.h:92
void AddPOIArea(const FileOffset &fileOffset, const std::string &name, const std::vector< Point > &nodes, const GeoBox &boundingBox, bool &added)
uint32_t maxPOIWords
Definition GenLocationIndex.h:54
uint32_t maxRegionChars
Definition GenLocationIndex.h:51
uint32_t minRegionWords
Definition GenLocationIndex.h:52
RegionIndex(double cellWidth, double cellheight)
void IndexRegions(const std::vector< std::list< locidx::RegionRef > > &regionTree)
void AddLocationObject(const std::string &name, const ObjectFileRef &objectRef)
GeoBox GetBoundingBox() const
Checks whether child is within this.
Definition GenLocationIndex.h:211
std::map< std::string, RegionLocation > locations
list of indexed objects in this region
Definition GenLocationIndex.h:142
RegionPOI(const std::string &name, const ObjectFileRef &object)
Definition GenLocationIndex.h:84
std::string name
The alias itself.
Definition GenLocationIndex.h:72
void AddLocationObject(const std::string &name, const std::string &postalCode, const ObjectFileRef &objectRef)
std::list< RegionPOI > pois
A list of POIs in this region.
Definition GenLocationIndex.h:196
std::vector< std::vector< GeoCoord > > areas
the geometric area of this region
Definition GenLocationIndex.h:195
uint32_t minRegionChars
Definition GenLocationIndex.h:50
RegionRef GetRegionForNode(const RegionRef &rootRegion, const GeoCoord &coord) const
FileOffset indexOffset
Offset into the index file.
Definition GenLocationIndex.h:185
uint32_t maxLocationWords
Definition GenLocationIndex.h:58
uint32_t maxLocationChars
Definition GenLocationIndex.h:56
bool AddLocationWay(const Way &way, const std::string &name, const std::string &postalCode, const GeoBox &boundingBox)
int8_t level
Admin level or -1 if not set.
Definition GenLocationIndex.h:193
std::string GetName() const
std::list< RegionRef > regions
A list of sub regions.
Definition GenLocationIndex.h:200
std::list< ObjectFileRef > objects
Objects that represent this location.
Definition GenLocationIndex.h:129
PostalAreaMap postalAreas
Collection of objects without a postal code.
Definition GenLocationIndex.h:197
std::list< RegionAddress > addresses
Addresses at this location.
Definition GenLocationIndex.h:130
uint32_t maxRegionWords
Definition GenLocationIndex.h:53
std::string altName
Definition GenLocationIndex.h:73
std::string isIn
Name of the parent region as stated in OSM (is_in tag).
Definition GenLocationIndex.h:191
PostalAreaMap::iterator defaultPostalArea
PostalArea for postal code "".
Definition GenLocationIndex.h:198