Changeset 15196 in josm


Ignore:
Timestamp:
2019-06-29T23:07:03+02:00 (5 years ago)
Author:
Don-vip
Message:

fix #17845 - Add a MapCSS method to check for roles in relations (patch by taylor.smock)

Location:
trunk
Files:
2 edited

Legend:

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

    r14802 r15196  
    2929import org.openstreetmap.josm.data.osm.Node;
    3030import org.openstreetmap.josm.data.osm.OsmPrimitive;
     31import org.openstreetmap.josm.data.osm.Relation;
     32import org.openstreetmap.josm.data.osm.RelationMember;
    3133import org.openstreetmap.josm.data.osm.Way;
    3234import org.openstreetmap.josm.data.osm.search.SearchCompiler;
     
    571573
    572574        /**
     575         * Returns true if role is in relation. Returns false if not a relation or it does not have the role.
     576         * @param env the environment
     577         * @param roles The roles to count in the relation
     578         * @return The number of relation members with the specified role
     579         * @since 15196
     580         */
     581        public static int count_roles(final Environment env, String... roles) { // NO_UCD (unused code)
     582            int rValue = 0;
     583            if (env.osm instanceof Relation) {
     584                List<String> roleList = Arrays.asList(roles);
     585                Relation rel = (Relation) env.osm;
     586                for (RelationMember member : rel.getMembers()) {
     587                    if (roleList.contains(member.getRole())) rValue++;
     588                }
     589            }
     590            return rValue;
     591        }
     592
     593        /**
    573594         * Returns the area of a closed way or multipolygon in square meters or {@code null}.
    574595         * @param env the environment
  • trunk/test/unit/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSParserTest.java

    r14796 r15196  
    1414import org.junit.Rule;
    1515import org.junit.Test;
     16import org.openstreetmap.josm.TestUtils;
    1617import org.openstreetmap.josm.data.coor.LatLon;
    1718import org.openstreetmap.josm.data.osm.DataSet;
    1819import org.openstreetmap.josm.data.osm.Node;
    1920import org.openstreetmap.josm.data.osm.OsmUtils;
     21import org.openstreetmap.josm.data.osm.Relation;
     22import org.openstreetmap.josm.data.osm.RelationMember;
    2023import org.openstreetmap.josm.data.osm.Way;
    2124import org.openstreetmap.josm.gui.mappaint.Environment;
     
    405408
    406409    @Test
     410    public void testCountRoles() throws Exception {
     411        DataSet ds = new DataSet();
     412        Way way1 = TestUtils.newWay("highway=residential name=1",
     413                new Node(new LatLon(0, 0)), new Node((new LatLon(0.001, 0.001))));
     414        for (Node node : way1.getNodes()) {
     415            ds.addPrimitive(node);
     416        }
     417        ds.addPrimitive(way1);
     418
     419        Relation rel1 = TestUtils.newRelation("type=destination_sign", new RelationMember("", way1));
     420        ds.addPrimitive(rel1);
     421
     422        /* Check with empty role and one object */
     423        Environment e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     424        assertEquals(1, ExpressionFactory.Functions.count_roles(e, ""));
     425
     426        /* Check with non-empty role and one object */
     427        e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     428        assertEquals(0, ExpressionFactory.Functions.count_roles(e, "from"));
     429
     430        /* Check with empty role and two objects */
     431        Way way2 = TestUtils.newWay("highway=residential name=2", way1.firstNode(), way1.lastNode());
     432        ds.addPrimitive(way2);
     433        rel1.addMember(new RelationMember("", way2));
     434        e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     435        assertEquals(2, ExpressionFactory.Functions.count_roles(e, ""));
     436
     437        /* Check with non-empty role and two objects */
     438        rel1.setMember(0, new RelationMember("from", way1));
     439        e = new Environment(rel1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     440        assertEquals(1, ExpressionFactory.Functions.count_roles(e, "from"));
     441
     442        /* Check with multiple roles */
     443        assertEquals(1, ExpressionFactory.Functions.count_roles(e, "from", "to"));
     444
     445        /* Check with non-relation */
     446        e = new Environment(way1, new MultiCascade(), Environment.DEFAULT_LAYER, null);
     447        assertEquals(0, ExpressionFactory.Functions.count_roles(e, "from", "to"));
     448    }
     449
     450    @Test
    407451    public void testSiblingSelectorInterpolation() throws Exception {
    408452        ChildOrParentSelector s1 = (Selector.ChildOrParentSelector) getParser(
Note: See TracChangeset for help on using the changeset viewer.