Area of a Polygon in PHP/Mapscript

Rene Teniere TENIERER at GOV.NS.CA
Fri Sep 17 04:15:45 PDT 2004


Nicol,
 
I have the same problem. Stepan made me aware of this. I am not sure why this happens. Hopefully someone on the list can explain.
 
Rene

>>> Nicol Hermann <mapserver at geoworld.de> 2004-09-17 9:34:41 AM >>>

Hi Rene,

i have a question to your implementation of Stepan's c-code.
You identify e.g. the number of points with:
$theShape->line[0]->numpoints

According to the docs i expect the following line
$theShape->line(0)->numpoints

unfortunately this statement returns the following error:
Parse error: parse error, unexpected T_OBJECT_OPERATOR in
/home/nicol/public_html/mapserver/dev/area_calculation.php on line 23

Your statement returns NULL. 
Do you know what i am doing wrong.

Thank you very much
Nicol


Am Do, den 16.09.2004 schrieb Rene Teniere um 15:07:
> 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;
> }
>  
-- 
Dipl.-Geogr. Nicol Hermann
GDV Gesellschaft für geografische Datenverarbeitung mbH
Binger Straße 49-51
55218 Ingelheim
Tel. 06132-7148-18
Fax: 06132-7148-28



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/mapserver-users/attachments/20040917/79bc47eb/attachment.htm>


More information about the MapServer-users mailing list