| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |