File: blxdataset.cpp

package info (click to toggle)
gdal 1.10.1%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,320 kB
  • ctags: 74,726
  • sloc: cpp: 677,199; ansic: 162,820; python: 13,816; cs: 11,163; sh: 10,446; java: 5,279; perl: 4,429; php: 2,971; xml: 1,500; yacc: 934; makefile: 494; sql: 112
file content (451 lines) | stat: -rw-r--r-- 14,466 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/******************************************************************************
 * $Id: blxdataset.cpp 23619 2011-12-20 22:40:19Z rouault $
 *
 * Project:  BLX Driver
 * Purpose:  GDAL BLX support.
 * Author:   Henrik Johansson, henrik@johome.net
 *
 ******************************************************************************
 * Copyright (c) 2006, Henrik Johansson <henrik@johome.net>
 *
 * 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 "gdal_pam.h"
#include "cpl_string.h"

CPL_CVSID("$Id: blxdataset.cpp 23619 2011-12-20 22:40:19Z rouault $");

CPL_C_START
#include <blx.h>
CPL_C_END

CPL_C_START
void    GDALRegister_BLX(void);
CPL_C_END

class BLXDataset : public GDALPamDataset
{
    friend class BLXRasterBand;

    CPLErr      GetGeoTransform( double * padfTransform );
    const char *GetProjectionRef();

    blxcontext_t *blxcontext;

    int	nOverviewCount;
    int	bIsOverview;
    BLXDataset *papoOverviewDS[BLX_OVERVIEWLEVELS];

  public:
    BLXDataset();
    ~BLXDataset();

    static GDALDataset *Open( GDALOpenInfo * );
};

class BLXRasterBand : public GDALPamRasterBand
{
    int overviewLevel;
  public:
    BLXRasterBand( BLXDataset *, int, int overviewLevel=0 );

    virtual double  GetNoDataValue( int *pbSuccess = NULL );
    virtual GDALColorInterp GetColorInterpretation(void);
    virtual int GetOverviewCount();
    virtual GDALRasterBand *GetOverview( int );

    virtual CPLErr IReadBlock( int, int, void * );
};

GDALDataset *BLXDataset::Open( GDALOpenInfo * poOpenInfo )

{
    // -------------------------------------------------------------------- 
    //      First that the header looks like a BLX header
    // -------------------------------------------------------------------- 
    if( poOpenInfo->fp == NULL || poOpenInfo->nHeaderBytes < 102 )
        return NULL;

    if(!blx_checkheader((char *)poOpenInfo->pabyHeader))
	return NULL;
    
    // -------------------------------------------------------------------- 
    //      Create a corresponding GDALDataset.                             
    // -------------------------------------------------------------------- 
    BLXDataset 	*poDS;

    poDS = new BLXDataset();

    // -------------------------------------------------------------------- 
    //      Open BLX file
    // -------------------------------------------------------------------- 
    poDS->blxcontext = blx_create_context();
    if(poDS->blxcontext==NULL)
    {
        delete poDS;
        return NULL;
    }
    if (blxopen(poDS->blxcontext, poOpenInfo->pszFilename, "rb") != 0)
    {
        delete poDS;
        return NULL;
    }

    if ((poDS->blxcontext->cell_xsize % (1 << (1+BLX_OVERVIEWLEVELS))) != 0 ||
        (poDS->blxcontext->cell_ysize % (1 << (1+BLX_OVERVIEWLEVELS))) != 0)
    {
        delete poDS;
        return NULL;
    }

    // Update dataset header from BLX context
    poDS->nRasterXSize = poDS->blxcontext->xsize;
    poDS->nRasterYSize = poDS->blxcontext->ysize;

    // -------------------------------------------------------------------- 
    //      Create band information objects.                                
    // -------------------------------------------------------------------- 
    poDS->nBands = 1;
    poDS->SetBand( 1, new BLXRasterBand( poDS, 1 ));

    // Create overview bands
    poDS->nOverviewCount = BLX_OVERVIEWLEVELS;	
    for(int i=0; i < poDS->nOverviewCount; i++) {
	poDS->papoOverviewDS[i] = new BLXDataset();
	poDS->papoOverviewDS[i]->blxcontext = poDS->blxcontext;
	poDS->papoOverviewDS[i]->bIsOverview = TRUE;
	poDS->papoOverviewDS[i]->nRasterXSize = poDS->nRasterXSize >> (i+1);
	poDS->papoOverviewDS[i]->nRasterYSize = poDS->nRasterYSize >> (i+1);
	poDS->nBands = 1;
	poDS->papoOverviewDS[i]->SetBand(1, new BLXRasterBand( poDS->papoOverviewDS[i], 1, i+1));
    }
    
/* -------------------------------------------------------------------- */
/*      Confirm the requested access is supported.                      */
/* -------------------------------------------------------------------- */
    if( poOpenInfo->eAccess == GA_Update )
    {
        delete poDS;
        CPLError( CE_Failure, CPLE_NotSupported, 
                  "The BLX driver does not support update access to existing"
                  " datasets.\n" );
        return NULL;
    }

