📄 tigerpolygon.cpp
字号:
{ "SLDU", 'R', 'A', OFTString, 85, 87, 3, 1, 1, 1 }, { "SLDL", 'R', 'A', OFTString, 88, 90, 3, 1, 1, 1 }, { "UGA", 'L', 'A', OFTString, 91, 95, 5, 1, 1, 1 }, { "BLKGRP", 'L', 'N', OFTInteger, 96, 96, 1, 1, 1, 1 }, { "VTD", 'R', 'A', OFTString, 97, 102, 6, 1, 1, 1 }, { "STATECOL", 'L', 'N', OFTInteger, 103, 104, 2, 1, 1, 1 }, { "COUNTYCOL", 'L', 'N', OFTInteger, 105, 107, 3, 1, 1, 1 }, { "BLOCKCOL", 'R', 'N', OFTInteger, 108, 112, 5, 1, 1, 1 }, { "BLKSUFCOL", 'L', 'A', OFTString, 113, 113, 1, 1, 1, 1 }, { "ZCTA5", 'L', 'A', OFTString, 114, 118, 5, 1, 1, 1 }};static TigerRecordInfo rtS_2000_Redistricting_info = { rtS_2000_Redistricting_fields, sizeof(rtS_2000_Redistricting_fields) / sizeof(TigerFieldInfo), 120 };static TigerFieldInfo rtS_fields[] = { { "FILE", 'L', 'N', OFTString, 6, 10, 5, 0, 0, 1 }, { "STATE", 'L', 'N', OFTInteger, 6, 7, 2, 0, 0, 1 }, { "COUNTY", 'L', 'N', OFTInteger, 8, 10, 3, 0, 0, 1 }, { "CENID", 'L', 'A', OFTString, 11, 15, 5, 0, 0, 1 }, { "POLYID", 'R', 'N', OFTInteger, 16, 25, 10, 0, 0, 1 }, { "WATER", 'L', 'N', OFTString, 26, 26, 1, 1, 1, 1 }, { "CMSAMSA", 'L', 'N', OFTInteger, 27, 30, 4, 1, 1, 1 }, { "PMSA", 'L', 'N', OFTInteger, 31, 34, 4, 1, 1, 1 }, { "AIANHH", 'L', 'N', OFTInteger, 35, 39, 5, 1, 1, 1 }, { "AIR", 'L', 'N', OFTInteger, 40, 43, 4, 1, 1, 1 }, { "TRUST", 'L', 'A', OFTString, 44, 44, 1, 1, 1, 1 }, { "ANRC", 'L', 'A', OFTInteger, 45, 46, 2, 1, 1, 1 }, { "STATECU", 'L', 'N', OFTInteger, 47, 48, 2, 1, 1, 1 }, { "COUNTYCU", 'L', 'N', OFTInteger, 49, 51, 3, 1, 1, 1 }, { "FCCITY", 'L', 'N', OFTInteger, 52, 56, 5, 1, 1, 1 }, { "FMCD", 'L', 'N', OFTInteger, 57, 61, 5, 0, 0, 1 }, { "FSMCD", 'L', 'N', OFTInteger, 62, 66, 5, 1, 1, 1 }, { "PLACE", 'L', 'N', OFTInteger, 67, 71, 5, 1, 1, 1 }, { "CTBNA00", 'L', 'N', OFTInteger, 72, 77, 6, 1, 1, 1 }, { "BLK00", 'L', 'N', OFTString, 78, 81, 4, 1, 1, 1 }, { "RS10", 'R', 'N', OFTInteger, 82, 82, 0, 0, 1, 1 }, { "CDCU", 'L', 'N', OFTInteger, 83, 84, 2, 1, 1, 1 }, { "STSENATE", 'L', 'A', OFTString, 85, 90, 6, 1, 1, 1 }, { "STHOUSE", 'L', 'A', OFTString, 91, 96, 6, 1, 1, 1 }, { "VTD00", 'L', 'A', OFTString, 97, 102, 6, 1, 1, 1 }};static TigerRecordInfo rtS_info = { rtS_fields, sizeof(rtS_fields) / sizeof(TigerFieldInfo), 120 };/************************************************************************//* TigerPolygon() *//************************************************************************/TigerPolygon::TigerPolygon( OGRTigerDataSource * poDSIn, const char * pszPrototypeModule ){ poDS = poDSIn; poFeatureDefn = new OGRFeatureDefn( "Polygon" ); poFeatureDefn->SetGeomType( wkbNone ); fpRTS = NULL; bUsingRTS = TRUE; if( poDS->GetVersion() >= TIGER_2003 ) { psRTAInfo = &rtA_2003_info; } else if( poDS->GetVersion() >= TIGER_2002 ) { psRTAInfo = &rtA_2002_info; } else { psRTAInfo = &rtA_info; } if( poDS->GetVersion() >= TIGER_2002 ) { psRTSInfo = &rtS_2002_info; } else if( poDS->GetVersion() >= TIGER_2000_Redistricting ) { psRTSInfo = &rtS_2000_Redistricting_info; } else { psRTSInfo = &rtS_info; } /* -------------------------------------------------------------------- */ /* Fields from type A record. */ /* -------------------------------------------------------------------- */ AddFieldDefns(psRTAInfo, poFeatureDefn); /* -------------------------------------------------------------------- */ /* Add the RTS records if it is available. */ /* -------------------------------------------------------------------- */ if( bUsingRTS ) { AddFieldDefns(psRTSInfo, poFeatureDefn); }}/************************************************************************//* ~TigerPolygon() *//************************************************************************/TigerPolygon::~TigerPolygon(){ if( fpRTS != NULL ) VSIFClose( fpRTS );}/************************************************************************//* SetModule() *//************************************************************************/int TigerPolygon::SetModule( const char * pszModule ){ if( !OpenFile( pszModule, "A" ) ) return FALSE; EstablishFeatureCount(); /* -------------------------------------------------------------------- *//* Open the RTS file *//* -------------------------------------------------------------------- */ if( bUsingRTS ) { if( fpRTS != NULL ) { VSIFClose( fpRTS ); fpRTS = NULL; } if( pszModule ) { char *pszFilename; pszFilename = poDS->BuildFilename( pszModule, "S" ); fpRTS = VSIFOpen( pszFilename, "rb" ); CPLFree( pszFilename ); nRTSRecLen = EstablishRecordLength( fpRTS ); } } return TRUE;}/************************************************************************//* GetFeature() *//************************************************************************/OGRFeature *TigerPolygon::GetFeature( int nRecordId ){ char achRecord[OGR_TIGER_RECBUF_LEN]; if( nRecordId < 0 || nRecordId >= nFeatures ) { CPLError( CE_Failure, CPLE_FileIO, "Request for out-of-range feature %d of %sA", nRecordId, pszModule ); return NULL; }/* -------------------------------------------------------------------- *//* Read the raw record data from the file. *//* -------------------------------------------------------------------- */ if( fpPrimary == NULL ) return NULL; if( VSIFSeek( fpPrimary, nRecordId * nRecordLength, SEEK_SET ) != 0 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to seek to %d of %sA", nRecordId * nRecordLength, pszModule ); return NULL; } if( VSIFRead( achRecord, nRecordLength, 1, fpPrimary ) != 1 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to read record %d of %sA", nRecordId, pszModule ); return NULL; } /* -------------------------------------------------------------------- */ /* Set fields. */ /* -------------------------------------------------------------------- */ OGRFeature *poFeature = new OGRFeature( poFeatureDefn ); SetFields( psRTAInfo, poFeature, achRecord ); /* -------------------------------------------------------------------- */ /* Read RTS record, and apply fields. */ /* -------------------------------------------------------------------- */ if( fpRTS != NULL ) { char achRTSRec[OGR_TIGER_RECBUF_LEN]; if( VSIFSeek( fpRTS, nRecordId * nRTSRecLen, SEEK_SET ) != 0 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to seek to %d of %sS", nRecordId * nRTSRecLen, pszModule ); return NULL; } if( VSIFRead( achRTSRec, psRTSInfo->nRecordLength, 1, fpRTS ) != 1 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to read record %d of %sS", nRecordId, pszModule ); return NULL; } SetFields( psRTSInfo, poFeature, achRTSRec ); } return poFeature;}/************************************************************************//* SetWriteModule() *//************************************************************************/int TigerPolygon::SetWriteModule( const char *pszFileCode, int nRecLen, OGRFeature *poFeature ){ int bSuccess; bSuccess = TigerFileBase::SetWriteModule( pszFileCode, nRecLen, poFeature); if( !bSuccess ) return bSuccess;/* -------------------------------------------------------------------- *//* Open the RT3 file *//* -------------------------------------------------------------------- */ if( bUsingRTS ) { if( fpRTS != NULL ) { VSIFClose( fpRTS ); fpRTS = NULL; } if( pszModule ) { char *pszFilename; pszFilename = poDS->BuildFilename( pszModule, "S" ); fpRTS = VSIFOpen( pszFilename, "ab" ); CPLFree( pszFilename ); } } return TRUE;}/************************************************************************//* CreateFeature() *//************************************************************************/OGRErr TigerPolygon::CreateFeature( OGRFeature *poFeature ){ char szRecord[OGR_TIGER_RECBUF_LEN];/* -------------------------------------------------------------------- *//* Write basic data record ("RTA") *//* -------------------------------------------------------------------- */ if( !SetWriteModule( "A", psRTAInfo->nRecordLength+2, poFeature ) ) return OGRERR_FAILURE; memset( szRecord, ' ', psRTAInfo->nRecordLength ); WriteFields( psRTAInfo, poFeature, szRecord ); WriteRecord( szRecord, psRTAInfo->nRecordLength, "A" );/* -------------------------------------------------------------------- *//* Prepare S record. *//* -------------------------------------------------------------------- */ memset( szRecord, ' ', psRTSInfo->nRecordLength ); WriteFields( psRTSInfo, poFeature, szRecord ); WriteRecord( szRecord, psRTSInfo->nRecordLength, "S", fpRTS ); return OGRERR_NONE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -