📄 tigercompletechain.cpp
字号:
nBytesRead = VSIFRead( achShapeRec, 1, psRT2Info->nRecordLength, fpShape ); /* ** Handle case where the last record in the file is full. We will ** try to read another record but not find it. We require that we ** have found at least one shape record for this case though. */ if( nBytesRead <= 0 && VSIFEof( fpShape ) && poLine->getNumPoints() > 0 ) break; if( nBytesRead != psRT2Info->nRecordLength ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to read %d bytes of record %d of %s2 at offset %d", psRT2Info->nRecordLength, nShapeRecId, pszModule, (nShapeRecId-1) * nShapeRecLen ); return FALSE; } if( atoi(GetField(achShapeRec,6,15)) != nTLID ) break;/* -------------------------------------------------------------------- *//* Translate the locations into OGRLineString vertices. *//* -------------------------------------------------------------------- */ int iVertex; for( iVertex = 0; iVertex < 10; iVertex++ ) { int iStart = 19 + 19*iVertex; int nX = atoi(GetField(achShapeRec,iStart,iStart+9)); int nY = atoi(GetField(achShapeRec,iStart+10,iStart+18)); if( nX == 0 && nY == 0 ) break; poLine->addPoint( nX / 1000000.0, nY / 1000000.0 ); }/* -------------------------------------------------------------------- *//* Don't get another record if this one was incomplete. *//* -------------------------------------------------------------------- */ if( iVertex < 10 ) break; } return TRUE;}/************************************************************************//* GetShapeRecordId() *//* *//* Get the record id of the first record of shape points for *//* the provided TLID (complete chain). *//************************************************************************/int TigerCompleteChain::GetShapeRecordId( int nChainId, int nTLID ){ CPLAssert( nChainId >= 0 && nChainId < GetFeatureCount() ); if( fpShape == NULL || panShapeRecordId == NULL ) return -1; /* -------------------------------------------------------------------- *//* Do we already have the answer? *//* -------------------------------------------------------------------- */ if( panShapeRecordId[nChainId] != 0 ) return panShapeRecordId[nChainId]; /* -------------------------------------------------------------------- *//* If we don't already have this value, then search from the *//* previous known record. *//* -------------------------------------------------------------------- */ int iTestChain, nWorkingRecId; for( iTestChain = nChainId-1; iTestChain >= 0 && panShapeRecordId[iTestChain] <= 0; iTestChain-- ) {} if( iTestChain < 0 ) { iTestChain = -1; nWorkingRecId = 1; } else { nWorkingRecId = panShapeRecordId[iTestChain]+1; }/* -------------------------------------------------------------------- *//* If we have non existent records following (-1's) we can *//* narrow our search a bit. *//* -------------------------------------------------------------------- */ while( panShapeRecordId[iTestChain+1] == -1 ) { iTestChain++; }/* -------------------------------------------------------------------- *//* Read records up to the maximum distance that is possibly *//* required, looking for our target TLID. *//* -------------------------------------------------------------------- */ int nMaxChainToRead = nChainId - iTestChain; int nChainsRead = 0; char achShapeRec[OGR_TIGER_RECBUF_LEN]; int nShapeRecLen = psRT2Info->nRecordLength + nRecordLength - psRT1Info->nRecordLength; while( nChainsRead < nMaxChainToRead ) { if( VSIFSeek( fpShape, (nWorkingRecId-1) * nShapeRecLen, SEEK_SET ) != 0 ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to seek to %d of %s2", (nWorkingRecId-1) * nShapeRecLen, pszModule ); return -2; } if( VSIFRead( achShapeRec, psRT2Info->nRecordLength, 1, fpShape ) != 1 ) { if( !VSIFEof( fpShape ) ) { CPLError( CE_Failure, CPLE_FileIO, "Failed to read record %d of %s2", nWorkingRecId-1, pszModule ); return -2; } else return -1; } if( atoi(GetField(achShapeRec,6,15)) == nTLID ) { panShapeRecordId[nChainId] = nWorkingRecId; return nWorkingRecId; } if( atoi(GetField(achShapeRec,16,18)) == 1 ) { nChainsRead++; } nWorkingRecId++; } panShapeRecordId[nChainId] = -1; return -1;}/************************************************************************//* SetWriteModule() *//************************************************************************/int TigerCompleteChain::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( bUsingRT3 ) { if( fpRT3 != NULL ) { VSIFClose( fpRT3 ); fpRT3 = NULL; } if( pszModule ) { char *pszFilename; pszFilename = poDS->BuildFilename( pszModule, "3" ); fpRT3 = VSIFOpen( pszFilename, "ab" ); CPLFree( pszFilename ); } } /* -------------------------------------------------------------------- *//* Close the shape point file, if open and free the list of *//* record ids. *//* -------------------------------------------------------------------- */ if( fpShape != NULL ) { VSIFClose( fpShape ); fpShape = NULL; } if( pszModule ) { char *pszFilename; pszFilename = poDS->BuildFilename( pszModule, "2" ); fpShape = VSIFOpen( pszFilename, "ab" ); CPLFree( pszFilename ); } return TRUE;}/************************************************************************//* CreateFeature() *//************************************************************************/OGRErr TigerCompleteChain::CreateFeature( OGRFeature *poFeature ){ char szRecord[OGR_TIGER_RECBUF_LEN]; OGRLineString *poLine = (OGRLineString *) poFeature->GetGeometryRef(); if( poLine == NULL || (poLine->getGeometryType() != wkbLineString && poLine->getGeometryType() != wkbLineString25D) ) return OGRERR_FAILURE; /* -------------------------------------------------------------------- */ /* Write basic data record ("RT1") */ /* -------------------------------------------------------------------- */ if( !SetWriteModule( "1", psRT1Info->nRecordLength+2, poFeature ) ) return OGRERR_FAILURE; memset( szRecord, ' ', psRT1Info->nRecordLength ); WriteFields( psRT1Info, poFeature, szRecord ); WritePoint( szRecord, 191, poLine->getX(0), poLine->getY(0) ); WritePoint( szRecord, 210, poLine->getX(poLine->getNumPoints()-1), poLine->getY(poLine->getNumPoints()-1) ); WriteRecord( szRecord, psRT1Info->nRecordLength, "1" ); /* -------------------------------------------------------------------- */ /* Write geographic entity codes (RT3) */ /* -------------------------------------------------------------------- */ if (bUsingRT3) { memset( szRecord, ' ', psRT3Info->nRecordLength ); WriteFields( psRT3Info, poFeature, szRecord ); WriteRecord( szRecord, psRT3Info->nRecordLength, "3", fpRT3 ); } /* -------------------------------------------------------------------- */ /* Write shapes sections (RT2) */ /* -------------------------------------------------------------------- */ if( poLine->getNumPoints() > 2 ) { int nPoints = poLine->getNumPoints(); int iPoint, nRTSQ = 1; for( iPoint = 1; iPoint < nPoints-1; ) { int i; char szTemp[5]; memset( szRecord, ' ', psRT2Info->nRecordLength ); WriteField( poFeature, "TLID", szRecord, 6, 15, 'R', 'N' ); sprintf( szTemp, "%3d", nRTSQ ); strncpy( ((char *)szRecord) + 15, szTemp, 4 ); for( i = 0; i < 10; i++ ) { if( iPoint < nPoints-1 ) WritePoint( szRecord, 19+19*i, poLine->getX(iPoint), poLine->getY(iPoint) ); else WritePoint( szRecord, 19+19*i, 0.0, 0.0 ); iPoint++; } WriteRecord( szRecord, psRT2Info->nRecordLength, "2", fpShape ); nRTSQ++; } } return OGRERR_NONE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -