File: gdal_featureset.cpp

package info (click to toggle)
mapnik 0.5.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,136 kB
  • ctags: 14,550
  • sloc: cpp: 68,887; python: 24,895; xml: 1,534; makefile: 503; sh: 79
file content (164 lines) | stat: -rw-r--r-- 5,973 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
/*****************************************************************************
 * 
 * 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$

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

using mapnik::query;
using mapnik::Envelope;
using mapnik::Feature;
using mapnik::feature_ptr;
using mapnik::CoordTransform;

gdal_featureset::gdal_featureset(GDALDataset & dataset, query const& q)
   : dataset_(dataset),
     query_extent_(q.get_bbox()),
     first_(true) {}

gdal_featureset::~gdal_featureset() {}

feature_ptr gdal_featureset::next()
{
   if (first_)
   {
      first_ = false;
      feature_ptr feature(new Feature(1));
      GDALRasterBand * red = 0;
      GDALRasterBand * green = 0;
      GDALRasterBand * blue = 0;
      GDALRasterBand * alpha = 0;
      GDALRasterBand * grey = 0; 
      for (int i = 0; i < dataset_.GetRasterCount() ;++i)
      {
         GDALRasterBand * band = dataset_.GetRasterBand(i+1);
         //if (band->GetOverviewCount() > 0)
         //{
         //  band = band->GetOverview(0);
         //}
         
#ifdef MAPNIK_DEBUG         
         int bsx,bsy;
         band->GetBlockSize(&bsx,&bsy);
         std::cout << boost::format("Block=%dx%d Type=%s Color=%s \n") % bsx % bsy
            % GDALGetDataTypeName(band->GetRasterDataType())
            % GDALGetColorInterpretationName(band->GetColorInterpretation());   
#endif
         GDALColorInterp color_interp = band->GetColorInterpretation();
         switch (color_interp)
         {
            case GCI_RedBand:
               red = band;
               break;
            case GCI_GreenBand:
               green = band;
               break;
            case GCI_BlueBand:
               blue = band;
               break;
            case GCI_AlphaBand:
               alpha = band;
               break;
            case GCI_GrayIndex:
               grey = band;
               break;
            case GCI_PaletteIndex:
            {	
               grey = band;
               GDALColorTable *color_table = band->GetColorTable();
				
               if ( color_table)
               {
                  int count = color_table->GetColorEntryCount();
#ifdef MAPNIK_DEBUG
                  std::cout << "Color Table count = " << count << "\n";
#endif 
                  for ( int i = 0; i < count; i++ )
                  {
                     const GDALColorEntry *ce = color_table->GetColorEntry ( i );
                     if (!ce ) continue;
#ifdef MAPNIK_DEBUG
                     std::cout << "color entry RGB (" << ce->c1 <<"," <<ce->c2 << "," << ce->c3 << ")\n"; 
#endif
                  }
               }	
               break;
            }
            case GCI_Undefined:
               grey = band;
               break;	
            default:
               break;
               ;
         }
      }
      
      unsigned raster_xsize = dataset_.GetRasterXSize();
      unsigned raster_ysize = dataset_.GetRasterYSize();
      double tr[6];
      dataset_.GetGeoTransform(tr);
      double x0 = tr[0];
      double y0 = tr[3];
      double x1 = tr[0] + raster_xsize * tr[1] + raster_ysize * tr[2];
      double y1 = tr[3] + raster_xsize * tr[4] + raster_ysize * tr[5];
      Envelope<double> raster_extent(x0,y0,x1,y1);
      CoordTransform t (raster_xsize,raster_ysize,raster_extent,0,0);
      Envelope<double> intersection = raster_extent.intersect(query_extent_);
      Envelope<double> box = t.forward(intersection);
      
      int start_x = int(box.minx());
      int start_y = int(box.miny());
      int width = int(box.width());
      int height = int(box.height());
      
      if (width > 0 && height > 0)
      {
         mapnik::ImageData32 image(width,height);
         image.set(0xffffffff);
         
         if (red && green && blue)
         {
            red->RasterIO  (GF_Read,start_x,start_y,width,height, image.getBytes() + 0, width,height, GDT_Byte,4,4*width);
            green->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 1, width,height, GDT_Byte,4,4*width);
            blue->RasterIO (GF_Read,start_x,start_y,width,height, image.getBytes() + 2, width,height, GDT_Byte,4,4*width);
            if (alpha)
            {
               alpha->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 3, width,height, GDT_Byte,4,4*width);
            }
         }
         else if (grey)
         {
            grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 0, width, height,GDT_Byte,4,4*width);
            grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 1, width, height,GDT_Byte,4,4*width);
            grey->RasterIO(GF_Read,start_x,start_y,width,height,image.getBytes() + 2, width, height,GDT_Byte,4,4*width);
            
            if (alpha)
            {
               alpha->RasterIO(GF_Read,start_x,start_y,width,height, image.getBytes() + 3, width,height, GDT_Byte,4,4*width);
            }
         }
         feature->set_raster(mapnik::raster_ptr(new mapnik::raster(intersection,image)));
         return feature;
      }
   }
   return feature_ptr();
}