All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.zuq.osm.parser.WayParser Maven / Gradle / Ivy

package br.zuq.osm.parser;

import br.zuq.osm.parser.model.OSMNode;
import br.zuq.osm.parser.model.Way;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author zuq
 */
public class WayParser {

    public static boolean isWay(Node node) {
        return node.getNodeName().equals("way");
    }

    public static Way parseWay(Node wayNode, Map nodes) {
        Way way;

        NamedNodeMap atts = wayNode.getAttributes();

        String id = atts.getNamedItem("id").getNodeValue();

        way = new Way(
                id,
                getAttribute(atts, "visible"),
                getAttribute(atts, "timestamp"),
                getAttribute(atts, "version"),
                getAttribute(atts, "changeset"),
                getAttribute(atts, "user"),
                getAttribute(atts, "uid"),
                getNodes(wayNode.getChildNodes(), nodes),
                OSMParser.parseTags(wayNode.getChildNodes()));

        return way;
    }

    // Private Methods ---------------------------------------------------------

    private static String getAttribute(NamedNodeMap atts, String key) {
        Node node = atts.getNamedItem(key);
        return (node == null) ? null : node.getNodeValue();
    }

    private static List getNodes(NodeList children, Map nodes) {
        List result = new ArrayList();

        Node node;
        String nodeName;
        
        for (int i = 0; i < children.getLength(); i++) {
            
            node = children.item(i);
            nodeName = node.getNodeName();

            if (nodeName.equals("nd")) {

                result.add(nodes.get(node.getAttributes().
                        getNamedItem("ref").getNodeValue()));
                
            }
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy