Ejemplo n.º 1
0
void render_to_file2(const mapnik::Map& map,
                    const std::string& filename)
{
    mapnik::image_32 image(map.getWidth(),map.getHeight());
    render(map,image,0,0);
    mapnik::save_to_file(image,filename); 
}
Ejemplo n.º 2
0
void render_agg(mapnik::Map const& map, double scaling_factor, QPixmap & pix)
{
    unsigned width=map.width();
    unsigned height=map.height();

    image_32 buf(width,height);
    mapnik::agg_renderer<image_32> ren(map,buf,scaling_factor);

    try
    {
        mapnik::auto_cpu_timer t(std::clog, "rendering took: ");
        ren.apply();
        QImage image((uchar*)buf.raw_data(),width,height,QImage::Format_ARGB32);
        pix = QPixmap::fromImage(image.rgbSwapped());
    }
    catch (mapnik::config_error & ex)
    {
        std::cerr << ex.what() << std::endl;
    }
    catch (const std::exception & ex)
    {
        std::cerr << "exception: " << ex.what() << std::endl;
    }
    catch (...)
    {
        std::cerr << "Unknown exception caught!\n";
    }
}
Ejemplo n.º 3
0
 image_type render(mapnik::Map const & map, double scale_factor) const
 {
     image_type image(map.width(), map.height());
     mapnik::agg_renderer<image_type> ren(map, image, scale_factor);
     ren.apply();
     return image;
 }
Ejemplo n.º 4
0
void render_to_file1(mapnik::Map const& map,
                     std::string const& filename,
                     std::string const& format)
{
    if (format == "svg-ng")
    {
#if defined(SVG_RENDERER)
        std::ofstream file (filename.c_str(), std::ios::out|std::ios::trunc|std::ios::binary);
        if (!file)
        {
            throw mapnik::image_writer_exception("could not open file for writing: " + filename);
        }
        using iter_type = std::ostream_iterator<char>;
        iter_type output_stream_iterator(file);
        mapnik::svg_renderer<iter_type> ren(map,output_stream_iterator);
        ren.apply();
#else
        throw mapnik::image_writer_exception("SVG backend not available, cannot write to format: " + format);
#endif
    }
    else if (format == "pdf" || format == "svg" || format =="ps" || format == "ARGB32" || format == "RGB24")
    {
#if defined(HAVE_CAIRO)
        mapnik::save_to_cairo_file(map,filename,format,1.0);
#else
        throw mapnik::image_writer_exception("Cairo backend not available, cannot write to format: " + format);
#endif
    }
    else
    {
        mapnik::image_any image(map.width(),map.height());
        render(map,image,1.0,0,0);
        mapnik::save_to_file(image,filename,format);
    }
}
Ejemplo n.º 5
0
    result test(std::string const & name, mapnik::Map const & map, double scale_factor) const
    {
        typename Renderer::image_type image(ren.render(map, scale_factor));
        boost::filesystem::path reference = reference_dir / image_file_name(name, map.width(), map.height(), scale_factor, true, Renderer::ext);
        bool reference_exists = boost::filesystem::exists(reference);
        result res;

        res.state = reference_exists ? STATE_OK : STATE_OVERWRITE;
        res.name = name;
        res.renderer_name = Renderer::name;
        res.scale_factor = scale_factor;
        res.size = map_size(map.width(), map.height());
        res.reference_image_path = reference;
        res.diff = reference_exists ? ren.compare(image, reference) : 0;

        if (res.diff)
        {
            boost::filesystem::create_directories(output_dir);
            boost::filesystem::path path = output_dir / image_file_name(name, map.width(), map.height(), scale_factor, false, Renderer::ext);
            res.actual_image_path = path;
            res.state = STATE_FAIL;
            ren.save(image, path);
        }

        if ((res.diff && overwrite) || !reference_exists)
        {
            ren.save(image, reference);
            res.state = STATE_OVERWRITE;
        }

        return res;
    }
Ejemplo n.º 6
0
 image_type render(mapnik::Map const & map, double scale_factor) const
 {
     mapnik::grid grid(map.width(), map.height(), "__id__");
     mapnik::grid_renderer<mapnik::grid> ren(map, grid, scale_factor);
     ren.apply();
     image_type image(map.width(), map.height());
     convert(grid.data(), image);
     return image;
 }
Ejemplo n.º 7
0
void set_maximum_extent(mapnik::Map & m, boost::optional<mapnik::box2d<double> > const& box)
{
    if (box)
    {
        m.set_maximum_extent(*box);
    }
    else
    {
        m.reset_maximum_extent();
    }
}
Ejemplo n.º 8
0
 image_type render(mapnik::Map const & map, double scale_factor) const
 {
     mapnik::cairo_surface_ptr image_surface(
         cairo_image_surface_create(CAIRO_FORMAT_ARGB32, map.width(), map.height()),
         mapnik::cairo_surface_closer());
     mapnik::cairo_ptr image_context(mapnik::create_context(image_surface));
     mapnik::cairo_renderer<mapnik::cairo_ptr> ren(map, image_context, scale_factor);
     ren.apply();
     image_type image(map.width(), map.height());
     mapnik::cairo_image_to_rgba8(image, image_surface);
     return image;
 }
Ejemplo n.º 9
0
 image_type render(mapnik::Map const & map, double scale_factor) const
 {
     std::ostringstream ss(std::stringstream::binary);
     mapnik::cairo_surface_ptr image_surface(
         SurfaceCreateFunction(write, &ss, map.width(), map.height()),
         mapnik::cairo_surface_closer());
     mapnik::cairo_ptr image_context(mapnik::create_context(image_surface));
     mapnik::cairo_renderer<mapnik::cairo_ptr> ren(map, image_context, scale_factor);
     ren.apply();
     cairo_surface_finish(&*image_surface);
     return ss.str();
 }
Ejemplo n.º 10
0
void render_with_vars(mapnik::Map const& map,
            mapnik::image_any& image,
            boost::python::dict const& d,
            double scale_factor = 1.0,
            unsigned offset_x = 0u,
            unsigned offset_y = 0u)
{
    mapnik::attributes vars = mapnik::dict2attr(d);
    mapnik::request req(map.width(),map.height(),map.get_current_extent());
    req.set_buffer_size(map.buffer_size());
    python_unblock_auto_block b;
    mapnik::util::apply_visitor(agg_renderer_visitor_3(map, req, vars, scale_factor, offset_x, offset_y), image);
}
Ejemplo n.º 11
0
static void parameterize_map_language(mapnik::Map &m, char * parameter) { 
    int i; 
    char * data = strdup(parameter); 
    char * tok; 
    char ** ctx; 
    char name_replace[256]; 
    
    name_replace[0] = 0; 
    syslog(LOG_DEBUG, "Internationalizing map to language parameter: %s", parameter); 
    tok = strtok(data,","); 
    if (!tok) return; //No parameterization given 
    strncat(name_replace, ", coalesce(", 255); 
    while (tok) { 
        if (strcmp(tok,"_") == 0) { 
            strncat(name_replace,"name,", 255); 
        } else { 
            strncat(name_replace,"tags->'name:", 255); 
            strncat(name_replace, tok, 255); 
            strncat(name_replace,"',", 255); 
        } 
        tok = strtok(NULL, ","); 
        
    }
    free(data);
    name_replace[strlen(name_replace) - 1] = 0; 
    strncat(name_replace,") as name", 255); 
    for (i = 0; i < m.layer_count(); i++) { 
        mapnik::layer& l = m.getLayer(i); 
        
        mapnik::parameters params = l.datasource()->params(); 
        if (params.find("table") != params.end()) { 
            if (boost::get<std::string>(params["table"]).find(",name") != std::string::npos) { 
                std::string str = boost::get<std::string>(params["table"]); 
                size_t pos = str.find(",name"); 
                str.replace(pos,5,name_replace); 
                params["table"] = str; 
#if MAPNIK_VERSION >= 200200
                std::shared_ptr<mapnik::datasource> ds = mapnik::datasource_cache::instance().create(params); 
#else
                std::shared_ptr<mapnik::datasource> ds = mapnik::datasource_cache::instance()->create(params);
#endif
                l.set_datasource(ds); 
            } 
        } 
        
    } 
}
Ejemplo n.º 12
0
mapnik::featureset_ptr query_map_point(mapnik::Map const& m, int index, double x, double y)
{
    if (index < 0){
        PyErr_SetString(PyExc_IndexError, "Please provide a layer index >= 0");
        boost::python::throw_error_already_set();
    }
    unsigned idx = index;
    return m.query_map_point(idx, x, y);
}
Ejemplo n.º 13
0
void render_to_file2(const mapnik::Map& map,const std::string& filename)
{
    std::string format = mapnik::guess_type(filename);
    if (format == "pdf" || format == "svg" || format =="ps")
    {
#if defined(HAVE_CAIRO)
        mapnik::save_to_cairo_file(map,filename,format);
#else
        throw mapnik::ImageWriterException("Cairo backend not available, cannot write to format: " + format);
#endif
    }
    else 
    {
        mapnik::image_32 image(map.width(),map.height());
        render(map,image,1.0,0,0);
        mapnik::save_to_file(image,filename); 
    }
}
Ejemplo n.º 14
0
void render_to_file1(const mapnik::Map& map,
                    const std::string& filename,
                    const std::string& format)
{
    if (format == "pdf" || format == "svg" || format =="ps" || format == "ARGB32" || format == "RGB24")
    {
#if defined(HAVE_CAIRO)
        mapnik::save_to_cairo_file(map,filename,format);
#else
        throw mapnik::ImageWriterException("Cairo backend not available, cannot write to format: " + format);
#endif
    }
    else 
    {
        mapnik::Image32 image(map.getWidth(),map.getHeight());
        render(map,image,0,0);
        mapnik::save_to_file(image,filename,format); 
    }
}
Ejemplo n.º 15
0
mapnik::feature_type_style find_style(mapnik::Map const& m, std::string const& name)
{
    boost::optional<mapnik::feature_type_style const&> style = m.find_style(name);
    if (!style)
    {
        PyErr_SetString(PyExc_KeyError, "Invalid style name");
        boost::python::throw_error_already_set();
    }
    return *style;
}
Ejemplo n.º 16
0
mapnik::font_set find_fontset(mapnik::Map const& m, std::string const& name)
{
    boost::optional<mapnik::font_set const&> fontset = m.find_fontset(name);
    if (!fontset)
    {
        PyErr_SetString(PyExc_KeyError, "Invalid font_set name");
        boost::python::throw_error_already_set();
    }
    return *fontset;
}
Ejemplo n.º 17
0
void render_cairo(mapnik::Map const& map, double scaling_factor, QPixmap & pix)
{
// FIXME
#ifdef HAVE_CAIRO
    mapnik::cairo_surface_ptr image_surface(cairo_image_surface_create(CAIRO_FORMAT_ARGB32,map.width(),map.height()),
                                            mapnik::cairo_surface_closer());
    mapnik::cairo_ptr cairo = mapnik::create_context(image_surface);
    if (cairo)
    {
        mapnik::auto_cpu_timer t(std::clog, "rendering took: ");
        mapnik::cairo_renderer<mapnik::cairo_ptr> renderer(map, cairo, scaling_factor);
        renderer.apply();
    }
    mapnik::image_rgba8 data(map.width(), map.height());
    mapnik::cairo_image_to_rgba8(data, image_surface);
    QImage image((uchar*)data.getBytes(),data.width(),data.height(),QImage::Format_ARGB32);
    pix = QPixmap::fromImage(image.rgbSwapped());
#endif
}
Ejemplo n.º 18
0
void render_to_file3(mapnik::Map const& map,
                     std::string const& filename,
                     std::string const& format,
                     double scale_factor = 1.0
    )
{
    if (format == "pdf" || format == "svg" || format =="ps" || format == "ARGB32" || format == "RGB24")
    {
#if defined(HAVE_CAIRO)
        mapnik::save_to_cairo_file(map,filename,format,scale_factor);
#else
        throw mapnik::ImageWriterException("Cairo backend not available, cannot write to format: " + format);
#endif
    }
    else
    {
        mapnik::image_32 image(map.width(),map.height());
        render(map,image,scale_factor,0,0);
        mapnik::save_to_file(image,filename,format);
    }
}
Ejemplo n.º 19
0
void render_layer_for_grid(mapnik::Map const& map,
                                  mapnik::grid & grid,
                                  unsigned layer_idx,
                                  boost::python::list const& fields,
                                  double scale_factor,
                                  unsigned offset_x,
                                  unsigned offset_y)
{
    std::vector<mapnik::layer> const& layers = map.layers();
    std::size_t layer_num = layers.size();
    if (layer_idx >= layer_num) {
        std::ostringstream s;
        s << "Zero-based layer index '" << layer_idx << "' not valid, only '"
          << layer_num << "' layers are in map\n";
        throw std::runtime_error(s.str());
    }

    // convert python list to std::set
    boost::python::ssize_t num_fields = boost::python::len(fields);
    for(boost::python::ssize_t i=0; i<num_fields; i++) {
        boost::python::extract<std::string> name(fields[i]);
        if (name.check())
        {
            grid.add_field(name());
        }
        else
        {
            std::stringstream s;
            s << "list of field names must be strings";
            throw mapnik::value_error(s.str());
        }
    }

    // copy field names
    std::set<std::string> attributes = grid.get_fields();
    // todo - make this a static constant
    std::string known_id_key = "__id__";
    if (attributes.find(known_id_key) != attributes.end())
    {
        attributes.erase(known_id_key);
    }

    std::string join_field = grid.get_key();
    if (known_id_key != join_field &&
        attributes.find(join_field) == attributes.end())
    {
        attributes.insert(join_field);
    }

    mapnik::grid_renderer<mapnik::grid> ren(map,grid,scale_factor,offset_x,offset_y);
    mapnik::layer const& layer = layers[layer_idx];
    ren.apply(layer,attributes);
}
Ejemplo n.º 20
0
 image_type render(mapnik::Map & map, double scale_factor, map_size const & tiles) const
 {
     mapnik::box2d<double> box = map.get_current_extent();
     image_type image(map.width(), map.height());
     map.resize(image.width() / tiles.width, image.height() / tiles.height);
     double tile_box_width = box.width() / tiles.width;
     double tile_box_height = box.height() / tiles.height;
     for (std::size_t tile_y = 0; tile_y < tiles.height; tile_y++)
     {
         for (std::size_t tile_x = 0; tile_x < tiles.width; tile_x++)
         {
             mapnik::box2d<double> tile_box(
                 box.minx() + tile_x * tile_box_width,
                 box.miny() + tile_y * tile_box_height,
                 box.minx() + (tile_x + 1) * tile_box_width,
                 box.miny() + (tile_y + 1) * tile_box_height);
             map.zoom_to_box(tile_box);
             image_type tile(ren.render(map, scale_factor));
             set_rectangle(tile, image, tile_x * tile.width(), (tiles.height - 1 - tile_y) * tile.height());
         }
     }
     return image;
 }
