-
Notifications
You must be signed in to change notification settings - Fork 826
/
map.cpp
418 lines (367 loc) · 11.6 KB
/
map.cpp
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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2006 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: map.cpp 17 2005-03-08 23:58:43Z pavlenko $,
#include <mapnik/style.hpp>
#include <mapnik/datasource.hpp>
#include <mapnik/projection.hpp>
#include <mapnik/layer.hpp>
#include <mapnik/filter_featureset.hpp>
#include <mapnik/hit_test_filter.hpp>
#include <mapnik/map.hpp>
namespace mapnik
{
Map::Map()
: width_(400),
height_(400),
srs_("+proj=latlong +datum=WGS84") {}
Map::Map(int width,int height, std::string const& srs)
: width_(width),
height_(height),
srs_(srs),
background_(Color(255,255,255)) {}
Map::Map(const Map& rhs)
: width_(rhs.width_),
height_(rhs.height_),
srs_(rhs.srs_),
background_(rhs.background_),
styles_(rhs.styles_),
layers_(rhs.layers_),
currentExtent_(rhs.currentExtent_) {}
Map& Map::operator=(const Map& rhs)
{
if (this==&rhs) return *this;
width_=rhs.width_;
height_=rhs.height_;
srs_=rhs.srs_;
background_=rhs.background_;
styles_=rhs.styles_;
layers_=rhs.layers_;
return *this;
}
Map::style_iterator Map::begin_styles()
{
return styles_.begin();
}
Map::style_iterator Map::end_styles()
{
return styles_.end();
}
Map::const_style_iterator Map::begin_styles() const
{
return styles_.begin();
}
Map::const_style_iterator Map::end_styles() const
{
return styles_.end();
}
bool Map::insert_style(std::string const& name,feature_type_style const& style)
{
return styles_.insert(make_pair(name,style)).second;
}
void Map::remove_style(std::string const& name)
{
styles_.erase(name);
}
feature_type_style const& Map::find_style(std::string const& name) const
{
std::map<std::string,feature_type_style>::const_iterator itr = styles_.find(name);
if (itr!=styles_.end())
return itr->second;
static feature_type_style default_style;
return default_style;
}
size_t Map::layerCount() const
{
return layers_.size();
}
void Map::addLayer(const Layer& l)
{
layers_.push_back(l);
}
void Map::removeLayer(size_t index)
{
layers_.erase(layers_.begin()+index);
}
void Map::remove_all()
{
layers_.clear();
styles_.clear();
}
const Layer& Map::getLayer(size_t index) const
{
return layers_[index];
}
Layer& Map::getLayer(size_t index)
{
return layers_[index];
}
std::vector<Layer> const& Map::layers() const
{
return layers_;
}
std::vector<Layer> & Map::layers()
{
return layers_;
}
unsigned Map::getWidth() const
{
return width_;
}
unsigned Map::getHeight() const
{
return height_;
}
void Map::setWidth(unsigned width)
{
if (width >= MIN_MAPSIZE && width <= MAX_MAPSIZE)
{
width_=width;
fixAspectRatio();
}
}
void Map::setHeight(unsigned height)
{
if (height >= MIN_MAPSIZE && height <= MAX_MAPSIZE)
{
height_=height;
fixAspectRatio();
}
}
void Map::resize(unsigned width,unsigned height)
{
if (width >= MIN_MAPSIZE && width <= MAX_MAPSIZE &&
height >= MIN_MAPSIZE && height <= MAX_MAPSIZE)
{
width_=width;
height_=height;
fixAspectRatio();
}
}
std::string const& Map::srs() const
{
return srs_;
}
void Map::set_srs(std::string const& srs)
{
srs_ = srs;
}
void Map::setBackground(const Color& c)
{
background_=c;
}
const Color& Map::getBackground() const
{
return background_;
}
void Map::zoom(double factor)
{
coord2d center = currentExtent_.center();
double w = factor * currentExtent_.width();
double h = factor * currentExtent_.height();
currentExtent_ = Envelope<double>(center.x - 0.5 * w,
center.y - 0.5 * h,
center.x + 0.5 * w,
center.y + 0.5 * h);
fixAspectRatio();
}
void Map::zoom_all()
{
try
{
projection proj0(srs_);
Envelope<double> ext;
bool first = true;
std::vector<Layer>::const_iterator itr = layers_.begin();
std::vector<Layer>::const_iterator end = layers_.end();
while (itr != end)
{
std::string const& layer_srs = itr->srs();
projection proj1(layer_srs);
proj_transform prj_trans(proj0,proj1);
Envelope<double> layerExt = itr->envelope();
double x0 = layerExt.minx();
double y0 = layerExt.miny();
double z0 = 0.0;
double x1 = layerExt.maxx();
double y1 = layerExt.maxy();
double z1 = 0.0;
prj_trans.backward(x0,y0,z0);
prj_trans.backward(x1,y1,z1);
Envelope<double> layerExt2(x0,y0,x1,y1);
#ifdef MAPNIK_DEBUG
std::clog << " layer1 - > " << layerExt << "\n";
std::clog << " layer2 - > " << layerExt2 << "\n";
#endif
if (first)
{
ext = layerExt2;
first = false;
}
else
{
ext.expand_to_include(layerExt2);
}
++itr;
}
zoomToBox(ext);
}
catch (proj_init_error & ex)
{
std::clog << ex.what() << '\n';
}
}
void Map::zoomToBox(const Envelope<double> &box)
{
currentExtent_=box;
fixAspectRatio();
}
void Map::fixAspectRatio()
{
double ratio1 = (double) width_ / (double) height_;
double ratio2 = currentExtent_.width() / currentExtent_.height();
if (ratio2 > ratio1)
{
currentExtent_.height(currentExtent_.width() / ratio1);
}
else if (ratio2 < ratio1)
{
currentExtent_.width(currentExtent_.height() * ratio1);
}
}
const Envelope<double>& Map::getCurrentExtent() const
{
return currentExtent_;
}
void Map::pan(int x,int y)
{
int dx = x - int(0.5 * width_);
int dy = int(0.5 * height_) - y;
double s = width_/currentExtent_.width();
double minx = currentExtent_.minx() + dx/s;
double maxx = currentExtent_.maxx() + dx/s;
double miny = currentExtent_.miny() + dy/s;
double maxy = currentExtent_.maxy() + dy/s;
currentExtent_.init(minx,miny,maxx,maxy);
}
void Map::pan_and_zoom(int x,int y,double factor)
{
pan(x,y);
zoom(factor);
}
double Map::scale() const
{
if (width_>0)
return currentExtent_.width()/width_;
return currentExtent_.width();
}
CoordTransform Map::view_transform() const
{
return CoordTransform(width_,height_,currentExtent_);
}
featureset_ptr Map::query_point(unsigned index, double lat, double lon) const
{
if ( index< layers_.size())
{
mapnik::Layer const& layer = layers_[index];
try
{
double x = lon;
double y = lat;
double z = 0;
mapnik::projection dest(srs_);
if (!dest.is_geographic())
{
dest.forward(x,y);
}
mapnik::projection source(layer.srs());
proj_transform prj_trans(source,dest);
prj_trans.backward(x,y,z);
double minx = currentExtent_.minx();
double miny = currentExtent_.miny();
double maxx = currentExtent_.maxx();
double maxy = currentExtent_.maxy();
prj_trans.backward(minx,miny,z);
prj_trans.backward(maxx,maxy,z);
double tol = (maxx - minx) / width_ * 3;
mapnik::datasource_ptr ds = layer.datasource();
if (ds)
{
#ifdef MAPNIK_DEBUG
std::clog << " query at point tol = " << tol << " (" << x << "," << y << ")\n";
#endif
featureset_ptr fs = ds->features_at_point(mapnik::coord2d(x,y));
if (fs)
return featureset_ptr(new filter_featureset<hit_test_filter>(fs,hit_test_filter(x,y,tol)));
}
}
catch (...)
{
#ifdef MAPNIK_DEBUG
std::clog << "exception caught in \"query_map_point\"\n";
#endif
}
}
return featureset_ptr();
}
featureset_ptr Map::query_map_point(unsigned index, double x, double y) const
{
if ( index< layers_.size())
{
mapnik::Layer const& layer = layers_[index];
CoordTransform tr = view_transform();
tr.backward(&x,&y);
try
{
mapnik::projection dest(srs_);
mapnik::projection source(layer.srs());
proj_transform prj_trans(source,dest);
double z = 0;
prj_trans.backward(x,y,z);
double minx = currentExtent_.minx();
double miny = currentExtent_.miny();
double maxx = currentExtent_.maxx();
double maxy = currentExtent_.maxy();
prj_trans.backward(minx,miny,z);
prj_trans.backward(maxx,maxy,z);
double tol = (maxx - minx) / width_ * 3;
mapnik::datasource_ptr ds = layer.datasource();
if (ds)
{
#ifdef MAPNIK_DEBUG
std::clog << " query at point tol = " << tol << " (" << x << "," << y << ")\n";
#endif
featureset_ptr fs = ds->features_at_point(mapnik::coord2d(x,y));
if (fs)
return featureset_ptr(new filter_featureset<hit_test_filter>(fs,hit_test_filter(x,y,tol)));
}
}
catch (...)
{
#ifdef MAPNIK_DEBUG
std::clog << "exception caught in \"query_map_point\"\n";
#endif
}
}
return featureset_ptr();
}
Map::~Map() {}
}