calculate polygon area

Ed McNierney ed at TOPOZONE.COM
Tue Jul 18 13:12:58 EDT 2006


Julien -

If you are displaying an image in a "geographic" projection using
latitude and longitude as if they were X and Y coordinates, you have a
very challenging problem ahead of you.  If someone draws a polygon on
that map, the polygon will only have straight sides in "geographic"
space, and you can compute the area of that polygon in "square degrees".
Since the concept of a "square degree" is pretty meaningless, that is
probably not what you want.  Each row of pixels on your image - measured
in something standard like square meters - will be a different size, so
it's difficult to calculate the size.  You are letting users draw
polygons on the surface on an ellipsoid (approximately) so you would
need to do ellipsoidal geometry to calculate the area of that polygon in
reasonable units.

You cannot reproject the vertices into a standard projection and then
calculate the area, because the lines between those vertices won't be
straight any more - they will be curves.

If you use a useful output projection (such as UTM or a standard
regional projection) then the math should be quite simple.  Otherwise
you'll need to start coding up some elliptical geometry.

	- Ed

Ed McNierney
President and Chief Mapmaker
TopoZone.com / Maps a la carte, Inc.
73 Princeton Street, Suite 305
North Chelmsford, MA  01863
ed at topozone.com
(978) 251-4242 

-----Original Message-----
From: UMN MapServer Users List [mailto:MAPSERVER-USERS at LISTS.UMN.EDU] On
Behalf Of Julien Bessiere
Sent: Tuesday, July 18, 2006 9:13 AM
To: MAPSERVER-USERS at LISTS.UMN.EDU
Subject: [UMN_MAPSERVER-USERS] calculate polygon area

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],$polygo
n_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