added label_position method for geometry objects (todo line_string)

This commit is contained in:
Artem Pavlenko 2006-01-26 16:43:10 +00:00
parent 4ed7158908
commit b89195f9e6
2 changed files with 63 additions and 23 deletions

View file

@ -56,6 +56,7 @@ namespace mapnik
virtual int type() const=0;
virtual bool hit_test(value_type x,value_type y) const=0;
virtual void label_position(double *x, double *y) const=0;
virtual void move_to(value_type x,value_type y)=0;
virtual void line_to(value_type x,value_type y)=0;
virtual void transform(const mapnik::CoordTransform& t)=0;
@ -82,6 +83,11 @@ namespace mapnik
{
return Point;
}
void label_position(double *x, double *y) const
{
*x = pt_.x;
*y = pt_.y;
}
void move_to(value_type x,value_type y)
{
@ -139,6 +145,40 @@ namespace mapnik
return Polygon;
}
void label_position(double *x, double *y) const
{
unsigned size = cont_.size();
if (size < 3)
{
cont_.get_vertex(0,x,y);
return;
}
value_type x0,y0,x1,y1;
double ai;
double atmp = 0;
double xtmp = 0;
double ytmp = 0;
unsigned i,j;
for (i = size-1,j = 0; j < size; i = j, ++j)
{
cont_.get_vertex(i,&x0,&y0);
cont_.get_vertex(j,&x1,&y1);
ai = x0 * y1 - x1 * y0;
atmp += ai;
xtmp += (x1 + x0) * ai;
ytmp += (y1 + y0) * ai;
}
if (atmp != 0)
{
*x = xtmp/(3*atmp);
*y = ytmp /(3*atmp);
return;
}
*x=x0;
*y=y0;
}
void line_to(value_type x,value_type y)
{
cont_.push_back(x,y,SEG_LINETO);
@ -200,7 +240,10 @@ namespace mapnik
{
return LineString;
}
void label_position(double *x, double *y) const
{
cont_.get_vertex(0,x,y);
}
void line_to(value_type x,value_type y)
{
cont_.push_back(x,y,SEG_LINETO);

View file

@ -29,36 +29,33 @@ namespace mapnik
unsigned width,unsigned height)
: symbolizer(),
symbol_(width,height)
{
try
{
try
{
std::auto_ptr<ImageReader> reader(get_image_reader(type,file));
reader->read(0,0,symbol_);
}
catch (...)
{
std::cerr<<"exception caught..." << std::endl;
}
std::auto_ptr<ImageReader> reader(get_image_reader(type,file));
reader->read(0,0,symbol_);
}
catch (...)
{
std::cerr<<"exception caught..." << std::endl;
}
}
void image_symbolizer::render(Feature const& feat,CoordTransform const& t,Image32& image) const
{
typedef coord_transform<CoordTransform,geometry_type> path_type;
geometry_ptr const& geom=feat.get_geometry();
if (geom)
{
path_type path(t,*geom);
unsigned num_points=geom->num_points();
double x;
double y;
geom->label_position(&x,&y);
t.forward_x(&x);
t.forward_y(&y);
int w=symbol_.width();
int h=symbol_.height();
double x,y;
for(unsigned i=0;i<num_points;++i)
{
path.vertex(&x,&y);
int px=int(x - 0.5 * w);
int py=int(y - 0.5 * h);
image.set_rectangle_alpha(px,py,symbol_);
}
int h=symbol_.height();
int px=int(x - 0.5 * w);
int py=int(y - 0.5 * h);
image.set_rectangle_alpha(px,py,symbol_);
}
}
}