mapnik/src/marker_cache.cpp

105 lines
3.2 KiB
C++
Raw Normal View History

2010-05-27 13:41:02 +02:00
/*****************************************************************************
*
* 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>
2010-05-28 18:34:58 +02:00
#include <mapnik/svg/svg_parser.hpp>
#include <mapnik/svg/svg_storage.hpp>
#include <mapnik/svg/svg_path_adapter.hpp>
#include <mapnik/svg/svg_converter.hpp>
2010-05-27 13:41:02 +02:00
// 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())
{
2010-06-02 13:03:30 +02:00
result.reset(itr->second);
return result;
2010-05-27 13:41:02 +02:00
}
// we can't find marker in cache, lets try to load it from filesystem
boost::filesystem::path path(uri);
if (exists(path))
{
using namespace mapnik::svg;
2010-06-02 13:03:30 +02:00
try
{
path_ptr marker(new svg_storage_type);
vertex_stl_adapter<svg_path_storage> stl_storage(marker->source());
svg_path_adapter svg_path(stl_storage);
svg_converter_type svg(svg_path, marker->attributes());
svg_parser p(svg);
2010-06-02 13:03:30 +02:00
p.parse(uri);
2010-06-10 16:11:55 +02:00
//svg.arrange_orientations();
2010-06-02 13:03:30 +02:00
double lox,loy,hix,hiy;
svg.bounding_rect(&lox, &loy, &hix, &hiy);
2010-06-08 12:16:31 +02:00
marker->set_bounding_box(lox,loy,hix,hiy);
2010-06-02 13:03:30 +02:00
if (update_cache)
{
cache_.insert(std::make_pair(uri,marker));
}
return marker;
2010-06-02 13:03:30 +02:00
}
catch (...)
{
std::cerr << "Exception caught while loading SVG: " << uri << std::endl;
}
2010-05-27 13:41:02 +02:00
}
else
{
std::cerr << "### WARNING SVG does not exist: " << uri << std::endl;
}
return result;
}
#ifdef MAPNIK_THREADSAFE
2010-06-02 13:03:30 +02:00
boost::mutex marker_cache::mutex_;
2010-05-27 13:41:02 +02:00
#endif
}