📄 dted_ptstream.c
字号:
{ int nCrLong, nCrLat; nCrLong = (int) floor(dfLong); nCrLat = (int) floor(dfLat); if( !DTEDPtStreamNewTile( psStream, nCrLong, nCrLat ) ) return FALSE; }/* -------------------------------------------------------------------- *//* Write data out to selected tile. *//* -------------------------------------------------------------------- */ return DTEDWritePtLL( psStream, psStream->pasCF + psStream->nLastFile, dfLong, dfLat, dfElev ); }/* ==================================================================== *//* Handle case where we are on a boundary. We may be writing *//* the value to as many as four tiles. *//* ==================================================================== */ else { int nLatMin, nLatMax, nLongMin, nLongMax; int nCrLong, nCrLat; nLongMin = (int) floor( dfLong - 0.5*psStream->dfPixelSize ); nLongMax = (int) floor( dfLong + 0.5*psStream->dfPixelSize ); nLatMin = (int) floor( dfLat - 0.5*psStream->dfPixelSize ); nLatMax = (int) floor( dfLat + 0.5*psStream->dfPixelSize ); for( nCrLong = nLongMin; nCrLong <= nLongMax; nCrLong++ ) { for( nCrLat = nLatMin; nCrLat <= nLatMax; nCrLat++ ) { psStream->nLastFile = -1;/* -------------------------------------------------------------------- *//* Find this tile in our existing list. *//* -------------------------------------------------------------------- */ for( i = 0; i < psStream->nOpenFiles; i++ ) { if( psStream->pasCF[i].nLLLong == nCrLong && psStream->pasCF[i].nLLLat == nCrLat ) { psStream->nLastFile = i; break; } }/* -------------------------------------------------------------------- *//* Create the tile if not found. *//* -------------------------------------------------------------------- */ if( psStream->nLastFile == -1 ) { if( !DTEDPtStreamNewTile( psStream, nCrLong, nCrLat ) ) return FALSE; }/* -------------------------------------------------------------------- *//* Write to the tile. *//* -------------------------------------------------------------------- */ if( !DTEDWritePtLL( psStream, psStream->pasCF + psStream->nLastFile, dfLong, dfLat, dfElev ) ) return FALSE; } } } return TRUE;}/************************************************************************//* DTEDClosePtStream() *//************************************************************************/void DTEDClosePtStream( void *hStream ){ DTEDPtStream *psStream = (DTEDPtStream *) hStream; int iFile, iMD;/* -------------------------------------------------------------------- *//* Flush all DTED files. *//* -------------------------------------------------------------------- */ for( iFile = 0; iFile < psStream->nOpenFiles; iFile++ ) { int iProfile; DTEDCachedFile *psCF = psStream->pasCF + iFile; for( iProfile = 0; iProfile < psCF->psInfo->nXSize; iProfile++ ) { if( psCF->papanProfiles[iProfile] != NULL ) { DTEDWriteProfile( psCF->psInfo, iProfile, psCF->papanProfiles[iProfile] ); CPLFree( psCF->papanProfiles[iProfile] ); } } CPLFree( psCF->papanProfiles ); for( iMD = 0; iMD < DTEDMD_MAX+1; iMD++ ) { if( psStream->apszMetadata[iMD] != NULL ) DTEDSetMetadata( psCF->psInfo, iMD, psStream->apszMetadata[iMD] ); } DTEDClose( psCF->psInfo ); }/* -------------------------------------------------------------------- *//* Final cleanup. *//* -------------------------------------------------------------------- */ for( iMD = 0; iMD < DTEDMD_MAX+1; iMD++ ) CPLFree( psStream->apszMetadata[iMD] ); CPLFree( psStream->pasCF ); CPLFree( psStream->pszPath ); CPLFree( psStream );}/************************************************************************//* DTEDFillPixel() *//************************************************************************/void DTEDFillPixel( DTEDInfo *psInfo, GInt16 **papanProfiles, GInt16 **papanDstProfiles, int iX, int iY, int nPixelSearchDist, float *pafKernel ){ int nKernelWidth = 2 * nPixelSearchDist + 1; int nXMin, nXMax, nYMin, nYMax; double dfCoefSum = 0.0, dfValueSum = 0.0; int iXS, iYS; nXMin = MAX(0,iX - nPixelSearchDist); nXMax = MIN(psInfo->nXSize-1,iX + nPixelSearchDist); nYMin = MAX(0,iY - nPixelSearchDist); nYMax = MIN(psInfo->nYSize-1,iY + nPixelSearchDist); for( iXS = nXMin; iXS <= nXMax; iXS++ ) { GInt16 *panThisProfile = papanProfiles[iXS]; if( panThisProfile == NULL ) continue; for( iYS = nYMin; iYS <= nYMax; iYS++ ) { if( panThisProfile[iYS] != DTED_NODATA_VALUE ) { int iXK, iYK; float fKernelCoef; iXK = iXS - iX + nPixelSearchDist; iYK = iYS - iY + nPixelSearchDist; fKernelCoef = pafKernel[iXK + iYK * nKernelWidth]; dfCoefSum += fKernelCoef; dfValueSum += fKernelCoef * panThisProfile[iYS]; } } } if( dfCoefSum == 0.0 ) papanDstProfiles[iX][iY] = DTED_NODATA_VALUE; else papanDstProfiles[iX][iY] = (GInt16) floor(dfValueSum / dfCoefSum + 0.5);}/************************************************************************//* DTEDFillPtStream() *//* *//* Apply simple inverse distance interpolator to all no-data *//* pixels based on available values within the indicated search *//* distance (rectangular). *//************************************************************************/void DTEDFillPtStream( void *hStream, int nPixelSearchDist ){ DTEDPtStream *psStream = (DTEDPtStream *) hStream; int iFile, nKernelWidth; float *pafKernel; int iX, iY;/* -------------------------------------------------------------------- *//* Setup inverse distance weighting kernel. *//* -------------------------------------------------------------------- */ nKernelWidth = 2 * nPixelSearchDist + 1; pafKernel = (float *) CPLMalloc(nKernelWidth*nKernelWidth*sizeof(float)); for( iX = 0; iX < nKernelWidth; iX++ ) { for( iY = 0; iY < nKernelWidth; iY++ ) { pafKernel[iX + iY * nKernelWidth] = (float) (1.0 / sqrt( (nPixelSearchDist-iX) * (nPixelSearchDist-iX) + (nPixelSearchDist-iY) * (nPixelSearchDist-iY) )); } } /* ==================================================================== *//* Process each cached file. *//* ==================================================================== */ for( iFile = 0; iFile < psStream->nOpenFiles; iFile++ ) { DTEDInfo *psInfo = psStream->pasCF[iFile].psInfo; GInt16 **papanProfiles = psStream->pasCF[iFile].papanProfiles; GInt16 **papanDstProfiles; papanDstProfiles = (GInt16 **) CPLCalloc(sizeof(GInt16*),psInfo->nXSize); /* -------------------------------------------------------------------- *//* Setup output image. *//* -------------------------------------------------------------------- */ for( iX = 0; iX < psInfo->nXSize; iX++ ) { papanDstProfiles[iX] = (GInt16 *) CPLMalloc(sizeof(GInt16) * psInfo->nYSize); }/* -------------------------------------------------------------------- *//* Interpolate all missing values, and copy over available values. *//* -------------------------------------------------------------------- */ for( iX = 0; iX < psInfo->nXSize; iX++ ) { for( iY = 0; iY < psInfo->nYSize; iY++ ) { if( papanProfiles[iX] == NULL || papanProfiles[iX][iY] == DTED_NODATA_VALUE ) { DTEDFillPixel( psInfo, papanProfiles, papanDstProfiles, iX, iY, nPixelSearchDist, pafKernel ); } else { papanDstProfiles[iX][iY] = papanProfiles[iX][iY]; } } }/* -------------------------------------------------------------------- *//* Push new values back into cache. *//* -------------------------------------------------------------------- */ for( iX = 0; iX < psInfo->nXSize; iX++ ) { CPLFree( papanProfiles[iX] ); papanProfiles[iX] = papanDstProfiles[iX]; } CPLFree( papanDstProfiles ); } CPLFree( pafKernel );}/************************************************************************//* DTEDPtStreamSetMetadata() *//************************************************************************/void DTEDPtStreamSetMetadata( void *hStream, DTEDMetaDataCode eCode, const char *pszValue ){ DTEDPtStream *psStream = (DTEDPtStream *) hStream; if( eCode >= 0 && eCode < DTEDMD_MAX+1 ) { CPLFree( psStream->apszMetadata[eCode] ); psStream->apszMetadata[eCode] = CPLStrdup( pszValue ); }}/************************************************************************//* DTEDPtStreamTrimEdgeOnlyTiles() *//* *//* Erase all tiles that only have boundary values set. *//************************************************************************/void DTEDPtStreamTrimEdgeOnlyTiles( void *hStream ){ DTEDPtStream *psStream = (DTEDPtStream *) hStream; int iFile; for( iFile = psStream->nOpenFiles-1; iFile >= 0; iFile-- ) { DTEDInfo *psInfo = psStream->pasCF[iFile].psInfo; GInt16 **papanProfiles = psStream->pasCF[iFile].papanProfiles; int iProfile, iPixel, bGotNonEdgeData = FALSE; for( iProfile = 1; iProfile < psInfo->nXSize-1; iProfile++ ) { if( papanProfiles[iProfile] == NULL ) continue; for( iPixel = 1; iPixel < psInfo->nYSize-1; iPixel++ ) { if( papanProfiles[iProfile][iPixel] != DTED_NODATA_VALUE ) { bGotNonEdgeData = TRUE; break; } } } if( bGotNonEdgeData ) continue; /* Remove this tile */ for( iProfile = 0; iProfile < psInfo->nXSize; iProfile++ ) { if( papanProfiles[iProfile] != NULL ) CPLFree( papanProfiles[iProfile] ); } CPLFree( papanProfiles ); DTEDClose( psInfo ); VSIUnlink( psStream->pasCF[iFile].pszFilename ); CPLFree( psStream->pasCF[iFile].pszFilename ); memmove( psStream->pasCF + iFile, psStream->pasCF + iFile + 1, sizeof(DTEDCachedFile) * (psStream->nOpenFiles-iFile-1) ); psStream->nOpenFiles--; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -