[Mapserver-users] area Calculation in polygon-shapes

Jan Hartmann jhart at frw.uva.nl
Mon Feb 2 06:43:03 PST 2004


Margaritha Vogt wrote:
> Hello,
> is there an existing function in PHP-MapScript to calculate areas from a 
> polygon-shape
> (like "shape.return area" in ArcView).
> Or any other possibility to get area and perimeters from shapes?
> Thanks for answering
> Maggy
> 

I don't think there is such a function in PHP-MapScript (there is one in 
PostGIS). It is quite easy to write one however. To get the area, just 
iterate through the points of the ShapeObj and apply the formula below. 
Perimeter is even simpler: get the length of each line segment 
(Pythagoras) and sum those.

quoted from:

http://www.faqs.org/faqs/graphics/algorithms-faq/ , section 2.01:

--------------------------------------------------------------------

The signed area can be computed in linear time by a simple sum.
     The key formula is this:

         If the coordinates of vertex v_i are x_i and y_i,
         twice the signed area of a polygon is given by

         2 A( P ) = sum_{i=0}^{n-1} (x_i y_{i+1} - y_i x_{i+1}).

     Here n is the number of vertices of the polygon, and index
     arithmetic is mod n, so that x_n = x_0, etc. A rearrangement
     of terms in this equation can save multiplications and operate on
     coordinate differences, and so may be both faster and more
     accurate:

        2 A(P) = sum_{i=0}^{n-1} ( x_i  (y_{i+1} - y_{i-1}) )

     Here again modular index arithmetic is implied, with n=0 and -1=n-1.
     This can be avoided by extending the x[] and y[] arrays up to [n+1]
     with x[n]=x[0], y[n]=y[0] and y[n+1]=y[1], and using instead

        2 A(P) = sum_{i=1}^{n} ( x_i  (y_{i+1} - y_{i-1}) )

-----------------------------------------------------------------------

HTH,

Jan







More information about the MapServer-users mailing list