OGRFeatureDefn * cvct2gdal::CVCT2GDALFeatureDefn ( VCTFeatureDefn * poVCTFeatDefn ) { OGRFeatureDefn * poOGRFeatDefn = new OGRFeatureDefn(); for ( auto iVCTFieldDefn = poVCTFeatDefn->fielddefnlist.begin(); iVCTFieldDefn != poVCTFeatDefn->fielddefnlist.end(); ++iVCTFieldDefn ) { OGRFieldDefn oOGRFieldDefn ( "", OFTInteger ); oOGRFieldDefn.SetName ( iVCTFieldDefn->name.c_str() ); oOGRFieldDefn.SetWidth ( iVCTFieldDefn->width ); oOGRFieldDefn.SetPrecision ( iVCTFieldDefn->precision ); oOGRFieldDefn.SetType ( CVCT2GDALFieldType ( iVCTFieldDefn->type ) ); poOGRFeatDefn->AddFieldDefn ( &oOGRFieldDefn ); } return poOGRFeatDefn; }
OGRErr OGRLayerWithTransaction::AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags ) { if( !m_poDecoratedLayer ) return OGRERR_FAILURE; OGRErr eErr = m_poDecoratedLayer->AlterFieldDefn(iField, poNewFieldDefn, nFlags); if( m_poFeatureDefn && eErr == OGRERR_NONE ) { OGRFieldDefn* poSrcFieldDefn = m_poDecoratedLayer->GetLayerDefn()->GetFieldDefn(iField); OGRFieldDefn* poDstFieldDefn = m_poFeatureDefn->GetFieldDefn(iField); poDstFieldDefn->SetName(poSrcFieldDefn->GetNameRef()); poDstFieldDefn->SetType(poSrcFieldDefn->GetType()); poDstFieldDefn->SetSubType(poSrcFieldDefn->GetSubType()); poDstFieldDefn->SetWidth(poSrcFieldDefn->GetWidth()); poDstFieldDefn->SetPrecision(poSrcFieldDefn->GetPrecision()); poDstFieldDefn->SetDefault(poSrcFieldDefn->GetDefault()); poDstFieldDefn->SetNullable(poSrcFieldDefn->IsNullable()); } return eErr; }
OGRErr OGRMemLayer::AlterFieldDefn( int iField, OGRFieldDefn* poNewFieldDefn, int nFlags ) { if (!bUpdatable) return OGRERR_FAILURE; if (iField < 0 || iField >= poFeatureDefn->GetFieldCount()) { CPLError( CE_Failure, CPLE_NotSupported, "Invalid field index"); return OGRERR_FAILURE; } OGRFieldDefn* poFieldDefn = poFeatureDefn->GetFieldDefn(iField); if ((nFlags & ALTER_TYPE_FLAG) && poFieldDefn->GetType() != poNewFieldDefn->GetType()) { if ((poNewFieldDefn->GetType() == OFTDate || poNewFieldDefn->GetType() == OFTTime || poNewFieldDefn->GetType() == OFTDateTime) && (poFieldDefn->GetType() == OFTDate || poFieldDefn->GetType() == OFTTime || poFieldDefn->GetType() == OFTDateTime)) { /* do nothing on features */ } else if (poNewFieldDefn->GetType() == OFTInteger64 && poFieldDefn->GetType() == OFTInteger) { /* -------------------------------------------------------------------- */ /* Update all the internal features. Hopefully there aren't any */ /* external features referring to our OGRFeatureDefn! */ /* -------------------------------------------------------------------- */ for( GIntBig i = 0; i < nMaxFeatureCount; i++ ) { if( papoFeatures[i] == NULL ) continue; OGRField* poFieldRaw = papoFeatures[i]->GetRawFieldRef(iField); if( papoFeatures[i]->IsFieldSet(iField) ) { poFieldRaw->Integer64 = poFieldRaw->Integer; } } } else if (poNewFieldDefn->GetType() == OFTReal && poFieldDefn->GetType() == OFTInteger) { /* -------------------------------------------------------------------- */ /* Update all the internal features. Hopefully there aren't any */ /* external features referring to our OGRFeatureDefn! */ /* -------------------------------------------------------------------- */ for( GIntBig i = 0; i < nMaxFeatureCount; i++ ) { if( papoFeatures[i] == NULL ) continue; OGRField* poFieldRaw = papoFeatures[i]->GetRawFieldRef(iField); if( papoFeatures[i]->IsFieldSet(iField) ) { poFieldRaw->Real = poFieldRaw->Integer; } } } else if (poNewFieldDefn->GetType() == OFTReal && poFieldDefn->GetType() == OFTInteger64) { /* -------------------------------------------------------------------- */ /* Update all the internal features. Hopefully there aren't any */ /* external features referring to our OGRFeatureDefn! */ /* -------------------------------------------------------------------- */ for( GIntBig i = 0; i < nMaxFeatureCount; i++ ) { if( papoFeatures[i] == NULL ) continue; OGRField* poFieldRaw = papoFeatures[i]->GetRawFieldRef(iField); if( papoFeatures[i]->IsFieldSet(iField) ) { poFieldRaw->Real = (double) poFieldRaw->Integer64; } } } else { if (poNewFieldDefn->GetType() != OFTString) { CPLError( CE_Failure, CPLE_NotSupported, "Can only convert from OFTInteger to OFTReal, or from anything to OFTString"); return OGRERR_FAILURE; } /* -------------------------------------------------------------------- */ /* Update all the internal features. Hopefully there aren't any */ /* external features referring to our OGRFeatureDefn! */ /* -------------------------------------------------------------------- */ for( GIntBig i = 0; i < nMaxFeatureCount; i++ ) { if( papoFeatures[i] == NULL ) continue; OGRField* poFieldRaw = papoFeatures[i]->GetRawFieldRef(iField); if( papoFeatures[i]->IsFieldSet(iField) ) { char* pszVal = CPLStrdup(papoFeatures[i]->GetFieldAsString(iField)); /* Little trick to unallocate the field */ OGRField sField; sField.Set.nMarker1 = OGRUnsetMarker; sField.Set.nMarker2 = OGRUnsetMarker; papoFeatures[i]->SetField(iField, &sField); poFieldRaw->String = pszVal; } } } poFieldDefn->SetType(poNewFieldDefn->GetType()); } if (nFlags & ALTER_NAME_FLAG) poFieldDefn->SetName(poNewFieldDefn->GetNameRef()); if (nFlags & ALTER_WIDTH_PRECISION_FLAG) { poFieldDefn->SetWidth(poNewFieldDefn->GetWidth()); poFieldDefn->SetPrecision(poNewFieldDefn->GetPrecision()); } return OGRERR_NONE; }