/* -------------------------------------------------------------------- */
/*      Initialize any PAM information.                                 */
/* -------------------------------------------------------------------- */
    poDS->SetDescription( poOpenInfo->pszFilename );
    poDS->TryLoadXML();

    return( poDS );
}

BLXDataset::BLXDataset() {
    blxcontext = NULL;
    bIsOverview = FALSE;
    nOverviewCount = 0;
    
    for(int i=0; i < nOverviewCount; i++)
	papoOverviewDS[i]=NULL;
}

BLXDataset::~BLXDataset() {
    if(!bIsOverview) {
	if(blxcontext) {
	    blxclose(blxcontext);
	    blx_free_context(blxcontext);
	}
	for(int i=0; i < nOverviewCount; i++)
	    if(papoOverviewDS[i])
		delete papoOverviewDS[i];
    }
}


CPLErr BLXDataset::GetGeoTransform( double * padfTransform )

{
    padfTransform[0] = blxcontext->lon;
    padfTransform[1] = blxcontext->pixelsize_lon;
    padfTransform[2] = 0.0;
    padfTransform[3] = blxcontext->lat;
    padfTransform[4] = 0.0;
    padfTransform[5] = blxcontext->pixelsize_lat;

    return CE_None;
}

const char *BLXDataset::GetProjectionRef()
{
    return( "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]]" );
}

BLXRasterBand::BLXRasterBand( BLXDataset *poDS, int nBand, int overviewLevel )

{
    BLXDataset *poGDS = (BLXDataset *) poDS;

    this->poDS = poDS;
    this->nBand = nBand;
    this->overviewLevel = overviewLevel;
    
    eDataType = GDT_Int16;

    nBlockXSize = poGDS->blxcontext->cell_xsize >> overviewLevel;
    nBlockYSize = poGDS->blxcontext->cell_ysize >> overviewLevel;
}

int BLXRasterBand::GetOverviewCount()
{
    BLXDataset *poGDS = (BLXDataset *) poDS;
    
    return poGDS->nOverviewCount;
}

GDALRasterBand *BLXRasterBand::GetOverview( int i )
{
    BLXDataset        *poGDS = (BLXDataset *) poDS;

    if( i < 0 || i >= poGDS->nOverviewCount )
	return NULL;
    else
    	return poGDS->papoOverviewDS[i]->GetRasterBand(nBand);
}

CPLErr BLXRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff,
                                  void * pImage )

{
    BLXDataset *poGDS = (BLXDataset *) poDS;

    if(blx_readcell(poGDS->blxcontext, nBlockYOff, nBlockXOff, (short *)pImage, nBlockXSize*nBlockYSize*2, overviewLevel) == NULL) {
	CPLError(CE_Failure, CPLE_AppDefined,
		 "Failed to read BLX cell");
	return CE_Failure;
    }

    return CE_None;
}

double BLXRasterBand::GetNoDataValue( int * pbSuccess )
{
    if (pbSuccess)
        *pbSuccess = TRUE;
    return BLX_UNDEF;
}

GDALColorInterp BLXRasterBand::GetColorInterpretation(void) {
    return GCI_GrayIndex;
}

/* TODO: check if georeference is the same as for BLX files, WGS84
*/
static GDALDataset *
BLXCreateCopy( const char * pszFilename, GDALDataset *poSrcDS, 
                int bStrict, char ** papszOptions, 
                GDALProgressFunc pfnProgress, void * pProgressData )

