| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Project: LV BAG Translator |
| 4 | * Purpose: Implements OGRLVBAGDataSource. |
| 5 | * Author: Laixer B.V., info at laixer dot com |
| 6 | * |
| 7 | ****************************************************************************** |
| 8 | * Copyright (c) 2020, Laixer B.V. <info at laixer dot com> |
| 9 | * |
| 10 | * SPDX-License-Identifier: MIT |
| 11 | ****************************************************************************/ |
| 12 | |
| 13 | #include "ogr_lvbag.h"cxx . . $ `<file>/ogr/ogrsf_frmts/lvbag/ogr_lvbag.h`/ |
| 14 | #include "ogrsf_frmts.h"cxx . . $ `<file>/ogr/ogrsf_frmts/ogrsf_frmts.h`/ |
| 15 | #include "ogrunionlayer.h"cxx . . $ `<file>/ogr/ogrsf_frmts/generic/ogrunionlayer.h`/ |
| 16 | #include "ogrlayerpool.h"cxx . . $ `<file>/ogr/ogrsf_frmts/generic/ogrlayerpool.h`/ |
| 17 | |
| 18 | #include <algorithm> |
| 19 | |
| 20 | /************************************************************************/ |
| 21 | /* OGRLVBAGDataSource() */ |
| 22 | /************************************************************************/ |
| 23 | |
| 24 | OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource#::OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource#OGRLVBAGDataSource(49f6e7a06ebc5aa8).() |
| 25 | : poPoolcxx . . $ OGRLVBAGDataSource#poPool.{new OGRLayerPoolcxx . . $ OGRLayerPool#OGRLayerPool(d4f767463ce0a6b3).{}}, papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers.{OGRLVBAGcxx . . $ OGRLVBAG/::LayerVectorcxx . . $ OGRLVBAG/LayerVector#{}} |
| 26 | { |
| 27 | const int nMaxSimultaneouslyOpened = |
| 28 | stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a).(atoicxx . . $ atoi(f5ecf3191ed0cd93).(CPLGetConfigOption("OGR_LVBAG_MAX_OPENED", "100")), 1); |
| 29 | if (poPoolcxx . . $ OGRLVBAGDataSource#poPool.->GetMaxSimultaneouslyOpenedcxx . . $ OGRLayerPool#GetMaxSimultaneouslyOpened(455f465bc33b4cdf).() != nMaxSimultaneouslyOpened) |
| 30 | { |
| 31 | poPoolcxx . . $ OGRLVBAGDataSource#poPool..reset(new OGRLayerPoolcxx . . $ OGRLayerPool#OGRLayerPool(d4f767463ce0a6b3).(nMaxSimultaneouslyOpened)); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /************************************************************************/ |
| 36 | /* Open() */ |
| 37 | /************************************************************************/ |
| 38 | |
| 39 | int OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource#::Opencxx . . $ OGRLVBAGDataSource#Open(c4dada3283861623).(const char *pszFilename, char **papszOpenOptionsIn) |
| 40 | { |
| 41 | auto poLayer = stdcxx . . $ std/::unique_ptr<OGRLVBAGLayercxx . . $ OGRLVBAGLayer#>{ |
| 42 | new OGRLVBAGLayercxx . . $ OGRLVBAGLayer#{pszFilename, poPoolcxx . . $ OGRLVBAGDataSource#poPool..get(), papszOpenOptionsIn}}; |
| 43 | if (poLayer && !poLayer->TouchLayercxx . . $ OGRLVBAGLayer#TouchLayer(c9857a73d68e59ba).()) |
| 44 | { |
| 45 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 46 | } |
| 47 | |
| 48 | papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..push_back({OGRLVBAGcxx . . $ OGRLVBAG/::LayerTypecxx . . $ OGRLVBAG/LayerType#::LYR_RAWcxx . . $ OGRLVBAG/LYR_RAW., stdcxx . . $ std/::move(poLayer)}); |
| 49 | |
| 50 | if ((static_cast<int>(papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..size()) + 1) % |
| 51 | poPoolcxx . . $ OGRLVBAGDataSource#poPool.->GetMaxSimultaneouslyOpenedcxx . . $ OGRLayerPool#GetMaxSimultaneouslyOpened(455f465bc33b4cdf).() == |
| 52 | 0 && |
| 53 | poPoolcxx . . $ OGRLVBAGDataSource#poPool.->GetSizecxx . . $ OGRLayerPool#GetSize(455f465bc33b4cdf).() > 0) |
| 54 | { |
| 55 | TryCoalesceLayerscxx . . $ OGRLVBAGDataSource#TryCoalesceLayers(49f6e7a06ebc5aa8).(); |
| 56 | } |
| 57 | |
| 58 | return TRUEcxx . . $ `port/cpl_port.h:1184:9`!; |
| 59 | } |
| 60 | |
| 61 | /************************************************************************/ |
| 62 | /* TryCoalesceLayers() */ |
| 63 | /************************************************************************/ |
| 64 | |
| 65 | void OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource#::TryCoalesceLayerscxx . . $ OGRLVBAGDataSource#TryCoalesceLayers(49f6e7a06ebc5aa8).() |
| 66 | { |
| 67 | stdcxx . . $ std/::vector<int> paGroup = {}; |
| 68 | stdcxx . . $ std/::mapcxx . . $ std/map#<int, stdcxx . . $ std/::vector<int>> paMergeVector = {cxx . . $ std/map#map(49f6e7a06ebc5aa8).}; |
| 69 | |
| 70 | // FUTURE: This can be optimized |
| 71 | // Find similar layers by doing a triangular matrix |
| 72 | // comparison across all layers currently enlisted. |
| 73 | for (size_t i = 0; i < papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..size(); ++i) |
| 74 | { |
| 75 | stdcxx . . $ std/::vector<int> paVector = {}; |
| 76 | for (size_t j = 0; j < papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..size(); ++j) |
| 77 | { |
| 78 | if (stdcxx . . $ std/::findcxx . . $ std/find(a92da12e15934614).(paGroup.cbegin(), paGroup.cend(), |
| 79 | static_cast<int>(j)) != paGroup.cend()) |
| 80 | { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | OGRLayercxx . . $ OGRLayer# *poLayerLHS = papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers.[i].second.get(); |
| 85 | OGRLayercxx . . $ OGRLayer# *poLayerRHS = papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers.[j].second.get(); |
| 86 | |
| 87 | if (j > i && EQUALcxx . . $ `port/cpl_port.h:532:9`!(poLayerLHS->GetName(), poLayerRHS->GetName())) |
| 88 | { |
| 89 | if (poLayerLHS->GetGeomTypecxx . . $ OGRLayer#GetGeomType(5a40c54c5ff210ce).() == poLayerRHS->GetGeomTypecxx . . $ OGRLayer#GetGeomType(5a40c54c5ff210ce).() && |
| 90 | poLayerLHS->GetLayerDefncxx . . $ OGRLayer#GetLayerDefn(407fccde18759965).()->IsSamecxx . . $ OGRFeatureDefn#IsSame(97b2217ba76fcfcf).( |
| 91 | poLayerRHS->GetLayerDefncxx . . $ OGRLayer#GetLayerDefn(407fccde18759965).())) |
| 92 | { |
| 93 | paVector.push_back(static_cast<int>(j)); |
| 94 | paGroup.push_back(static_cast<int>(j)); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | if (!paVector.empty()) |
| 99 | { |
| 100 | paMergeVector.insertcxx . . $ std/map#insert(274b9441a76f0de8).({static_cast<int>(i), paVector}); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (paMergeVector.emptycxx . . $ std/map#empty(3482b152b9333168).()) |
| 105 | { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | for (const auto &mergeLayer : paMergeVector) |
| 110 | { |
| 111 | const int baseLayerIdx = mergeLayer.first; |
| 112 | const stdcxx . . $ std/::vector<int> papoLayersIdx = mergeLayer.second; |
| 113 | |
| 114 | int nSrcLayers = static_cast<int>(papoLayersIdx.size()) + 1; |
| 115 | OGRLayercxx . . $ OGRLayer# **papoSrcLayers = static_cast<OGRLayercxx . . $ OGRLayer# **>( |
| 116 | CPLRealloc(nullptr, sizeof(OGRLayercxx . . $ OGRLayer# *) * nSrcLayers)); |
| 117 | |
| 118 | CPLAssertcxx . . $ `port/cpl_error.h:353:9`!(papoLayers[baseLayerIdx].second); |
| 119 | |
| 120 | int idx = 0; |
| 121 | papoSrcLayers[idx++] = papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers.[baseLayerIdx].second.release(); |
| 122 | for (const auto &poLayerIdx : papoLayersIdx) |
| 123 | { |
| 124 | papoSrcLayers[idx++] = papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers.[poLayerIdx].second.release(); |
| 125 | } |
| 126 | |
| 127 | OGRLayercxx . . $ OGRLayer# *poBaseLayer = papoSrcLayers[0]; |
| 128 | |
| 129 | auto poLayer = stdcxx . . $ std/::make_unique<OGRUnionLayercxx . . $ OGRUnionLayer#>( |
| 130 | poBaseLayer->GetNamecxx . . $ OGRLayer#GetName(a9f41ea0e82d88cf).(), nSrcLayers, papoSrcLayers, TRUEcxx . . $ `port/cpl_port.h:1184:9`!); |
| 131 | |
| 132 | OGRFeatureDefncxx . . $ OGRFeatureDefn# *poBaseLayerDefn = poBaseLayer->GetLayerDefncxx . . $ OGRLayer#GetLayerDefn(407fccde18759965).(); |
| 133 | |
| 134 | poLayer->SetFieldscxx . . $ OGRUnionLayer#SetFields(39dbfb4f4cda589).(FIELD_FROM_FIRST_LAYERcxx . . $ FIELD_FROM_FIRST_LAYER., poBaseLayerDefn); |
| 135 | |
| 136 | papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..push_back({OGRLVBAGcxx . . $ OGRLVBAG/::LayerTypecxx . . $ OGRLVBAG/LayerType#::LYR_RAWcxx . . $ OGRLVBAG/LYR_RAW., |
| 137 | OGRLayerUniquePtrcxx . . $ OGRLayerUniquePtr#{poLayer.release()}}); |
| 138 | } |
| 139 | |
| 140 | // Erase all released pointers |
| 141 | auto it = papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..begin(); |
| 142 | while (it != papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..end()) |
| 143 | { |
| 144 | if (!it->second) |
| 145 | { |
| 146 | it = papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..erase(it); |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | ++it; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /************************************************************************/ |
| 156 | /* GetLayer() */ |
| 157 | /************************************************************************/ |
| 158 | |
| 159 | const OGRLayercxx . . $ OGRLayer# *OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource#::GetLayercxx . . $ OGRLVBAGDataSource#GetLayer(3ead67c79a3185c4).(int iLayer) const |
| 160 | { |
| 161 | if (iLayer < 0 || iLayer >= GetLayerCountcxx . . $ OGRLVBAGDataSource#GetLayerCount(455f465bc33b4cdf).()) |
| 162 | { |
| 163 | return nullptr; |
| 164 | } |
| 165 | return papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers.[iLayer].second.get(); |
| 166 | } |
| 167 | |
| 168 | /************************************************************************/ |
| 169 | /* GetLayerCount() */ |
| 170 | /************************************************************************/ |
| 171 | |
| 172 | int OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource#::GetLayerCountcxx . . $ OGRLVBAGDataSource#GetLayerCount(455f465bc33b4cdf).() const |
| 173 | { |
| 174 | const_cast<OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource# *>(this)->TryCoalesceLayerscxx . . $ OGRLVBAGDataSource#TryCoalesceLayers(49f6e7a06ebc5aa8).(); |
| 175 | return static_cast<int>(papoLayerscxx . . $ OGRLVBAGDataSource#papoLayers..size()); |
| 176 | } |
| 177 | |
| 178 | /************************************************************************/ |
| 179 | /* TestCapability() */ |
| 180 | /************************************************************************/ |
| 181 | |
| 182 | int OGRLVBAGDataSourcecxx . . $ OGRLVBAGDataSource#::TestCapabilitycxx . . $ OGRLVBAGDataSource#TestCapability(78f035161ffa9fe2).(const char * /* pszCap */) const |
| 183 | { |
| 184 | return FALSEcxx . . $ `port/cpl_port.h:1180:9`!; |
| 185 | } |