1#ifndef LIBOSMSCOUT_AREAINDEXGENERATOR_H
2#define LIBOSMSCOUT_AREAINDEXGENERATOR_H
46 template <
typename Object>
72 std::string typeNamePlural;
74 std::string indexFile;
78 const std::string& typeNamePlural,
79 const std::string& dataFile,
80 const std::string& indexFile):
82 typeNamePlural(typeNamePlural),
88 const TypeInfo& typeInfo,
104 const ImportParameter& parameter,
106 const std::vector<TypeInfoRef>& types,
107 std::vector<TypeData>& typeData,
108 const MagnificationLevel& minLevelParam,
109 const MagnificationLevel& maxLevelParam,
111 MagnificationLevel& maxLevel)
const;
126 const TypeInfo& typeInfo,
127 const TypeData& typeData,
132 FileWriter &writer)
const = 0;
135 const ImportParameter& parameter,
137 const std::vector<TypeInfoRef> &types,
138 const MagnificationLevel &areaIndexMinMag,
139 const MagnificationLevel &areaIndexMaxMag,
143 template <
typename Object>
146 const TypeInfo& typeInfo,
151 std::array<char,10> buffer;
158 for (
const auto& cell : typeCellOffsets) {
164 for (
const auto& offset : cell.second) {
170 previousOffset=offset;
178 GeoBox boundingBox=typeData.
tileBox.GetCenter().GetBoundingBox(typeData.
indexLevel);
180 progress.
Info(
"Writing map for "+
194 writer.WriteFileOffset(bitmapOffset);
195 writer.Write(dataOffsetBytes);
197 writer.SetPos(bitmapOffset);
202 for (
size_t i=0; i<typeData.
tileBox.GetCount(); i++) {
203 writer.WriteFileOffset(0,
210 for (
const auto& cell : typeCellOffsets) {
212 ((cell.first.GetY()-typeData.
tileBox.GetMinY())*typeData.
tileBox.GetWidth()+
216 assert(bitmapCellOffset>=bitmapOffset);
220 writer.SetPos(bitmapCellOffset);
222 assert(cellOffset>bitmapCellOffset);
225 writer.WriteFileOffset(cellOffset-dataStartOffset+1,dataOffsetBytes);
227 writer.SetPos(cellOffset);
229 writer.WriteNumber((uint32_t)cell.second.size());
233 for (
const auto& offset : cell.second) {
234 assert(offset>previousOffset);
236 writer.WriteNumber((
FileOffset)(offset-previousOffset));
238 previousOffset=offset;
243 template <
typename Object>
245 const TypeInfo& typeInfo,
248 if (cellFillCount.empty()) {
252 size_t overallCount=0;
253 size_t maxCellCount=0;
255 for (
const auto& cell : cellFillCount) {
256 overallCount+=cell.second;
257 maxCellCount=std::max(maxCellCount,cell.second);
261 double average=double(overallCount)/double(cellFillCount.size());
264 size_t tooLowCount=0;
265 size_t tooHighCount=0;
266 size_t muchTooHighCount=0;
270 size_t tooLowValue=4*average/10;
271 size_t tooHighValue=64+32;
272 size_t muchTooHighValue=128+64;
274 for (
const auto& cell : cellFillCount) {
277 if (cell.second==0) {
280 else if (cell.second<tooLowValue) {
283 else if (cell.second>muchTooHighValue) {
286 else if (cell.second>tooHighValue) {
294 progress.
Info(typeInfo.GetName()+
" "+
295 std::to_string(emptyCount)+
" | "+
296 std::to_string(tooLowCount)+
" < "+
297 std::to_string(okCount)+
" < "+
298 std::to_string(tooHighCount)+
" *"+
299 std::to_string(muchTooHighCount)+
"* - "+
300 std::to_string(allCount));
302 if (
double(muchTooHighCount) /
double(allCount) >= 0.01) {
303 progress.
Warning(typeInfo.GetName() +
" has more than 1% cells with much too high entry count, will use smaller tile size");
307 if (
double(tooHighCount) /
double(allCount) >= 0.05) {
308 progress.
Warning(typeInfo.GetName() +
" has more than 5% cells with too high entry count, will use smaller tile size");
312 if (
double(tooLowCount) /
double(allCount) >= 0.2) {
313 progress.
Warning(typeInfo.GetName() +
" has more than 20% cells with <40% of average filling");
333 template <
typename Object>
335 const ImportParameter& parameter,
337 const std::vector<TypeInfoRef> &types,
338 const MagnificationLevel &areaIndexMinMag,
339 const MagnificationLevel &areaIndexMaxMag,
342 using namespace std::string_literals;
346 std::vector<TypeData> typeData;
347 MagnificationLevel maxLevel;
349 progress.
Info(
"Minimum magnification: "s + areaIndexMinMag);
355 progress.
SetAction(
"Scanning level distribution of "s + typeName +
" types"s);
371 auto indexEntries=std::count_if(types.begin(),
374 return typeData[type->GetIndex()].HasEntries();
381 progress.
SetAction(
"Generating '"s + indexFile +
"'"s);
384 writer.Open(
AppendFileToDir(parameter.GetDestinationDirectory(),indexFile));
386 writer.Write((uint32_t)indexEntries);
388 for (
const auto &type : types) {
389 size_t i=type->GetIndex();
391 if (typeData[i].HasEntries()) {
392 uint8_t dataOffsetBytes=0;
399 typeData[i].indexOffset=writer.GetPos();
401 writer.WriteFileOffset(bitmapOffset);
402 writer.Write(dataOffsetBytes);
403 writer.WriteNumber(typeData[i].indexLevel);
404 writer.WriteNumber(typeData[i].tileBox.GetMinX());
405 writer.WriteNumber(typeData[i].tileBox.GetMaxX());
406 writer.WriteNumber(typeData[i].tileBox.GetMinY());
407 writer.WriteNumber(typeData[i].tileBox.GetMaxY());
413 FileScanner::Sequential,
416 for (MagnificationLevel l=areaIndexMinMag; l <= maxLevel; l++) {
417 Magnification magnification(l);
418 TypeInfoSet indexTypes(*typeConfig);
422 for (
const auto &type : types) {
423 if (typeData[type->GetIndex()].HasEntries() &&
424 typeData[type->GetIndex()].indexLevel==l) {
425 indexTypes.Set(type);
429 if (indexTypes.Empty()) {
433 progress.
Info(
"Scanning "s + typeNamePlural +
" for index level "s + l);
435 std::vector<CoordOffsetsMap> typeCellOffsets(typeConfig->GetTypeCount());
437 uint32_t objectCount=scanner.ReadUInt32();
441 for (uint32_t w=1; w <= objectCount; w++) {
446 obj.Read(*typeConfig,
449 if (!indexTypes.IsSet(obj.GetType())) {
453 TileIdBox box(magnification, obj.GetBoundingBox());
455 for (
const auto& tileId : box) {
456 typeCellOffsets[obj.GetType()->GetIndex()][tileId].push_back(offset);
460 for (
const auto &type : indexTypes) {
461 size_t index=type->GetIndex();
465 *typeConfig->GetTypeInfo(index),
467 typeCellOffsets[index]);
477 scanner.CloseFailsafe();
478 writer.CloseFailsafe();
486 template <
typename Object>
497 if (cellFillCount.empty()) {
501 typeData.
tileBox=TileIdBox(cellFillCount.begin()->first,cellFillCount.begin()->first);
503 for (
const auto& cell : cellFillCount) {
510 template <
typename Object>
512 const ImportParameter& parameter,
514 const std::vector<TypeInfoRef>& types,
515 std::vector<TypeData>& typeData,
516 const MagnificationLevel& minLevelParam,
517 const MagnificationLevel& maxLevelParam,
519 MagnificationLevel& maxLevel)
const
522 TypeInfoSet remainingObjectTypes;
523 MagnificationLevel level=minLevelParam;
525 maxLevel=MagnificationLevel(0);
526 typeData.resize(typeConfig.GetTypeCount());
531 FileScanner::Sequential,
534 remainingObjectTypes.Set(types);
536 while (!remainingObjectTypes.Empty() &&
537 level <= maxLevelParam) {
538 Magnification magnification(level);
539 TypeInfoSet currentObjectTypes(remainingObjectTypes);
540 std::vector<CoordCountMap> cellFillCount(typeConfig.GetTypeCount());
542 progress.
Info(
"Scanning Level " + level +
" (" + std::to_string(remainingObjectTypes.Size()) +
" types remaining)");
546 uint32_t objectCount=scanner.ReadUInt32();
550 for (uint32_t objI=1; objI <= objectCount; objI++) {
557 if (!currentObjectTypes.IsSet(obj.GetType())) {
561 GeoBox boundingBox=obj.GetBoundingBox();
563 TileIdBox box(
TileId::GetTile(magnification,boundingBox.GetMinCoord()),
566 for (
const auto& tileId : box) {
567 cellFillCount[obj.GetType()->GetIndex()][tileId]++;
572 for (
const auto &type : currentObjectTypes) {
573 size_t typeIndex=type->GetIndex();
576 *typeConfig.GetTypeInfo(typeIndex),
577 cellFillCount[typeIndex])) {
578 if (level < maxLevelParam) {
579 currentObjectTypes.Remove(type);
582 progress.
Warning(typeConfig.GetTypeInfo(typeIndex)->GetName()+
" still does not fit good index criteria");
587 for (
const auto &type : currentObjectTypes) {
588 size_t typeIndex=type->GetIndex();
592 cellFillCount[typeIndex]);
594 maxLevel=std::max(maxLevel,level);
596 progress.
Info(
"Type " + type->GetName() +
", " +
597 std::to_string(typeData[type->GetIndex()].indexCells) +
" cells, " +
598 std::to_string(typeData[type->GetIndex()].indexEntries) +
" objects");
600 remainingObjectTypes.Remove(type);
std::map< TileId, size_t > CoordCountMap
Definition AreaIndexGenerator.h:50
AreaIndexGenerator(const std::string &typeName, const std::string &typeNamePlural, const std::string &dataFile, const std::string &indexFile)
Definition AreaIndexGenerator.h:77
void CalculateStatistics(const MagnificationLevel &level, TypeData &typeData, const CoordCountMap &cellFillCount) const
Definition AreaIndexGenerator.h:487
std::map< TileId, std::list< FileOffset > > CoordOffsetsMap
Definition AreaIndexGenerator.h:51
bool CalculateDistribution(const TypeConfig &typeConfig, const ImportParameter ¶meter, Progress &progress, const std::vector< TypeInfoRef > &types, std::vector< TypeData > &typeData, const MagnificationLevel &minLevelParam, const MagnificationLevel &maxLevelParam, bool useMmap, MagnificationLevel &maxLevel) const
Definition AreaIndexGenerator.h:511
virtual bool FitsIndexCriteria(Progress &progress, const TypeInfo &typeInfo, const CoordCountMap &cellFillCount) const
Definition AreaIndexGenerator.h:244
bool MakeAreaIndex(const TypeConfigRef &typeConfig, const ImportParameter ¶meter, Progress &progress, const std::vector< TypeInfoRef > &types, const MagnificationLevel &areaIndexMinMag, const MagnificationLevel &areaIndexMaxMag, bool useMmap)
Definition AreaIndexGenerator.h:334
virtual void WriteTypeId(const TypeConfigRef &typeConfig, const TypeInfoRef &type, FileWriter &writer) const =0
void WriteBitmap(Progress &progress, FileWriter &writer, const TypeInfo &typeInfo, const TypeData &typeData, const CoordOffsetsMap &typeCellOffsets)
Definition AreaIndexGenerator.h:144
Definition Exception.h:73
std::string GetDescription() const override
Definition ImportModule.h:101
virtual void Error(const std::string &text)
virtual void Info(const std::string &text)
virtual void Warning(const std::string &text)
virtual void SetAction(const std::string &action)
virtual void SetProgress(double current, double total, const std::string &label="")
static TileId GetTile(const Magnification &magnification, const GeoCoord &coord)
OSMSCOUT_API std::string AppendFileToDir(const std::string &dir, const std::string &file)
OSMSCOUT_API Distance GetEllipsoidalDistance(double aLon, double aLat, double bLon, double bLat)
uint8_t BytesNeededToEncodeNumber(N number)
Definition Number.h:318
OSMSCOUT_API std::string ByteSizeToString(FileOffset size, const Locale &locale=Locale::ByEnvironmentSafe())
uint64_t FileOffset
Definition OSMScoutTypes.h:46
unsigned int EncodeNumber(N number, char *buffer)
Definition Number.h:145
std::shared_ptr< TypeConfig > TypeConfigRef
Definition TypeConfig.h:1396
std::shared_ptr< TypeInfo > TypeInfoRef
Definition TypeConfig.h:61
Definition AreaIndexGenerator.h:54
MagnificationLevel indexLevel
Definition AreaIndexGenerator.h:55
TileIdBox tileBox
Number of entries over all cells.
Definition AreaIndexGenerator.h:59
FileOffset indexOffset
Definition AreaIndexGenerator.h:61
size_t indexCells
magnification level of index
Definition AreaIndexGenerator.h:56
size_t indexEntries
Number of filled cells in index.
Definition AreaIndexGenerator.h:57
bool HasEntries()
Position in file where the offset of the bitmap is written to.
Definition AreaIndexGenerator.h:63