Debian Codegraph @

src/dr/geo/KMLCoordinates.java

raw symbols SCIP
1 /*
2 * KMLCoordinates.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.geo;
27
28 import dr.xml.*;
29 import org.jdom.Element;
30
31 import java.util.Arrays;
32 import java.util.StringTokenizer;
33
34 /**
35 * @author Marc A. Suchard
36 */
37
38 public class KMLCoordinates {
39
40 public static final String COORDINATES = "coordinates";
41 public static final String FORMAT = "%7.5f";
42 public static final String SEPARATOR = ",";
43 public static final String NEWLINE = "\n";
44 public static final String POINT_SEPARATORS = NEWLINE + " ";
45
46 public KMLCoordinates(double[] x, double[] y) {
47 this(x, y, 0.0);
48 }
49
50 public KMLCoordinates(double[] x, double[] y, double z) {
51 this.x = x;
52 this.y = y;
53
54 if (x.length != y.length)
55 throw new RuntimeException("Cannot create coordinate system with unbalanced entries");
56
57 this.z = new double[x.length];
58 Arrays.fill(this.z, z);
59 }
60
61 public KMLCoordinates(double[] x, double[] y, double[] z) {
62
63 this.x = x;
64 this.y = y;
65 this.z = z;
66
67 if (x.length != y.length && x.length != z.length)
68 throw new RuntimeException("Cannot create coordinate system with unbalanced entries");
69
70 length = x.length;
71 }
72
73 public void switchXY() {
74 double[] tempX = x;
75 x = y;
76 y = tempX;
77
78 }
79
80 public Element toXML() {
81 Element thisElement = new Element(COORDINATES);
82 StringBuffer bf = new StringBuffer();
83 bf.append(POINT_SEPARATORS);
84 for (int i = 0; i < x.length; i++) {
85 bf.append(String.format(FORMAT, x[i])).append(SEPARATOR);
86 bf.append(String.format(FORMAT, y[i])).append(SEPARATOR);
87 bf.append(String.format(FORMAT, z[i])).append(POINT_SEPARATORS);
88 }
89 thisElement.addContent(bf.toString());
90 return thisElement;
91 }
92
93 public static XMLObjectParser COORDINATESPARSER = new AbstractXMLObjectParser() {
94
95 public String getParserName() {
96 return COORDINATES;
97 }
98
99 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
100
101 String value = (String) xo.getChild(0);
102
103 StringTokenizer st1 = new StringTokenizer(value, POINT_SEPARATORS);
104 int count = st1.countTokens();
105
106 double[] x = new double[count];
107 double[] y = new double[count];
108 double[] z = new double[count];
109
110 for (int i = 0; i < count; i++) {
111 String line = st1.nextToken();
112 StringTokenizer st2 = new StringTokenizer(line, SEPARATOR);
113 if (st2.countTokens() != 3)
114 throw new XMLParseException("All KML coordinates must contain (X,Y,Z) values. Three dimensions not found in element '" + line + "'");
115 x[i] = Double.valueOf(st2.nextToken());
116 y[i] = Double.valueOf(st2.nextToken());
117 z[i] = Double.valueOf(st2.nextToken());
118 }
119
120 return new KMLCoordinates(x, y, z);
121 }
122
123 //************************************************************************
124 // AbstractXMLObjectParser implementation
125 //************************************************************************
126
127 public String getParserDescription() {
128 return "This element represents a set of (X,Y,Z) coordinates in KML format";
129 }
130
131 public Class getReturnType() {
132 return KMLCoordinates.class;
133 }
134
135 public XMLSyntaxRule[] getSyntaxRules() {
136 return rules;
137 }
138
139 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
140 // AttributeRule.newDoubleRule(TIME,true),
141 new ElementRule(String.class)
142 };
143 };
144
145 public double[] x;
146 public double[] y;
147 public double[] z;
148
149 public int length;
150
151 }