[geos-devel] GEOSUnaryUnion performances

Pascal Leroux pa.leroux at gmail.com
Fri Oct 26 01:00:27 PDT 2012


Hi everyone,

I want to build a planar graph with a large set of LineString geometries.
When I use OpenJump, it takes 3 or 4 seconds to determine and create the
edges of the graph. I took a look at JTS source code and this calculation
is based on the class UnaryUnionOp .

I have tried to do the same thing in Python (Shapely) with unary_union (new
in release 1.2.16, thanks Sean) and in C (geos C API) with GEOSUnaryUnion
(GEOS_MULTILINESTRING or GEOS_GEOMETRYCOLLECTION geometry types).

With the same input data, I have got the same results but it takes about 50
seconds (in C, 1 or 2 seconds more in Python).

Can anyone explain the differences ?

I use the release 3.3.3 of geos , on Mac OS X (10.6, Snow Leopard).

Here is the code I used to do tests in C :

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <geos_c.h>
#include "shapefil.h"

GEOSGeometry *SHPObject_to_LineString(SHPObject *object)
{
    GEOSCoordSequence *coords = GEOSCoordSeq_create(object->nVertices,2);
    int i;

    assert(object->nParts == 1);
    for (i=0; i<object->nVertices; i++)
    {
        GEOSCoordSeq_setX(coords,i,object->padfX[i]);
        GEOSCoordSeq_setY(coords,i,object->padfY[i]);
    }
    return GEOSGeom_createLineString(coords);
}

GEOSGeometry *load_shapefile_as_collection(char *pathname)
{
    SHPHandle shape;
    int type, nobjs, i;
    double minBounds[4], maxBounds[4];
    GEOSGeometry **geometries;
    GEOSGeometry *collection;

    shape = SHPOpen(pathname,"rb");

    SHPGetInfo(shape,&nobjs,&type,minBounds,maxBounds);
    assert((type % 10) == SHPT_ARC);

    assert(geometries = malloc(nobjs*sizeof(GEOSGeometry *)));

    for (i=0; i<nobjs ;i++)
    {
        SHPObject *object = SHPReadObject(shape,i);
        geometries[i] = SHPObject_to_LineString(object);
    }

    SHPClose(shape);

    collection = GEOSGeom_createCollection(GEOS_GEOMETRYCOLLECTION,
geometries, nobjs);
    // collection = GEOSGeom_createCollection(GEOS_MULTILINESTRING,
geometries, nobjs);

    return collection;
}


int main(int argc,char *argv[])
{
    GEOSGeometry *collection;
    GEOSGeometry *unaryunion;
    GEOSWKTWriter *writer;
    FILE *output;
    int g;

    initGEOS(printf,printf);

    collection = load_shapefile_as_collection(argv[1]);

    unaryunion = GEOSUnaryUnion(collection);

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/geos-devel/attachments/20121026/39ad8ca9/attachment.html>


More information about the geos-devel mailing list