{
    int  nBands = poSrcDS->GetRasterCount();
    int  nXSize = poSrcDS->GetRasterXSize();
    int  nYSize = poSrcDS->GetRasterYSize();
    
    int zscale = 1;
    int fillundef = 1, fillundefval = 0;
    int endian = LITTLEENDIAN;

// -------------------------------------------------------------------- 
//      Some rudimentary checks                                    
// -------------------------------------------------------------------- 
    if( nBands != 1 )
    {
        CPLError( CE_Failure, CPLE_NotSupported, 
                  "BLX driver doesn't support %d bands.  Must be 1 (grey) ",
                  nBands );
        return NULL;
    }

    if( poSrcDS->GetRasterBand(1)->GetRasterDataType() != GDT_Int16 && bStrict )
    {
        CPLError( CE_Failure, CPLE_NotSupported, 
                  "BLX driver doesn't support data type %s. "
                  "Only 16 bit byte bands supported.\n", 
                  GDALGetDataTypeName( 
                      poSrcDS->GetRasterBand(1)->GetRasterDataType()) );

        return NULL;
    }

    if( (nXSize % 128 != 0) || (nYSize % 128 != 0) ) {
        CPLError( CE_Failure, CPLE_NotSupported, 
                  "BLX driver doesn't support dimensions that are not a multiple of 128.\n");

        return NULL;
    }
    
// -------------------------------------------------------------------- 
//      What options has the user selected?                             
// -------------------------------------------------------------------- 
    if( CSLFetchNameValue(papszOptions,"ZSCALE") != NULL ) {
        zscale = atoi(CSLFetchNameValue(papszOptions,"ZSCALE"));
        if( zscale < 1 ) {
            CPLError( CE_Failure, CPLE_IllegalArg,
                      "ZSCALE=%s is not a legal value in the range >= 1.",
                      CSLFetchNameValue(papszOptions,"ZSCALE") );
            return NULL;
        }
    }

    if( CSLFetchNameValue(papszOptions,"FILLUNDEF") != NULL 
                && EQUAL(CSLFetchNameValue(papszOptions,"FILLUNDEF"),"NO") )
	fillundef = 0;
    else	
	fillundef = 1;

    if( CSLFetchNameValue(papszOptions,"FILLUNDEFVAL") != NULL ) {
        fillundefval = atoi(CSLFetchNameValue(papszOptions,"FILLUNDEFVAL"));
        if( (fillundefval < -32768) || (fillundefval > 32767) ) {
            CPLError( CE_Failure, CPLE_IllegalArg,
                      "FILLUNDEFVAL=%s is not a legal value in the range -32768, 32767.",
                      CSLFetchNameValue(papszOptions,"FILLUNDEFVAL") );
            return NULL;
        }
    }
    if( CSLFetchNameValue(papszOptions,"BIGENDIAN") != NULL 
	&& !EQUAL(CSLFetchNameValue(papszOptions,"BIGENDIAN"),"NO") )
	endian = BIGENDIAN;
	
    

// -------------------------------------------------------------------- 
//      Create the dataset.                                             
// -------------------------------------------------------------------- 
    blxcontext_t *ctx;

    // Create a BLX context
    ctx = blx_create_context();

    // Setup BLX parameters
    ctx->cell_rows = nYSize / ctx->cell_ysize;
    ctx->cell_cols = nXSize / ctx->cell_xsize;
    ctx->zscale = zscale;
    ctx->fillundef = fillundef;
    ctx->fillundefval = fillundefval;
    ctx->endian = endian;

    if(blxopen(ctx, pszFilename, "wb")) {
        CPLError( CE_Failure, CPLE_OpenFailed, 
                  "Unable to create blx file %s.\n", 
                  pszFilename );
        blx_free_context(ctx);
        return NULL;
    }

// -------------------------------------------------------------------- 
//      Loop over image, copying image data.                            
// -------------------------------------------------------------------- 
    GInt16     *pabyTile;
    CPLErr      eErr=CE_None;

    pabyTile = (GInt16 *) VSIMalloc( sizeof(GInt16)*ctx->cell_xsize*ctx->cell_ysize );
    if (pabyTile == NULL)
    {
        CPLError( CE_Failure, CPLE_OutOfMemory, "Out of memory");
        blxclose(ctx);
        blx_free_context(ctx);
        return NULL;
    }

    if( !pfnProgress( 0.0, NULL, pProgressData ) )
        eErr = CE_Failure;

    for(int i=0; (i < ctx->cell_rows) && (eErr == CE_None); i++)
	for(int j=0; j < ctx->cell_cols; j++) {
	    blxdata *celldata;
	    GDALRasterBand * poBand = poSrcDS->GetRasterBand( 1 );
	    eErr = poBand->RasterIO( GF_Read, j*ctx->cell_xsize, i*ctx->cell_ysize, 
				     ctx->cell_xsize, ctx->cell_ysize, 
				     pabyTile, ctx->cell_xsize, ctx->cell_ysize, GDT_Int16,
				     0, 0 );
	    if(eErr >= CE_Failure) 
	       break;
	    celldata = pabyTile;
	    if (blx_writecell(ctx, celldata, i, j) != 0)
            {
                eErr = CE_Failure;
                break;
            }

            if ( ! pfnProgress( 1.0 * (i * ctx->cell_cols + j) / (ctx->cell_rows * ctx->cell_cols), NULL, pProgressData ))
            {
                eErr = CE_Failure;
                break;
            }
    }

    pfnProgress( 1.0, NULL, pProgressData );

    CPLFree( pabyTile );

    double        adfGeoTransform[6];
    if( poSrcDS->GetGeoTransform( adfGeoTransform ) == CE_None )
    {
	ctx->lon = adfGeoTransform[0];
	ctx->lat = adfGeoTransform[3];
	ctx->pixelsize_lon = adfGeoTransform[1];	
	ctx->pixelsize_lat = adfGeoTransform[5];	
    }

    blxclose(ctx);
    blx_free_context(ctx);

    if (eErr == CE_None)
        return (GDALDataset *) GDALOpen( pszFilename, GA_ReadOnly );
    else
        return NULL;
}


void GDALRegister_BLX()

{
    GDALDriver  *poDriver;

    if( GDALGetDriverByName( "BLX" ) == NULL )
    {
        poDriver = new GDALDriver();
        
        poDriver->SetDescription( "BLX" );
        poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, 
                                   "Magellan topo (.blx)" );
        poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, 
                                   "frmt_various.html#BLX" );
        poDriver->SetMetadataItem( GDAL_DMD_EXTENSION, "blx" );

        poDriver->pfnOpen = BLXDataset::Open;
	poDriver->pfnCreateCopy = BLXCreateCopy;

        GetGDALDriverManager()->RegisterDriver( poDriver );
    }
}