[postgis-users] plpgsql function for surface of multipolygon with holes

David TECHER davidtecher at yahoo.fr
Thu Jan 29 08:58:08 PST 2004


Hi

last times Dirk ask me that he have problem about
 finding surfaces of multipolygon with holes

That's right that area2d() doesn't give the good
surface. So here is a plpgsql function

-------- aire_geometrique ----------------

DROP FUNCTION aire_geometrique(geometry); 

CREATE FUNCTION aire_geometrique(geometry) RETURNS
real AS' 

DECLARE 
--tableau alias for $2;
colonne_geometrique ALIAS FOR $1; 
aire real;
aire_en_cours real;
iter_aire integer; 
nombre_d_objects integer;
BEGIN 
	aire=0;
	iter_aire = 1;
  SELECT INTO nombre_d_objects 
  numgeometries(colonne_geometrique);
	while iter_aire < nombre_d_objects
	loop
		select into aire_en_cours 
		area2d(geometryn(colonne_geometrique,iter_aire));
		iter_aire = iter_aire + 1;
		aire = aire + aire_en_cours;
	end loop;
RETURN aire; 

END; 

' LANGUAGE 'plpgsql'; 

--------------------------------------------

put this function in a file (like area.sql for
example). Then do 

psql -d <yourdatabase> -f area.sql

for the surface in psql

root# select aire_geometrique(the_geom) <from
yourtable>

example:

for this multipolygon

MULTIPOLYGON ((
(140 220, 160 140, 200 140, 240 320, 140 300, 140
220), 
  (160 260, 180 280, 200 260, 180 220, 160 240, 160
240, 160 260), 
  (160 200, 180 200, 180 180, 180 160, 160 180, 160
180, 160 200)), 
  ((400 300, 460 260, 420 200, 360 200, 340 260, 340
300, 360 340, 400 300), 
    (380 280, 360 260, 380 220, 420 240, 420 240, 380
280)))

area2d() gives me : 19000

whereas airegeometrique gives: 10600

_________________________________________________________________
Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
Yahoo! Mail : http://fr.mail.yahoo.com



More information about the postgis-users mailing list