calculate polygon area

Julien Bessiere julien.63 at FREE.FR
Tue Jul 18 09:12:43 EDT 2006


Hi,
I'm using phpMapscript / Mapserver.
I wrote a little script that allows users to draw polygons on the map. I try to
figure out how to calculate the area of this polygon, but so far...
I found a formula and wrote the script below but as i'm using decimal latitude
and longitude, the result is unusable :
So my questions is :

1- Does phpmapscript has a function to calculate area
(something like distanceToPoint(pointObj poPoint))?
2- If no, how to calculate area wiht lat long coordinates?

thanks for your advices,

Julien

============================================================
function polygonArea($polygon_coordinates){
        $array_length = count($polygon_coordinates);
        if ($array_length > 2){
                // add the first coordinates at the end to close de polygon

array_push($polygon_coordinates,array($polygon_coordinates[0][0],$polygon_coordinates[0][1]));
                $polygon_area = 0;
                for ($i=0;$i <= $array_length;$i++){
                        $polygon_area += (
                                ($polygon_coordinates[$i][0] *
$polygon_coordinates[$i+1][1])-
                                ($polygon_coordinates[$i][1] *
$polygon_coordinates[$i+1][0]));
                }
                $polygon_area = abs($polygon_area / 2);
        }
        else {
                $polygon_area = 'need at least 3 points';
        }
        return $polygon_area;
}
=====================================================================



More information about the mapserver-users mailing list