| 1125 | |
| 1126 | template <class GridType, class GenericGridType> |
| 1127 | static void |
| 1128 | insertIntoHierarchy(PJ_CONTEXT *ctx, std::unique_ptr<GridType> &&grid, |
| 1129 | const std::string &gridName, const std::string &parentName, |
| 1130 | std::vector<std::unique_ptr<GenericGridType>> &topGrids, |
| 1131 | std::map<std::string, GridType *> &mapGrids) { |
| 1132 | const auto &extent = grid->extentAndRes(); |
| 1133 | |
| 1134 | // If we have one or both of grid_name and parent_grid_name, try to use |
| 1135 | // the names to recreate the hierarchy |
| 1136 | if (!gridName.empty()) { |
| 1137 | if (mapGrids.find(gridName) != mapGrids.end()) { |
| 1138 | pj_log(ctx, PJ_LOG_DEBUG, "Several grids called %s found!", |
| 1139 | gridName.c_str()); |
| 1140 | } |
| 1141 | mapGrids[gridName] = grid.get(); |
| 1142 | } |
| 1143 | |
| 1144 | if (!parentName.empty()) { |
| 1145 | auto iter = mapGrids.find(parentName); |
| 1146 | if (iter == mapGrids.end()) { |
| 1147 | pj_log(ctx, PJ_LOG_DEBUG, |
| 1148 | "Grid %s refers to non-existing parent %s. " |
| 1149 | "Using bounding-box method.", |
| 1150 | gridName.c_str(), parentName.c_str()); |
| 1151 | } else { |
| 1152 | if (iter->second->extentAndRes().contains(extent)) { |
| 1153 | iter->second->m_children.emplace_back(std::move(grid)); |
| 1154 | return; |
| 1155 | } else { |
| 1156 | pj_log(ctx, PJ_LOG_DEBUG, |
| 1157 | "Grid %s refers to parent %s, but its extent is " |
| 1158 | "not included in it. Using bounding-box method.", |
| 1159 | gridName.c_str(), parentName.c_str()); |
| 1160 | } |
| 1161 | } |
| 1162 | } else if (!gridName.empty()) { |
| 1163 | topGrids.emplace_back(std::move(grid)); |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | // Fallback to analyzing spatial extents |
| 1168 | for (const auto &candidateParent : topGrids) { |
| 1169 | const auto &candidateParentExtent = candidateParent->extentAndRes(); |
| 1170 | if (candidateParentExtent.contains(extent)) { |
| 1171 | static_cast<GridType *>(candidateParent.get()) |
| 1172 | ->insertGrid(ctx, std::move(grid)); |
| 1173 | return; |
| 1174 | } else if (candidateParentExtent.intersects(extent)) { |
| 1175 | pj_log(ctx, PJ_LOG_DEBUG, "Partially intersecting grids found!"); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | topGrids.emplace_back(std::move(grid)); |
| 1180 | } |
| 1181 | |
| 1182 | #ifdef TIFF_ENABLED |
| 1183 | // --------------------------------------------------------------------------- |
no test coverage detected