?? tigerfilebase.cpp
字號:
{ const char *pszFieldValue = GetField( pachRecord, nStart, nEnd ); if( pszFieldValue[0] == '\0' ) return; poFeature->SetField( pszField, pszFieldValue );}/************************************************************************//* WriteField() *//* *//* Write a field into a record buffer with the indicated *//* formatting, or leave blank if not found. *//************************************************************************/int TigerFileBase::WriteField( OGRFeature *poFeature, const char *pszField, char *pachRecord, int nStart, int nEnd, char chFormat, char chType ){ int iField = poFeature->GetFieldIndex( pszField ); char szValue[512], szFormat[32]; CPLAssert( nEnd - nStart + 1 < (int) sizeof(szValue)-1 ); if( iField < 0 || !poFeature->IsFieldSet( iField ) ) return FALSE; if( chType == 'N' && chFormat == 'L' ) { sprintf( szFormat, "%%0%dd", nEnd - nStart + 1 ); sprintf( szValue, szFormat, poFeature->GetFieldAsInteger( iField ) ); } else if( chType == 'N' && chFormat == 'R' ) { sprintf( szFormat, "%%%dd", nEnd - nStart + 1 ); sprintf( szValue, szFormat, poFeature->GetFieldAsInteger( iField ) ); } else if( chType == 'A' && chFormat == 'L' ) { strncpy( szValue, poFeature->GetFieldAsString( iField ), sizeof(szValue) - 1 ); if( (int) strlen(szValue) < nEnd - nStart + 1 ) memset( szValue + strlen(szValue), ' ', nEnd - nStart + 1 - strlen(szValue) ); } else if( chType == 'A' && chFormat == 'R' ) { sprintf( szFormat, "%%%ds", nEnd - nStart + 1 ); sprintf( szValue, szFormat, poFeature->GetFieldAsString( iField ) ); } else { CPLAssert( FALSE ); return FALSE; } strncpy( pachRecord + nStart - 1, szValue, nEnd - nStart + 1 ); return TRUE;}/************************************************************************//* WritePoint() *//************************************************************************/int TigerFileBase::WritePoint( char *pachRecord, int nStart, double dfX, double dfY ){ char szTemp[20]; if( dfX == 0.0 && dfY == 0.0 ) { strncpy( pachRecord + nStart - 1, "+000000000+00000000", 19 ); } else { sprintf( szTemp, "%+10d%+9d", (int) floor(dfX * 1000000 + 0.5), (int) floor(dfY * 1000000 + 0.5) ); strncpy( pachRecord + nStart - 1, szTemp, 19 ); } return TRUE;}/************************************************************************//* WriteRecord() *//************************************************************************/int TigerFileBase::WriteRecord( char *pachRecord, int nRecLen, const char *pszType, FILE * fp ){ if( fp == NULL ) fp = fpPrimary; pachRecord[0] = *pszType; /* * Prior to TIGER_2002, type 5 files lacked the version. So write * the version in the record iff we're using TIGER_2002 or higher, * or if this is not type "5" */ if ( (poDS->GetVersion() >= TIGER_2002) || (!EQUAL(pszType, "5")) ) { char szVersion[5]; sprintf( szVersion, "%04d", poDS->GetVersionCode() ); strncpy( pachRecord + 1, szVersion, 4 ); } VSIFWrite( pachRecord, nRecLen, 1, fp ); VSIFWrite( (void *) "\r\n", 2, 1, fp ); return TRUE;}/************************************************************************//* SetWriteModule() *//* *//* Setup our access to be to the module indicated in the feature. *//************************************************************************/int TigerFileBase::SetWriteModule( const char *pszExtension, int nRecLen, OGRFeature *poFeature ){/* -------------------------------------------------------------------- *//* Work out what module we should be writing to. *//* -------------------------------------------------------------------- */ const char *pszTargetModule = poFeature->GetFieldAsString( "MODULE" ); char szFullModule[30]; /* TODO/notdef: eventually more logic based on FILE and STATE/COUNTY can be inserted here. */ if( pszTargetModule == NULL ) return FALSE; sprintf( szFullModule, "%s.RT", pszTargetModule );/* -------------------------------------------------------------------- *//* Is this our current module? *//* -------------------------------------------------------------------- */ if( pszModule != NULL && EQUAL(szFullModule,pszModule) ) return TRUE;/* -------------------------------------------------------------------- *//* Cleanup the previous file, if any. *//* -------------------------------------------------------------------- */ if( fpPrimary != NULL ) { VSIFClose( fpPrimary ); fpPrimary = NULL; } if( pszModule != NULL ) { CPLFree( pszModule ); pszModule = NULL; }/* -------------------------------------------------------------------- *//* Is this a module we have never written to before? If so, we *//* will try to blow away any existing files in this file set. *//* -------------------------------------------------------------------- */ if( !poDS->CheckModule( szFullModule ) ) { poDS->DeleteModuleFiles( szFullModule ); poDS->AddModule( szFullModule ); } /* -------------------------------------------------------------------- *//* Does this file already exist? *//* -------------------------------------------------------------------- */ const char *pszFilename; pszFilename = poDS->BuildFilename( szFullModule, pszExtension ); fpPrimary = VSIFOpen( pszFilename, "ab" ); if( fpPrimary == NULL ) return FALSE; pszModule = CPLStrdup( szFullModule ); return TRUE;}/************************************************************************//* AddFieldDefns() *//************************************************************************/void TigerFileBase::AddFieldDefns(TigerRecordInfo *psRTInfo, OGRFeatureDefn *poFeatureDefn){ OGRFieldDefn oField("",OFTInteger); int i, bLFieldHack; bLFieldHack = CSLTestBoolean( CPLGetConfigOption( "TIGER_LFIELD_AS_STRING", "NO" ) ); for (i=0; i<psRTInfo->nFieldCount; ++i) { if (psRTInfo->pasFields[i].bDefine) { OGRFieldType eFT = psRTInfo->pasFields[i].OGRtype; if( bLFieldHack && psRTInfo->pasFields[i].cFmt == 'L' && psRTInfo->pasFields[i].cType == 'N' ) eFT = OFTString; oField.Set( psRTInfo->pasFields[i].pszFieldName, eFT, psRTInfo->pasFields[i].nLen ); poFeatureDefn->AddFieldDefn( &oField ); } }}/************************************************************************//* SetFields() *//************************************************************************/void TigerFileBase::SetFields(TigerRecordInfo *psRTInfo, OGRFeature *poFeature, char *achRecord){ int i; for (i=0; i<psRTInfo->nFieldCount; ++i) { if (psRTInfo->pasFields[i].bSet) { SetField( poFeature, psRTInfo->pasFields[i].pszFieldName, achRecord, psRTInfo->pasFields[i].nBeg, psRTInfo->pasFields[i].nEnd ); } }}/************************************************************************//* WriteField() *//************************************************************************/void TigerFileBase::WriteFields(TigerRecordInfo *psRTInfo, OGRFeature *poFeature, char *szRecord){ int i; for (i=0; i<psRTInfo->nFieldCount; ++i) { if (psRTInfo->pasFields[i].bWrite) { WriteField( poFeature, psRTInfo->pasFields[i].pszFieldName, szRecord, psRTInfo->pasFields[i].nBeg, psRTInfo->pasFields[i].nEnd, psRTInfo->pasFields[i].cFmt, psRTInfo->pasFields[i].cType ); } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -