Changeset 18664 in josm


Ignore:
Timestamp:
2023-02-15T18:22:07+01:00 (20 months ago)
Author:
taylor.smock
Message:

Fix #22704: Add parent_way_angle mapcss function (patch by Woazboat)

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r17916 r18664  
    217217        FACTORY_MAP.put("parent_tag", Factory.ofEnv(String.class, Functions::parent_tag));
    218218        FACTORY_MAP.put("parent_tags", Factory.ofEnv(String.class, Functions::parent_tags));
     219        FACTORY_MAP.put("parent_way_angle", Factory.ofEnv(Functions::parent_way_angle));
    219220        FACTORY_MAP.put("plus", Factory.ofNumberVarArgs(0.0, DoubleUnaryOperator.identity(), Functions::plus));
    220221        FACTORY_MAP.put("print", Factory.of(Object.class, Functions::print));
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java

    r18489 r18664  
    4242import org.openstreetmap.josm.tools.Territories;
    4343import org.openstreetmap.josm.tools.Utils;
     44import org.openstreetmap.josm.tools.RotationAngle.WayDirectionRotationAngle;
    4445
    4546/**
     
    479480
    480481    /**
     482     * Get the rotation angle of the preceding parent way segment at the node location.
     483     * If there is no preceding parent way segment, the following way segment is used instead.
     484     * Requires a parent way object matched via
     485     * <a href="https://josm.openstreetmap.de/wiki/Help/Styles/MapCSSImplementation#LinkSelector">child selector</a>.
     486     *
     487     * @param env the environment
     488     * @return the rotation angle of the parent way segment at the node in radians,
     489     * otherwise null if there is no matching parent way or the object is not a node
     490     * @since 18664
     491     */
     492    public static Double parent_way_angle(final Environment env) {
     493        if (env.osm instanceof Node && env.parent instanceof Way) {
     494            return WayDirectionRotationAngle.getRotationAngleForNodeOnWay((Node) env.osm, (Way) env.parent);
     495        }
     496        return null;
     497    }
     498
     499    /**
    481500     * Gets the value of the key {@code key} from the object's child.
    482501     * @param env the environment
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/FunctionsTest.java

    r17613 r18664  
    33
    44import static org.junit.jupiter.api.Assertions.assertEquals;
     5import static org.junit.jupiter.api.Assertions.assertNotNull;
    56import static org.junit.jupiter.api.Assertions.assertNull;
    67import static org.junit.jupiter.api.Assertions.assertTrue;
     
    89
    910import java.util.Collections;
     11import java.util.Objects;
    1012
    11 import org.junit.jupiter.api.extension.RegisterExtension;
    1213import org.junit.jupiter.api.Test;
    1314import org.openstreetmap.josm.TestUtils;
     15import org.openstreetmap.josm.data.coor.LatLon;
     16import org.openstreetmap.josm.data.osm.Node;
    1417import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1518import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1619import org.openstreetmap.josm.data.osm.User;
     20import org.openstreetmap.josm.data.osm.Way;
    1721import org.openstreetmap.josm.data.preferences.NamedColorProperty;
    1822import org.openstreetmap.josm.gui.mappaint.Environment;
    1923import org.openstreetmap.josm.gui.util.GuiHelper;
    2024import org.openstreetmap.josm.spi.preferences.Config;
    21 import org.openstreetmap.josm.testutils.JOSMTestRules;
    22 
    23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     25import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
     26import org.openstreetmap.josm.testutils.annotations.Projection;
    2427
    2528/**
    2629 * Unit tests of {@link Functions}.
    2730 */
     31@BasicPreferences
     32@Projection
    2833class FunctionsTest {
    29 
    30     /**
    31      * Setup rule
    32      */
    33     @RegisterExtension
    34     @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    35     public JOSMTestRules test = new JOSMTestRules();
    36 
    3734    private static class EnvBuilder {
    3835        private final OsmPrimitive osm;
     
    108105
    109106    /**
     107     * Test for {@link Functions#parent_way_angle(Environment)}
     108     */
     109    @Test
     110    void testParentWayAngle() {
     111        assertNull(Functions.parent_way_angle(new EnvBuilder(NODE).build()));
     112        final Environment environment = new EnvBuilder(NODE).build();
     113        ((Node) environment.osm).setCoor(LatLon.ZERO);
     114        final Way parent = TestUtils.newWay("", new Node(new LatLon(-.1, 0)), (Node) environment.osm, new Node(new LatLon(.1, 0)));
     115        environment.parent = parent;
     116        Double actual = Functions.parent_way_angle(environment);
     117        assertNotNull(actual);
     118        assertEquals(Math.toRadians(0), actual, 1e-9);
     119        // Reverse node order
     120        Objects.requireNonNull(parent.firstNode()).setCoor(LatLon.NORTH_POLE);
     121        Objects.requireNonNull(parent.lastNode()).setCoor(LatLon.SOUTH_POLE);
     122        actual = Functions.parent_way_angle(environment);
     123        assertNotNull(actual);
     124        assertEquals(Math.toRadians(180), actual, 1e-9);
     125    }
     126
     127    /**
    110128     * Unit test of {@code Functions#to_xxx}
    111129     */
     
    163181        Config.getPref().put(colorKey, null);
    164182    }
    165 
    166183}
Note: See TracChangeset for help on using the changeset viewer.