# Geometry Engine functions for middlePoint and bearing?

222
3
10-06-2020 04:17 AM New Contributor II

Hello community,

I am currently working with lines on a Java application that requires some geometry processing such the bearing of this line and also the midpoint.
I was browsing the API and couldn't find any implementation of those by myself. There are functions for this that I am missing?

Thanks!

Tags (1)
1 Solution

Accepted Solutions by Esri Regular Contributor

I'm sure there are many ways of achieving this but I've found the following works.

`// create a purple (0xFF800080) simple line symbolSimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF800080, 4);// create a new point collection for polylinePointCollection points = new PointCollection(SpatialReferences.getWebMercator());// create and add points to the point collectionpoints.add(new Point(-302232.417504, 7570568.626755));points.add(new Point(-294306.469759, 7574158.422559));// create the polyline from the point collectionPolyline polyline = new Polyline(points);// create the graphic with polyline and symbolGraphic graphicLine = new Graphic(polyline, lineSymbol);graphicsOverlay.getGraphics().add(graphicLine);`

Then we get the mid point in 2 steps.  First, you find the length then draw a point along the line at a distance of half that length:

`// get start and end pointsPoint startPoint = polyline.getParts().get(0).getStartPoint();Point endPoint = polyline.getParts().get(0).getEndPoint();// get the length in metresdouble length = GeometryEngine.distanceBetween(startPoint, endPoint);// use the length to get the mid pointPoint midPoint = GeometryEngine.createPointAlong(polyline, length/2);// draw the mid pointSimpleMarkerSymbol sms = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFF00FF00,10);Graphic intersectGraphic = new Graphic(midPoint, sms);graphicsOverlay.getGraphics().add(intersectGraphic);`

Calculating the bearing can be done using basic geometry, but note this will only work with a projected coordinate system.

`// working out the bearing of the line.double xDiff = endPoint.getX() - startPoint.getX();double yDiff = endPoint.getY() - startPoint.getY();// get the angledouble angleRad = Math.atan(xDiff/ yDiff);double angleDeg = Math.toDegrees(angleRad);System.out.println("angle " + angleDeg);`

Does this help?

3 Replies by Esri Regular Contributor

I'm sure there are many ways of achieving this but I've found the following works.

`// create a purple (0xFF800080) simple line symbolSimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF800080, 4);// create a new point collection for polylinePointCollection points = new PointCollection(SpatialReferences.getWebMercator());// create and add points to the point collectionpoints.add(new Point(-302232.417504, 7570568.626755));points.add(new Point(-294306.469759, 7574158.422559));// create the polyline from the point collectionPolyline polyline = new Polyline(points);// create the graphic with polyline and symbolGraphic graphicLine = new Graphic(polyline, lineSymbol);graphicsOverlay.getGraphics().add(graphicLine);`

Then we get the mid point in 2 steps.  First, you find the length then draw a point along the line at a distance of half that length:

`// get start and end pointsPoint startPoint = polyline.getParts().get(0).getStartPoint();Point endPoint = polyline.getParts().get(0).getEndPoint();// get the length in metresdouble length = GeometryEngine.distanceBetween(startPoint, endPoint);// use the length to get the mid pointPoint midPoint = GeometryEngine.createPointAlong(polyline, length/2);// draw the mid pointSimpleMarkerSymbol sms = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, 0xFF00FF00,10);Graphic intersectGraphic = new Graphic(midPoint, sms);graphicsOverlay.getGraphics().add(intersectGraphic);`

Calculating the bearing can be done using basic geometry, but note this will only work with a projected coordinate system.

`// working out the bearing of the line.double xDiff = endPoint.getX() - startPoint.getX();double yDiff = endPoint.getY() - startPoint.getY();// get the angledouble angleRad = Math.atan(xDiff/ yDiff);double angleDeg = Math.toDegrees(angleRad);System.out.println("angle " + angleDeg);`

Does this help? New Contributor II

Hi Mark!
This should do the work, I was just wondering if the arcgis java lib had something more out of the box solution like a function call with two points parameter.

Thanks for the time to answer my post! by Esri Regular Contributor

Hi Rafael,

For the bearing calculation there isn't a method which takes 2 points.  I can however see the value in it so we may consider if for a future release.

In the meantime my method should work, but the calculation will only be correct for project coordinate systems and over small distances to be accurate. 