Area of a Polygon in PHP/Mapscript

Rene Teniere TENIERER at GOV.NS.CA
Thu Sep 16 09:07:51 EDT 2004


Hi all,

I have been implementing Stepan Kafka's c-code for calculating area of
a polygon in php/mapscript. His code seems to work fine. The only
problem is that I keep getting a numlines result of 1 and numpoints
result of NULL for my shape BEFORE I get a chance to use it in the area
code. This, of course will give me an invalid area of 0. The numshapes
variable within the shapefile object works great, always gives me the
correct result.

Wonder if anyone could let me know what they think is going on?

Thanks,
Rene

Here is the output:
[15-Sep-2004 15:47:23] Numshapes:2
[15-Sep-2004 15:47:23] Numlines:1
[15-Sep-2004 15:47:23] Numpoints:
[15-Sep-2004 15:47:23] --------------------------
[15-Sep-2004 15:47:23] Numlines:1
[15-Sep-2004 15:47:23] Numpoints:
[15-Sep-2004 15:47:23] The sum2:0
[15-Sep-2004 15:47:23] Area is:0

Here is the code:
error_log('Numshapes:'.$oShapeFile->numshapes);
$theShape = $oShapeFile->getShape(0);
error_log('Numlines:'.$theShape->numlines);
error_log('Numpoints:'.$theShape->line[0]->numpoints);
$shpArea = shapeArea($theShape);
error_log('Area is:'.$shpArea);


function shapeArea($myShape)
{
  $area = 0;
  if($myShape->type!=MS_SHAPE_POLYGON)
  {
    error_log('Not a Polygon!');
    return $area;
  }
  error_log('--------------------------');
  error_log('Numlines:'.$myShape->numlines);
  error_log('Numpoints:'.$myShape->line[0]->numpoints);
  for($i=0;$i<$myShape->numlines;$i++)
  {
    $sum = 0;
    for ($v=0;$v<$myShape->line[$i]->numpoints-2;$v++)
    {

$sum+=(($myShape->line[$i]->point[0]->x)*($myShape->line[$i]->point[$v]->y)-($myShape->line[$i]->point[0]->y)*($myShape->line[$i]->point[$v]->x)+($myShape->line[$i]->point[0]->y)*($myShape->line[$i]->point[$v+1]->x)-($myShape->line[$i]->point[0]->x)*($myShape->line[$i]->point[$v+1]->y)+($myShape->line[$i]->point[$v]->x)*($myShape->line[$i]->point[$v+1]->y)-($myShape->line[$i]->point[$v+1]->x)*($myShape->line[$i]->point[$v]->y));
      error_log('The sum1:'.$sum);
    }
    error_log('The sum2:'.$sum);
    $sum = $sum;
    //test if line lies inside other ones
    for($v=0;$v<$i;$v++)
    {
      if(pointInLine($myShape->line[$v],$myShape->line[$i]->point[0]))
      {
       $sum = -$sum;
   $area += $sum;
      }
    }
    return $area/2;
  }
}

function pointInLine($myLine,$myPoint)
{
  //test - point in poly - for one arc of shape

  $status=0;
  for ($i=0, $j=$myLine->numpoints-1; i<$myLine->numpoints; $j=$i++)
  {
    if (((($myLine->point[$i]->y <= $myPoint->y) && ($myPoint->y <
$myLine->point[$j]->y)) || (($myLine->point[$j]->y <= $myPoint->y) &&
($myPoint->y < $myLine->point[$i]->y))) && ($myPoint->x <
($myLine->point[$j]->x - $myLine->point[$i]->x) * ($myPoint->y -
$myLine->point[$i]->y) / ($myLine->point[$j]->y - $myLine->point[$i]->y)
+ $myLine->point[$i]->x))
         $status = !$status;
  }
  return $status;
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapserver-users/attachments/20040916/d8739751/attachment.html


More information about the mapserver-users mailing list