1 #ifndef LIBOSMSCOUT_AREAINDEXGENERATOR_H 2 #define LIBOSMSCOUT_AREAINDEXGENERATOR_H 42 template <
typename Object>
83 const TypeInfo& typeInfo,
99 const ImportParameter& parameter,
101 const std::vector<TypeInfoRef>& types,
102 std::vector<TypeData>& typeData,
103 const MagnificationLevel& minLevelParam,
104 const MagnificationLevel& maxLevelParam,
106 MagnificationLevel& maxLevel)
const;
122 const TypeInfo& typeInfo,
123 const TypeData& typeData,
128 FileWriter &writer)
const = 0;
131 const ImportParameter& parameter,
133 const std::vector<TypeInfoRef> &types,
134 const MagnificationLevel &areaIndexMinMag,
135 const MagnificationLevel &areaIndexMaxLevel,
139 template <
typename Object>
142 const TypeInfo& typeInfo,
143 const TypeData& typeData,
146 size_t indexEntries=0;
148 std::array<char,10> buffer;
155 for (
const auto& cell : typeCellOffsets) {
156 indexEntries+=cell.second.size();
163 for (
const auto& offset : cell.second) {
169 previousOffset=offset;
176 progress.
Info(
"Writing map for "+
177 typeInfo.GetName()+
" , "+
178 ByteSizeToString(1.0*dataOffsetBytes*typeData.tileBox.GetCount()+dataSize));
182 bitmapOffset=writer.GetPos();
184 assert(typeData.indexOffset!=0);
186 writer.SetPos(typeData.indexOffset);
188 writer.WriteFileOffset(bitmapOffset);
189 writer.Write(dataOffsetBytes);
191 writer.SetPos(bitmapOffset);
196 for (
size_t i=0; i<typeData.tileBox.GetCount(); i++) {
197 writer.WriteFileOffset(0,
203 dataStartOffset=writer.GetPos();
206 for (
const auto& cell : typeCellOffsets) {
208 ((cell.first.GetY()-typeData.tileBox.GetMinY())*typeData.tileBox.GetWidth()+
209 cell.first.GetX()-typeData.tileBox.GetMinX())*(
FileOffset)dataOffsetBytes;
213 assert(bitmapCellOffset>=bitmapOffset);
215 cellOffset=writer.GetPos();
217 writer.SetPos(bitmapCellOffset);
219 assert(cellOffset>bitmapCellOffset);
222 writer.WriteFileOffset(cellOffset-dataStartOffset+1,dataOffsetBytes);
224 writer.SetPos(cellOffset);
226 writer.WriteNumber((uint32_t)cell.second.size());
230 for (
const auto& offset : cell.second) {
231 assert(offset>previousOffset);
233 writer.WriteNumber((
FileOffset)(offset-previousOffset));
235 previousOffset=offset;
242 template <
typename Object>
244 const TypeInfo& typeInfo,
247 if (cellFillCount.empty()) {
251 size_t overallCount=0;
252 size_t maxCellCount=0;
254 for (
const auto& cell : cellFillCount) {
255 overallCount+=cell.second;
256 maxCellCount=std::max(maxCellCount,cell.second);
260 double average=overallCount*1.0/cellFillCount.size();
264 size_t toHighCount=0;
268 for (
const auto& cell : cellFillCount) {
269 if (cell.second==0) {
272 else if (cell.second<0.4*average) {
275 else if (cell.second>128){
285 if (toHighCount*1.0/allCount>=0.05) {
289 if (toLowCount*1.0/allCount>=0.2) {
290 progress.
Warning(typeInfo.GetName()+
" has more than 20% cells with <40% of average filling ("+std::to_string(toLowCount)+
"/"+std::to_string(allCount)+
")");
310 template <
typename Object>
312 const ImportParameter& parameter,
314 const std::vector<TypeInfoRef> &types,
315 const MagnificationLevel &areaIndexMinMag,
316 const MagnificationLevel &areaIndexMaxLevel,
319 using namespace std::string_literals;
323 std::vector<TypeData> typeData;
324 MagnificationLevel maxLevel;
326 progress.
Info(
"Minimum magnification: "s + areaIndexMinMag);
332 progress.
SetAction(
"Scanning level distribution of "s + typeName +
" types"s);
334 if (!CalculateDistribution(*typeConfig,
348 auto indexEntries=std::count_if(types.begin(),
351 return typeData[type->GetIndex()].HasEntries();
358 progress.
SetAction(
"Generating '"s + indexFile +
"'"s);
361 writer.Open(
AppendFileToDir(parameter.GetDestinationDirectory(),indexFile));
363 writer.Write((uint32_t)indexEntries);
365 for (
const auto &type : types) {
366 size_t i=type->GetIndex();
368 if (typeData[i].HasEntries()) {
369 uint8_t dataOffsetBytes=0;
372 WriteTypeId(typeConfig,
376 typeData[i].indexOffset=writer.GetPos();
378 writer.WriteFileOffset(bitmapOffset);
379 writer.Write(dataOffsetBytes);
380 writer.WriteNumber(typeData[i].indexLevel);
381 writer.WriteNumber(typeData[i].tileBox.GetMinX());
382 writer.WriteNumber(typeData[i].tileBox.GetMaxX());
383 writer.WriteNumber(typeData[i].tileBox.GetMinY());
384 writer.WriteNumber(typeData[i].tileBox.GetMaxY());
390 FileScanner::Sequential,
393 for (MagnificationLevel l=areaIndexMinMag; l<=maxLevel; l++) {
394 Magnification magnification(l);
395 TypeInfoSet indexTypes(*typeConfig);
399 for (
const auto &type : types) {
400 if (typeData[type->GetIndex()].HasEntries() &&
401 typeData[type->GetIndex()].indexLevel==l) {
402 indexTypes.Set(type);
406 if (indexTypes.Empty()) {
410 progress.
Info(
"Scanning "s + typeNamePlural +
" for index level "s + l);
412 std::vector<CoordOffsetsMap> typeCellOffsets(typeConfig->GetTypeCount());
414 uint32_t objectCount=scanner.ReadUInt32();
418 for (uint32_t w=1; w <= objectCount; w++) {
423 offset=scanner.GetPos();
425 obj.Read(*typeConfig,
428 if (!indexTypes.IsSet(obj.GetType())) {
432 TileIdBox box(magnification, obj.GetBoundingBox());
434 for (
const auto& tileId : box) {
435 typeCellOffsets[obj.GetType()->GetIndex()][tileId].push_back(offset);
439 for (
const auto &type : indexTypes) {
440 size_t index=type->GetIndex();
442 if (!WriteBitmap(progress,
444 *typeConfig->GetTypeInfo(index),
446 typeCellOffsets[index])) {
458 scanner.CloseFailsafe();
459 writer.CloseFailsafe();
467 template <
typename Object>
473 typeData.indexLevel=level;
474 typeData.indexCells=cellFillCount.size();
475 typeData.indexEntries=0;
478 if (cellFillCount.empty()) {
482 typeData.tileBox=TileIdBox(cellFillCount.begin()->first,cellFillCount.begin()->first);
484 for (
const auto& cell : cellFillCount) {
485 typeData.indexEntries+=cell.second;
487 typeData.tileBox=typeData.tileBox.Include(cell.first);
491 template <
typename Object>
493 const ImportParameter& parameter,
495 const std::vector<TypeInfoRef>& types,
496 std::vector<TypeData>& typeData,
497 const MagnificationLevel& minLevelParam,
498 const MagnificationLevel& maxLevelParam,
500 MagnificationLevel& maxLevel)
const 503 TypeInfoSet remainingObjectTypes;
504 MagnificationLevel level=minLevelParam;
506 maxLevel=MagnificationLevel(0);
507 typeData.resize(typeConfig.GetTypeCount());
512 FileScanner::Sequential,
515 remainingObjectTypes.Set(types);
517 while (!remainingObjectTypes.Empty() &&
518 level <= maxLevelParam) {
519 Magnification magnification(level);
520 TypeInfoSet currentObjectTypes(remainingObjectTypes);
521 std::vector<CoordCountMap> cellFillCount(typeConfig.GetTypeCount());
523 progress.
Info(
"Scanning Level " + level +
" (" + std::to_string(remainingObjectTypes.Size()) +
" types remaining)");
527 uint32_t objectCount=scanner.ReadUInt32();
531 for (uint32_t objI=1; objI <= objectCount; objI++) {
538 if (!currentObjectTypes.IsSet(obj.GetType())) {
542 GeoBox boundingBox=obj.GetBoundingBox();
544 TileIdBox box(
TileId::GetTile(magnification,boundingBox.GetMinCoord()),
547 for (
const auto& tileId : box) {
548 cellFillCount[obj.GetType()->GetIndex()][tileId]++;
553 for (
const auto &type : currentObjectTypes) {
554 size_t typeIndex=type->GetIndex();
556 if (!FitsIndexCriteria(progress,
557 *typeConfig.GetTypeInfo(typeIndex),
558 cellFillCount[typeIndex])) {
559 if (level < maxLevelParam) {
560 currentObjectTypes.Remove(type);
563 progress.
Warning(typeConfig.GetTypeInfo(typeIndex)->GetName()+
" has too many index cells, that area filled over the limit");
568 for (
const auto &type : currentObjectTypes) {
569 size_t typeIndex=type->GetIndex();
571 CalculateStatistics(level,
573 cellFillCount[typeIndex]);
575 maxLevel=std::max(maxLevel,level);
577 progress.
Info(
"Type " + type->GetName() +
", " +
578 std::to_string(typeData[type->GetIndex()].indexCells) +
" cells, " +
579 std::to_string(typeData[type->GetIndex()].indexEntries) +
" objects");
581 remainingObjectTypes.Remove(type);
599 #endif //LIBOSMSCOUT_AREAINDEXGENERATOR_H std::string indexFile
Definition: AreaIndexGenerator.h:69
bool WriteBitmap(Progress &progress, FileWriter &writer, const TypeInfo &typeInfo, const TypeData &typeData, const CoordOffsetsMap &typeCellOffsets)
Definition: AreaIndexGenerator.h:140
virtual bool FitsIndexCriteria(Progress &progress, const TypeInfo &typeInfo, const CoordCountMap &cellFillCount) const
Definition: AreaIndexGenerator.h:243
bool HasEntries()
Position in file where the offset of the bitmap is written to.
Definition: AreaIndexGenerator.h:59
TileIdBox tileBox
Number of entries over all cells.
Definition: AreaIndexGenerator.h:55
OSMSCOUT_API std::string ByteSizeToString(FileOffset size)
std::string dataFile
Definition: AreaIndexGenerator.h:68
uint8_t BytesNeededToEncodeNumber(N number)
Definition: Number.h:318
Definition: Exception.h:72
std::map< TileId, std::list< FileOffset > > CoordOffsetsMap
Definition: AreaIndexGenerator.h:47
OSMSCOUT_API std::string AppendFileToDir(const std::string &dir, const std::string &file)
FileOffset indexOffset
Definition: AreaIndexGenerator.h:57
bool MakeAreaIndex(const TypeConfigRef &typeConfig, const ImportParameter ¶meter, Progress &progress, const std::vector< TypeInfoRef > &types, const MagnificationLevel &areaIndexMinMag, const MagnificationLevel &areaIndexMaxLevel, bool useMmap)
Definition: AreaIndexGenerator.h:311
MagnificationLevel indexLevel
Definition: AreaIndexGenerator.h:51
void CalculateStatistics(const MagnificationLevel &level, TypeData &typeData, const CoordCountMap &cellFillCount) const
Definition: AreaIndexGenerator.h:468
std::map< TileId, size_t > CoordCountMap
Definition: AreaIndexGenerator.h:46
virtual void Error(const std::string &text)
virtual void SetAction(const std::string &action)
std::string typeNamePlural
Definition: AreaIndexGenerator.h:67
Definition: AreaIndexGenerator.h:49
std::string GetDescription() const override
Definition: ImportModule.h:100
unsigned int EncodeNumber(N number, char *buffer)
Definition: Number.h:145
virtual void Info(const std::string &text)
AreaIndexGenerator(const std::string &typeName, const std::string &typeNamePlural, const std::string &dataFile, const std::string &indexFile)
Definition: AreaIndexGenerator.h:72
size_t indexEntries
Number of filled cells in index.
Definition: AreaIndexGenerator.h:53
virtual void SetProgress(double current, double total, const std::string &label="")
std::shared_ptr< TypeInfo > TypeInfoRef
Definition: TypeConfig.h:58
uint64_t FileOffset
Definition: OSMScoutTypes.h:47
size_t indexCells
magnification level of index
Definition: AreaIndexGenerator.h:52
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:492
Definition: Progress.h:34
static TileId GetTile(const Magnification &magnification, const GeoCoord &coord)
std::shared_ptr< TypeConfig > TypeConfigRef
Definition: TypeConfig.h:1227
virtual void WriteTypeId(const TypeConfigRef &typeConfig, const TypeInfoRef &type, FileWriter &writer) const =0
virtual void Warning(const std::string &text)
Definition: AreaIndexGenerator.h:43
std::string typeName
Definition: AreaIndexGenerator.h:66