Skip to content

Commit

Permalink
+ impl metawriter_renderer - initial stub
Browse files Browse the repository at this point in the history
  • Loading branch information
artemp committed Jun 8, 2012
1 parent 03e6f9d commit b37212d
Show file tree
Hide file tree
Showing 25 changed files with 881 additions and 333 deletions.
32 changes: 17 additions & 15 deletions bindings/python/mapnik_map.cpp
Expand Up @@ -150,16 +150,17 @@ bool has_metawriter(mapnik::Map const& m)


// returns empty shared_ptr when the metawriter isn't found, or is // returns empty shared_ptr when the metawriter isn't found, or is
// of the wrong type. empty pointers make it back to Python as a None. // of the wrong type. empty pointers make it back to Python as a None.
mapnik::metawriter_inmem_ptr find_inmem_metawriter(const mapnik::Map & m, std::string const& name) {
mapnik::metawriter_ptr metawriter = m.find_metawriter(name);
mapnik::metawriter_inmem_ptr inmem;


if (metawriter) { //mapnik::metawriter_inmem_ptr find_inmem_metawriter(const mapnik::Map & m, std::string const& name) {
inmem = boost::dynamic_pointer_cast<mapnik::metawriter_inmem>(metawriter); // mapnik::metawriter_ptr metawriter = m.find_metawriter(name);
} /// mapnik::metawriter_inmem_ptr inmem;


return inmem; // if (metawriter) {
} // inmem = boost::dynamic_pointer_cast<mapnik::metawriter_inmem>(metawriter);
// }
//
// return inmem;
//}


// TODO - we likely should allow indexing by negative number from python // TODO - we likely should allow indexing by negative number from python
// for now, protect against negative values and kindly throw // for now, protect against negative values and kindly throw
Expand Down Expand Up @@ -482,13 +483,14 @@ void export_map()
"\n" "\n"
"Use a path like \"[z]/[x]/[y].json\" to create filenames.\n" "Use a path like \"[z]/[x]/[y].json\" to create filenames.\n"
) )
.def("find_inmem_metawriter", find_inmem_metawriter,
(arg("name")), // .def("find_inmem_metawriter", find_inmem_metawriter,
"Gets an inmem metawriter, or None if no such metawriter " // (arg("name")),
"exists.\n" // "Gets an inmem metawriter, or None if no such metawriter "
"Use this after the map has been rendered to retrieve information " // "exists.\n"
"about the hit areas rendered on the map.\n" // "Use this after the map has been rendered to retrieve information "
) // "about the hit areas rendered on the map.\n"
// )


.def("__deepcopy__",&map_deepcopy) .def("__deepcopy__",&map_deepcopy)
.add_property("parameters",make_function(params_nonconst,return_value_policy<reference_existing_object>()),"TODO") .add_property("parameters",make_function(params_nonconst,return_value_policy<reference_existing_object>()),"TODO")
Expand Down
68 changes: 68 additions & 0 deletions demo/viewer/mainwindow.cpp
Expand Up @@ -39,6 +39,12 @@
#include <mapnik/config_error.hpp> #include <mapnik/config_error.hpp>
#include <mapnik/load_map.hpp> #include <mapnik/load_map.hpp>
#include <mapnik/save_map.hpp> #include <mapnik/save_map.hpp>
#include <mapnik/metawriter_renderer.hpp>
#ifdef HAVE_CAIRO
// cairo
#include <mapnik/cairo_renderer.hpp>
#include <cairomm/surface.h>
#endif
#endif #endif


// qt // qt
Expand All @@ -49,6 +55,8 @@
#include "layerdelegate.hpp" #include "layerdelegate.hpp"
#include "about_dialog.hpp" #include "about_dialog.hpp"




