c++geometryoffsetgeosjts

GEOS (​JTS Topology Suite) BufferOp / Offset Curve producing additional points


I am using GEOS (the C port of the JTS Topology suite) to produce an offset curve of a linestring.

I have successfully produced the offset curve however for some cases (namely where the start and end lines are both horizontal and end at the same x position / or vertical and end at the same y position), an additional point is created at the beginning and end of the offset.

It's easiest to explain this in images:

Example Image

Example Image 2

I can't work out if there is something I have failed to do or if this is an bug with the library, here's my code:

#include "geos_c.h"

std::vector<vec2> Geos::offsetLine(const std::vector<vec2>& points, float offset, int quadrantSegments, int joinStyle, double mitreLimit) 
{    
    // make coord sequence from points
    GEOSCoordSequence* seq = makeCoordSequence(points);

    // Define line string
    GEOSGeometry* lineString = GEOSGeom_createLineString(seq); 
    if(!lineString) return {};

    // offset line
    GEOSGeometry* bufferOp = GEOSOffsetCurve(lineString, offset, quadrantSegments, joinStyle, mitreLimit);
    if(!bufferOp) return {};

    // put coords into vector
    std::vector<vec2> output = outputCoords(bufferOp, (offset < 0.0f));

    // Frees memory of all as memory ownership is passed along
    GEOSGeom_destroy(bufferOp);

    return move(output);
}

GEOSCoordSequence* Geos::makeCoordSequence(const std::vector<vec2>& points) 
{
    GEOSCoordSequence* seq = GEOSCoordSeq_create(points.size(), 2);
    if(!seq) return {};

    for (size_t i = 0; i < points.size(); i++) { 
        GEOSCoordSeq_setX(seq, i, points[i].x);
        GEOSCoordSeq_setY(seq, i, points[i].y);
    }
    return seq;
}

std::vector<vec2> Geos::outputCoords(const GEOSGeometry* points, bool reversePoints) 
{
    // Convert to coord sequence and draw points
    const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq(points);
    if(!coordSeq) return {};

    // get number of points
    int nPoints = GEOSGeomGetNumPoints(points);
    if(nPoints == -1) return {};

    // output onto vector to return
    std::vector<vec2> output;

    // build vector
    for (size_t i = 0; i < (size_t)nPoints; i++) {
        // points are in reverse order if negative offset
        size_t index = reversePoints ? nPoints-i-1 : i;
        double xCoord, yCoord;
        GEOSCoordSeq_getX(coordSeq, index, &xCoord);
        GEOSCoordSeq_getY(coordSeq, index, &yCoord);
        output.push_back({ xCoord, yCoord });
    }
    return move(output);
}

Solution

  • Latest release of Geos resolves this now.

    See here: Issue