From e75752ef381ec50368ddb6ca6f64261204563bb0 Mon Sep 17 00:00:00 2001
Date: Wed, 1 Feb 2023 01:22:56 +0100
Subject: [PATCH 1/2] Fix mapcss rotation offset for *-rotation: way

---
 .../josm/tools/RotationAngle.java             | 22 ++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/org/openstreetmap/josm/tools/RotationAngle.java b/src/org/openstreetmap/josm/tools/RotationAngle.java
index 491891276..3a9cb02d8 100644
--- a/src/org/openstreetmap/josm/tools/RotationAngle.java
+++ b/src/org/openstreetmap/josm/tools/RotationAngle.java
@@ -32,11 +32,27 @@ public interface RotationAngle {
                 return 0;
             }
             final Way w = ways.iterator().next();
+            Double angle = getRotationAngleForNodeOnWay(n, w);
+            return angle != null ? angle : 0;
+        }
+
+        /**
+         * Calculates the rotation angle of a node in a way based on the preceding way segment.
+         * If the node is the first node in the given way, the angle of the following way segment is used instead.
+         * @param n the node to get the rotation angle for
+         * @param w the way containing the node
+         * @return the rotation angle in radians or null if the way does not contain the node
+         * @since xxx
+         */
+        public static Double getRotationAngleForNodeOnWay(Node n, Way w) {
             final int idx = w.getNodes().indexOf(n);
+            if (idx == -1) {
+                return null;
+            }
             if (idx == 0) {
-                return -Geometry.getSegmentAngle(n.getEastNorth(), w.getNode(idx + 1).getEastNorth());
+                return -(Geometry.getSegmentAngle(n.getEastNorth(), w.getNode(idx + 1).getEastNorth()) - Math.PI/2);
             } else {
-                return -Geometry.getSegmentAngle(w.getNode(idx - 1).getEastNorth(), n.getEastNorth());
+                return -(Geometry.getSegmentAngle(w.getNode(idx - 1).getEastNorth(), n.getEastNorth()) - Math.PI/2);
             }
         }
 
@@ -106,7 +122,7 @@ public interface RotationAngle {
     /**
      * Calculates the rotation angle depending on the primitive to be displayed.
      * @param p primitive
-     * @return rotation angle
+     * @return rotation angle in radians, clockwise starting from up/north
      * @since 13623 (signature)
      */
     double getRotationAngle(IPrimitive p);
-- 
2.39.1

