int DDFField::GetRepeatCount() { if( !poDefn->IsRepeating() ) return 1; /* -------------------------------------------------------------------- */ /* The occurance count depends on how many copies of this */ /* field's list of subfields can fit into the data space. */ /* -------------------------------------------------------------------- */ if( poDefn->GetFixedWidth() ) { return nDataSize / poDefn->GetFixedWidth(); } /* -------------------------------------------------------------------- */ /* Note that it may be legal to have repeating variable width */ /* subfields, but I don't have any samples, so I ignore it for */ /* now. */ /* */ /* The file data/cape_royal_AZ_DEM/1183XREF.DDF has a repeating */ /* variable length field, but the count is one, so it isn't */ /* much value for testing. */ /* -------------------------------------------------------------------- */ int iOffset = 0, iRepeatCount = 1; while( TRUE ) { for( int iSF = 0; iSF < poDefn->GetSubfieldCount(); iSF++ ) { int nBytesConsumed; DDFSubfieldDefn * poThisSFDefn = poDefn->GetSubfield( iSF ); if( poThisSFDefn->GetWidth() > nDataSize - iOffset ) nBytesConsumed = poThisSFDefn->GetWidth(); else poThisSFDefn->GetDataLength( pachData+iOffset, nDataSize - iOffset, &nBytesConsumed); iOffset += nBytesConsumed; if( iOffset > nDataSize ) return iRepeatCount - 1; } if( iOffset > nDataSize - 2 ) return iRepeatCount; iRepeatCount++; } }
void SDTSFeature::ApplyATID( DDFField * poField ) { DDFSubfieldDefn *poMODN = poField->GetFieldDefn()->FindSubfieldDefn( "MODN" ); if( poMODN == nullptr ) { // CPLAssert( false ); return; } bool bUsualFormat = poMODN->GetWidth() == 4; const int nRepeatCount = poField->GetRepeatCount(); for( int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++ ) { paoATID = reinterpret_cast<SDTSModId *>( CPLRealloc( paoATID, sizeof(SDTSModId)*(nAttributes+1) ) ); SDTSModId *poModId = paoATID + nAttributes; *poModId = SDTSModId(); if( bUsualFormat ) { const char * pabyData = poField->GetSubfieldData( poMODN, nullptr, iRepeat ); if( pabyData == nullptr || strlen(pabyData) < 5 ) return; memcpy( poModId->szModule, pabyData, 4 ); poModId->szModule[4] = '\0'; poModId->nRecord = atoi(pabyData + 4); poModId->szOBRP[0] = '\0'; } else { poModId->Set( poField ); } nAttributes++; } }
void SDTSFeature::ApplyATID( DDFField * poField ) { int nRepeatCount = poField->GetRepeatCount(); int bUsualFormat; DDFSubfieldDefn *poMODN; poMODN = poField->GetFieldDefn()->FindSubfieldDefn( "MODN" ); if( poMODN == NULL ) { CPLAssert( FALSE ); return; } bUsualFormat = poMODN->GetWidth() == 4; for( int iRepeat = 0; iRepeat < nRepeatCount; iRepeat++ ) { paoATID = (SDTSModId *) CPLRealloc(paoATID, sizeof(SDTSModId)*(nAttributes+1)); const char * pabyData; SDTSModId *poModId = paoATID + nAttributes; if( bUsualFormat ) { pabyData = poField->GetSubfieldData( poMODN, NULL, iRepeat ); memcpy( poModId->szModule, pabyData, 4 ); poModId->szModule[4] = '\0'; poModId->nRecord = atoi(pabyData + 4); poModId->szOBRP[0] = '\0'; } else { poModId->Set( poField ); } nAttributes++; } }
static void AddPrimaryAttrToDBFSchema( DBFHandle hDBF, SDTSTransfer *poTransfer, char ** papszModuleList ) { for( int iModule = 0; papszModuleList != NULL && papszModuleList[iModule] != NULL; iModule++ ) { SDTSAttrReader *poAttrReader; /* -------------------------------------------------------------------- */ /* Get a reader on the desired module. */ /* -------------------------------------------------------------------- */ poAttrReader = (SDTSAttrReader *) poTransfer->GetLayerIndexedReader( poTransfer->FindLayer( papszModuleList[iModule] ) ); if( poAttrReader == NULL ) { printf( "Unable to open attribute module %s, skipping.\n" , papszModuleList[iModule] ); continue; } poAttrReader->Rewind(); /* -------------------------------------------------------------------- */ /* Read the first record so we can clone schema information off */ /* of it. */ /* -------------------------------------------------------------------- */ SDTSAttrRecord *poAttrFeature; poAttrFeature = (SDTSAttrRecord *) poAttrReader->GetNextFeature(); if( poAttrFeature == NULL ) { fprintf( stderr, "Didn't find any meaningful attribute records in %s.\n", papszModuleList[iModule] ); continue; } /* -------------------------------------------------------------------- */ /* Clone schema off the first record. Eventually we need to */ /* get the information out of the DDR record, but it isn't */ /* clear to me how to accomplish that with the SDTS++ API. */ /* */ /* The following approach may fail (dramatically) if some */ /* records do not include all subfields. Furthermore, no */ /* effort is made to make DBF field names unique. The SDTS */ /* attributes often have names much beyond the 14 character dbf */ /* limit which may result in non-unique attributes. */ /* -------------------------------------------------------------------- */ DDFFieldDefn *poFDefn = poAttrFeature->poATTR->GetFieldDefn(); int iSF; DDFField *poSR = poAttrFeature->poATTR; for( iSF=0; iSF < poFDefn->GetSubfieldCount(); iSF++ ) { DDFSubfieldDefn *poSFDefn = poFDefn->GetSubfield( iSF ); int nWidth = poSFDefn->GetWidth(); switch( poSFDefn->GetType() ) { case DDFString: if( nWidth == 0 ) { int nMaxBytes; const char * pachData = poSR->GetSubfieldData(poSFDefn, &nMaxBytes); nWidth = strlen(poSFDefn->ExtractStringData(pachData, nMaxBytes, NULL )); } DBFAddField( hDBF, poSFDefn->GetName(), FTString, nWidth, 0 ); break; case DDFInt: if( nWidth == 0 ) nWidth = 9; DBFAddField( hDBF, poSFDefn->GetName(), FTInteger, nWidth, 0 ); break; case DDFFloat: DBFAddField( hDBF, poSFDefn->GetName(), FTDouble, 18, 6 ); break; default: fprintf( stderr, "Dropping attribute `%s' of module `%s'. " "Type unsupported\n", poSFDefn->GetName(), papszModuleList[iModule] ); break; } } if( !poAttrReader->IsIndexed() ) delete poAttrFeature; } /* next module */ }
OGRSDTSLayer::OGRSDTSLayer( SDTSTransfer * poTransferIn, int iLayerIn, OGRSDTSDataSource * poDSIn ) : bPolygonsBuilt(FALSE) { poDS = poDSIn; poTransfer = poTransferIn; iLayer = iLayerIn; poReader = poTransfer->GetLayerIndexedReader( iLayer ); /* -------------------------------------------------------------------- */ /* Define the feature. */ /* -------------------------------------------------------------------- */ int iCATDEntry = poTransfer->GetLayerCATDEntry( iLayer ); poFeatureDefn = new OGRFeatureDefn(poTransfer->GetCATD()->GetEntryModule(iCATDEntry)); SetDescription( poFeatureDefn->GetName() ); poFeatureDefn->Reference(); poFeatureDefn->GetGeomFieldDefn(0)->SetSpatialRef(poDS->GetSpatialRef()); OGRFieldDefn oRecId( "RCID", OFTInteger ); poFeatureDefn->AddFieldDefn( &oRecId ); if( poTransfer->GetLayerType(iLayer) == SLTPoint ) { poFeatureDefn->SetGeomType( wkbPoint ); } else if( poTransfer->GetLayerType(iLayer) == SLTLine ) { poFeatureDefn->SetGeomType( wkbLineString ); oRecId.SetName( "SNID" ); poFeatureDefn->AddFieldDefn( &oRecId ); oRecId.SetName( "ENID" ); poFeatureDefn->AddFieldDefn( &oRecId ); } else if( poTransfer->GetLayerType(iLayer) == SLTPoly ) { poFeatureDefn->SetGeomType( wkbPolygon ); } else if( poTransfer->GetLayerType(iLayer) == SLTAttr ) { poFeatureDefn->SetGeomType( wkbNone ); } /* -------------------------------------------------------------------- */ /* Add schema from referenced attribute records. */ /* -------------------------------------------------------------------- */ char **papszATIDRefs = NULL; if( poTransfer->GetLayerType(iLayer) != SLTAttr ) papszATIDRefs = poReader->ScanModuleReferences(); else papszATIDRefs = CSLAddString( papszATIDRefs, poTransfer->GetCATD()->GetEntryModule(iCATDEntry) ); for( int iTable = 0; papszATIDRefs != NULL && papszATIDRefs[iTable] != NULL; iTable++ ) { SDTSAttrReader *poAttrReader; DDFFieldDefn *poFDefn; /* -------------------------------------------------------------------- */ /* Get the attribute table reader, and the associated user */ /* attribute field. */ /* -------------------------------------------------------------------- */ int nLayerIdx = poTransfer->FindLayer( papszATIDRefs[iTable] ); if( nLayerIdx < 0 ) continue; poAttrReader = (SDTSAttrReader *) poTransfer->GetLayerIndexedReader(nLayerIdx); if( poAttrReader == NULL ) continue; poFDefn = poAttrReader->GetModule()->FindFieldDefn( "ATTP" ); if( poFDefn == NULL ) poFDefn = poAttrReader->GetModule()->FindFieldDefn( "ATTS" ); if( poFDefn == NULL ) continue; /* -------------------------------------------------------------------- */ /* Process each user subfield on the attribute table into an */ /* OGR field definition. */ /* -------------------------------------------------------------------- */ for( int iSF=0; iSF < poFDefn->GetSubfieldCount(); iSF++ ) { DDFSubfieldDefn *poSFDefn = poFDefn->GetSubfield( iSF ); int nWidth = poSFDefn->GetWidth(); char *pszFieldName; if( poFeatureDefn->GetFieldIndex( poSFDefn->GetName() ) != -1 ) pszFieldName = CPLStrdup( CPLSPrintf( "%s_%s", papszATIDRefs[iTable], poSFDefn->GetName() ) ); else pszFieldName = CPLStrdup( poSFDefn->GetName() ); switch( poSFDefn->GetType() ) { case DDFString: { OGRFieldDefn oStrField( pszFieldName, OFTString ); if( nWidth != 0 ) oStrField.SetWidth( nWidth ); poFeatureDefn->AddFieldDefn( &oStrField ); } break; case DDFInt: { OGRFieldDefn oIntField( pszFieldName, OFTInteger ); if( nWidth != 0 ) oIntField.SetWidth( nWidth ); poFeatureDefn->AddFieldDefn( &oIntField ); } break; case DDFFloat: { OGRFieldDefn oRealField( pszFieldName, OFTReal ); // We don't have a precision in DDF files, so we never even // use the width. Otherwise with a precision of zero the // result would look like an integer. poFeatureDefn->AddFieldDefn( &oRealField ); } break; default: break; } CPLFree( pszFieldName ); } /* next iSF (subfield) */ } /* next iTable */ CSLDestroy( papszATIDRefs ); }