added an angle_mode attribute to GlyphSymbolizer to define the way the angle expression should be interpreted: as an azimuth or as a trigonometric angle

This commit is contained in:
Alberto Valverde 2010-03-22 11:21:17 +00:00
parent 415029c0ca
commit c79865586b
8 changed files with 89 additions and 6 deletions

View file

@ -1,10 +1,15 @@
#include <mapnik/glyph_symbolizer.hpp>
#include <boost/python.hpp>
#include <mapnik/glyph_symbolizer.hpp>
#include "mapnik_enumeration.hpp"
#include <boost/tuple/tuple.hpp>
using mapnik::glyph_symbolizer;
using mapnik::position;
using mapnik::enumeration_;
using mapnik::angle_mode_e;
using mapnik::AZIMUTH;
using mapnik::TRIGONOMETRIC;
using namespace boost::python;
list get_displacement_list(const glyph_symbolizer& t)
@ -20,6 +25,11 @@ list get_displacement_list(const glyph_symbolizer& t)
void export_glyph_symbolizer()
{
enumeration_<angle_mode_e>("angle_mode")
.value("AZIMUTH", AZIMUTH)
.value("TRIGONOMETRIC", TRIGONOMETRIC)
;
class_<glyph_symbolizer>("GlyphSymbolizer",
init<std::string,mapnik::expression_ptr>())
.add_property("face_name",
@ -68,6 +78,13 @@ void export_glyph_symbolizer()
"Get/Set the angle expression used to rotate the glyph "
"along its center."
)
.add_property("angle_mode",
&glyph_symbolizer::get_angle_mode,
&glyph_symbolizer::set_angle_mode,
"Get/Set the angle_mode property. This controls how the "
"angle is interpreted. Valid values are AZIMUTH and "
"TRIGONOMETRIC."
)
.add_property("value",
&glyph_symbolizer::get_value,
&glyph_symbolizer::set_value,

View file

@ -160,6 +160,11 @@ const markers_symbolizer& markers_( const symbolizer& symbol )
return boost::get<markers_symbolizer>(symbol);
}
const glyph_symbolizer& glyph_( const symbolizer& symbol )
{
return boost::get<glyph_symbolizer>(symbol);
}
void export_symbolizer()
{
using namespace boost::python;
@ -198,6 +203,9 @@ void export_symbolizer()
.def("markers",markers_,
return_value_policy<copy_const_reference>())
.def("glyph",glyph_,
return_value_policy<copy_const_reference>())
;
}

View file

@ -2,6 +2,7 @@
#define GLYPH_SYMBOLIZER_HPP
// mapnik
#include <mapnik/enumeration.hpp>
#include <mapnik/raster_colorizer.hpp>
#include <mapnik/expression_node.hpp>
#include <mapnik/text_path.hpp>
@ -18,6 +19,14 @@ namespace mapnik
typedef boost::tuple<double,double> position;
enum angle_mode_enum {
AZIMUTH,
TRIGONOMETRIC,
angle_mode_enum_MAX
};
DEFINE_ENUM(angle_mode_e, angle_mode_enum);
struct MAPNIK_DECL glyph_symbolizer
{
glyph_symbolizer(std::string face_name, expression_ptr c)
@ -32,7 +41,8 @@ struct MAPNIK_DECL glyph_symbolizer
avoid_edges_(false),
displacement_(0.0, 0.0),
halo_fill_(color(255,255,255)),
halo_radius_(0) {}
halo_radius_(0),
angle_mode_(TRIGONOMETRIC) {}
std::string const& get_face_name() const
{
@ -142,6 +152,14 @@ struct MAPNIK_DECL glyph_symbolizer
{
colorizer_ = colorizer;
}
void set_angle_mode(angle_mode_e angle_mode)
{
angle_mode_ = angle_mode;
}
angle_mode_e get_angle_mode() const
{
return angle_mode_;
}
text_path_ptr get_text_path(face_set_ptr const& faces,
@ -165,6 +183,7 @@ private:
position displacement_;
color halo_fill_;
unsigned halo_radius_;
angle_mode_e angle_mode_;
};
}; // end mapnik namespace

View file

@ -4,6 +4,13 @@
namespace mapnik
{
static const char * angle_mode_strings[] = {
"azimuth",
"trigonometric",
""
};
IMPLEMENT_ENUM( mapnik::angle_mode_e, angle_mode_strings );
text_path_ptr glyph_symbolizer::get_text_path(face_set_ptr const& faces,
Feature const& feature) const
{
@ -46,6 +53,9 @@ UnicodeString glyph_symbolizer::eval_char(Feature const& feature) const
evaluate<Feature,value_type>(feature),
*expr
);
#ifdef MAPNIK_DEBUG
std::clog << "char_result=" << result.to_string() << "\n";
#endif
return result.to_unicode();
}
@ -58,11 +68,14 @@ double glyph_symbolizer::eval_angle(Feature const& feature) const
evaluate<Feature,value_type>(feature),
*expr
);
#ifdef MAPNIK_DEBUG
std::clog << "angle_result=" << result.to_string() << "\n";
#endif
angle = result.to_double();
// normalize to first rotation in case an expression has made it go past
angle = std::fmod(angle, 360);
angle *= (M_PI/180); // convert to radians
if (true) { //TODO: if (get_mode()==AZIMUTH)
if (get_angle_mode()==AZIMUTH) {
// angle is an azimuth, convert into trigonometric angle
angle = std::atan2(std::cos(angle), std::sin(angle));
}
@ -80,7 +93,14 @@ unsigned glyph_symbolizer::eval_size(Feature const& feature) const
evaluate<Feature,value_type>(feature),
*expr
);
return static_cast<unsigned>(result.to_int());
#ifdef MAPNIK_DEBUG
std::clog << "size_result=" << result.to_string() << "\n";
#endif
unsigned size = static_cast<unsigned>(result.to_int());
#ifdef MAPNIK_DEBUG
std::clog << "size=" << size << "\n";
#endif
return size;
}
color glyph_symbolizer::eval_color(Feature const& feature) const
@ -97,6 +117,9 @@ color glyph_symbolizer::eval_color(Feature const& feature) const
evaluate<Feature,value_type>(feature),
*value_expr
);
#ifdef MAPNIK_DEBUG
std::clog << "value_result=" << value_result.to_string() << "\n";
#endif
return colorizer->get_color((float)value_result.to_double());
} else {
expression_ptr color_expr = get_color();
@ -105,6 +128,9 @@ color glyph_symbolizer::eval_color(Feature const& feature) const
evaluate<Feature,value_type>(feature),
*color_expr
);
#ifdef MAPNIK_DEBUG
std::clog << "color_result=" << color_result.to_string() << "\n";
#endif
return color(color_result.to_string());
} else {
return color("black");

View file

@ -1544,6 +1544,10 @@ namespace mapnik
get_opt_attr<std::string>(sym, "angle");
if (angle)
glyph_sym.set_angle(parse_expression(*angle, "utf8"));
angle_mode_e angle_mode =
get_attr<angle_mode_e>(sym, "angle_mode", TRIGONOMETRIC);
glyph_sym.set_angle_mode(angle_mode);
// value
optional<std::string> value =

View file

@ -321,6 +321,10 @@ namespace mapnik
// halo fill & radius
set_attr( node, "halo_fill", sym.get_halo_fill() );
set_attr( node, "halo_radius", sym.get_halo_radius() );
// angle_mode
set_attr( node, "angle_mode", sym.get_angle_mode() );
}
private:

View file

@ -10,7 +10,8 @@
halo_fill="rgba(0%,0%,0%,4%)"
halo_radius="1"
value="[value]"
angle="[azumuth]+90"
angle="[azimuth]+90"
angle_mode="azimuth"
>
<RasterColorizer>
<ColorBand value="0" color="#0044cc"/>

View file

@ -43,6 +43,10 @@ def test_load_save_load_map():
map = mapnik2.Map(256,256)
in_map = "../data/good_maps/glyph_symbolizer.xml"
mapnik2.load_map(map, in_map)
style = map.find_style('arrows')
sym = style.rules[0].symbols[0]
assert isinstance(sym, mapnik2.GlyphSymbolizer)
assert sym.angle_mode == mapnik2.angle_mode.AZIMUTH
out_map = mapnik2.save_map_to_string(map).decode('utf8')
map = mapnik2.Map(256,256)