📄 dted_ptstream.c
字号:
/****************************************************************************** * $Id: dted_ptstream.c 10646 2007-01-18 02:38:10Z warmerdam $ * * Project: DTED Translator * Purpose: DTED Point Stream Writer. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2001, Frank Warmerdam * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. ****************************************************************************/#include "dted_api.h"#include <sys/types.h>
#include <sys/stat.h>
CPL_CVSID("$Id: dted_ptstream.c 10646 2007-01-18 02:38:10Z warmerdam $");typedef struct { char *pszFilename; DTEDInfo *psInfo; GInt16 **papanProfiles; int nLLLong; int nLLLat;} DTEDCachedFile;typedef struct { int nLevel; char *pszPath; double dfPixelSize; int nOpenFiles; DTEDCachedFile *pasCF; int nLastFile; char *apszMetadata[DTEDMD_MAX+1];} DTEDPtStream;/************************************************************************//* DTEDCreatePtStream() *//************************************************************************/void *DTEDCreatePtStream( const char *pszPath, int nLevel ){ DTEDPtStream *psStream; int i; struct _stat buf;/* -------------------------------------------------------------------- *//* Does the target directory already exist? If not try to *//* create it. *//* -------------------------------------------------------------------- */ if( _stat( pszPath, &buf ) != 0 ) { if( VSIMkdir( pszPath, 0755 ) != 0 ) {#ifndef AVOID_CPL CPLError( CE_Failure, CPLE_OpenFailed, "Unable to find, or create directory `%s'.", pszPath );#endif return NULL; } }/* -------------------------------------------------------------------- *//* Create the stream and initialize it. *//* -------------------------------------------------------------------- */ psStream = (DTEDPtStream *) CPLCalloc( sizeof(DTEDPtStream), 1 ); psStream->nLevel = nLevel; psStream->pszPath = CPLStrdup( pszPath ); psStream->nOpenFiles = 0; psStream->pasCF = NULL; psStream->nLastFile = -1; for( i = 0; i < DTEDMD_MAX+1; i++ ) psStream->apszMetadata[i] = NULL; if( nLevel == 0 ) psStream->dfPixelSize = 1.0 / 120.0; else if( nLevel == 1 ) psStream->dfPixelSize = 1.0 / 1200.0; else if( nLevel == 2 ) psStream->dfPixelSize = 1.0 / 3600.0; else psStream->dfPixelSize = 1.0 / 3600.0; return (void *) psStream;}/************************************************************************//* DTEDPtStreamNewTile() *//* *//* Create a new DTED file file, add it to our list, and make it *//* "current". *//************************************************************************/static int DTEDPtStreamNewTile( DTEDPtStream *psStream, int nCrLong, int nCrLat ){ DTEDInfo *psInfo; char szFile[128]; char chNSHemi, chEWHemi; char *pszFullFilename; char pszSubDir[64]; char pszSubDirFile[256]; char pszPathDir[256]; const char *pszError; struct _stat buf; /* work out filename */ if( nCrLat < 0 ) chNSHemi = 's'; else chNSHemi = 'n'; if( nCrLong < 0 ) chEWHemi = 'w'; else chEWHemi = 'e'; memset(pszSubDir,0,sizeof(char)); memset(pszSubDirFile,0,sizeof(char)); memset(pszPathDir,0,sizeof(char)); memset(szFile,0,sizeof(char)); sprintf( pszSubDir, "%c%03d",chEWHemi, ABS(nCrLong)); sprintf( szFile, "%c%02d.dt%d", chNSHemi, ABS(nCrLat),psStream->nLevel ); sprintf( pszPathDir, "%s\\%s", psStream->pszPath, pszSubDir ); if( _stat( pszPathDir, &buf ) != 0 ) { if( VSIMkdir( pszPathDir, 0755 ) != 0 ) {#ifndef AVOID_CPL CPLError( CE_Failure, CPLE_OpenFailed, "Unable to find, or create directory `%s'.", pszPathDir );#endif return -1; } } pszFullFilename = CPLStrdup(CPLFormFilename(pszPathDir, szFile, NULL )); /* -------------------------------------------------------------------- */ /* Does the target directory already exist? If not try to */ /* create it. */ /* -------------------------------------------------------------------- */ /* create the dted file */ pszError = DTEDCreate( pszFullFilename, psStream->nLevel, nCrLat, nCrLong ); if( pszError != NULL ) {#ifndef AVOID_CPL CPLError( CE_Failure, CPLE_OpenFailed, "Failed to create DTED file `%s'.\n%s", pszFullFilename, pszError );#endif return FALSE; } psInfo = DTEDOpen( pszFullFilename, "rb+", FALSE ); if( psInfo == NULL ) { CPLFree( pszFullFilename ); return FALSE; } /* add cached file to stream */ psStream->nOpenFiles++; psStream->pasCF = CPLRealloc(psStream->pasCF, sizeof(DTEDCachedFile)*psStream->nOpenFiles); sprintf( pszSubDirFile, "%s\\%s",pszSubDir, szFile); printf("创建文件 [ %s ]\n",pszSubDirFile); psStream->pasCF[psStream->nOpenFiles-1].psInfo = psInfo; psStream->pasCF[psStream->nOpenFiles-1].papanProfiles = CPLCalloc(sizeof(GInt16*),psInfo->nXSize); psStream->pasCF[psStream->nOpenFiles-1].pszFilename = pszFullFilename; psStream->pasCF[psStream->nOpenFiles-1].nLLLat = nCrLat; psStream->pasCF[psStream->nOpenFiles-1].nLLLong = nCrLong; psStream->nLastFile = psStream->nOpenFiles-1; memset(pszSubDir,0,sizeof(char)); memset(pszSubDirFile,0,sizeof(char)); memset(pszPathDir,0,sizeof(char)); memset(szFile,0,sizeof(char)); return TRUE;}/************************************************************************//* DTEDWritePtLL() *//************************************************************************/static int DTEDWritePtLL( DTEDPtStream *psStream, DTEDCachedFile *psCF, double dfLong, double dfLat, double dfElev ){/* -------------------------------------------------------------------- *//* Determine what profile this belongs in, and initialize the *//* profile if it doesn't already exist. *//* -------------------------------------------------------------------- */ DTEDInfo *psInfo = psCF->psInfo; int iProfile, i, iRow; iProfile = (int) ((dfLong - psInfo->dfULCornerX) / psInfo->dfPixelSizeX); iProfile = MAX(0,MIN(psInfo->nXSize-1,iProfile)); if( psCF->papanProfiles[iProfile] == NULL ) { psCF->papanProfiles[iProfile] = CPLMalloc(sizeof(GInt16) * psInfo->nYSize); for( i = 0; i < psInfo->nYSize; i++ ) psCF->papanProfiles[iProfile][i] = DTED_NODATA_VALUE; }/* -------------------------------------------------------------------- *//* Establish where we fit in the profile. *//* -------------------------------------------------------------------- */ iRow = (int) ((psInfo->dfULCornerY-dfLat) / psInfo->dfPixelSizeY); iRow = MAX(0,MIN(psInfo->nYSize-1,iRow)); psCF->papanProfiles[iProfile][iRow] = (GInt16) floor(dfElev+0.5); return TRUE;}/************************************************************************//* DTEDWritePt() *//* *//* Write a single point out, creating a new file if necessary *//* to hold it. *//************************************************************************/int DTEDWritePt( void *hStream, double dfLong, double dfLat, double dfElev ){ DTEDPtStream *psStream = (DTEDPtStream *) hStream; int i; DTEDInfo *psInfo; int bOnBoundary = FALSE;/* -------------------------------------------------------------------- *//* Determine if we are in a boundary region ... that is in the *//* area of the edge "pixel" that is shared with adjacent *//* tiles. *//* -------------------------------------------------------------------- */ if( (floor(dfLong - 0.5*psStream->dfPixelSize) != floor(dfLong + 0.5*psStream->dfPixelSize)) || (floor(dfLat - 0.5*psStream->dfPixelSize) != floor(dfLat + 0.5*psStream->dfPixelSize)) ) { bOnBoundary = TRUE; psStream->nLastFile = -1; }/* ==================================================================== *//* Handle case where the tile is not on a boundary. We only *//* need one output tile. *//* ==================================================================== *//* -------------------------------------------------------------------- *//* Is the last file used still applicable? *//* -------------------------------------------------------------------- */ if( !bOnBoundary ) { if( psStream->nLastFile != -1 ) { psInfo = psStream->pasCF[psStream->nLastFile].psInfo; if( dfLat > psInfo->dfULCornerY || dfLat < psInfo->dfULCornerY - 1.0 - psInfo->dfPixelSizeY || dfLong < psInfo->dfULCornerX || dfLong > psInfo->dfULCornerX + 1.0 + psInfo->dfPixelSizeX ) psStream->nLastFile = -1; }/* -------------------------------------------------------------------- *//* Search for the file to write to. *//* -------------------------------------------------------------------- */ for( i = 0; i < psStream->nOpenFiles && psStream->nLastFile == -1; i++ ) { psInfo = psStream->pasCF[i].psInfo; if( !(dfLat > psInfo->dfULCornerY || dfLat < psInfo->dfULCornerY - 1.0 - psInfo->dfPixelSizeY || dfLong < psInfo->dfULCornerX || dfLong > psInfo->dfULCornerX + 1.0 + psInfo->dfPixelSizeX) ) { psStream->nLastFile = i; } }/* -------------------------------------------------------------------- *//* If none found, create a new file. *//* -------------------------------------------------------------------- */ if( psStream->nLastFile == -1 )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -