/
metawriter.cpp
368 lines (312 loc) · 11.3 KB
/
metawriter.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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2011 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
*
*****************************************************************************/
// Mapnik
#include <mapnik/debug.hpp>
#include <mapnik/metawriter.hpp>
#include <mapnik/metawriter_json.hpp>
#include <mapnik/text_placements/base.hpp>
#include <mapnik/text_path.hpp>
// Boost
#include <boost/foreach.hpp>
#include <boost/algorithm/string.hpp>
// STL
#include <iomanip>
namespace mapnik {
UnicodeString const& metawriter_property_map::operator[](std::string const& key) const
{
std::map<std::string, UnicodeString>::const_iterator it;
it = m_.find(key);
if (it == m_.end()) return not_found_;
return (*it).second;
}
metawriter_properties::metawriter_properties(boost::optional<std::string> str)
{
if (str) {
boost::split(*this, *str, boost::is_any_of(", "), boost::token_compress_on);
}
}
std::string metawriter_properties::to_string() const
{
return boost::algorithm::join(*this, ",");
}
/********************************************************************************************/
void metawriter_json_stream::start(metawriter_property_map const& /*properties*/)
{
assert(trans_);
if (output_empty_) {
write_header();
} else {
count_ = HEADER_NOT_WRITTEN;
}
}
void metawriter_json_stream::write_header()
{
assert(f_);
*f_ << "{ \"type\": \"FeatureCollection\", \"features\": [\n" << std::fixed << std::setprecision(pixel_coordinates_ ? 0 : 8);
count_ = STARTED;
}
void metawriter_json_stream::stop()
{
if (count_ >= STARTED && f_)
{
*f_ << " ] }\n";
}
count_ = STOPPED;
}
metawriter_json_stream::~metawriter_json_stream()
{
if (count_ >= STARTED)
{
MAPNIK_LOG_WARN(metawriter) << "WARNING: GeoJSON metawriter not stopped before destroying it.";
stop();
}
if (trans_) delete trans_;
}
metawriter_json_stream::metawriter_json_stream(metawriter_properties dflt_properties)
: metawriter(dflt_properties), count_(-1), output_empty_(true),
trans_(0), output_srs_("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"),
pixel_coordinates_(false), f_(0)
{
}
void metawriter_json_stream::write_properties(Feature const& feature, metawriter_properties const& properties)
{
*f_ << "}," << //Close coordinates object
"\n \"properties\": {";
int i = 0;
BOOST_FOREACH(std::string const& p, properties)
{
if (feature.has_key(p))
{
mapnik::value const& val = feature.get(p);
std::string str = val.to_string();
if (str.size() == 0) continue; // ignore empty attributes
//Property found
std::string text = boost::replace_all_copy(boost::replace_all_copy(str, "\\", "\\\\"), "\"", "\\\"");
if (i++) *f_ << ",";
*f_ << "\n \"" << p << "\":\"" << text << "\"";
}
}
*f_ << "\n} }";
}
/* Coordinate transform in renderer:
input: layer srs
prj_trans.backwards() [prj_trans: map -> layer]
intermediate: map srs
t_.forward()
output: pixels
Our transform:
input: pixels
t_.backward()
intermediate: map srs
trans_.forward()
output: WGS84
*/
void metawriter_json_stream::add_box(box2d<double> const &box, Feature const& feature,
CoordTransform const& t, metawriter_properties const& properties)
{
/* Check if feature is in bounds. */
if (box.maxx() < 0 || box.maxy() < 0 || box.minx() > width_ || box.miny() > height_) return;
double minx, miny, maxx, maxy;
if (pixel_coordinates_) {
minx = box.minx();
miny = box.miny();
maxx = box.maxx();
maxy = box.maxy();
} else {
//t_ coord transform has transform for box2d combined with proj_transform
box2d<double> transformed = t.backward(box, *trans_);
minx = transformed.minx();
miny = transformed.miny();
maxx = transformed.maxx();
maxy = transformed.maxy();
}
write_feature_header("Polygon");
*f_ << " [ [ [" <<
minx << ", " << miny << "], [" <<
maxx << ", " << miny << "], [" <<
maxx << ", " << maxy << "], [" <<
minx << ", " << maxy << "] ] ]";
write_properties(feature, properties);
}
void metawriter_json_stream::add_text(
boost::ptr_vector<text_path> &placements, box2d<double> const& extents,
Feature const& feature, CoordTransform const& t,
metawriter_properties const& properties)
{
/* Note:
Map coordinate system (and starting_{x,y}) starts in upper left corner
and grows towards lower right corner.
Font + placement vertex coordinate system starts in lower left corner
and grows towards upper right corner.
Therefore y direction is different. Keep this in mind while doing calculations.
The y value returned by vertex() is always the baseline.
Lowest y = baseline of bottom line
Hightest y = baseline of top line
*/
for (unsigned n = 0; n < placements.size(); n++)
{
text_path ¤t_placement = placements[n];
bool inside = false; /* Part of text is inside rendering region */
bool straight = true;
char_info_ptr c;
double x, y, angle;
current_placement.rewind();
for (int i = 0; i < current_placement.num_nodes(); ++i) {
int cx = current_placement.center.x;
int cy = current_placement.center.y;
current_placement.vertex(&c, &x, &y, &angle);
if (cx+x >= 0 && cx+x < width_ && cy-y >= 0 && cy-y < height_) inside = true;
if (angle > 0.001 || angle < -0.001) straight = false;
if (inside && !straight) break;
}
if (!inside) continue;
current_placement.rewind();
if (straight) {
//Reduce number of polygons
double minx = INT_MAX, miny = INT_MAX, maxx = INT_MIN, maxy = INT_MIN;
for (int i = 0; i < current_placement.num_nodes(); ++i) {
current_placement.vertex(&c, &x, &y, &angle);
minx = std::min(minx, x);
maxx = std::max(maxx, x+c->width);
maxy = std::max(maxy, y+c->ymax);
miny = std::min(miny, y+c->ymin);
}
add_box(box2d<double>(current_placement.center.x+minx,
current_placement.center.y-miny,
current_placement.center.x+maxx,
current_placement.center.y-maxy), feature, t, properties);
continue;
}
write_feature_header("MultiPolygon");
*f_ << "[";
for (int i = 0; i < current_placement.num_nodes(); ++i) {
current_placement.vertex(&c, &x, &y, &angle);
if (c->c == ' ') continue;
*f_ << ",";
double x0, y0, x1, y1, x2, y2, x3, y3;
double sina = sin(angle);
double cosa = cos(angle);
x0 = current_placement.center.x + x - sina*c->ymin;
y0 = current_placement.center.y - y - cosa*c->ymin;
x1 = x0 + c->width * cosa;
y1 = y0 - c->width * sina;
x2 = x0 + (c->width * cosa - c->height() * sina);
y2 = y0 - (c->width * sina + c->height() * cosa);
x3 = x0 - c->height() * sina;
y3 = y0 - c->height() * cosa;
*f_ << "\n [[";
write_point(t, x0, y0);
write_point(t, x1, y1);
write_point(t, x2, y2);
write_point(t, x3, y3, true);
*f_ << "]]";
}
*f_ << "]";
write_properties(feature, properties);
}
}
void metawriter_json_stream::add_polygon(path_type & path,
Feature const& feature,
CoordTransform const& t,
metawriter_properties const& properties)
{
write_feature_header("Polygon");
write_line_polygon(path, t, true);
write_properties(feature, properties);
}
void metawriter_json_stream::add_line(path_type & path,
Feature const& feature,
CoordTransform const& t,
metawriter_properties const& properties)
{
write_feature_header("MultiLineString");
write_line_polygon(path, t, false);
write_properties(feature, properties);
}
void metawriter_json_stream::write_line_polygon(path_type & path, CoordTransform const& t, bool /*polygon*/){
*f_ << " [";
double x, y, last_x=0.0, last_y=0.0;
unsigned cmd, last_cmd = SEG_END;
path.rewind(0);
int polygon_count = 0;
while ((cmd = path.vertex(&x, &y)) != SEG_END) {
if (cmd == SEG_LINETO) {
if (last_cmd == SEG_MOVETO) {
//Start new polygon/line
if (polygon_count++) *f_ << "], ";
*f_ << "[";
write_point(t, last_x, last_y, true);
}
*f_ << ",";
write_point(t, x, y, true);
}
last_x = x;
last_y = y;
last_cmd = cmd;
}
*f_ << "]]";
}
void metawriter_json_stream::set_map_srs(projection const& input_srs_)
{
if (trans_) delete trans_;
trans_ = new proj_transform(input_srs_, output_srs_);
}
/********************************************************************************************/
metawriter_json::metawriter_json(metawriter_properties dflt_properties, path_expression_ptr fn)
: metawriter_json_stream(dflt_properties), fn_(fn) {}
void metawriter_json::start(metawriter_property_map const& properties)
{
filename_ = path_processor<metawriter_property_map>::evaluate(*fn_, properties);
MAPNIK_LOG_DEBUG(metawriter) << "metawriter_json: Filename=" << filename_;
metawriter_json_stream::start(properties);
}
void metawriter_json::write_header()
{
f_.open(filename_.c_str(), std::fstream::out | std::fstream::trunc);
if (f_.fail())
{
MAPNIK_LOG_DEBUG(metawriter) << "metawriter_json: Failed to open file " << filename_;
}
set_stream(&f_);
metawriter_json_stream::write_header();
}
void metawriter_json::stop()
{
metawriter_json_stream::stop();
if (f_.is_open())
{
f_.close();
}
else if (count_ >= STARTED)
{
MAPNIK_LOG_DEBUG(metawriter) << "metawriter_json: File not open when stopping";
}
}
void metawriter_json::set_filename(path_expression_ptr fn)
{
fn_ = fn;
}
path_expression_ptr metawriter_json::get_filename() const
{
return fn_;
}
}