Repository navigation
Expand file tree
/
Copy pathmarker_cache.cpp
More file actions
269 lines (249 loc) · 9.69 KB
/
Copy pathmarker_cache.cpp
File metadata and controls
269 lines (249 loc) · 9.69 KB
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
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2014 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/marker.hpp>
#include <mapnik/marker_cache.hpp>
#include <mapnik/svg/svg_parser.hpp>
#include <mapnik/svg/svg_storage.hpp>
#include <mapnik/svg/svg_converter.hpp>
#include <mapnik/svg/svg_path_adapter.hpp>
#include <mapnik/svg/svg_path_attributes.hpp>
#include <mapnik/image_util.hpp>
#include <mapnik/image_reader.hpp>
#include <mapnik/util/fs.hpp>
// boost
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
#include <boost/assert.hpp>
#include <boost/algorithm/string.hpp>
#pragma GCC diagnostic pop
// agg
#include "agg_rendering_buffer.h"
#include "agg_pixfmt_rgba.h"
namespace mapnik
{
marker_cache::marker_cache()
: known_svg_prefix_("shape://"),
known_image_prefix_("image://")
{
insert_svg("ellipse",
"<?xml version='1.0' standalone='no'?>"
"<svg width='100%' height='100%' version='1.1' xmlns='http://www.w3.org/2000/svg'>"
"<ellipse rx='5' ry='5' fill='#0000FF' stroke='black' stroke-width='.5'/>"
"</svg>");
insert_svg("arrow",
"<?xml version='1.0' standalone='no'?>"
"<svg width='100%' height='100%' version='1.1' xmlns='http://www.w3.org/2000/svg'>"
"<path fill='#0000FF' stroke='black' stroke-width='.5' d='m 31.698405,7.5302648 -8.910967,-6.0263712 0.594993,4.8210971 -18.9822542,0 0,2.4105482 18.9822542,0 -0.594993,4.8210971 z'/>"
"</svg>");
marker_cache_.emplace("image://square",std::make_shared<mapnik::marker const>(mapnik::marker_rgba8()));
}
marker_cache::~marker_cache() {}
void marker_cache::clear()
{
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::mutex> lock(mutex_);
#endif
using iterator_type = boost::unordered_map<std::string, std::shared_ptr<mapnik::marker const> >::const_iterator;
iterator_type itr = marker_cache_.begin();
while(itr != marker_cache_.end())
{
if (!is_uri(itr->first))
{
marker_cache_.erase(itr++);
}
else
{
++itr;
}
}
}
bool marker_cache::is_svg_uri(std::string const& path)
{
return boost::algorithm::starts_with(path,known_svg_prefix_);
}
bool marker_cache::is_image_uri(std::string const& path)
{
return boost::algorithm::starts_with(path,known_image_prefix_);
}
bool marker_cache::insert_svg(std::string const& name, std::string const& svg_string)
{
std::string key = known_svg_prefix_ + name;
using iterator_type = boost::unordered_map<std::string, std::string>::const_iterator;
iterator_type itr = svg_cache_.find(key);
if (itr == svg_cache_.end())
{
return svg_cache_.emplace(key,svg_string).second;
}
return false;
}
bool marker_cache::insert_marker(std::string const& uri, mapnik::marker && path)
{
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::mutex> lock(mutex_);
#endif
return marker_cache_.emplace(uri,std::make_shared<mapnik::marker const>(std::move(path))).second;
}
namespace detail
{
struct visitor_create_marker
{
marker operator() (image_rgba8 & data)
{
mapnik::premultiply_alpha(data);
return mapnik::marker(mapnik::marker_rgba8(data));
}
marker operator() (image_null & data)
{
throw std::runtime_error("Can not make marker from null image data type");
}
template <typename T>
marker operator() (T & data)
{
throw std::runtime_error("Can not make marker from this data type");
}
};
} // end detail ns
std::shared_ptr<mapnik::marker const> marker_cache::find(std::string const& uri,
bool update_cache)
{
if (uri.empty())
{
return std::make_shared<mapnik::marker const>(mapnik::marker_null());
}
#ifdef MAPNIK_THREADSAFE
std::lock_guard<std::mutex> lock(mutex_);
#endif
using iterator_type = boost::unordered_map<std::string, std::shared_ptr<mapnik::marker const> >::const_iterator;
iterator_type itr = marker_cache_.find(uri);
if (itr != marker_cache_.end())
{
return itr->second;
}
try
{
// if uri references a built-in marker
if (is_svg_uri(uri))
{
boost::unordered_map<std::string, std::string>::const_iterator mark_itr = svg_cache_.find(uri);
if (mark_itr == svg_cache_.end())
{
MAPNIK_LOG_ERROR(marker_cache) << "Marker does not exist: " << uri;
return std::make_shared<mapnik::marker const>(mapnik::marker_null());
}
std::string known_svg_string = mark_itr->second;
using namespace mapnik::svg;
svg_path_ptr marker_path(std::make_shared<svg_storage_type>());
vertex_stl_adapter<svg_path_storage> stl_storage(marker_path->source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, marker_path->attributes());
svg_parser p(svg);
p.parse_from_string(known_svg_string);
//svg.arrange_orientations();
double lox,loy,hix,hiy;
svg.bounding_rect(&lox, &loy, &hix, &hiy);
marker_path->set_bounding_box(lox,loy,hix,hiy);
marker_path->set_dimensions(svg.width(),svg.height());
if (update_cache)
{
auto emplace_result = marker_cache_.emplace(uri,std::make_shared<mapnik::marker const>(mapnik::marker_svg(marker_path)));
return emplace_result.first->second;
}
else
{
return std::make_shared<mapnik::marker const>(mapnik::marker_svg(marker_path));
}
}
// otherwise assume file-based
else
{
if (!mapnik::util::exists(uri))
{
MAPNIK_LOG_ERROR(marker_cache) << "Marker does not exist: " << uri;
return std::make_shared<mapnik::marker const>(mapnik::marker_null());
}
if (is_svg(uri))
{
using namespace mapnik::svg;
svg_path_ptr marker_path(std::make_shared<svg_storage_type>());
vertex_stl_adapter<svg_path_storage> stl_storage(marker_path->source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, marker_path->attributes());
svg_parser p(svg);
p.parse(uri);
//svg.arrange_orientations();
double lox,loy,hix,hiy;
svg.bounding_rect(&lox, &loy, &hix, &hiy);
marker_path->set_bounding_box(lox,loy,hix,hiy);
marker_path->set_dimensions(svg.width(),svg.height());
if (update_cache)
{
auto emplace_result = marker_cache_.emplace(uri,std::make_shared<mapnik::marker const>(mapnik::marker_svg(marker_path)));
return emplace_result.first->second;
}
else
{
return std::make_shared<mapnik::marker const>(mapnik::marker_svg(marker_path));
}
}
else
{
// TODO - support reading images from string
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(uri));
if (reader.get())
{
unsigned width = reader->width();
unsigned height = reader->height();
BOOST_ASSERT(width > 0 && height > 0);
image_any im = reader->read(0,0,width,height);
if (update_cache)
{
auto emplace_result = marker_cache_.emplace(uri,
std::make_shared<mapnik::marker const>(
util::apply_visitor(detail::visitor_create_marker(), im)
));
return emplace_result.first->second;
}
else
{
return std::make_shared<mapnik::marker const>(
util::apply_visitor(detail::visitor_create_marker(), im)
);
}
}
else
{
MAPNIK_LOG_ERROR(marker_cache) << "could not intialize reader for: '" << uri << "'";
return std::make_shared<mapnik::marker const>(mapnik::marker_null());
}
}
}
}
catch (std::exception const& ex)
{
MAPNIK_LOG_ERROR(marker_cache) << "Exception caught while loading: '" << uri << "' (" << ex.what() << ")";
}
return std::make_shared<mapnik::marker const>(mapnik::marker_null());
}
}