Search in sources :

Example 46 with Element

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

the class ElementQuadTreeNode method split.

/**
 * Splits this quadtree node into 4 subnodes.
 */
private void split() {
    if (bounds.getHeight() <= 5 || bounds.getWidth() <= 5) {
        log.error("Do not split more due to too small bounds: " + bounds);
        return;
    }
    int halfLat = (bounds.getMinLat() + bounds.getMaxLat()) / 2;
    int halfLong = (bounds.getMinLong() + bounds.getMaxLong()) / 2;
    children = new ElementQuadTreeNode[4];
    Area[] childBounds = new Area[4];
    childBounds[0] = new Area(bounds.getMinLat(), bounds.getMinLong(), halfLat, halfLong);
    childBounds[1] = new Area(halfLat, bounds.getMinLong(), bounds.getMaxLat(), halfLong);
    childBounds[2] = new Area(bounds.getMinLat(), halfLong, halfLat, bounds.getMaxLong());
    childBounds[3] = new Area(halfLat, halfLong, bounds.getMaxLat(), bounds.getMaxLong());
    List<Map<Element, List<Coord>>> childElems = new ArrayList<Map<Element, List<Coord>>>(4);
    for (int i = 0; i < 4; i++) {
        childElems.add(new HashMap<Element, List<Coord>>());
    }
    for (Entry<Element, List<Coord>> elem : elementMap.entrySet()) {
        if (elem.getKey() instanceof Node) {
            Node node = (Node) elem.getKey();
            for (int i = 0; i < childBounds.length; i++) {
                if (childBounds[i].contains(node.getLocation())) {
                    childElems.get(i).put(node, EMPTY_LIST);
                    break;
                }
            }
        } else if (elem.getKey() instanceof Way) {
            List<List<Coord>> points = new ArrayList<List<Coord>>(4);
            for (int i = 0; i < 4; i++) {
                // usually ways are quite local
                // therefore there is a high probability that only one child is covered
                // dim the new list as the old list
                points.add(new ArrayList<Coord>(elem.getValue().size()));
            }
            for (Coord c : elem.getValue()) {
                for (int i = 0; i < childBounds.length; i++) {
                    if (childBounds[i].contains(c)) {
                        points.get(i).add(c);
                        break;
                    }
                }
            }
            for (int i = 0; i < 4; i++) {
                if (points.get(i).isEmpty() == false) {
                    childElems.get(i).put(elem.getKey(), points.get(i));
                }
            }
        }
    }
    for (int i = 0; i < 4; i++) {
        children[i] = new ElementQuadTreeNode(childBounds[i], childElems.get(i));
    }
    elementMap = null;
}
Also used : Element(uk.me.parabola.mkgmap.reader.osm.Element) Node(uk.me.parabola.mkgmap.reader.osm.Node) ArrayList(java.util.ArrayList) Way(uk.me.parabola.mkgmap.reader.osm.Way) Coord(uk.me.parabola.imgfmt.app.Coord) Area(uk.me.parabola.imgfmt.app.Area) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 47 with Element

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

the class ElementQuadTreeNode method get.

/**
 * Retrieves all elements that intersects the given polygon.
 * @param polygon the polygon
 * @param resultList results are stored in this collection
 * @return the resultList
 */
public Set<Element> get(ElementQuadTreePolygon polygon, Set<Element> resultList) {
    if (isEmpty()) {
        return resultList;
    }
    if (polygon.getBbox().intersects(getBounds())) {
        if (isLeaf()) {
            for (Entry<Element, List<Coord>> elem : elementMap.entrySet()) {
                if (resultList.contains(elem.getKey())) {
                    continue;
                }
                if (elem.getKey() instanceof Node) {
                    Node n = (Node) elem.getKey();
                    Coord c = n.getLocation();
                    if (polygon.getArea().contains(c.getLongitude(), c.getLatitude())) {
                        resultList.add(n);
                    }
                } else if (elem.getKey() instanceof Way) {
                    for (Coord c : elem.getValue()) {
                        if (polygon.getArea().contains(c.getLongitude(), c.getLatitude())) {
                            resultList.add(elem.getKey());
                            break;
                        }
                    }
                }
            }
        } else {
            for (ElementQuadTreeNode child : children) {
                if (child.isEmpty() == false && polygon.getArea().intersects(child.getBoundsAsRectangle())) {
                    java.awt.geom.Area subArea = (java.awt.geom.Area) polygon.getArea().clone();
                    subArea.intersect(Java2DConverter.createBoundsArea(new Area(child.getBounds().getMinLat() - 1, child.getBounds().getMinLong() - 1, child.getBounds().getMaxLat() + 1, child.getBounds().getMaxLong() + 1)));
                    child.get(new ElementQuadTreePolygon(subArea), resultList);
                }
            }
        }
    }
    return resultList;
}
Also used : Element(uk.me.parabola.mkgmap.reader.osm.Element) Node(uk.me.parabola.mkgmap.reader.osm.Node) Way(uk.me.parabola.mkgmap.reader.osm.Way) Coord(uk.me.parabola.imgfmt.app.Coord) Area(uk.me.parabola.imgfmt.app.Area) ArrayList(java.util.ArrayList) List(java.util.List)

