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());
}
}
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"));
}
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"));
}
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"));
}
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;
}
Aggregations