Ejemplo n.º 21
0
bool make_vector_tile(tile &tile,
                      unsigned int path_multiplier,
                      mapnik::Map const& map,
                      int buffer_size,
                      double scale_factor,
                      unsigned int offset_x,
                      unsigned int offset_y,
                      unsigned int tolerance,
                      const std::string &image_format,
                      mapnik::scaling_method_e scaling_method,
                      double scale_denominator,
                      boost::optional<const post_processor &> pp) {
  
  typedef backend backend_type;
  typedef mapnik::vector::processor<backend_type> renderer_type;
  
  backend_type backend(tile.mapnik_tile(), path_multiplier, map, pp);
  
  mapnik::request request(map.width(),
                          map.height(),
                          map.get_current_extent());
  request.set_buffer_size(buffer_size);
  
  renderer_type ren(backend,
                    map,
                    request,
                    scale_factor,
                    offset_x,
                    offset_y,
                    tolerance,
                    image_format,
                    scaling_method);
  ren.apply(scale_denominator);
  
  return ren.painted();
}
Ejemplo n.º 22
0
void render_layer2(const mapnik::Map& map,
                   mapnik::image_32& image,
                   unsigned layer_idx)
{
    std::vector<mapnik::layer> const& layers = map.layers();
    std::size_t layer_num = layers.size();
    if (layer_idx >= layer_num) {
        std::ostringstream s;
        s << "Zero-based layer index '" << layer_idx << "' not valid, only '"
          << layer_num << "' layers are in map\n";
        throw std::runtime_error(s.str());
    }

    python_unblock_auto_block b;
    mapnik::layer const& layer = layers[layer_idx];
    mapnik::agg_renderer<mapnik::image_32> ren(map,image,1.0,0,0);
    std::set<std::string> names;
    ren.apply(layer,names);
}
Ejemplo n.º 23
0
void render_layer2(mapnik::Map const& map,
                   mapnik::image_any& image,
                   unsigned layer_idx,
                   double scale_factor,
                   unsigned offset_x,
                   unsigned offset_y)
{
    std::vector<mapnik::layer> const& layers = map.layers();
    std::size_t layer_num = layers.size();
    if (layer_idx >= layer_num) {
        std::ostringstream s;
        s << "Zero-based layer index '" << layer_idx << "' not valid, only '"
          << layer_num << "' layers are in map\n";
        throw std::runtime_error(s.str());
    }

    python_unblock_auto_block b;
    mapnik::layer const& layer = layers[layer_idx];
    std::set<std::string> names;
    mapnik::util::apply_visitor(agg_renderer_visitor_4(map, scale_factor, offset_x, offset_y, layer, names), image);
}
Ejemplo n.º 24
0
//std
#include <string>
#include <fstream>
#include <streambuf>

// libprotobuf
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#include "vector_tile.pb.h"
#pragma GCC diagnostic pop

