+ marker_cache impl

This commit is contained in:
Artem Pavlenko 2010-05-27 11:41:02 +00:00
parent 823b7ba0fc
commit bb0ae08280
4 changed files with 103 additions and 8 deletions

View file

@ -28,7 +28,7 @@
// mapnik // mapnik
#include <mapnik/utils.hpp> #include <mapnik/utils.hpp>
#include <mapnik/config.hpp> #include <mapnik/config.hpp>
#include <mapnik/image_data.hpp> #include <mapnik/svg/agg_svg_path_renderer.h>
// boost // boost
#include <boost/utility.hpp> #include <boost/utility.hpp>
@ -40,18 +40,18 @@
namespace mapnik namespace mapnik
{ {
typedef boost::shared_ptr<image_data_32> path_ptr; typedef boost::shared_ptr<agg::svg::path_renderer> path_ptr;
struct MAPNIK_DECL image_cache : struct MAPNIK_DECL marker_cache :
public singleton <image_cache, CreateStatic>, public singleton <marker_cache, CreateStatic>,
private boost::noncopyable private boost::noncopyable
{ {
friend class CreateStatic<image_cache>; friend class CreateStatic<marker_cache>;
static boost::mutex mutex_; static boost::mutex mutex_;
static boost::unordered_map<std::string,image_ptr> cache_; static boost::unordered_map<std::string,path_ptr> cache_;
static bool insert(std::string const& key, image_ptr); static bool insert(std::string const& key, path_ptr);
static boost::optional<image_ptr> find(std::string const& key, bool update_cache = false); static boost::optional<path_ptr> find(std::string const& key, bool update_cache = false);
}; };
} }

View file

@ -126,6 +126,7 @@ if True :
svg_path_parser.cpp svg_path_parser.cpp
svg_points_parser.cpp svg_points_parser.cpp
svg_transform_parser.cpp svg_transform_parser.cpp
marker_cache.cpp
""" """
) )

View file

@ -25,6 +25,7 @@
#include <mapnik/agg_renderer.hpp> #include <mapnik/agg_renderer.hpp>
#include <mapnik/image_util.hpp> #include <mapnik/image_util.hpp>
#include <mapnik/image_cache.hpp> #include <mapnik/image_cache.hpp>
#include <mapnik/svg/marker_cache.hpp>
#include <mapnik/unicode.hpp> #include <mapnik/unicode.hpp>
#include <mapnik/placement_finder.hpp> #include <mapnik/placement_finder.hpp>
#include <mapnik/markers_converter.hpp> #include <mapnik/markers_converter.hpp>

93
src/marker_cache.cpp Normal file
View file

@ -0,0 +1,93 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2010 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$
// mapnik
#include <mapnik/svg/marker_cache.hpp>
#include <mapnik/svg/agg_svg_parser.h>
// boost
#include <boost/assert.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string.hpp>
namespace mapnik
{
boost::unordered_map<std::string, path_ptr> marker_cache::cache_;
bool marker_cache::insert (std::string const& uri, path_ptr path)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
return cache_.insert(std::make_pair(uri,path)).second;
}
boost::optional<path_ptr> marker_cache::find(std::string const& uri, bool update_cache)
{
#ifdef MAPNIK_THREADSAFE
mutex::scoped_lock lock(mutex_);
#endif
typedef boost::unordered_map<std::string, path_ptr>::const_iterator iterator_type;
boost::optional<path_ptr> result;
iterator_type itr = cache_.find(uri);
if (itr != cache_.end())
{
result.reset(itr->second);
return result;
}
// we can't find marker in cache, lets try to load it from filesystem
boost::filesystem::path path(uri);
if (exists(path))
{
try
{
mapnik::path_ptr marker(new agg::svg::path_renderer);
agg::svg::parser p(*marker);
marker->arrange_orientations();
p.parse(uri.c_str());
//marker->bounding_rect(&lox, &loy, &hix, &hiy);
if (update_cache)
{
cache_.insert(std::make_pair(uri,marker));
}
}
catch (...)
{
std::cerr << "Exception caught while loading SVG: " << uri << std::endl;
}
}
else
{
std::cerr << "### WARNING SVG does not exist: " << uri << std::endl;
}
return result;
}
#ifdef MAPNIK_THREADSAFE
boost::mutex marker_cache::mutex_;
#endif
}