Hi all,
I'm looking to calculate the angle between 2 points. Both are in different services. The first one it's a pole location and the second one it's an anchor location.
The anchor location must have a particular symbol and should point to the pole. That's what i'm looking for. It's not just a "looking good" nice to have, we need that angle to use it on AutoCad drawing.
I'm using this code:
---------------------------------------------------------------------------
// Define the reference point (could be a specific feature)
var referencePoint = Geometry($feature); // Reference feature geometry
// Define the feature set containing other points to compare
var otherPoints = FeatureSetByName($map, "POLE"); // Replace with your layer name
// Variables to store nearest point and minimum distance
var nearestPoint = -1;
var minDist = 1000; // Start with a high number for comparison
// return Count(otherPoints)
// Loop through each point to find the closest one
for (var pointv in otherPoints) {
var dist = Distance(referencePoint, Geometry(pointv)); // Calculate distance
//return dist
if (dist < minDist) { // If this point is closer
minDist = dist; // Update minimum distance,
nearestPoint = Geometry(pointv); // Update the nearest point
}
}
// Now calculate the angle using Atan2 based on nearestPoint's coordinates
if (nearestPoint!= -1) {
var deltaY = nearestPoint.y - referencePoint.y; // Change in Y
var deltaX = nearestPoint.x - referencePoint.x; // Change in X
var anglev = Atan2(deltaY, deltaX); // Calculate angle in radians
return anglev * abs((180 / PI)); // Convert radians to degrees
} else {
return 0; // Handle case where no point is found
------------------------------------------------------------------------------------------
The results are not what I would have wished. First, the "North" at 0 degrees it's pointing West. Then at 0, 90, 180 degrees are looking good but not at 270 degrees. Between those numbers, none of the angles are correct. Here an example:
Any idea what is wrong or what I need to change...
Thank you all!!
Solved! Go to Solution.
@ErikRose and @DavidSolari this works wonderfully!!! Thank you so mutch!
Mutch appreciated! I will make those last setting on the code!
As usual, this community ROCKS!
Cheers!!!
A couple issues:
To convert the mathematical angle (from East, CCW) to a bearing (from North, CW), use:
bearing = (450 - degreesFromEastCCW) % 360
I can't test this on my end, but you want something like:
// Reference: Anchor feature geometry
var referencePoint = Geometry($feature);
// Feature set containing poles (replace "POLE" with your layer name if different)
var otherPoints = FeatureSetByName($map, "POLE");
// Guard: if there are no poles, bail out
if (Count(otherPoints) == 0) {
return null;
}
// Find nearest pole to this anchor
var nearestPoint = null;
var minDist = Infinity;
for (var pointv in otherPoints) {
var g = Geometry(pointv);
var dist = Distance(referencePoint, g);
if (dist < minDist) {
minDist = dist;
nearestPoint = g;
}
}
// If we found one, compute bearing (clockwise from North)
if (!IsEmpty(nearestPoint)) {
var deltaY = nearestPoint.y - referencePoint.y; // change in Northing
var deltaX = nearestPoint.x - referencePoint.x; // change in Easting
// Mathematical angle from +X (East), CCW
var angDegFromEastCCW = (Atan2(deltaY, deltaX) * 180 / PI + 360) % 360;
// Convert to bearing: clockwise from North
var bearing = (450 - angDegFromEastCCW) % 360;
return bearing; // degrees in [0, 360), North=0°, East=90°, South=180°, West=270°
} else {
return null;
My faint memories of high school trig tell me something's wrong with how you're calculating the angle. Thankfully, nobody writing arcade has to remember how to do this right thanks to the Angle function. Replace the last few lines of calculations with Angle(referencePoint, nearestPoint) - 90 and you should be golden.
An addendum: as your dataset grows you'll run geometry checks against the entire Pole dataset (at least, the entire set that the map has access to) and your edit performance will slowly grind to a halt. The usual fix is to run a Buffer on your anchor point, then use Intersects to reduce the available poles. You can also use Count to see if that intersection is empty and return an error to prevent adding anchors in the middle of the Atlantic.
@ErikRose and @DavidSolari this works wonderfully!!! Thank you so mutch!
Mutch appreciated! I will make those last setting on the code!
As usual, this community ROCKS!
Cheers!!!