File: gdal_featureset.cpp

package info (click to toggle)
mapnik 2.0.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 35,496 kB
  • sloc: cpp: 91,793; python: 6,051; xml: 3,528; sh: 848; makefile: 70; lisp: 10
file content (487 lines) | stat: -rw-r--r-- 18,418 bytes parent folder | download
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*****************************************************************************
 * 
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2007 Artem Pavlenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/
//$Id$

// mapnik
#include <mapnik/feature_factory.hpp>

#include "gdal_featureset.hpp"
#include <gdal_priv.h>

// boost
#include <boost/format.hpp>

using mapnik::query;
using mapnik::coord2d;
using mapnik::box2d;
using mapnik::Feature;
using mapnik::feature_ptr;
using mapnik::CoordTransform;
using mapnik::geometry_type;
using mapnik::datasource_exception;
using mapnik::feature_factory;


gdal_featureset::gdal_featureset(GDALDataset & dataset, int band, gdal_query q, 
      mapnik::box2d<double> extent, double width, double height, int nbands, 
      double dx, double dy, double filter_factor)
    : dataset_(dataset),
      band_(band),
      gquery_(q),
      raster_extent_(extent),
      raster_width_(width),
      raster_height_(height),
      dx_(dx),
      dy_(dy),
      nbands_(nbands),
      filter_factor_(filter_factor),
      first_(true)
{
}

gdal_featureset::~gdal_featureset()
{
#ifdef MAPNIK_DEBUG
    std::clog << "GDAL Plugin: closing dataset = " << &dataset_ << std::endl;
#endif
    GDALClose(&dataset_);
}

feature_ptr gdal_featureset::next()
{
    if (first_)
    {
        first_ = false;
#ifdef MAPNIK_DEBUG
        std::clog << "GDAL Plugin: featureset, dataset = " << &dataset_ << std::endl;
#endif

        query *q = boost::get<query>(&gquery_);
        if(q) {
            return get_feature(*q);
        } else {
            coord2d *p = boost::get<coord2d>(&gquery_);
            if(p) {
                return get_feature_at_point(*p);
            }
        }
        // should never reach here
    }
    return feature_ptr();
}

feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
{
    feature_ptr feature(feature_factory::create(1));

    GDALRasterBand * red = 0;
    GDALRasterBand * green = 0;
    GDALRasterBand * blue = 0;
    GDALRasterBand * alpha = 0;
    GDALRasterBand * grey = 0; 
   
    /*
    double tr[6];
    dataset_.GetGeoTransform(tr);
    
    double dx = tr[1];
    double dy = tr[5];
    std::clog << "dx_: " << dx_ << " dx: " << dx << " dy_: " << dy_ << "dy: " << dy << "\n";
    */
          
    CoordTransform t(raster_width_,raster_height_,raster_extent_,0,0);
    box2d<double> intersect = raster_extent_.intersect(q.get_bbox());
    box2d<double> box = t.forward(intersect);

    //size of resized output pixel in source image domain
    double margin_x = 1.0/(fabs(dx_)*boost::get<0>(q.resolution()));
    double margin_y = 1.0/(fabs(dy_)*boost::get<1>(q.resolution()));
    if (margin_x < 1)
        margin_x = 1.0;
    if (margin_y < 1)
        margin_y = 1.0;
    //select minimum raster containing whole box
    int x_off = rint(box.minx() - margin_x);
    int y_off = rint(box.miny() - margin_y);
    int end_x = rint(box.maxx() + margin_x);
    int end_y = rint(box.maxy() + margin_y);
    //clip to available data
    if (x_off < 0)
        x_off = 0;
    if (y_off < 0)
        y_off = 0;
    if (end_x > (int)raster_width_)
        end_x = raster_width_;
    if (end_y > (int)raster_height_)
        end_y = raster_height_;
    int width = end_x - x_off;
    int height = end_y - y_off;
    // don't process almost invisible data
    if (box.width() < 0.5)
        width = 0;
    if (box.height() < 0.5)
        height = 0;
    //calculate actual box2d of returned raster
    box2d<double> feature_raster_extent(x_off, y_off, x_off+width, y_off+height); 
    intersect = t.backward(feature_raster_extent);
    
#ifdef MAPNIK_DEBUG         
    std::clog << "GDAL Plugin: Raster extent=" << raster_extent_ << std::endl;
    std::clog << "GDAL Plugin: View extent=" << intersect << std::endl;
    std::clog << "GDAL Plugin: Query resolution=" << boost::get<0>(q.resolution()) << "," << boost::get<1>(q.resolution()) << std::endl;
    std::clog << boost::format("GDAL Plugin: StartX=%d StartY=%d Width=%d Height=%d") % x_off % y_off % width % height << std::endl;
#endif
   
    if (width > 0 && height > 0)
    {
        double width_res = boost::get<0>(q.resolution());
        double height_res = boost::get<1>(q.resolution());
        int im_width = int(width_res * intersect.width() + 0.5);
        int im_height = int(height_res * intersect.height() + 0.5);
        
        // if layer-level filter_factor is set, apply it
        if (filter_factor_)
        {
            im_width *= filter_factor_;
            im_height *= filter_factor_;
#ifdef MAPNIK_DEBUG
            std::clog << "GDAL Plugin: applying layer filter_factor: " << filter_factor_ << "\n";
#endif
        }
        // otherwise respect symbolizer level factor applied to query, default of 1.0
        else
        {
            double sym_downsample_factor = q.get_filter_factor();
            im_width *= sym_downsample_factor;
            im_height *= sym_downsample_factor;
        }

        // case where we need to avoid upsampling so that the
        // image can be later scaled within raster_symbolizer 
        if (im_width >= width || im_height >= height)
        {
            im_width = width;
            im_height = height;
        }

        if (im_width > 0 && im_height > 0)
        {
            mapnik::image_data_32 image(im_width, im_height);
            image.set(0xffffffff); 
             
#ifdef MAPNIK_DEBUG
            std::clog << "GDAL Plugin: Image Size=(" << im_width << "," << im_height << ")" << std::endl;
            std::clog << "GDAL Plugin: Reading band " << band_ << std::endl;
#endif
            typedef std::vector<int,int> pallete;
        
            if (band_ > 0) // we are querying a single band
            {
                if (band_ > nbands_)
                    throw datasource_exception((boost::format("GDAL Plugin: '%d' is an invalid band, dataset only has '%d' bands\n") % band_ % nbands_).str());

                float *imageData = (float*)image.getBytes();
                GDALRasterBand * band = dataset_.GetRasterBand(band_);
                int hasNoData;
                double nodata = band->GetNoDataValue(&hasNoData);
                band->RasterIO(GF_Read, x_off, y_off, width, height,
                               imageData, image.width(), image.height(),
                               GDT_Float32, 0, 0);
    
                feature->set_raster(mapnik::raster_ptr(boost::make_shared<mapnik::raster>(intersect,image)));
                if (hasNoData)
                    feature->props()["NODATA"]=nodata;
            }
          
            else // working with all bands
            {
                for (int i = 0; i < nbands_; ++i)
                {
                    GDALRasterBand * band = dataset_.GetRasterBand(i+1);
             
#ifdef MAPNIK_DEBUG
                    get_overview_meta(band);  
#endif
    
                    GDALColorInterp color_interp = band->GetColorInterpretation();
                    switch (color_interp)
                    {
                    case GCI_RedBand:
                        red = band;
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Found red band" << std::endl;
#endif
                        break;
                    case GCI_GreenBand:
                        green = band;
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Found green band" << std::endl;
#endif
                        break;
                    case GCI_BlueBand:
                        blue = band;
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Found blue band" << std::endl;
#endif
                        break;
                    case GCI_AlphaBand:
                        alpha = band;
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Found alpha band" << std::endl;
#endif
                        break;
                    case GCI_GrayIndex:
                        grey = band;
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Found gray band" << std::endl;
#endif
                        break;
                    case GCI_PaletteIndex:
                    {
                        grey = band;
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Found gray band, and colortable..." << std::endl;
#endif
                        GDALColorTable *color_table = band->GetColorTable();
                
                        if ( color_table)
                        {
                            int count = color_table->GetColorEntryCount();
#ifdef MAPNIK_DEBUG
                            std::clog << "GDAL Plugin: Color Table count = " << count << std::endl;
#endif 
                            for (int j = 0; j < count; j++)
                            {
                                const GDALColorEntry *ce = color_table->GetColorEntry (j);
                                if (! ce) continue;
#ifdef MAPNIK_DEBUG
                                std::clog << "GDAL Plugin: Color entry RGB (" << ce->c1 << "," <<ce->c2 << "," << ce->c3 << ")" << std::endl; 
#endif
                            }
                        }
                        break;
                    }
                    case GCI_Undefined:
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Found undefined band (assumming gray band)" << std::endl;
#endif
                        grey = band;
                        break;
                    default:
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: band type unknown!" << std::endl;
#endif
                        break;
                    }
                }
    
                if (red && green && blue)
                {
#ifdef MAPNIK_DEBUG
                    std::clog << "GDAL Plugin: processing rgb bands..." << std::endl;
#endif
                    int hasNoData;
                    float nodata = red->GetNoDataValue(&hasNoData);
                    GDALColorTable *color_table = red->GetColorTable();

                    if (!alpha && hasNoData && !color_table)
                    {
                        // first read the data in and create an alpha channel from the nodata values
                        float *imageData = (float*)image.getBytes();
                        red->RasterIO(GF_Read, x_off, y_off, width, height,
                                imageData, image.width(), image.height(),
                                GDT_Float32, 0, 0);

                        int len = image.width() * image.height();

                        for (int i=0; i<len; ++i)
                        {
                            if (nodata == imageData[i])
                                *reinterpret_cast<unsigned *> (&imageData[i]) = 0;
                            else
                                *reinterpret_cast<unsigned *> (&imageData[i]) = 0xFFFFFFFF;
                        }

                    }

                    red->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 0,
                                  image.width(),image.height(),GDT_Byte,4,4*image.width());
                    green->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 1,
                                    image.width(),image.height(),GDT_Byte,4,4*image.width());
                    blue->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 2,
                                   image.width(),image.height(),GDT_Byte,4,4*image.width());

                }
                else if (grey)
                {
#ifdef MAPNIK_DEBUG
                    std::clog << "GDAL Plugin: processing gray band..." << std::endl;
#endif
                    int hasNoData;
                    float nodata = grey->GetNoDataValue(&hasNoData);
                    GDALColorTable *color_table = grey->GetColorTable();

                    if (hasNoData && !color_table)
                    {
#ifdef MAPNIK_DEBUG
                        std::clog << "\tno data value for layer: " << nodata << std::endl;
#endif
                        // first read the data in and create an alpha channel from the nodata values
                        float *imageData = (float*)image.getBytes();
                        grey->RasterIO(GF_Read, x_off, y_off, width, height,
                                imageData, image.width(), image.height(),
                                GDT_Float32, 0, 0);

                        int len = image.width() * image.height();

                        for (int i=0; i<len; ++i)
                        {
                            if (nodata == imageData[i])
                                *reinterpret_cast<unsigned *> (&imageData[i]) = 0;
                            else
                                *reinterpret_cast<unsigned *> (&imageData[i]) = 0xFFFFFFFF;
                        }
                    }

                    grey->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 0,
                                   image.width(),image.height(),GDT_Byte, 4, 4 * image.width());
                    grey->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 1,
                                   image.width(),image.height(),GDT_Byte, 4, 4 * image.width());
                    grey->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 2,
                                   image.width(),image.height(),GDT_Byte, 4, 4 * image.width());

                    if (color_table)
                    {
#ifdef MAPNIK_DEBUG
                        std::clog << "GDAL Plugin: Loading colour table..." << std::endl;
#endif
                        for (unsigned y = 0; y < image.height(); ++y)
                        {
                            unsigned int* row = image.getRow(y);
                            for (unsigned x = 0; x < image.width(); ++x)
                            {
                                unsigned value = row[x] & 0xff;
                                const GDALColorEntry *ce = color_table->GetColorEntry ( value );
                                if (ce ){
                                    // TODO - big endian support
                                    row[x] = (ce->c4 << 24)| (ce->c3 << 16) |  (ce->c2 << 8) | (ce->c1) ;
                                }
                            }
                        }
                    }
                }
    
                if (alpha)
                {
#ifdef MAPNIK_DEBUG
                    std::clog << "GDAL Plugin: processing alpha band..." << std::endl;
#endif
                    alpha->RasterIO(GF_Read,x_off,y_off,width,height,image.getBytes() + 3,
                                    image.width(),image.height(),GDT_Byte,4,4*image.width());
                }
    
                feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersect,image)));
            }
            return feature;
        }
    }
    return feature_ptr();
}


