File: rasterlite_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 (158 lines) | stat: -rw-r--r-- 4,930 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
/*****************************************************************************
 * 
 * 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 "rasterlite_featureset.hpp"

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

// boost
#include <boost/make_shared.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::query;
using mapnik::feature_factory;


rasterlite_featureset::rasterlite_featureset(void* dataset, rasterlite_query q)
   : dataset_(dataset),
     gquery_(q),
     first_(true)
{
    rasterliteSetBackgroundColor(dataset_, 255, 0, 255);
    rasterliteSetTransparentColor(dataset_, 255, 0, 255);
}

rasterlite_featureset::~rasterlite_featureset()
{
#ifdef MAPNIK_DEBUG
    std::clog << "Rasterlite Plugin: closing dataset" << std::endl;
#endif

    rasterliteClose(dataset_);
}

feature_ptr rasterlite_featureset::next()
{
    if (first_)
    {
        first_ = false;

        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 rasterlite_featureset::get_feature(mapnik::query const& q)
{
#ifdef MAPNIK_DEBUG
    std::clog << "Rasterlite Plugin: get_feature" << std::endl;
#endif

    feature_ptr feature(feature_factory::create(1));

    double x0, y0, x1, y1;
    rasterliteGetExtent (dataset_, &x0, &y0, &x1, &y1);

    box2d<double> raster_extent(x0,y0,x1,y1); 
    box2d<double> intersect = raster_extent.intersect(q.get_bbox());

    const int width = static_cast<int>(boost::get<0>(q.resolution()) * intersect.width() + 0.5);
    const int height = static_cast<int>(boost::get<0>(q.resolution()) * intersect.height() + 0.5);

    const double pixel_size = (intersect.width() >= intersect.height()) ?
        (intersect.width() / (double) width) : (intersect.height() / (double) height);

#ifdef MAPNIK_DEBUG         
    std::clog << "Rasterlite Plugin: Raster extent=" << raster_extent << std::endl;
    std::clog << "Rasterlite Plugin: View extent=" << q.get_bbox() << std::endl;
    std::clog << "Rasterlite Plugin: Intersect extent=" << intersect << std::endl;
    std::clog << "Rasterlite Plugin: Query resolution=" << boost::get<0>(q.resolution()) 
        << "," << boost::get<1>(q.resolution())  << std::endl;
    std::clog << "Rasterlite Plugin: Size=" << width << " " << height << std::endl;
    std::clog << "Rasterlite Plugin: Pixel Size=" << pixel_size << std::endl;
#endif

    if (width > 0 && height > 0)
    {
        int size = 0;
        void *raster = 0;

        if (rasterliteGetRawImageByRect(dataset_,
            intersect.minx(), intersect.miny(), intersect.maxx(), intersect.maxy(),
            pixel_size, width, height, GAIA_RGBA_ARRAY, &raster, &size) == RASTERLITE_OK)
        {
            if (size > 0)
            {
                mapnik::image_data_32 image(width, height);
                image.set(0xffffffff);

                unsigned char* raster_data = static_cast<unsigned char*>(raster);
                unsigned char* image_data = image.getBytes();

                memcpy (image_data, raster_data, size);

                feature->set_raster(boost::make_shared<mapnik::raster>(intersect,image));

                free (raster);

#ifdef MAPNIK_DEBUG         
                std::clog << "Rasterlite Plugin: done" << std::endl;
#endif
            }
            else
            {
#ifdef MAPNIK_DEBUG         
                std::clog << "Rasterlite Plugin: error=" << rasterliteGetLastError (dataset_) << std::endl;
#endif
            }
        }
      
        return feature;
    }
    return feature_ptr();
}


feature_ptr rasterlite_featureset::get_feature_at_point(mapnik::coord2d const& pt)
{
    return feature_ptr();
}