[Gdal-dev] python, multipolygon creation

Frank Warmerdam warmerdam at pobox.com
Wed Feb 8 21:20:58 EST 2006


On 2/8/06, Didrik Pinte <dpinte at itae.be> wrote:
> Hi,
>
> I'm trying to write a function for simplifying polygons.
>
> My input file is a shapefile containing 11 MULTIPOLYGON composed of only
> 1 LINEARRING.
>
> I can read and simplify the LINEARRINGS but I cannot create add them to
> a new MULTIPOLYGON. The AddGeometryDirectly() method seems to be doing
> nothing.
>
> Here is the problematic code :
> -----------------------------------------------------------
>  if geom.GetGeometryCount() > 0:
>       print 'Multiple geometries : simplifying %s geometries' %
> geom.GetGeometryCount()
>       new_geom = []
>       # for each LinearRing, simplify it and store it in a list
>       for i in xrange(geom.GetGeometryCount()):
>         in_geom = geom.GetGeometryRef(i)
>         new_geom.append( SimplifyGeometry( in_geom, tolerance ) )
>       # Creating the output new MultiPolygon
>       seg = ogr.Geometry( type= ogr.wkbMultiPolygon )
>       # Adding the circles into the polygon
>       for ng in new_geom:
>         print "Ng points %i" % ng.GetPointCount()
>         seg.AddGeometryDirectly(ng)
>       seg.CloseRings()
>       print "New multipolygon has %i geometries" %
> seg.GetGeometryCount()
>       return seg
> ------------------------------------------------------------
> Here is the output i always have :
> ------------------------------------------------------------
> Multiple geometries : simplifying 1 geometries
> Ng points 1841
> New multipolygon has 0 geometries
>
>
> At the end of the process my seg object does not contain any
> geometry ... Is it a problem with the AddGeometryDirectly ?
>
> I think i've missed something.
>
> Does anybody have an idea about my problem ?

Didrik,

I believe the problem is that you are attempting
add linearring's directly to a multipolygon.   A multipolygon
is supposed to hold only polygons.  And polygons have
linear rings.  The most direct fix was be to modify
SimplifyGeometry() to return a polygon instead of
a linearring.

The AddGeometryDirectly() method tests certain assumptions,
like the that multipolygons are only allowed to hold polygons, and
takes no action if the assumption fails.  I imagine it is returning
an error code.   It would perhaps be better if it issue an error
message or possibly threw an exception.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list