Newer
Older
osmCoverage / src / osm / jp / api / OsmnodeArea.java
@hayashi hayashi on 30 May 2018 2 KB OverpassAPI
package osm.jp.api;

import java.util.ArrayList;
import org.w3c.dom.Node;

/**
 * OSM.xml の「Area(way)」ノード
 * 例)
 * <pre>{@code 
 *  <node id="5645448449" lat="35.7506438" lon="139.7516866"/>
 *  <node id="5645448450" lat="35.7507261" lon="139.7516913"/>
 *  <node id="5645448451" lat="35.7507196" lon="139.7518665"/>
 *  <node id="5645448452" lat="35.7506374" lon="139.7518619"/>
 *  <node id="289445748" lat="35.5251815" lon="139.3256576">
 *   <tag k="amenity" v="fuel"/>
 *   <tag k="brand" v="出光"/>
 *   <tag k="name" v="出光"/>
 *   <tag k="name:en" v="Idemitsu"/>
 *   <tag k="opening_hours" v="24/7"/>
 * </node>
 *   <way id="591327850">
 *     <nd ref="5645448449"/>
 *     <nd ref="5645448450"/>
 *     <nd ref="5645448451"/>
 *     <nd ref="5645448452"/>
 *     <nd ref="5645448449"/>
 *     <tag k="amenity" v="fuel"/>
 *     <tag k="name" v="Mobil"/>
 *     <tag k="name:ja" v="モービル"/>
 *   </way>
 * }</pre>
 * 
 * @author yuu
 */
public class OsmnodeArea extends OsmnodeNode {
    ArrayList<OsmnodeNd> ndList = new ArrayList<>();
    
    public OsmnodeArea(Node node) {
        super(node);

        Node ndNode = node.getFirstChild();
        while(ndNode != null) {
            String nodeName = ndNode.getNodeName();
            switch (nodeName) {
            case "nd":
                OsmnodeNd nodeNd = new OsmnodeNd(ndNode);
                ndList.add(nodeNd);
                break;
            }
            ndNode = ndNode.getNextSibling();
        }
    }
    
    public void setPosition(ArrayList<OsmnodeNode> nodelist) {
        int size = 0;
        double lat = 0.0D;
        double lon = 0.0D;
        for (OsmnodeNd nd : ndList) {
            OsmnodeNode node1 = getNode(nodelist, nd.ref);
            if (node1 != null) {
                lat += Double.parseDouble(node1.latStr);
                lon += Double.parseDouble(node1.lonStr);
                size++;
            }
        }
        lat = lat / size;
        lon = lon / size;
        this.latStr = String.format("%2.5f", lat);
        this.lonStr = String.format("%3.5f", lon);
    }
    
    static OsmnodeNode getNode(ArrayList<OsmnodeNode> nodelist, String id) {
        for (OsmnodeNode node : nodelist) {
            if (node.id.equals(id)) {
                return node;
            }
        }
        return null;
    }

}