TEST_CASE("pbf vector tile input")
{
    unsigned tile_size = 4096;
    mapnik::Map map(256,256,"+init=epsg:3857");
    mapnik::layer lyr("layer",map.srs());
    lyr.set_datasource(testing::build_ds(0,0));
    map.add_layer(lyr);
    mapnik::vector_tile_impl::processor ren(map);
    mapnik::vector_tile_impl::tile out_tile = ren.create_tile(0,0,0,tile_size);
    CHECK(out_tile.is_painted() == true);
    CHECK(out_tile.is_empty() == false);
    vector_tile::Tile tile;
    tile.ParseFromString(out_tile.get_buffer());
    // serialize to message
    std::string buffer;
    CHECK(tile.SerializeToString(&buffer));
    CHECK(147 == buffer.size());
    // now create new objects
    mapnik::Map map2(256,256,"+init=epsg:3857");
Ejemplo n.º 25
0
void render_cairo(mapnik::Map const& map, double scaling_factor, QPixmap & pix)
{

#ifdef HAVE_CAIRO
    mapnik::cairo_surface_ptr image_surface(cairo_image_surface_create(CAIRO_FORMAT_ARGB32,map.width(),map.height()),
                                            mapnik::cairo_surface_closer());
    mapnik::cairo_renderer<mapnik::cairo_surface_ptr> renderer(map, image_surface, scaling_factor);
    {
        mapnik::auto_cpu_timer t(std::clog, "rendering took: ");
        renderer.apply();
    }
    image_32 buf(image_surface);
    QImage image((uchar*)buf.raw_data(),buf.width(),buf.height(),QImage::Format_ARGB32);
    pix = QPixmap::fromImage(image.rgbSwapped());
#endif
}
Ejemplo n.º 26
0
#include "catch.hpp"

#include <mapnik/map.hpp>
#include <mapnik/feature_type_style.hpp>
#include <mapnik/rule.hpp>

TEST_CASE("map")
{
    SECTION("map resize")
    {
        mapnik::Map map(256, 256);

        CHECK(map.width() == 256);
        CHECK(map.height() == 256);

        map.resize(0, 0);

        CHECK(map.width() == 256);
        CHECK(map.height() == 256);

        map.resize(0, 1);

        CHECK(map.width() == 256);
        CHECK(map.height() == 256);

        map.resize(1, 0);

        CHECK(map.width() == 256);
        CHECK(map.height() == 256);

        map.resize(1, 1);
Ejemplo n.º 27
0
            // create a renderable map with a fontset and a text symbolizer
            // and do not register any fonts, to ensure the error thrown is reasonable
            mapnik::context_ptr ctx = std::make_shared<mapnik::context_type>();
            ctx->push("name");
            mapnik::feature_ptr feature(mapnik::feature_factory::create(ctx,1));
            mapnik::transcoder tr("utf-8");
            mapnik::value_unicode_string ustr = tr.transcode("hello world!");
            feature->put("name",ustr);
            mapnik::geometry::point<double> pt(128,128);
            feature->set_geometry(std::move(pt));

            mapnik::parameters params;
            params["type"]="memory";
            auto ds = std::make_shared<mapnik::memory_datasource>(params);
            ds->push(feature);
            mapnik::Map m(256,256);
            mapnik::font_set fontset("fontset");
            // NOTE: this is a valid font, but will fail because none are registered
            fontset.add_face_name("DejaVu Sans Book");
            m.insert_fontset("fontset", fontset);
            mapnik::layer lyr("layer");
            lyr.set_datasource(ds);
            lyr.add_style("style");
            m.add_layer(lyr);
            mapnik::feature_type_style the_style;
            mapnik::rule r;
            mapnik::text_symbolizer text_sym;
            mapnik::text_placements_ptr placement_finder = std::make_shared<mapnik::text_placements_dummy>();
            placement_finder->defaults.format_defaults.face_name = "DejaVu Sans Book";
            placement_finder->defaults.format_defaults.text_size = 10.0;
            placement_finder->defaults.format_defaults.fill = mapnik::color(0,0,0);
Ejemplo n.º 28
0
void insert_fontset(mapnik::Map & m, std::string const& name, mapnik::font_set const& fontset)
{
    m.insert_fontset(name,fontset);
}
Ejemplo n.º 29
0
void insert_style(mapnik::Map & m, std::string const& name, mapnik::feature_type_style const& style)
{
    m.insert_style(name,style);
}
Ejemplo n.º 30
0
style_range _styles_ (mapnik::Map const& m)
{
    return style_range(
        boost::make_transform_iterator<extract_style>(m.begin_styles(), extract_style()),
        boost::make_transform_iterator<extract_style>(m.end_styles(), extract_style()));
}