Undocumented */
| 123 | |
| 124 | /** Undocumented */ |
| 125 | OGRErr OGRGNMWrappedResultLayer::InsertFeature(OGRFeature *poFeature, |
| 126 | const CPLString &soLayerName, |
| 127 | int nPathNo, bool bIsEdge) |
| 128 | { |
| 129 | VALIDATE_POINTER1(poFeature, "Input feature is invalid", |
| 130 | OGRERR_INVALID_HANDLE); |
| 131 | // add fields from input feature |
| 132 | const OGRFeatureDefn *poSrcDefn = poFeature->GetDefnRef(); |
| 133 | const OGRFeatureDefn *poDstFDefn = GetLayerDefn(); |
| 134 | if (nullptr == poSrcDefn || nullptr == poDstFDefn) |
| 135 | return OGRERR_INVALID_HANDLE; |
| 136 | |
| 137 | const int nSrcFieldCount = poSrcDefn->GetFieldCount(); |
| 138 | int nDstFieldCount = poDstFDefn->GetFieldCount(); |
| 139 | |
| 140 | // Initialize the index-to-index map to -1's |
| 141 | std::vector<int> anMap(nSrcFieldCount, -1); |
| 142 | |
| 143 | for (int iField = 0; iField < nSrcFieldCount; iField++) |
| 144 | { |
| 145 | const OGRFieldDefn *poSrcFieldDefn = poSrcDefn->GetFieldDefn(iField); |
| 146 | OGRFieldDefn oFieldDefn(poSrcFieldDefn); |
| 147 | |
| 148 | /* The field may have been already created at layer creation */ |
| 149 | const int iDstField = |
| 150 | poDstFDefn->GetFieldIndex(oFieldDefn.GetNameRef()); |
| 151 | if (iDstField >= 0) |
| 152 | { |
| 153 | // TODO: by now skip fields with different types. In future should |
| 154 | // cast types |
| 155 | const OGRFieldDefn *poDstField = |
| 156 | poDstFDefn->GetFieldDefn(iDstField); |
| 157 | if (nullptr != poDstField && |
| 158 | oFieldDefn.GetType() == poDstField->GetType()) |
| 159 | anMap[iField] = iDstField; |
| 160 | } |
| 161 | else if (CreateField(&oFieldDefn) == OGRERR_NONE) |
| 162 | { |
| 163 | /* Sanity check : if it fails, the driver is buggy */ |
| 164 | if (poDstFDefn->GetFieldCount() != nDstFieldCount + 1) |
| 165 | { |
| 166 | CPLError(CE_Warning, CPLE_AppDefined, |
| 167 | "The output driver has claimed to have added the %s " |
| 168 | "field, but it did not!", |
| 169 | oFieldDefn.GetNameRef()); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | anMap[iField] = nDstFieldCount; |
| 174 | nDstFieldCount++; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | auto poInsertFeature = std::make_unique<OGRFeature>(GetLayerDefn()); |
| 180 | if (poInsertFeature->SetFrom(poFeature, anMap.data(), TRUE) != OGRERR_NONE) |
| 181 | { |
| 182 | CPLError(CE_Failure, CPLE_AppDefined, |
no test coverage detected