MainWindow::MainWindow() MainWindow::MainWindow()
: filename_(), : filename_(),
default_extent_(-20037508.3428,-20037508.3428,20037508.3428,20037508.3428) default_extent_(-20037508.3428,-20037508.3428,20037508.3428,20037508.3428)
Expand Down Expand Up @@ -256,6 +264,56 @@ void MainWindow::export_as()
} }
} }


void MainWindow::export_as_pdf()
{
QAction *action = qobject_cast<QAction *>(sender());
QString initialPath = QDir::currentPath() + "/mapnik-cairo.pdf";

QString fileName = QFileDialog::getSaveFileName(this, tr("Export As PDF"),
initialPath,
tr("%1 Files (*.%2);;All Files (*)")
.arg(QString("PDF"))
.arg(QString("pdf")));
if (!fileName.isEmpty())
{
std::cout << "FILE NAME:" << fileName.toStdString() << std::endl;
#ifdef HAVE_CAIRO
boost::shared_ptr<mapnik::Map> map_ptr = mapWidget_->getMap();
if (map_ptr)
{
Cairo::RefPtr<Cairo::Surface> surface;
surface = Cairo::PdfSurface::create(fileName.toStdString().c_str(), map_ptr->width(),map_ptr->height());
mapnik::cairo_renderer<Cairo::Surface> pdf_render(*map_ptr, surface);
pdf_render.apply();
}
#endif
}
}

void MainWindow::export_as_meta()
{
QAction *action = qobject_cast<QAction *>(sender());
QString initialPath = QDir::currentPath() + "/mapnik-meta.json";

QString fileName = QFileDialog::getSaveFileName(this, tr("Export As Meta JSON"),
initialPath,
tr("%1 Files (*.%2);;All Files (*)")
.arg(QString("META"))
.arg(QString("json")));
if (!fileName.isEmpty())
{
std::cout << "FILE NAME:" << fileName.toStdString() << std::endl;
#ifdef HAVE_CAIRO
boost::shared_ptr<mapnik::Map> map_ptr = mapWidget_->getMap();
if (map_ptr)
{
mapnik::metawriter_renderer ren(*map_ptr);
ren.apply();
}
#endif
}
}

void MainWindow::print() void MainWindow::print()
{ {


Expand Down Expand Up @@ -328,6 +386,14 @@ void MainWindow::createActions()
exportAsActs.append(action); exportAsActs.append(action);
} }


// export PDF
exportPdfAction = new QAction(QString("PDF (cairo)"), this);
connect(exportPdfAction, SIGNAL(triggered()), this, SLOT(export_as_pdf()));

// export META Json
exportMetaAction = new QAction(QString("META (json)"), this);
connect(exportMetaAction, SIGNAL(triggered()), this, SLOT(export_as_meta()));

printAct = new QAction(QIcon(":/images/print.png"),tr("&Print ..."),this); printAct = new QAction(QIcon(":/images/print.png"),tr("&Print ..."),this);
printAct->setShortcut(tr("Ctrl+E")); printAct->setShortcut(tr("Ctrl+E"));
connect(printAct, SIGNAL(triggered()), this, SLOT(print())); connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
Expand All @@ -349,6 +415,8 @@ void MainWindow::createMenus()
fileMenu = new QMenu(tr("&File"),this); fileMenu = new QMenu(tr("&File"),this);
fileMenu->addAction(openAct); fileMenu->addAction(openAct);
fileMenu->addAction(saveAct); fileMenu->addAction(saveAct);
fileMenu->addAction(exportPdfAction);
fileMenu->addAction(exportMetaAction);
fileMenu->addMenu(exportMenu); fileMenu->addMenu(exportMenu);
fileMenu->addAction(printAct); fileMenu->addAction(printAct);
fileMenu->addSeparator(); fileMenu->addSeparator();
Expand Down
4 changes: 4 additions & 0 deletions demo/viewer/mainwindow.hpp
Expand Up @@ -54,6 +54,8 @@ public slots:
void pan(); void pan();
void info(); void info();
void export_as(); void export_as();
void export_as_pdf();
void export_as_meta();
void open(QString const& path = QString()); void open(QString const& path = QString());
void reload(); void reload();
void save(); void save();
Expand Down Expand Up @@ -88,6 +90,8 @@ public slots:
QAction *infoAct; QAction *infoAct;
QAction *openAct; QAction *openAct;
QAction *saveAct; QAction *saveAct;
QAction *exportPdfAction;
QAction *exportMetaAction;
QAction *printAct; QAction *printAct;
QAction *exitAct; QAction *exitAct;
QAction *aboutAct; QAction *aboutAct;
Expand Down
12 changes: 6 additions & 6 deletions include/mapnik/map.hpp
Expand Up @@ -28,7 +28,7 @@
#include <mapnik/feature_type_style.hpp> #include <mapnik/feature_type_style.hpp>
#include <mapnik/datasource.hpp> #include <mapnik/datasource.hpp>
#include <mapnik/layer.hpp> #include <mapnik/layer.hpp>
#include <mapnik/metawriter.hpp> #include <mapnik/metawriter_factory.hpp>
#include <mapnik/params.hpp> #include <mapnik/params.hpp>