feature_ptr gdal_featureset::get_feature_at_point(mapnik::coord2d const& pt)
{
    if (band_ > 0)
    {
        unsigned raster_xsize = dataset_.GetRasterXSize();
        unsigned raster_ysize = dataset_.GetRasterYSize();

        double gt[6];
        dataset_.GetGeoTransform(gt);

        double det = gt[1]*gt[5] - gt[2]*gt[4];
        // subtract half a pixel width & height because gdal coord reference
        // is the top-left corner of a pixel, not the center.
        double X = pt.x - gt[0] - gt[1]/2;
        double Y = pt.y - gt[3] - gt[5]/2;
        double det1 = gt[1]*Y + gt[4]*X;
        double det2 = gt[2]*Y + gt[5]*X;
        unsigned x = det2/det, y = det1/det;

        if (x < raster_xsize && y < raster_ysize)
        {
#ifdef MAPNIK_DEBUG
            std::clog << boost::format("GDAL Plugin: pt.x=%f pt.y=%f") % pt.x % pt.y << std::endl;
            std::clog << boost::format("GDAL Plugin: x=%f y=%f") % x % y << std::endl;
#endif
            GDALRasterBand * band = dataset_.GetRasterBand(band_);
            int hasNoData;
            double nodata = band->GetNoDataValue(&hasNoData);
            double value;
            band->RasterIO(GF_Read, x, y, 1, 1, &value, 1, 1, GDT_Float64, 0, 0);

            if (! hasNoData || value != nodata)
            {
                // construct feature
                feature_ptr feature(new Feature(1));
                geometry_type * point = new geometry_type(mapnik::Point);
                point->move_to(pt.x, pt.y);
                feature->add_geometry(point);
                (*feature)["value"] = value;
                return feature;
            }
        }
    }
    return feature_ptr();
}

#ifdef MAPNIK_DEBUG
void gdal_featureset::get_overview_meta(GDALRasterBand * band)
{
    int band_overviews = band->GetOverviewCount();    
    if (band_overviews > 0)
    {
        std::clog << "GDAL Plugin: "<< band_overviews << " overviews found!" << std::endl;

        for (int b = 0; b < band_overviews; b++)
        {
            GDALRasterBand * overview = band->GetOverview (b);
            std::clog << boost::format("GDAL Plugin: Overview=%d Width=%d Height=%d")
                % b % overview->GetXSize() % overview->GetYSize() << std::endl;
        }
    }
    else
    {
        std::clog << "GDAL Plugin: No overviews found!" << std::endl; 
    }
    
    int bsx,bsy;
    double scale;
    band->GetBlockSize(&bsx,&bsy);
    scale = band->GetScale();

    std::clog << boost::format("GDAL Plugin: Block=%dx%d Scale=%f Type=%s Color=%s") % bsx % bsy % scale
        % GDALGetDataTypeName(band->GetRasterDataType())
        % GDALGetColorInterpretationName(band->GetColorInterpretation()) << std::endl; 
}
#endif