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!
Solved! Go to Solution.
I'm sure there are many ways of achieving this but I've found the following works.
So lets start with a line and draw it.
// create a purple (0xFF800080) simple line symbol
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF800080, 4);
// create a new point collection for polyline
PointCollection points = new PointCollection(SpatialReferences.getWebMercator());
// create and add points to the point collection
points.add(new Point(-302232.417504, 7570568.626755));
points.add(new Point(-294306.469759, 7574158.422559));
// create the polyline from the point collection
Polyline polyline = new Polyline(points);
// create the graphic with polyline and symbol
Graphic 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 points
Point startPoint = polyline.getParts().get(0).getStartPoint();
Point endPoint = polyline.getParts().get(0).getEndPoint();
// get the length in metres
double length = GeometryEngine.distanceBetween(startPoint, endPoint);
// use the length to get the mid point
Point midPoint = GeometryEngine.createPointAlong(polyline, length/2);
// draw the mid point
SimpleMarkerSymbol 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 angle
double angleRad = Math.atan(xDiff/ yDiff);
double angleDeg = Math.toDegrees(angleRad);
System.out.println("angle " + angleDeg);
Does this help?
I'm sure there are many ways of achieving this but I've found the following works.
So lets start with a line and draw it.
// create a purple (0xFF800080) simple line symbol
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.DASH, 0xFF800080, 4);
// create a new point collection for polyline
PointCollection points = new PointCollection(SpatialReferences.getWebMercator());
// create and add points to the point collection
points.add(new Point(-302232.417504, 7570568.626755));
points.add(new Point(-294306.469759, 7574158.422559));
// create the polyline from the point collection
Polyline polyline = new Polyline(points);
// create the graphic with polyline and symbol
Graphic 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 points
Point startPoint = polyline.getParts().get(0).getStartPoint();
Point endPoint = polyline.getParts().get(0).getEndPoint();
// get the length in metres
double length = GeometryEngine.distanceBetween(startPoint, endPoint);
// use the length to get the mid point
Point midPoint = GeometryEngine.createPointAlong(polyline, length/2);
// draw the mid point
SimpleMarkerSymbol 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 angle
double angleRad = Math.atan(xDiff/ yDiff);
double angleDeg = Math.toDegrees(angleRad);
System.out.println("angle " + angleDeg);
Does this help?
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!
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.
Glad this helped.
Mark