// boost // boost
Expand Down Expand Up @@ -72,7 +72,7 @@ class MAPNIK_DECL Map
boost::optional<color> background_; boost::optional<color> background_;
boost::optional<std::string> background_image_; boost::optional<std::string> background_image_;
std::map<std::string,feature_type_style> styles_; std::map<std::string,feature_type_style> styles_;
std::map<std::string,metawriter_ptr> metawriters_; std::map<std::string,metawriter> metawriters_;
std::map<std::string,font_set> fontsets_; std::map<std::string,font_set> fontsets_;
std::vector<layer> layers_; std::vector<layer> layers_;
aspect_fix_mode aspectFixMode_; aspect_fix_mode aspectFixMode_;
Expand All @@ -87,7 +87,7 @@ class MAPNIK_DECL Map
typedef std::map<std::string,feature_type_style>::iterator style_iterator; typedef std::map<std::string,feature_type_style>::iterator style_iterator;
typedef std::map<std::string,font_set>::const_iterator const_fontset_iterator; typedef std::map<std::string,font_set>::const_iterator const_fontset_iterator;
typedef std::map<std::string,font_set>::iterator fontset_iterator; typedef std::map<std::string,font_set>::iterator fontset_iterator;
typedef std::map<std::string,metawriter_ptr>::const_iterator const_metawriter_iterator; typedef std::map<std::string,metawriter>::const_iterator const_metawriter_iterator;


/*! \brief Default constructor. /*! \brief Default constructor.
* *
Expand Down Expand Up @@ -173,7 +173,7 @@ class MAPNIK_DECL Map
* @return true If success. * @return true If success.
* @return false If no success. * @return false If no success.
*/ */
bool insert_metawriter(std::string const& name, metawriter_ptr const& writer); bool insert_metawriter(std::string const& name, metawriter const& writer);


/*! \brief Remove a metawriter from the map. /*! \brief Remove a metawriter from the map.
* @param name The name of the writer. * @param name The name of the writer.
Expand All @@ -184,12 +184,12 @@ class MAPNIK_DECL Map
* @param name The name of the writer. * @param name The name of the writer.
* @return The writer if found. If not found return 0. * @return The writer if found. If not found return 0.
*/ */
metawriter_ptr find_metawriter(std::string const& name) const; metawriter find_metawriter(std::string const& name) const;


/*! \brief Get all metawriters. /*! \brief Get all metawriters.
* @return Const reference to metawriters. * @return Const reference to metawriters.
*/ */
std::map<std::string,metawriter_ptr> const& metawriters() const; std::map<std::string,metawriter> const& metawriters() const;


