Search in sources :

Example 1 with Relation

use of uk.me.parabola.mkgmap.reader.osm.Relation in project mkgmap by openstreetmap.

the class LengthFunction method calcLength.

private double calcLength(Element el) {
    if (el instanceof Way) {
        Way w = (Way) el;
        double length = 0;
        Coord prevC = null;
        for (Coord c : w.getPoints()) {
            if (prevC != null) {
                length += prevC.distance(c);
            }
            prevC = c;
        }
        return length;
    } else if (el instanceof Relation) {
        Relation rel = (Relation) el;
        double length = 0;
        for (Entry<String, Element> relElem : rel.getElements()) {
            if (relElem.getValue() instanceof Way || relElem.getValue() instanceof Relation) {
                if (rel == relElem.getValue()) {
                    // avoid recursive call
                    log.error("Relation " + rel.getId() + " contains itself as element. This is not supported.");
                } else {
                    length += calcLength(relElem.getValue());
                }
            }
        }
        return length;
    } else {
        throw new SyntaxException("length() cannot calculate elements of type " + el.getClass().getName());
    }
}
Also used : Coord(uk.me.parabola.imgfmt.app.Coord) Relation(uk.me.parabola.mkgmap.reader.osm.Relation) Entry(java.util.Map.Entry) SyntaxException(uk.me.parabola.mkgmap.scan.SyntaxException) Way(uk.me.parabola.mkgmap.reader.osm.Way)

Example 2 with Relation

use of uk.me.parabola.mkgmap.reader.osm.Relation in project mkgmap by openstreetmap.

the class ActionReaderTest method testApplyAction.

/**
 * The apply action works on the members of relations.
 */
@Test
public void testApplyAction() {
    List<Action> actions = readActionsFromString("{apply {" + "add route=bike;" + "set foo=bar; }" + "}\n");
    Relation rel = makeRelation();
    Rule rule = new ActionRule(null, actions);
    rule.resolveType(rel, TypeResult.NULL_RESULT);
    assertNull("Tag not set on relation", rel.getTag("route"));
    // Will be set on all members as there is no role filter.
    List<Map.Entry<String, Element>> elements = rel.getElements();
    Element el1 = elements.get(0).getValue();
    assertEquals("route tag added to first", "bike", el1.getTag("route"));
    assertEquals("foo tag set to first", "bar", el1.getTag("foo"));
    Element el2 = elements.get(1).getValue();
    assertEquals("route tag added to second", "bike", el2.getTag("route"));
    assertEquals("foo tag set to second", "bar", el2.getTag("foo"));
}
Also used : Action(uk.me.parabola.mkgmap.osmstyle.actions.Action) Relation(uk.me.parabola.mkgmap.reader.osm.Relation) GeneralRelation(uk.me.parabola.mkgmap.reader.osm.GeneralRelation) Element(uk.me.parabola.mkgmap.reader.osm.Element) Rule(uk.me.parabola.mkgmap.reader.osm.Rule) Test(org.junit.Test)

Example 3 with Relation

use of uk.me.parabola.mkgmap.reader.osm.Relation in project mkgmap by openstreetmap.

the class ActionReaderTest method testApplyWithRole.

/**
 * You can have a role filter, so that the actions are only applied
 * to members with the given role.
 */
@Test
public void testApplyWithRole() {
    List<Action> actions = readActionsFromString("{apply role=bar {" + "add route=bike;" + "set foo=bar; }}");
    Relation rel = makeRelation();
    Rule rule = new ActionRule(null, actions);
    rule.resolveType(rel, TypeResult.NULL_RESULT);
    List<Map.Entry<String, Element>> elements = rel.getElements();
    Element el1 = elements.get(0).getValue();
    assertEquals("route tag added to first", "bike", el1.getTag("route"));
    assertEquals("foo tag set to first", "bar", el1.getTag("foo"));
    // Wrong role, so not applied.
    Element el2 = elements.get(1).getValue();
    assertNull("route tag not added to second element (role=foo)", el2.getTag("route"));
    assertNull("foo tag not set in second element (role=foo)", el2.getTag("foo"));
}
Also used : Action(uk.me.parabola.mkgmap.osmstyle.actions.Action) Relation(uk.me.parabola.mkgmap.reader.osm.Relation) GeneralRelation(uk.me.parabola.mkgmap.reader.osm.GeneralRelation) Element(uk.me.parabola.mkgmap.reader.osm.Element) Rule(uk.me.parabola.mkgmap.reader.osm.Rule) Test(org.junit.Test)

Example 4 with Relation

use of uk.me.parabola.mkgmap.reader.osm.Relation in project mkgmap by openstreetmap.

the class ActionReaderTest method testApplyWithSubst.

/**
 * When an apply statement runs, then substitutions on the value use
 * the tags of the relation and not of the sub element.
 */
@Test
public void testApplyWithSubst() {
    List<Action> actions = readActionsFromString("{apply {" + "add route='${route_no}';" + "}}");
    Relation rel = makeRelation();
    rel.addTag("route_no", "66");
    Element el1 = rel.getElements().get(0).getValue();
    el1.addTag("route_no", "42");
    Rule rule = new ActionRule(null, actions);
    rule.resolveType(rel, TypeResult.NULL_RESULT);
    assertEquals("route_no taken from relation tags", "66", el1.getTag("route"));
}
Also used : Action(uk.me.parabola.mkgmap.osmstyle.actions.Action) Relation(uk.me.parabola.mkgmap.reader.osm.Relation) GeneralRelation(uk.me.parabola.mkgmap.reader.osm.GeneralRelation) Element(uk.me.parabola.mkgmap.reader.osm.Element) Rule(uk.me.parabola.mkgmap.reader.osm.Rule) Test(org.junit.Test)

Example 5 with Relation

use of uk.me.parabola.mkgmap.reader.osm.Relation in project mkgmap by openstreetmap.

the class ActionReaderTest method makeRelation.

private Relation makeRelation() {
    Relation rel = new GeneralRelation(23);
    rel.addElement("bar", makeElement());
    rel.addElement("foo", makeElement());
    return rel;
}
Also used : Relation(uk.me.parabola.mkgmap.reader.osm.Relation) GeneralRelation(uk.me.parabola.mkgmap.reader.osm.GeneralRelation) GeneralRelation(uk.me.parabola.mkgmap.reader.osm.GeneralRelation)

Aggregations

Relation (uk.me.parabola.mkgmap.reader.osm.Relation)11 Element (uk.me.parabola.mkgmap.reader.osm.Element)7 Action (uk.me.parabola.mkgmap.osmstyle.actions.Action)5 GeneralRelation (uk.me.parabola.mkgmap.reader.osm.GeneralRelation)5 Way (uk.me.parabola.mkgmap.reader.osm.Way)5 Test (org.junit.Test)3 Coord (uk.me.parabola.imgfmt.app.Coord)3 Rule (uk.me.parabola.mkgmap.reader.osm.Rule)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 Node (uk.me.parabola.mkgmap.reader.osm.Node)2 RestrictionRelation (uk.me.parabola.mkgmap.reader.osm.RestrictionRelation)2 Area (java.awt.geom.Area)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 CoordNode (uk.me.parabola.imgfmt.app.CoordNode)1 MapElement (uk.me.parabola.mkgmap.general.MapElement)1