~ubuntu-branches/ubuntu/trusty/mapnik/trusty-proposed

« back to all changes in this revision

Viewing changes to plugins/input/ogr/ogr_index_featureset.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2009-08-27 00:28:37 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090827002837-ztqzfg2rmclfh4i9
Tags: 0.6.1-0ubuntu1
* New upstream release.
* Change usr/lib to usr/lib* to enable build on 64 bits systems due to new
  configuration in SConstruct in :
  - debian/libmapnik-dev.install
  - debian/libmapnik0.6.install
  - debian/mapnik-plugin-base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****************************************************************************
 
2
 * 
 
3
 * This file is part of Mapnik (c++ mapping toolkit)
 
4
 *
 
5
 * Copyright (C) 2007 Artem Pavlenko
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 *
 
21
 *****************************************************************************/
 
22
//$Id$
 
23
 
 
24
// mapnik
 
25
#include <mapnik/global.hpp>
 
26
#include <mapnik/datasource.hpp>
 
27
#include <mapnik/envelope.hpp>
 
28
#include <mapnik/geometry.hpp>
 
29
#include <mapnik/feature.hpp>
 
30
#include <mapnik/feature_layer_desc.hpp>
 
31
#include <mapnik/wkb.hpp>
 
32
#include <mapnik/unicode.hpp>
 
33
 
 
34
// boost
 
35
#include <boost/iostreams/stream.hpp>
 
36
#include <boost/iostreams/device/file.hpp>
 
37
#include <boost/iostreams/device/mapped_file.hpp>
 
38
 
 
39
// ogr
 
40
#include "ogr_index_featureset.hpp"
 
41
#include "ogr_converter.hpp"
 
42
#include "ogr_index.hpp"
 
43
#include "ogr_feature_ptr.hpp"
 
44
 
 
45
using std::clog;
 
46
using std::endl;
 
47
 
 
48
using mapnik::query;
 
49
using mapnik::Envelope;
 
50
using mapnik::CoordTransform;
 
51
using mapnik::Feature;
 
52
using mapnik::feature_ptr;
 
53
using mapnik::geometry_utils;
 
54
using mapnik::transcoder;
 
55
 
 
56
using namespace boost::iostreams;
 
57
 
 
58
 
 
59
template <typename filterT>
 
60
ogr_index_featureset<filterT>::ogr_index_featureset(OGRDataSource & dataset,
 
61
                                                    OGRLayer & layer,
 
62
                                                    const filterT& filter,
 
63
                                                    const std::string& index_file,
 
64
                                                    const std::string& encoding,
 
65
                                                    const bool multiple_geometries)
 
66
   : dataset_(dataset),
 
67
     layer_(layer),
 
68
     layerdef_(layer.GetLayerDefn()),
 
69
     filter_(filter),
 
70
     tr_(new transcoder(encoding)),
 
71
     fidcolumn_(layer_.GetFIDColumn ()),
 
72
     multiple_geometries_(multiple_geometries),
 
73
     count_(0)
 
74
{
 
75
    stream<mapped_file_source> file (index_file);
 
76
    if (file)
 
77
    {
 
78
       ogr_index<filterT,stream<mapped_file_source> >::query(filter,file,ids_);
 
79
       file.close();
 
80
    }
 
81
    std::sort(ids_.begin(),ids_.end());    
 
82
    
 
83
#ifdef MAPNIK_DEBUG
 
84
    std::clog<< "query size=" << ids_.size() << "\n";
 
85
#endif
 
86
 
 
87
    itr_ = ids_.begin();
 
88
 
 
89
    // reset reading            
 
90
    layer_.ResetReading();
 
91
}
 
92
 
 
93
template <typename filterT>
 
94
ogr_index_featureset<filterT>::~ogr_index_featureset() {}
 
95
 
 
96
template <typename filterT>
 
97
feature_ptr ogr_index_featureset<filterT>::next()
 
98
{
 
99
    if (itr_ != ids_.end())
 
100
    {
 
101
       int pos = *itr_++;
 
102
       layer_.SetNextByIndex (pos);
 
103
 
 
104
       ogr_feature_ptr feat (layer_.GetNextFeature());
 
105
       if ((*feat) != NULL)
 
106
       {
 
107
          OGRGeometry* geom=(*feat)->GetGeometryRef();
 
108
          if (!geom->IsEmpty())
 
109
          {
 
110
              feature_ptr feature(new Feature((*feat)->GetFID()));
 
111
 
 
112
              ogr_converter::convert_geometry (geom, feature, multiple_geometries_);
 
113
              ++count_;
 
114
 
 
115
              int fld_count = layerdef_->GetFieldCount();
 
116
              for (int i = 0; i < fld_count; i++)
 
117
              {
 
118
                  OGRFieldDefn* fld = layerdef_->GetFieldDefn (i);
 
119
                  OGRFieldType type_oid = fld->GetType ();
 
120
                  std::string fld_name = fld->GetNameRef ();
 
121
 
 
122
                  switch (type_oid)
 
123
                  {
 
124
                   case OFTInteger:
 
125
                   {
 
126
                       boost::put(*feature,fld_name,(*feat)->GetFieldAsInteger (i));
 
127
                       break;
 
128
                   }
 
129
 
 
130
                   case OFTReal:
 
131
                   {
 
132
                       boost::put(*feature,fld_name,(*feat)->GetFieldAsDouble (i));
 
133
                       break;
 
134
                   }
 
135
                           
 
136
                   case OFTString:
 
137
                   case OFTWideString:     // deprecated !
 
138
                   {
 
139
                       UnicodeString ustr = tr_->transcode((*feat)->GetFieldAsString (i));
 
140
                       boost::put(*feature,fld_name,ustr);
 
141
                       break;
 
142
                   }
 
143
 
 
144
                   case OFTIntegerList:
 
145
                   case OFTRealList:
 
146
                   case OFTStringList:
 
147
                   case OFTWideStringList: // deprecated !
 
148
                   {
 
149
#ifdef MAPNIK_DEBUG
 
150
                       clog << "unhandled type_oid=" << type_oid << endl;
 
151
#endif
 
152
                       break;
 
153
                   }
 
154
 
 
155
                   case OFTBinary:
 
156
                   {
 
157
#ifdef MAPNIK_DEBUG
 
158
                       clog << "unhandled type_oid=" << type_oid << endl;
 
159
#endif
 
160
                       //boost::put(*feature,name,feat->GetFieldAsBinary (i, size));
 
161
                       break;
 
162
                   }
 
163
                       
 
164
                   case OFTDate:
 
165
                   case OFTTime:
 
166
                   case OFTDateTime:       // unhandled !
 
167
                   {
 
168
#ifdef MAPNIK_DEBUG
 
169
                       clog << "unhandled type_oid=" << type_oid << endl;
 
170
#endif
 
171
                       break;
 
172
                   }
 
173
 
 
174
                   default: // unknown
 
175
                   {
 
176
#ifdef MAPNIK_DEBUG
 
177
                       clog << "unknown type_oid=" << type_oid << endl;
 
178
#endif
 
179
                       break;
 
180
                   }
 
181
                  }
 
182
              }
 
183
          
 
184
              return feature;
 
185
          }
 
186
       }
 
187
   }
 
188
 
 
189
#ifdef MAPNIK_DEBUG
 
190
   clog << count_ << " features" << endl;
 
191
#endif
 
192
   return feature_ptr();
 
193
}
 
194
 
 
195
template class ogr_index_featureset<mapnik::filter_in_box>;
 
196
template class ogr_index_featureset<mapnik::filter_at_point>;
 
197