/*! \brief Get first iterator in metawriters. /*! \brief Get first iterator in metawriters.
* @return Constant metawriter iterator. * @return Constant metawriter iterator.
Expand Down
70 changes: 8 additions & 62 deletions include/mapnik/metawriter.hpp
Expand Up @@ -27,13 +27,12 @@
#include <mapnik/feature.hpp> #include <mapnik/feature.hpp>
#include <mapnik/ctrans.hpp> #include <mapnik/ctrans.hpp>
#include <mapnik/projection.hpp> #include <mapnik/projection.hpp>

// boost // boost
#include <boost/utility.hpp> #include <boost/utility.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/concept_check.hpp> #include <boost/concept_check.hpp>

#include <boost/variant.hpp>
// stl // stl
#include <set> #include <set>
#include <string> #include <string>
Expand Down Expand Up @@ -79,7 +78,7 @@ class metawriter_property_map
}; };




/** All properties to be output by a metawriter. */ // All properties to be output by a metawriter.
class metawriter_properties : public std::set<std::string> class metawriter_properties : public std::set<std::string>
{ {
public: public:
Expand All @@ -90,76 +89,23 @@ class metawriter_properties : public std::set<std::string>
std::string to_string() const; std::string to_string() const;
}; };


/** Abstract baseclass for all metawriter classes. */ // Abstract baseclass for all metawriter classes.
class metawriter class metawriter_base
{ {
public: public:
typedef coord_transform<CoordTransform,geometry_type> path_type; explicit metawriter_base(metawriter_properties dflt_properties) :
metawriter(metawriter_properties dflt_properties) :
dflt_properties_(dflt_properties), dflt_properties_(dflt_properties),
width_(0), width_(0),
height_(0) {} height_(0) {}
virtual ~metawriter() {}
/** Output a rectangular area. void set_size(int width, int height) { width_ = width; height_ = height; }
* \param box Area (in pixel coordinates)
* \param feature The feature being processed
* \param prj_trans Projection transformation
* \param t Coordinate transformation
* \param properties List of properties to output
*/
virtual void add_box(box2d<double> const& box, Feature const& feature,
CoordTransform const& t,
metawriter_properties const& properties)=0;
virtual void add_text(boost::ptr_vector<text_path> &placements,
box2d<double> const& extents,
Feature const& feature,
CoordTransform const& t,
metawriter_properties const& properties)=0;
virtual void add_polygon(path_type & path,
Feature const& feature,
CoordTransform const& t,
metawriter_properties const& properties)=0;
virtual void add_line(path_type & path,
Feature const& feature,
CoordTransform const& t,
metawriter_properties const& properties)=0;

/** Start processing.
* Write file header, init database connection, ...
*
* \param properties metawriter_property_map object with userdefined values.
* Useful for setting filename etc.
*/
virtual void start(metawriter_property_map const& properties)
{
boost::ignore_unused_variable_warning(properties);
}

/** Stop processing.
* Write file footer, close database connection, ...
*/
virtual void stop() {}
/** Set output size (pixels).
* All features that are completely outside this size are discarded.
*/
void set_size(int width, int height) { width_ = width; height_ = height; }
/** Set Map object's srs. */
virtual void set_map_srs(projection const& proj) { /* Not required when working with image coordinates. */ }
/** Return the list of default properties. */
metawriter_properties const& get_default_properties() const { return dflt_properties_;} metawriter_properties const& get_default_properties() const { return dflt_properties_;}
protected: protected:
metawriter_properties dflt_properties_; metawriter_properties dflt_properties_;
/** Output width (pixels). */
int width_; int width_;
/** Output height (pixels). */
int height_; int height_;
}; };


/** Shared pointer to metawriter object. */
typedef boost::shared_ptr<metawriter> metawriter_ptr;
/** Metawriter object + properties. */
typedef std::pair<metawriter_ptr, metawriter_properties> metawriter_with_properties;

} }


#endif // MAPNIK_METAWRITER_HPP #endif // MAPNIK_METAWRITER_HPP

0 comments on commit b37212d

Please sign in to comment.