Example 48 with Element

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

the class ActionReaderTest method testSimpleAdd.

@Test
public void testSimpleAdd() {
    List<Action> actions = readActionsFromString("{add park=yes}");
    assertEquals("one action", 1, actions.size());
    // add does not overwrite existing tags.
    Element el = stdElementRun(actions);
    assertEquals("park not overwritten", "no", el.getTag("park"));
}
Also used : Action(uk.me.parabola.mkgmap.osmstyle.actions.Action) Element(uk.me.parabola.mkgmap.reader.osm.Element) Test(org.junit.Test)

Example 49 with Element

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

the class ActionReaderTest method testFreeForm.

/**
 * Test with embedded comment, newlines, semicolon used as separator.
 */
@Test
public void testFreeForm() {
    List<Action> actions = readActionsFromString(" { set web='world wide';" + "set \nribbon = 'yellow' \n# a comment } ");
    assertEquals("number of actions", 2, actions.size());
    Element el = stdElementRun(actions);
    assertEquals("park not overwritten", "no", el.getTag("park"));
    assertEquals("word with spaces", "world wide", el.getTag("web"));
    assertEquals("yellow ribbon", "yellow", el.getTag("ribbon"));
}
Also used : Action(uk.me.parabola.mkgmap.osmstyle.actions.Action) Element(uk.me.parabola.mkgmap.reader.osm.Element) Test(org.junit.Test)

Example 50 with Element

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

the class ActionReaderTest method testDoubleName.

/**
 * Test with two name actions.  This works just the same as having several
 * name options on the same name command, in that it is still the
 * first one to match that counts.
 */
@Test
public void testDoubleName() {
    List<Action> actions = readActionsFromString("{name '${name} (${ref})' |" + "  '${ref}' | '${name}' ; " + " name 'fred';}");
    // Something that matches nothing in the first name command.
    Element el = makeElement();
    Rule rule = new ActionRule(null, actions);
    rule.resolveType(el, TypeResult.NULL_RESULT);
    assertEquals("no tags, second action matches", "fred", el.getName());
    el = makeElement();
    el.addTag("ref", "A1");
    rule.resolveType(el, TypeResult.NULL_RESULT);
    assertEquals("just a ref tag", "A1", el.getName());
    el = makeElement();
    el.addTag("ref", "A1");
    el.addTag("name", "Main St");
    rule.resolveType(el, TypeResult.NULL_RESULT);
    assertEquals("ref and name", "Main St (A1)", el.getName());
}
Also used : Action(uk.me.parabola.mkgmap.osmstyle.actions.Action) Element(uk.me.parabola.mkgmap.reader.osm.Element) Rule(uk.me.parabola.mkgmap.reader.osm.Rule) Test(org.junit.Test)

Aggregations

Element (uk.me.parabola.mkgmap.reader.osm.Element)94 Test (org.junit.Test)75 Way (uk.me.parabola.mkgmap.reader.osm.Way)48 GType (uk.me.parabola.mkgmap.reader.osm.GType)25 TestUtils.makeRuleSet (func.lib.TestUtils.makeRuleSet)23 Action (uk.me.parabola.mkgmap.osmstyle.actions.Action)15 Rule (uk.me.parabola.mkgmap.reader.osm.Rule)8 ArrayList (java.util.ArrayList)7 Node (uk.me.parabola.mkgmap.reader.osm.Node)7 Relation (uk.me.parabola.mkgmap.reader.osm.Relation)7 Coord (uk.me.parabola.imgfmt.app.Coord)5 StringStyleFileLoader (func.lib.StringStyleFileLoader)4 Map (java.util.Map)4 GeneralRelation (uk.me.parabola.mkgmap.reader.osm.GeneralRelation)4 TypeResult (uk.me.parabola.mkgmap.reader.osm.TypeResult)4 HashMap (java.util.HashMap)3 IdentityHashMap (java.util.IdentityHashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Area (uk.me.parabola.imgfmt.app.Area)2