[Mapserver-users] area Calculation in polygon-shapes

Stepan Kafka stepan.kafka at centrum.cz
Mon Feb 2 09:44:40 EST 2004


I have added area calculaliton function in php_mapscript 4.0.1 source code
(so We can call $myshape->area(); ). It computes shape areas including
"islands" regardless the lines orientation. I may send you this mapscript
code if you're interested. But you can also compute it in your PHP code by
accessing each vertex. Similar code may be written for perimeter. If more
users are interested in, it would be nice to include these functions in the
official code.

Here is our C implementation:

int pointInLine(lineObj *line, pointObj *point) {
   //test - point in poly - for one arc of shape
   int i, j, status;

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

double shapeObj_area(shapeObj *self){
	int i, v;
	double sum, area;
	area = 0;
	if(self->type!=MS_SHAPE_POLYGON) return area;
	for(i=0;i<self->numlines;i++){
	  sum = 0;
      for (v= 0; v < self->line[i].numpoints-2; v++){
		  sum += self->line[i].point[0].x * self->line[i].point[v].y -
self->line[i].point[0].y * self->line[i].point[v].x +
			    self->line[i].point[0].y * self->line[i].point[v+1].x -
self->line[i].point[0].x * self->line[i].point[v+1].y +
				self->line[i].point[v].x * self->line[i].point[v+1].y -
self->line[i].point[v+1].x * self->line[i].point[v].y;

	  }
	  sum = MS_ABS(sum);
	  //test if line lies inside other ones
	  for(v=0;v<i;v++) if(pointInLine(&(self->line[v]),
&(self->line[i].point[0]))) sum = -sum;
	  area += sum;
	}
	return area/2;
}


Stepan Kafka
Help Service Remote Sensing
tel: +420-327514118
mailto:kafka at email.cz
ICQ: 294002967
http://www.bnhelp.cz




> 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
>
> --
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> * Dipl.-Ing.(FH) Margaritha Vogt
> * FH Mainz, FR Geoinformatik
> * Holzstrasse 36, D-55116 Mainz
> * Tel: +49-6131-2859-612, Fax: +49-6131-2859-615
> * mailto:vogt at geoinform.fh-mainz.de
> * http://www.geoinform.fh-mainz.de
>
>
> _______________________________________________
> Mapserver-users mailing list
> Mapserver-users at lists.gis.umn.edu
> http://lists.gis.umn.edu/mailman/listinfo/mapserver-users
>
>




More information about the mapserver-users mailing list