[mapserver-users] PHP/MapScript : Retrieving the centroid of a shape

Stephen Woodbridge woodbri at swoodbridge.com
Tue Jun 11 10:29:47 EDT 2002


I don't know, it is just seemed to be a common sense solution to your
problem. Glad it worked out.

-Steve W.

LACROIX Gilles wrote:
> 
> Thanks for the trick !
> 
> The point layer used as an annotation layer was the right solution.
> I also tried the algorithm and it worked fine.
> 
> Is this algorithm exactly the one used by MapServer ?
> 
> Stephen Woodbridge a écrit :
> >
> > LACROIX Gilles wrote:
> > >
> > > I am trying to use PHP/MapScript to display (dynamic) labels on selected
> > > shapes from a layer. To do so, I need to retrieve the "centroid" of
> > > these shapes (a basic "middle of the bounding box" algorithm doesn't
> > > help because the centroid must fall into the shape, whatever the shape
> > > looks like). Is it possible with MapScript ?
> >
> > First, the centroid is NOT required to be inside the polygon, think of
> > an area like the letter "C".
> >
> > One way to do this is to construct a point layer that can be used as an
> > annotation layer for labeling the polygons, where the point is the label
> > location. You could construct this layer using php, perl or C using this
> > algorithm:
> >
> > 1) get the bounding box of the polygon.
> > 2) set X to the middle of the bounding box
> > 3) intersect every polygon edge with the line X=middle and make a list
> > of intersection points (most will not instersect)
> > 4) sort the points in Y
> > 5) set the label point to the midpoint of (X,Y1, X, Y2) which is
> > garanteed to be inside the polygon.
> >
> > You can adjust this to set Y and intersect for X, based on whether you
> > have tall narrow or short wide extents. also if you have only a few
> > poorly placed points you can just edit their location to a more
> > suitiable position.
> >
> > Hope this helps,
> >   -Steve
> 
> --
> Gilles LACROIX                              | mailto: lacroix at bertin.fr
> Bertin Technologies - BP 284                |
> 10, avenue Ampere - Montigny-le-Bretonneux  | Tel: +33 (0)1 39 30 62 66
> 78053 Saint Quentin en Yvelines Cedex       |



More information about the mapserver-users mailing list