2010-05-27 13:41:02 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2010-05-27 13:41:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-23 15:04:25 +02:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2010-05-27 13:41:02 +02:00
|
|
|
*
|
|
|
|
* 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
|
2012-04-10 00:51:04 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2012-03-08 17:37:58 +01:00
|
|
|
#include <mapnik/marker.hpp>
|
2011-01-26 02:18:40 +01:00
|
|
|
#include <mapnik/marker_cache.hpp>
|
2010-05-28 18:34:58 +02:00
|
|
|
#include <mapnik/svg/svg_parser.hpp>
|
2010-06-08 12:16:22 +02:00
|
|
|
#include <mapnik/svg/svg_storage.hpp>
|
|
|
|
#include <mapnik/svg/svg_converter.hpp>
|
2012-03-08 17:37:58 +01:00
|
|
|
#include <mapnik/svg/svg_path_adapter.hpp>
|
|
|
|
#include <mapnik/svg/svg_path_attributes.hpp>
|
2011-01-26 02:18:40 +01:00
|
|
|
#include <mapnik/image_util.hpp>
|
|
|
|
#include <mapnik/image_reader.hpp>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
2010-05-27 13:41:02 +02:00
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2012-06-19 14:36:03 +02:00
|
|
|
// agg
|
|
|
|
#include "agg_rendering_buffer.h"
|
|
|
|
#include "agg_pixfmt_rgba.h"
|
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
namespace mapnik
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
|
|
|
|
2012-07-11 07:38:53 +02:00
|
|
|
marker_cache::marker_cache()
|
2014-07-31 09:41:27 +02:00
|
|
|
: known_svg_prefix_("shape://"),
|
|
|
|
known_image_prefix_("image://")
|
2012-07-11 07:38:53 +02:00
|
|
|
{
|
|
|
|
insert_svg("ellipse",
|
|
|
|
"<?xml version='1.0' standalone='no'?>"
|
|
|
|
"<svg width='100%' height='100%' version='1.1' xmlns='http://www.w3.org/2000/svg'>"
|
2012-07-26 04:29:43 +02:00
|
|
|
"<ellipse rx='5' ry='5' fill='#0000FF' stroke='black' stroke-width='.5'/>"
|
2012-07-11 07:38:53 +02:00
|
|
|
"</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'>"
|
2012-07-28 18:27:37 +02:00
|
|
|
"<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'/>"
|
2012-07-11 07:38:53 +02:00
|
|
|
"</svg>");
|
2014-07-31 09:41:27 +02:00
|
|
|
boost::optional<mapnik::image_ptr> bitmap_data = boost::optional<mapnik::image_ptr>(std::make_shared<image_data_32>(4,4));
|
|
|
|
(*bitmap_data)->set(0xff000000);
|
|
|
|
marker_ptr mark = std::make_shared<mapnik::marker>(bitmap_data);
|
|
|
|
marker_cache_.insert(std::make_pair("image://square",mark));
|
2012-07-11 07:38:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
marker_cache::~marker_cache() {}
|
2010-05-27 13:41:02 +02:00
|
|
|
|
2012-03-10 00:16:01 +01:00
|
|
|
void marker_cache::clear()
|
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2013-10-11 14:20:46 +02:00
|
|
|
mapnik::scoped_lock lock(mutex_);
|
2012-03-10 00:16:01 +01:00
|
|
|
#endif
|
2014-07-07 19:23:15 +02:00
|
|
|
using iterator_type = boost::unordered_map<std::string, marker_ptr>::const_iterator;
|
2012-07-11 07:38:53 +02:00
|
|
|
iterator_type itr = marker_cache_.begin();
|
|
|
|
while(itr != marker_cache_.end())
|
|
|
|
{
|
|
|
|
if (!is_uri(itr->first))
|
|
|
|
{
|
|
|
|
marker_cache_.erase(itr++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
2012-03-10 00:16:01 +01:00
|
|
|
}
|
|
|
|
|
2014-07-31 09:41:27 +02:00
|
|
|
bool marker_cache::is_svg_uri(std::string const& path)
|
2012-07-11 07:38:53 +02:00
|
|
|
{
|
|
|
|
return boost::algorithm::starts_with(path,known_svg_prefix_);
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:41:27 +02:00
|
|
|
bool marker_cache::is_image_uri(std::string const& path)
|
|
|
|
{
|
|
|
|
return boost::algorithm::starts_with(path,known_image_prefix_);
|
|
|
|
}
|
|
|
|
|
2012-07-11 07:38:53 +02:00
|
|
|
bool marker_cache::insert_svg(std::string const& name, std::string const& svg_string)
|
|
|
|
{
|
|
|
|
std::string key = known_svg_prefix_ + name;
|
2014-07-07 19:23:15 +02:00
|
|
|
using iterator_type = boost::unordered_map<std::string, std::string>::const_iterator;
|
2012-07-11 07:38:53 +02:00
|
|
|
iterator_type itr = svg_cache_.find(key);
|
|
|
|
if (itr == svg_cache_.end())
|
|
|
|
{
|
|
|
|
return svg_cache_.insert(std::make_pair(key,svg_string)).second;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool marker_cache::insert_marker(std::string const& uri, marker_ptr path)
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2013-10-11 14:20:46 +02:00
|
|
|
mapnik::scoped_lock lock(mutex_);
|
2010-05-27 13:41:02 +02:00
|
|
|
#endif
|
2012-07-11 07:38:53 +02:00
|
|
|
return marker_cache_.insert(std::make_pair(uri,path)).second;
|
2010-05-27 13:41:02 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 07:38:53 +02:00
|
|
|
boost::optional<marker_ptr> marker_cache::find(std::string const& uri,
|
|
|
|
bool update_cache)
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
2012-06-30 01:52:54 +02:00
|
|
|
|
|
|
|
boost::optional<marker_ptr> result;
|
|
|
|
if (uri.empty())
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2012-07-11 07:38:53 +02:00
|
|
|
|
2010-05-27 13:41:02 +02:00
|
|
|
#ifdef MAPNIK_THREADSAFE
|
2013-10-11 14:20:46 +02:00
|
|
|
mapnik::scoped_lock lock(mutex_);
|
2010-05-27 13:41:02 +02:00
|
|
|
#endif
|
2014-07-07 19:23:15 +02:00
|
|
|
using iterator_type = boost::unordered_map<std::string, marker_ptr>::const_iterator;
|
2012-07-11 07:38:53 +02:00
|
|
|
iterator_type itr = marker_cache_.find(uri);
|
|
|
|
if (itr != marker_cache_.end())
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
result.reset(itr->second);
|
|
|
|
return result;
|
2010-05-27 13:41:02 +02:00
|
|
|
}
|
|
|
|
|
2012-04-13 01:06:40 +02:00
|
|
|
try
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
2012-07-11 07:38:53 +02:00
|
|
|
// if uri references a built-in marker
|
2014-07-31 09:41:27 +02:00
|
|
|
if (is_svg_uri(uri))
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-07-11 07:38:53 +02:00
|
|
|
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 result;
|
|
|
|
}
|
|
|
|
std::string known_svg_string = mark_itr->second;
|
|
|
|
using namespace mapnik::svg;
|
2013-09-20 15:00:11 +02:00
|
|
|
svg_path_ptr marker_path(std::make_shared<svg_storage_type>());
|
2012-07-11 07:38:53 +02:00
|
|
|
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);
|
2013-03-09 02:40:19 +01:00
|
|
|
marker_path->set_dimensions(svg.width(),svg.height());
|
2013-09-20 15:00:11 +02:00
|
|
|
marker_ptr mark(std::make_shared<marker>(marker_path));
|
2012-07-11 07:38:53 +02:00
|
|
|
result.reset(mark);
|
|
|
|
if (update_cache)
|
|
|
|
{
|
|
|
|
marker_cache_.insert(std::make_pair(uri,*result));
|
|
|
|
}
|
2012-04-13 01:06:40 +02:00
|
|
|
}
|
2012-07-11 07:38:53 +02:00
|
|
|
// otherwise assume file-based
|
2012-04-13 01:06:40 +02:00
|
|
|
else
|
|
|
|
{
|
2013-06-03 04:28:24 +02:00
|
|
|
if (!mapnik::util::exists(uri))
|
2012-07-11 07:38:53 +02:00
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(marker_cache) << "Marker does not exist: " << uri;
|
|
|
|
return result;
|
|
|
|
}
|
2012-04-13 01:06:40 +02:00
|
|
|
if (is_svg(uri))
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-04-13 01:06:40 +02:00
|
|
|
using namespace mapnik::svg;
|
2013-09-20 15:00:11 +02:00
|
|
|
svg_path_ptr marker_path(std::make_shared<svg_storage_type>());
|
2011-01-26 02:18:40 +01:00
|
|
|
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);
|
2013-03-09 06:03:41 +01:00
|
|
|
marker_path->set_dimensions(svg.width(),svg.height());
|
2013-09-20 15:00:11 +02:00
|
|
|
marker_ptr mark(std::make_shared<marker>(marker_path));
|
2011-01-26 02:18:40 +01:00
|
|
|
result.reset(mark);
|
|
|
|
if (update_cache)
|
|
|
|
{
|
2012-07-11 07:38:53 +02:00
|
|
|
marker_cache_.insert(std::make_pair(uri,*result));
|
2011-01-26 02:18:40 +01:00
|
|
|
}
|
2010-05-30 05:09:22 +02:00
|
|
|
}
|
2012-04-13 01:06:40 +02:00
|
|
|
else
|
2011-02-25 22:02:33 +01:00
|
|
|
{
|
2012-07-11 07:38:53 +02:00
|
|
|
// TODO - support reading images from string
|
2013-04-19 10:50:50 +02:00
|
|
|
std::unique_ptr<mapnik::image_reader> reader(mapnik::get_image_reader(uri));
|
2011-01-26 02:18:40 +01:00
|
|
|
if (reader.get())
|
|
|
|
{
|
|
|
|
unsigned width = reader->width();
|
|
|
|
unsigned height = reader->height();
|
|
|
|
BOOST_ASSERT(width > 0 && height > 0);
|
2013-09-20 15:00:11 +02:00
|
|
|
mapnik::image_ptr image(std::make_shared<mapnik::image_data_32>(width,height));
|
2011-01-26 02:18:40 +01:00
|
|
|
reader->read(0,0,*image);
|
2012-10-05 01:07:24 +02:00
|
|
|
if (!reader->premultiplied_alpha())
|
|
|
|
{
|
|
|
|
agg::rendering_buffer buffer(image->getBytes(),image->width(),image->height(),image->width() * 4);
|
|
|
|
agg::pixfmt_rgba32 pixf(buffer);
|
|
|
|
pixf.premultiply();
|
|
|
|
}
|
2013-09-20 15:00:11 +02:00
|
|
|
marker_ptr mark(std::make_shared<marker>(image));
|
2011-01-26 02:18:40 +01:00
|
|
|
result.reset(mark);
|
|
|
|
if (update_cache)
|
|
|
|
{
|
2012-07-11 07:38:53 +02:00
|
|
|
marker_cache_.insert(std::make_pair(uri,*result));
|
2011-01-26 02:18:40 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-11 20:28:09 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(marker_cache) << "could not intialize reader for: '" << uri << "'";
|
|
|
|
}
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2010-05-27 13:41:02 +02:00
|
|
|
}
|
2012-04-13 01:06:40 +02:00
|
|
|
catch (std::exception const& ex)
|
|
|
|
{
|
|
|
|
MAPNIK_LOG_ERROR(marker_cache) << "Exception caught while loading: '" << uri << "' (" << ex.what() << ")";
|
|
|
|
}
|
2010-05-27 13:41:02 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|