📄 tigercompletechain.cpp
字号:
psRT1Info = &rt1_2002_info; bUsingRT3 = FALSE; } else { psRT1Info = &rt1_info; bUsingRT3 = TRUE; } psRT2Info = &rt2_info; nRT1RecOffset = 0; if (poDS->GetVersion() >= TIGER_2000_Redistricting) { psRT3Info = &rt3_2000_Redistricting_info; } else { psRT3Info = &rt3_info; } fpRT3 = NULL; panShapeRecordId = NULL; fpShape = NULL; /* -------------------------------------------------------------------- */ /* Fields from type 1 record. */ /* -------------------------------------------------------------------- */ AddFieldDefns( psRT1Info, poFeatureDefn ); /* -------------------------------------------------------------------- */ /* Fields from type 3 record. Eventually we should verify that */ /* a .RT3 file is available before adding these fields. */ /* -------------------------------------------------------------------- */ if( bUsingRT3 ) { AddFieldDefns( psRT3Info, poFeatureDefn ); }}/************************************************************************//* ~TigerCompleteChain() *//************************************************************************/TigerCompleteChain::~TigerCompleteChain(){ CPLFree( panShapeRecordId ); if( fpRT3 != NULL ) VSIFClose( fpRT3 ); if( fpShape != NULL ) VSIFClose( fpShape );}/************************************************************************//* SetModule() *//************************************************************************/int TigerCompleteChain::SetModule( const char * pszModule ){ if( !OpenFile( pszModule, "1" ) ) return FALSE; EstablishFeatureCount();/* -------------------------------------------------------------------- *//* Is this a copyright record inserted at the beginning of the *//* RT1 file by the folks at GDT? If so, setup to ignore the *//* first record. *//* -------------------------------------------------------------------- */ nRT1RecOffset = 0; if( pszModule ) { char achHeader[10]; VSIFSeek( fpPrimary, 0, SEEK_SET ); VSIFRead( achHeader, sizeof(achHeader), 1, fpPrimary ); if( EQUALN(achHeader,"Copyright",8) ) { nRT1RecOffset = 1; nFeatures--; } } /* -------------------------------------------------------------------- *//* Open the RT3 file *//* -------------------------------------------------------------------- */ if( bUsingRT3 ) { if( fpRT3 != NULL ) { VSIFClose( fpRT3 ); fpRT3 = NULL; } if( pszModule ) { char *pszFilename; pszFilename = poDS->BuildFilename( pszModule, "3" ); fpRT3 = VSIFOpen( pszFilename, "rb" ); CPLFree( pszFilename ); } } /* -------------------------------------------------------------------- *//* Close the shape point file, if open and free the list of *//* record ids. *//* -------------------------------------------------------------------- */ if( fpShape != NULL ) { VSIFClose( fpShape ); fpShape = NULL; } CPLFree( panShapeRecordId ); panShapeRecordId = NULL;/* -------------------------------------------------------------------- *//* Try to open the RT2 file corresponding to this RT1 file. *//* -------------------------------------------------------------------- */ if( pszModule != NULL ) { char *pszFilename; pszFilename = poDS->BuildFilename( pszModule, "2" ); fpShape = VSIFOpen( pszFilename, "rb" ); if( fpShape == NULL ) { if( nRT1RecOffset == 0 ) CPLError( CE_Warning, CPLE_OpenFailed, "Failed to open %s, intermediate shape arcs will not be available.\n", pszFilename ); } else panShapeRecordId = (int *)CPLCalloc(sizeof(int),GetFeatureCount()); CPLFree( pszFilename ); } return TRUE;}/************************************************************************//* GetFeature() *//************************************************************************/OGRFeature *TigerCompleteChain::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 %s1", nRecordId, pszModule ); return NULL; }/* -------------------------------------------------------------------- *//* Read the raw record data from the file. *//* -------------------------------------------------------------------- */ if( fpPrimary == NULL ) return NULL; if( VSIFSeek( fpPrimary, (nRecordId+nRT1RecOffset) * nRecordLength, SEEK_SET ) != 0 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to seek to %d of %s1", nRecordId * nRecordLength, pszModule ); return NULL; } if( VSIFRead( achRecord, psRT1Info->nRecordLength, 1, fpPrimary ) != 1 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to read %d bytes of record %d of %s1 at offset %d", psRT1Info->nRecordLength, nRecordId, pszModule, (nRecordId+nRT1RecOffset) * nRecordLength ); return NULL; } /* -------------------------------------------------------------------- */ /* Set fields. */ /* -------------------------------------------------------------------- */ OGRFeature *poFeature = new OGRFeature( poFeatureDefn ); SetFields( psRT1Info, poFeature, achRecord ); /* -------------------------------------------------------------------- */ /* Read RT3 record, and apply fields. */ /* -------------------------------------------------------------------- */ if( fpRT3 != NULL ) { char achRT3Rec[OGR_TIGER_RECBUF_LEN]; int nRT3RecLen = psRT3Info->nRecordLength + nRecordLength - psRT1Info->nRecordLength; if( VSIFSeek( fpRT3, nRecordId * nRT3RecLen, SEEK_SET ) != 0 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to seek to %d of %s3", nRecordId * nRT3RecLen, pszModule ); return NULL; } if( VSIFRead( achRT3Rec, psRT3Info->nRecordLength, 1, fpRT3 ) != 1 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to read record %d of %s3", nRecordId, pszModule ); return NULL; } SetFields( psRT3Info, poFeature, achRT3Rec ); }/* -------------------------------------------------------------------- *//* Set geometry *//* -------------------------------------------------------------------- */ OGRLineString *poLine = new OGRLineString(); poLine->setPoint(0, atoi(GetField(achRecord, 191, 200)) / 1000000.0, atoi(GetField(achRecord, 201, 209)) / 1000000.0 ); if( !AddShapePoints( poFeature->GetFieldAsInteger("TLID"), nRecordId, poLine, 0 ) ) { delete poFeature; return NULL; } poLine->addPoint(atoi(GetField(achRecord, 210, 219)) / 1000000.0, atoi(GetField(achRecord, 220, 228)) / 1000000.0 ); poFeature->SetGeometryDirectly( poLine ); return poFeature;}/************************************************************************//* AddShapePoints() *//* *//* Record zero or more shape records associated with this line *//* and add the points to the passed line geometry. *//************************************************************************/int TigerCompleteChain::AddShapePoints( int nTLID, int nRecordId, OGRLineString * poLine, int nSeqNum ) { int nShapeRecId; nShapeRecId = GetShapeRecordId( nRecordId, nTLID ); // -2 means an error occured. if( nShapeRecId == -2 ) return FALSE; // -1 means there are no extra shape vertices, but things worked fine. if( nShapeRecId == -1 ) return TRUE;/* -------------------------------------------------------------------- *//* Read all the sequential records with the same TLID. *//* -------------------------------------------------------------------- */ char achShapeRec[OGR_TIGER_RECBUF_LEN]; int nShapeRecLen = psRT2Info->nRecordLength + nRecordLength - psRT1Info->nRecordLength; for( ; TRUE; nShapeRecId++ ) { int nBytesRead = 0; if( VSIFSeek( fpShape, (nShapeRecId-1) * nShapeRecLen, SEEK_SET ) != 0 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to seek to %d of %s2", (nShapeRecId-1) * nShapeRecLen, pszModule ); return FALSE; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -