⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tigerfilebase.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/************************************************************************//*                              GetField()                              *//************************************************************************/const char *TigerFileBase::GetField( const char * pachRawDataRecord,                                     int nStartChar, int nEndChar ){    static char         aszField[128];    int                 nLength = nEndChar - nStartChar + 1;        CPLAssert( nEndChar - nStartChar + 2 < (int) sizeof(aszField) );    strncpy( aszField, pachRawDataRecord + nStartChar - 1, nLength );    aszField[nLength] = '\0';    while( nLength > 0 && aszField[nLength-1] == ' ' )        aszField[--nLength] = '\0';    return aszField;}/************************************************************************//*                              SetField()                              *//*                                                                      *//*      Set a field on an OGRFeature from a tiger record, or leave      *//*      NULL if the value isn't found.                                  *//************************************************************************/void TigerFileBase::SetField( OGRFeature *poFeature, const char *pszField,                              const char *pachRecord, int nStart, int nEnd ){    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;  for (i=0; i<psRTInfo->nFieldCount; ++i) {    if (psRTInfo->pasFields[i].bDefine) {      oField.Set( psRTInfo->pasFields[i].pszFieldName,                  psRTInfo->pasFields[i].OGRtype,                  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 );    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -