Merge pull request #1050 from rfw/text-placements-python

Add text placements to Python API.
This commit is contained in:
Hermann Kraus 2012-01-24 18:11:13 -08:00
commit 0c5fc9f246
6 changed files with 97 additions and 0 deletions

View file

@ -59,6 +59,16 @@ void set_anchor(text_symbolizer & t, boost::python::tuple arg)
t.set_anchor(extract<double>(arg[0]),extract<double>(arg[1]));
}
void set_placement_options(text_symbolizer & t, placement_type_e arg, std::string const& placements)
{
t.set_placement_options(arg, placements);
}
void set_placement_options_2(text_symbolizer & t, placement_type_e arg)
{
t.set_placement_options(arg, "");
}
}
struct text_symbolizer_pickle_suite : boost::python::pickle_suite
@ -195,6 +205,11 @@ void export_text_symbolizer()
.value("CAPITALIZE",CAPITALIZE)
;
enumeration_<placement_type_e>("placement_type")
.value("SIMPLE",T_SIMPLE)
.value("DUMMY",T_DUMMY)
;
class_<text_symbolizer>("TextSymbolizer",init<expression_ptr,std::string const&, unsigned,color const&>())
/*
// todo - all python classes can have kwargs and default constructors
@ -287,6 +302,10 @@ void export_text_symbolizer()
&text_symbolizer::get_text_opacity,
&text_symbolizer::set_text_opacity,
"Set/get the text opacity")
.add_property("placement",
&text_symbolizer::get_label_placement,
&text_symbolizer::set_label_placement,
"Set/get the placement of the label")
.add_property("text_transform",
&text_symbolizer::get_text_transform,
&text_symbolizer::set_text_transform,
@ -310,5 +329,9 @@ void export_text_symbolizer()
.add_property("wrap_before",
&text_symbolizer::get_wrap_before,
&text_symbolizer::set_wrap_before)
.add_property("placement_type", &text_symbolizer::get_placement_type)
.add_property("placements", &text_symbolizer::get_placements)
.def("set_placement_options", set_placement_options)
.def("set_placement_options", set_placement_options_2)
;
}

View file

@ -93,6 +93,15 @@ enum text_transform
DEFINE_ENUM( text_transform_e, text_transform );
enum placement_type
{
T_SIMPLE = 0,
T_DUMMY,
placement_type_MAX
};
DEFINE_ENUM( placement_type_e, placement_type );
class text_placements;
class text_placement_info : boost::noncopyable

View file

@ -51,6 +51,7 @@ public:
text_placements_simple(std::string positions);
text_placement_info_ptr get_placement_info() const;
void set_positions(std::string positions);
std::string get_positions() const;
private:
std::string positions_;
std::vector<directions_t> direction_;

View file

@ -123,6 +123,9 @@ struct MAPNIK_DECL text_symbolizer : public symbolizer_base
justify_alignment_e get_justify_alignment() const;
text_placements_ptr get_placement_options() const;
void set_placement_options(text_placements_ptr placement_options);
void set_placement_options(placement_type_e arg, std::string const& placements);
placement_type_e get_placement_type() const;
std::string get_placements() const;
private:
expression_ptr name_;

View file

@ -187,6 +187,11 @@ text_placements_simple::text_placements_simple()
set_positions("X");
}
std::string text_placements_simple::get_positions() const
{
return positions_;
}
text_placements_simple::text_placements_simple(std::string positions)
{
set_positions(positions);

View file

@ -22,8 +22,13 @@
//$Id$
#include <string>
//mapnik
#include <mapnik/text_symbolizer.hpp>
#include <mapnik/text_placements.hpp>
#include <mapnik/text_placements_simple.hpp>
// boost
#include <boost/scoped_ptr.hpp>
@ -85,6 +90,15 @@ static const char * text_transform_strings[] = {
IMPLEMENT_ENUM( text_transform_e, text_transform_strings )
static const char * placement_type_strings[] = {
"dummy",
"simple",
""
};
IMPLEMENT_ENUM( placement_type_e, placement_type_strings )
text_symbolizer::text_symbolizer(expression_ptr name, std::string const& face_name,
float size, color const& fill,
@ -553,5 +567,47 @@ void text_symbolizer::set_placement_options(text_placements_ptr placement_option
placement_options_ = placement_options;
}
void text_symbolizer::set_placement_options(placement_type_e arg, std::string const& placements)
{
text_placements_ptr placement_finder;
switch (arg)
{
case T_SIMPLE:
placement_finder = text_placements_ptr(
new text_placements_simple(placements));
break;
case T_DUMMY:
placement_finder = text_placements_ptr(new text_placements_dummy());
break;
default:
throw config_error(std::string("Unknown placement type"));
break;
}
this->set_placement_options(placement_finder);
}
placement_type_e text_symbolizer::get_placement_type() const
{
text_placements_ptr placement_finder = this->get_placement_options();
if (dynamic_cast<text_placements_simple *>(placement_finder.get()) != NULL)
{
return T_SIMPLE;
}
return T_DUMMY;
}
std::string text_symbolizer::get_placements() const
{
text_placements_ptr placement_finder = this->get_placement_options();
text_placements_simple *placements_simple = dynamic_cast<text_placements_simple *>(placement_finder.get());
if (placements_simple != NULL)
{
return placements_simple->get_positions();
}
return "";
}
}