[geos-devel] using CoordinateOperation::edit cause memory leaks

Tereza Fiedlerová tfiedlerova at gmail.com
Wed May 15 13:42:08 PDT 2013


2013/5/13 Sandro Santilli <strk at keybit.net>:
>
> Hi Tereza, could you include a full testcase, including caller ?
>
> --strk;

Hi,

I tried to make it easier, but I see it wasn't good idea, so I'm
giving here the necessary code without changes:

vertexgeometryeditoroperation.h
--------------------------------

#ifndef VERTEXGEOMETRYEDITOROPERATION_H
#define VERTEXGEOMETRYEDITOROPERATION_H

// GEOS includes
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/util/CoordinateOperation.h>
#include <geos/geom/CoordinateSequenceFactory.h>

// local includes
#include "geoc.h"

#include <QtGui>

using namespace std;
using namespace geos;
using namespace geos::geom;
using namespace geos::geom::util;

using namespace geoc;
using namespace geoc::geo;
using namespace geoc::alg;
using namespace geoc::edit;

namespace geoc {
namespace edit {

/** Class for editing geometry - snap vertices to the closest points. */

class VertexGeometryEditorOperation: public CoordinateOperation
{

    using CoordinateOperation::edit;

public:

    /// Constructor
    VertexGeometryEditorOperation(  ): closeCoord(NULL),
tolDistance(0), changed(false){}

    /** Set private closeCoord to given coord.
     */
    void setCloseCoordSeq( CoordinateSequence *coord ) { closeCoord = coord; }

    /** Set tolerance distance
      */
    void setTolDistance( double tol ) { tolDistance = tol; }

    /** Return true if geometry was changed
      */
    bool isChanged() { return changed; }

    /** Virtual function for editing geometry - snap to closest vertices.
     * @param coordinates Not important.
     * @param gGeometry to be edited.
     */
    CoordinateSequence* edit(const CoordinateSequence *coordinates,
const Geometry *g );


private:

    CoordinateSequence *closeCoord;
    double tolDistance;
    bool changed;

};

} //namespace geoc
} //namespace edit

#endif // VERTEXGEOMETRYEDITOROPERATION_H


------------------------------
vertexgeometryoperation.cpp
------------------------------

#include "vertexgeometryeditoroperation.h"

namespace geoc {
namespace edit {

CoordinateSequence* VertexGeometryEditorOperation::edit(const
CoordinateSequence *, const Geometry *geom )
{
    CoordinateSequence* coord = geom->getCoordinates();

    // find closest point from closeCoord
    for ( size_t i = 0; i < coord->getSize(); i++)
    {
        // minimal distance
        double minDist = tolDistance;
        size_t indMin = 0;
        bool isMin = false;

        for ( size_t j = 0; j < closeCoord->getSize(); j++ )
        {
            // compute distance between two tested points
            double dist = coord->getAt(i).distance( closeCoord->getAt(j) );

            if( (0 < dist) && (dist < minDist) )
            {
                minDist = dist;
                indMin = j;
                isMin = true;
            }

        }

        // set new coordinate to the closest point if there is some
        if ( isMin )
        {
            coord->setAt( closeCoord->getAt(indMin), i );
            changed = true;
        }

    }

    return coord;

}

} //namespace geoc
} //namespace edit


--------------------------
function using code above
--------------------------

void VertexSnapper::snapVertices(Geometry *geom, CoordinateSequence *closeCoord)
{

    // create and set geometry editor
    VertexGeometryEditorOperation myOp;
    myOp.setCloseCoordSeq( closeCoord );
    myOp.setTolDistance( tolDistance );

    GeometryEditor geomEdit( geom->getFactory() );

    // set geometry to edited one
    geom=( geomEdit.edit( geom , &myOp ) );


}


Thanks for help in advance,

Tereza


More information about the geos-devel mailing list