geometry-api-java
geometry-api-java copied to clipboard
Polygon Offset Example
Hello,
I am wondering if there is any example on how to perform polygon offsetting.
I have a double[][] array with the polygon vertices points in clockwise order and I want to inflate it by 2mm. Is there any example code on how to perform the offseting ?
Thank you in advance
Hi @nsiatras - take a look at GeometryEngine.buffer / OperatorBuffer .
@nsiatras Is this the behavior that you want? http://esri.github.io/geometry-api-java/doc/Buffer.html If yes, then this is a sample code:
{
Polygon polygon = new Polygon();
//added one exterior ring (must be clockwise)
polygon.startPath(-10, -10);
polygon.lineTo(-10, 10);
polygon.lineTo(10, 10);
polygon.lineTo(10, -10);
//added one hole (must be counterclockwise)
polygon.startPath(-5, -5);
polygon.lineTo(5, -5);
polygon.lineTo(5, 5);
polygon.lineTo(-5, 5);
//buffer with the distance of 2
Geometry resultGeom = OperatorBuffer.local().execute(polygon, null, 2, null);
Polygon resultPolygon = (Polygon)resultGeom;
Point2D pt = new Point2D();
//Extract each vertex of the polygon
//loop over rings
for (int ipath = 0, npaths = resultPolygon.getPathCount(); ipath < npaths; ++ipath) {
//loop over vertices in each ring
for (int ivert = resultPolygon.getPathStart(ipath),
endVertex = resultPolygon.getPathEnd(ipath); ivert < endVertex; ++ivert) {
resultPolygon.getXY(ivert, pt);
//The x,y coordinates are pt.x, pt.y
}
}
}
Thank you all for your replies.
@stolstov I tried your example. For a 16 vertices polygon I get 104vertices after I inflate it :) Is there a way to make the inflated polygon a bit... smaller ?
PS this is my polygon (com.esri.core.geometry.Polygon) {"rings":[[[25,20],[20,25],[20,55],[25,60],[55,60],[60,55],[60,25],[55,20],[25,20]]]}
Perhaps I have to use the OperatorOffset.local().execute instead of OperatorBuffer.local().execute. All I want is to make an outline border outside of a polygon.
Hello again,
I managed to buffer a polygon.
On the left you can see the buffer in blue color. On the right I marked the initial curves as they were "extended". Is it possible to get the X,Y start and X,Y end of the initial curves after I buffer a polygon ? I do not need the other points.
Thank you in advance
@nsiatras Buffer operation does not remember the original vertices, so there is no way to obtain those directly from the buffer polygon.
@stolstov I see.. The reason I need the original vertices after they are buffered is because some polygons has Arcs. I was hopping to split the arc to 3 points and after the buffering, recovering the arc from the 3 initial points.
In some more complicated cases the arcs intersect with each other and only partial. That happens when the polygon border has a lot of details and the buffer width is large.
I managed to inflate a polygon with arcs. I split arc into small linear segments and I use the points to form a polygon. It works fine on all of my tests. Do you think there is a way to convert points back to arcs ?