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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
//$Id$
|
|
|
|
|
|
|
|
// mapnik
|
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>
|
2010-05-27 13:41:02 +02:00
|
|
|
|
|
|
|
// boost
|
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2012-03-08 17:37:58 +01:00
|
|
|
#include <boost/make_shared.hpp>
|
2010-05-27 13:41:02 +02:00
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
namespace mapnik
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
boost::unordered_map<std::string, marker_ptr> marker_cache::cache_;
|
2010-05-27 13:41:02 +02:00
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
bool marker_cache::insert (std::string const& uri, marker_ptr path)
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
mutex::scoped_lock lock(mutex_);
|
|
|
|
#endif
|
|
|
|
return cache_.insert(std::make_pair(uri,path)).second;
|
|
|
|
}
|
|
|
|
|
2011-01-26 02:18:40 +01:00
|
|
|
boost::optional<marker_ptr> marker_cache::find(std::string const& uri, bool update_cache)
|
2010-05-27 13:41:02 +02:00
|
|
|
{
|
|
|
|
#ifdef MAPNIK_THREADSAFE
|
|
|
|
mutex::scoped_lock lock(mutex_);
|
|
|
|
#endif
|
2011-01-26 02:18:40 +01:00
|
|
|
typedef boost::unordered_map<std::string, marker_ptr>::const_iterator iterator_type;
|
|
|
|
boost::optional<marker_ptr> result;
|
2010-05-27 13:41:02 +02:00
|
|
|
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))
|
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
if (is_svg(uri))
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
using namespace mapnik::svg;
|
|
|
|
try
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2012-03-08 17:37:58 +01:00
|
|
|
path_ptr marker_path(boost::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);
|
|
|
|
|
2012-03-08 17:37:58 +01:00
|
|
|
marker_ptr mark(boost::make_shared<marker>(marker_path));
|
2011-01-26 02:18:40 +01:00
|
|
|
result.reset(mark);
|
|
|
|
if (update_cache)
|
|
|
|
{
|
|
|
|
cache_.insert(std::make_pair(uri,*result));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << "Exception caught while loading SVG: " << uri << std::endl;
|
2010-05-30 05:09:22 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2011-01-26 02:18:40 +01:00
|
|
|
else
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2011-02-25 22:02:33 +01:00
|
|
|
try
|
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
std::auto_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);
|
2012-03-08 17:37:58 +01:00
|
|
|
mapnik::image_ptr image(boost::make_shared<mapnik::image_data_32>(width,height));
|
2011-01-26 02:18:40 +01:00
|
|
|
reader->read(0,0,*image);
|
2012-03-08 17:37:58 +01:00
|
|
|
marker_ptr mark(boost::make_shared<marker>(image));
|
2011-01-26 02:18:40 +01:00
|
|
|
result.reset(mark);
|
|
|
|
if (update_cache)
|
|
|
|
{
|
|
|
|
cache_.insert(std::make_pair(uri,*result));
|
|
|
|
}
|
|
|
|
}
|
2011-02-25 22:02:33 +01:00
|
|
|
}
|
2011-01-26 02:18:40 +01:00
|
|
|
|
2011-02-25 22:02:33 +01:00
|
|
|
catch (...)
|
2011-01-26 02:18:40 +01:00
|
|
|
{
|
|
|
|
std::cerr << "Exception caught while loading image: " << uri << std::endl;
|
2011-02-25 22:02:33 +01:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2010-05-27 13:41:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-26 02:18:40 +01:00
|
|
|
std::cerr << "### WARNING Marker does not exist: " << uri << std::endl;
|
2010-05-27 13:41:02 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|