geometry-api-java icon indicating copy to clipboard operation
geometry-api-java copied to clipboard

Polygon Offset Example

Open nsiatras opened this issue 2 years ago • 10 comments

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

nsiatras avatar Jun 02 '22 12:06 nsiatras

Hi @nsiatras - take a look at GeometryEngine.buffer / OperatorBuffer .

randallwhitman avatar Jun 02 '22 15:06 randallwhitman

@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
			}
		}
	}

stolstov avatar Jun 02 '22 15:06 stolstov

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 ?

nsiatras avatar Jun 02 '22 17:06 nsiatras

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]]]}

nsiatras avatar Jun 02 '22 17:06 nsiatras

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.

nsiatras avatar Jun 02 '22 17:06 nsiatras

Hello again,

I managed to buffer a polygon. Screenshot

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 avatar Jun 04 '22 10:06 nsiatras

@nsiatras Buffer operation does not remember the original vertices, so there is no way to obtain those directly from the buffer polygon.

stolstov avatar Jun 06 '22 16:06 stolstov

@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.

nsiatras avatar Jun 08 '22 15:06 nsiatras

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.

stolstov avatar Jun 08 '22 16:06 stolstov

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 ?

image

nsiatras avatar Jun 08 '22 18:06 nsiatras