| 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.app.phylogeography.tools; |
| 27 |
|
| 28 |
import jebl.evolution.trees.RootedTree; |
| 29 |
import jebl.evolution.graphs.Node; |
| 30 |
import jebl.evolution.taxa.Taxon; |
| 31 |
|
| 32 |
import java.io.PrintWriter; |
| 33 |
import java.io.FileWriter; |
| 34 |
import java.io.IOException; |
| 35 |
import java.io.PrintStream; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class DiscreteKMLString { |
| 43 |
|
| 44 |
String annotation = "state"; |
| 45 |
|
| 46 |
|
| 47 |
RootedTree treeToExport; |
| 48 |
String documentName; |
| 49 |
String[] stateNames; |
| 50 |
double[][] stateCoordinates; |
| 51 |
double timeScaler = 1; |
| 52 |
|
| 53 |
double branchWidthConstant = 2.0; |
| 54 |
double branchWidthMultiplier = 5.0; |
| 55 |
|
| 56 |
double divider = 50; |
| 57 |
|
| 58 |
boolean useStateProbability = true; |
| 59 |
double branchWidth = 10.0; |
| 60 |
boolean usePosterior = false; |
| 61 |
boolean useHeights = true; |
| 62 |
String startBranchColor = "FF00FF"; |
| 63 |
String endBranchColor = "FFFF00"; |
| 64 |
String branchColor = "ffffff"; |
| 65 |
boolean arcBranches = false; |
| 66 |
boolean arcTimeHeight = false; |
| 67 |
double altitudeFactor = 1000; |
| 68 |
boolean temporary = false; |
| 69 |
|
| 70 |
double mostRecentDate; |
| 71 |
boolean ancient = false; |
| 72 |
|
| 73 |
double[] posteriorMinAndMax = new double[2]; |
| 74 |
double[] heightMinAndMax = new double[2]; |
| 75 |
|
| 76 |
|
| 77 |
int numberOfIntervals = 100; |
| 78 |
boolean autoRadius = false; |
| 79 |
double radius = 40000; |
| 80 |
String circleOpacity = "8F"; |
| 81 |
|
| 82 |
|
| 83 |
boolean coordinatesForTaxa = false; |
| 84 |
String[] taxaNames; |
| 85 |
double[][] taxaCoordinates; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
double treeSliceBranchWidth = 3.5; |
| 90 |
boolean showBranchAtMidPoint = false; |
| 91 |
|
| 92 |
|
| 93 |
StringBuffer branchesBuffer = new StringBuffer(); |
| 94 |
StringBuffer circleBuffer = new StringBuffer(); |
| 95 |
StringBuffer locationsBuffer = new StringBuffer(); |
| 96 |
StringBuffer styleBuffer = new StringBuffer(); |
| 97 |
StringBuffer treeSliceBuffer = new StringBuffer(); |
| 98 |
|
| 99 |
private static final PrintStream progressStream = System.out; |
| 100 |
|
| 101 |
public DiscreteKMLString(){ |
| 102 |
} |
| 103 |
|
| 104 |
public DiscreteKMLString(RootedTree tree, String annotation, String name, double date, String[][] locations){ |
| 105 |
treeToExport = tree; |
| 106 |
this.annotation = annotation; |
| 107 |
documentName = name; |
| 108 |
mostRecentDate = date; |
| 109 |
double[][] locationCoordinates = new double[locations.length][2]; |
| 110 |
String[] locationNames = new String[locations.length]; |
| 111 |
|
| 112 |
double minLat = Double.MAX_VALUE; |
| 113 |
double maxLat = -Double.MAX_VALUE; |
| 114 |
double minLong = Double.MAX_VALUE; |
| 115 |
double maxLong = -Double.MAX_VALUE; |
| 116 |
|
| 117 |
getTaxaNamesCoordinates(locations, locationNames,locationCoordinates); |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
stateCoordinates = locationCoordinates; |
| 124 |
stateNames = locationNames; |
| 125 |
|
| 126 |
if (mostRecentDate - (treeToExport.getHeight(treeToExport.getRootNode())*timeScaler) < 0) { |
| 127 |
ancient = true; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public DiscreteKMLString(RootedTree tree, String annotation, String name, double date, String[][] stateLocations, String startBranchColor, String endBranchColor, double timeScaler){ |
| 132 |
treeToExport = tree; |
| 133 |
this.annotation = annotation; |
| 134 |
documentName = name; |
| 135 |
mostRecentDate = date; |
| 136 |
double[][] stateLocationCoordinates = new double[stateLocations.length][2]; |
| 137 |
String[] stateLocationNames = new String[stateLocations.length]; |
| 138 |
|
| 139 |
|
| 140 |
double minLat = Double.MAX_VALUE; |
| 141 |
double maxLat = -Double.MAX_VALUE; |
| 142 |
double minLong = Double.MAX_VALUE; |
| 143 |
double maxLong = -Double.MAX_VALUE; |
| 144 |
|
| 145 |
getTaxaNamesCoordinates(stateLocations, stateLocationNames,stateLocationCoordinates); |
| 146 |
|
| 147 |
coordinatesForTaxa = false; |
| 148 |
|
| 149 |
|
| 150 |
if (autoRadius) { |
| 151 |
radius = 100*Math.abs(maxLat-minLat)*Math.abs(maxLong-minLong); |
| 152 |
} |
| 153 |
|
| 154 |
stateCoordinates = stateLocationCoordinates; |
| 155 |
stateNames = stateLocationNames; |
| 156 |
this.startBranchColor = startBranchColor; |
| 157 |
this.endBranchColor = endBranchColor; |
| 158 |
this.timeScaler = timeScaler; |
| 159 |
|
| 160 |
if (mostRecentDate - (treeToExport.getHeight(treeToExport.getRootNode())*timeScaler) < 0) { |
| 161 |
ancient = true; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
public DiscreteKMLString(RootedTree tree, String annotation, String name, double date, String[][] stateLocations, String startBranchColor, String endBranchColor, double timeScaler, String[][] taxaLocations){ |
| 166 |
treeToExport = tree; |
| 167 |
this.annotation = annotation; |
| 168 |
documentName = name; |
| 169 |
mostRecentDate = date; |
| 170 |
double[][] stateLocationCoordinates = new double[stateLocations.length][2]; |
| 171 |
String[] stateLocationNames = new String[stateLocations.length]; |
| 172 |
|
| 173 |
double[][] taxaLocationCoordinates = new double[taxaLocations.length][2]; |
| 174 |
String[] taxaLocationNames = new String[taxaLocations.length]; |
| 175 |
|
| 176 |
double minLat = Double.MAX_VALUE; |
| 177 |
double maxLat = -Double.MAX_VALUE; |
| 178 |
double minLong = Double.MAX_VALUE; |
| 179 |
double maxLong = -Double.MAX_VALUE; |
| 180 |
|
| 181 |
|
| 182 |
getTaxaNamesCoordinates(taxaLocations, taxaLocationNames,taxaLocationCoordinates); |
| 183 |
coordinatesForTaxa = true; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
stateCoordinates = stateLocationCoordinates; |
| 191 |
stateNames = stateLocationNames; |
| 192 |
taxaCoordinates = taxaLocationCoordinates; |
| 193 |
taxaNames = taxaLocationNames; |
| 194 |
this.startBranchColor = startBranchColor; |
| 195 |
this.endBranchColor = endBranchColor; |
| 196 |
this.timeScaler = timeScaler; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
if (mostRecentDate - (treeToExport.getHeight(treeToExport.getRootNode())*timeScaler) < 0) { |
| 202 |
ancient = true; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
public DiscreteKMLString(RootedTree tree, String annotation, String[][] stateLocations, String name, double date, double timeScaler, double divider, double branchWidthConstant, double branchWidthMultiplier, boolean useStateProbability, double branchWidth, String startBranchColor, String endBranchColor, String branchColor, boolean useHeights, boolean usePosterior, boolean arcBranches, boolean arcTimeHeight, double altitudeFactor, boolean temporary, int numberOfIntervals, double radius, String circleOpacity, boolean coordinatesForTaxa, String[][] taxaCoordinates, boolean makeTreeSlices) { |
| 207 |
treeToExport = tree; |
| 208 |
this.annotation = annotation; |
| 209 |
documentName = name; |
| 210 |
mostRecentDate = date; |
| 211 |
double[][] stateLocationCoordinates = new double[stateLocations.length][2]; |
| 212 |
String[] stateLocationNames = new String[stateLocations.length]; |
| 213 |
|
| 214 |
double[][] taxaLocationCoordinates = null; |
| 215 |
String[] taxaLocationNames = null; |
| 216 |
if (coordinatesForTaxa) { |
| 217 |
taxaLocationCoordinates = new double[taxaCoordinates.length][2]; |
| 218 |
taxaLocationNames = new String[taxaCoordinates.length]; |
| 219 |
} |
| 220 |
|
| 221 |
double[] minMaxLatLong = getMinMaxLatLong(stateLocations, stateLocationNames,stateLocationCoordinates); |
| 222 |
getTaxaNamesCoordinates(stateLocations, stateLocationNames,stateLocationCoordinates); |
| 223 |
|
| 224 |
if (coordinatesForTaxa) { |
| 225 |
getTaxaNamesCoordinates(taxaCoordinates, taxaLocationNames,taxaLocationCoordinates); |
| 226 |
} |
| 227 |
|
| 228 |
stateCoordinates = stateLocationCoordinates; |
| 229 |
stateNames = stateLocationNames; |
| 230 |
this.taxaCoordinates = taxaLocationCoordinates; |
| 231 |
taxaNames = taxaLocationNames; |
| 232 |
this.startBranchColor = startBranchColor; |
| 233 |
this.endBranchColor = endBranchColor; |
| 234 |
this.timeScaler = timeScaler; |
| 235 |
|
| 236 |
if (mostRecentDate - (treeToExport.getHeight(treeToExport.getRootNode())*timeScaler) < 0) { |
| 237 |
ancient = true; |
| 238 |
} |
| 239 |
|
| 240 |
this.divider = divider; |
| 241 |
this.branchWidthConstant = branchWidthConstant; |
| 242 |
this.branchWidthMultiplier = branchWidthMultiplier; |
| 243 |
this.branchWidth = branchWidth; |
| 244 |
this.useStateProbability = useStateProbability; |
| 245 |
this.startBranchColor = startBranchColor; |
| 246 |
this.endBranchColor = endBranchColor; |
| 247 |
this.branchColor = branchColor; |
| 248 |
this.useHeights = useHeights; |
| 249 |
this.usePosterior = usePosterior; |
| 250 |
this.arcBranches = arcBranches; |
| 251 |
this.arcTimeHeight = arcTimeHeight; |
| 252 |
this.altitudeFactor = altitudeFactor; |
| 253 |
this.temporary = temporary; |
| 254 |
this.numberOfIntervals = numberOfIntervals; |
| 255 |
if (radius <= 0) { |
| 256 |
this.radius = radius = 10*Math.abs(minMaxLatLong[1]-minMaxLatLong[0])*Math.abs(minMaxLatLong[3]-minMaxLatLong[2]); |
| 257 |
} else { |
| 258 |
this.radius = radius; |
| 259 |
} |
| 260 |
this.circleOpacity = circleOpacity; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
if (!makeTreeSlices) { |
| 265 |
progressStream.println("\rtime attributes:"); |
| 266 |
String[] yearMonthDay = convertToYearMonthDay(mostRecentDate); |
| 267 |
|
| 268 |
progressStream.println("\tmost recent sampling date = "+mostRecentDate+" ("+yearMonthDay[0]+"-"+yearMonthDay[1]+"-"+yearMonthDay[2]+"), timeScaler = "+timeScaler); |
| 269 |
progressStream.println("\rbranch attributes:" ); |
| 270 |
progressStream.println("\tbranch segments = "+this.divider); |
| 271 |
if (useStateProbability) { |
| 272 |
progressStream.println("\tbranch width = branchWidthConstant("+this.branchWidthConstant+") + branchWithMultiplier("+this.branchWidthMultiplier+") + * state probability"); |
| 273 |
} else { |
| 274 |
progressStream.println("\tbranch width = "+this.branchWidth); |
| 275 |
} |
| 276 |
if ( this.useHeights || this.usePosterior) { |
| 277 |
if (this.startBranchColor.equals(this.endBranchColor)){ |
| 278 |
progressStream.println("\tbranch color = "+this.startBranchColor); |
| 279 |
} |
| 280 |
if (this.useHeights) { |
| 281 |
progressStream.println("\tusing heights for color"); |
| 282 |
} else if (this.usePosterior) { |
| 283 |
progressStream.println("\tusing posteriors for color"); |
| 284 |
} |
| 285 |
progressStream.println("\t\tbranch color range = "+this.startBranchColor+" - "+this.endBranchColor); |
| 286 |
} else { |
| 287 |
progressStream.println("\tbranch color = "+this.branchColor); |
| 288 |
} |
| 289 |
if (this.arcBranches) { |
| 290 |
progressStream.println("\tusing branch archs"); |
| 291 |
if (this.arcTimeHeight){ |
| 292 |
progressStream.println("\t\tarch height proportional to the time elapsed the branch"); |
| 293 |
} else { |
| 294 |
progressStream.println("\t\tarch height proportional to the distance represented by the state change along the branch"); |
| 295 |
|
| 296 |
} |
| 297 |
progressStream.println("\t\t arch height mulitplier = "+altitudeFactor); |
| 298 |
} |
| 299 |
if (this.temporary) { |
| 300 |
progressStream.println("\tbranches shown temporarily"); |
| 301 |
} |
| 302 |
progressStream.println("\rlocation circle attributes:" ); |
| 303 |
progressStream.println("\tcircle radius = "+ this.radius); |
| 304 |
progressStream.println("\tcircle opacity = "+ this.circleOpacity); |
| 305 |
progressStream.println("\tnumber of segments to represent the circle = "+ this.numberOfIntervals); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
public void writeTreeToKML() { |
| 311 |
|
| 312 |
int nodeNumber = 0; |
| 313 |
|
| 314 |
posteriorMinAndMax[1] = 1; |
| 315 |
posteriorMinAndMax[0] = getPosteriorMin(treeToExport); |
| 316 |
heightMinAndMax[0] = 0; |
| 317 |
heightMinAndMax[1] = treeToExport.getHeight(treeToExport.getRootNode()); |
| 318 |
|
| 319 |
for (Node node : treeToExport.getNodes()) { |
| 320 |
nodeNumber++; |
| 321 |
String state = ((((String)node.getAttribute(annotation)).replaceAll("\"","")).replaceAll(" ","")).trim(); |
| 322 |
|
| 323 |
if (state.contains("+")) { |
| 324 |
state = state.substring(0,state.indexOf("+")); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if (!treeToExport.isRoot(node)) { |
| 329 |
|
| 330 |
Node parentNode = treeToExport.getParent(node); |
| 331 |
|
| 332 |
|
| 333 |
Object testAttribute = parentNode.getAttribute(annotation); |
| 334 |
if (testAttribute == null) { |
| 335 |
System.err.print("An internal node has no state attribute; make sure to set the posterior probability limit to 0 when annotating an MCC tree in TreeAnnotator!"); |
| 336 |
} |
| 337 |
|
| 338 |
String parentState = ((((String)parentNode.getAttribute(annotation)).replaceAll("\"","")).replaceAll(" ","")).trim(); |
| 339 |
|
| 340 |
if (parentState.contains("+")) { |
| 341 |
parentState = parentState.substring(0,parentState.indexOf('+')); |
| 342 |
} |
| 343 |
|
| 344 |
boolean considerTaxonCoordinateForThisNode = false; |
| 345 |
if (coordinatesForTaxa) { |
| 346 |
if (treeToExport.isExternal(node)) { |
| 347 |
if (taxonHasSeparateCoordinate(treeToExport.getTaxon(node),taxaNames)){ |
| 348 |
considerTaxonCoordinateForThisNode = true; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (!(state.toLowerCase()).equals(parentState.toLowerCase()) || considerTaxonCoordinateForThisNode) { |
| 354 |
|
| 355 |
double stateProbability = (Double)node.getAttribute(annotation+".prob"); |
| 356 |
double latitude = getCoordinate(state, stateNames, stateCoordinates, 0); |
| 357 |
double longitude = getCoordinate(state, stateNames, stateCoordinates, 1); |
| 358 |
double posteriorProb = 1; |
| 359 |
if (!treeToExport.isExternal(node)) { |
| 360 |
posteriorProb = (Double)node.getAttribute("posterior"); |
| 361 |
} else { |
| 362 |
if (considerTaxonCoordinateForThisNode) { |
| 363 |
latitude = getTaxaCoordinate(treeToExport.getTaxon(node), taxaNames, taxaCoordinates, 0); |
| 364 |
longitude = getTaxaCoordinate(treeToExport.getTaxon(node), taxaNames, taxaCoordinates, 1); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
if ((latitude == 0) && (longitude == 0)) { |
| 369 |
System.err.println(state+" has no coordinate??"); |
| 370 |
} |
| 371 |
|
| 372 |
double parentStateProbability = (Double)parentNode.getAttribute(annotation+".prob"); |
| 373 |
double parentLatitude = getCoordinate(parentState, stateNames, stateCoordinates, 0); |
| 374 |
double parentLongitude = getCoordinate(parentState, stateNames, stateCoordinates, 1); |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
double distance = (3958*Math.PI*Math.sqrt((parentLatitude-latitude)*(parentLatitude-latitude)+Math.cos(parentLatitude/57.29578)*Math.cos(latitude/57.29578)*(parentLongitude-longitude)*(parentLongitude-longitude))/180); |
| 380 |
double maxAltitude; |
| 381 |
if (arcTimeHeight) { |
| 382 |
maxAltitude = (treeToExport.getHeight(parentNode) - treeToExport.getHeight(node))*altitudeFactor; |
| 383 |
} else { |
| 384 |
maxAltitude = distance*altitudeFactor; |
| 385 |
} |
| 386 |
|
| 387 |
boolean longitudeBreak = longitudeBreak(longitude,parentLongitude); |
| 388 |
double latitudeDifference = parentLatitude - latitude; |
| 389 |
double longitudeDifference; |
| 390 |
if (!longitudeBreak) { |
| 391 |
longitudeDifference = parentLongitude - longitude; |
| 392 |
} else { |
| 393 |
if (parentLongitude < 0){ |
| 394 |
longitudeDifference = (180+parentLongitude) + (180-longitude); |
| 395 |
} else { |
| 396 |
longitudeDifference = (180-parentLongitude) + (180+longitude); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
branchesBuffer.append("\t<Folder>\r"); |
| 401 |
|
| 402 |
branchesBuffer.append("\t\t\t<name>branch"+ nodeNumber +"</name>\r"); |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
double currentLongitude1 = 0; |
| 409 |
double currentLongitude2 = 0; |
| 410 |
for (int a = 0; a < divider; a ++) { |
| 411 |
|
| 412 |
branchesBuffer.append("\t\t<Placemark>\r"); |
| 413 |
|
| 414 |
branchesBuffer.append("\t\t\t<name>branch"+ nodeNumber +"_part"+(a+1)+"</name>\r"); |
| 415 |
|
| 416 |
branchesBuffer.append("\t\t\t<TimeSpan>\r"); |
| 417 |
|
| 418 |
double date = mostRecentDate - ((treeToExport.getHeight(node)*timeScaler) + (a + 1) * |
| 419 |
(((treeToExport.getHeight(parentNode)*timeScaler) - ((treeToExport.getHeight(node))*timeScaler))/divider)); |
| 420 |
|
| 421 |
double endDate = mostRecentDate - ((treeToExport.getHeight(node)*timeScaler) - (divider-(a + 1)) * |
| 422 |
(((treeToExport.getHeight(parentNode)*timeScaler) - ((treeToExport.getHeight(node))*timeScaler))/divider)); |
| 423 |
if (endDate > mostRecentDate) { |
| 424 |
endDate = mostRecentDate; |
| 425 |
} |
| 426 |
String[] yearMonthDay = convertToYearMonthDay(date); |
| 427 |
String[] endYearMonthDay = convertToYearMonthDay(endDate); |
| 428 |
|
| 429 |
if (ancient) { |
| 430 |
branchesBuffer.append("\t\t\t\t<begin>"+Math.round(date)+"</begin>\r"); |
| 431 |
} else { |
| 432 |
branchesBuffer.append("\t\t\t\t<begin>"+yearMonthDay[0]+"-"+yearMonthDay[1]+"-"+yearMonthDay[2]+"</begin>\r"); |
| 433 |
} |
| 434 |
|
| 435 |
if (temporary) { |
| 436 |
if (ancient) { |
| 437 |
branchesBuffer.append("\t\t\t\t<end>"+Math.round(endDate)+"</end>\r"); |
| 438 |
} else { |
| 439 |
branchesBuffer.append("\t\t\t\t<end>"+endYearMonthDay[0]+"-"+endYearMonthDay[1]+"-"+endYearMonthDay[2]+"</end>\r"); |
| 440 |
} |
| 441 |
} |
| 442 |
branchesBuffer.append("\t\t\t</TimeSpan>\r"); |
| 443 |
|
| 444 |
branchesBuffer.append("\t\t\t<styleUrl>#branch"+ nodeNumber +"_part"+(a+1)+"_style</styleUrl>\r"); |
| 445 |
|
| 446 |
branchesBuffer.append("\t\t\t<LineString>\r"); |
| 447 |
|
| 448 |
if (arcBranches) { |
| 449 |
branchesBuffer.append("\t\t\t\t<altitudeMode>absolute</altitudeMode>\r"); |
| 450 |
branchesBuffer.append("\t\t\t\t<tessellate>1</tessellate>\r"); |
| 451 |
} else { |
| 452 |
branchesBuffer.append("\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r"); |
| 453 |
} |
| 454 |
|
| 455 |
branchesBuffer.append("\t\t\t\t<coordinates>\r"); |
| 456 |
|
| 457 |
if (longitudeBreak) { |
| 458 |
|
| 459 |
if (longitude > 0) { |
| 460 |
currentLongitude1 = longitude+a*(longitudeDifference/divider); |
| 461 |
|
| 462 |
if (currentLongitude1 < 180) { |
| 463 |
branchesBuffer.append("\t\t\t\t\t"+currentLongitude1+","); |
| 464 |
|
| 465 |
|
| 466 |
} else { |
| 467 |
branchesBuffer.append("\t\t\t\t\t"+(-180-(180-currentLongitude1))+","); |
| 468 |
|
| 469 |
} |
| 470 |
} else { |
| 471 |
currentLongitude1 = longitude-a*(longitudeDifference/divider); |
| 472 |
|
| 473 |
if (currentLongitude1 > (-180)) { |
| 474 |
branchesBuffer.append("\t\t\t\t\t"+currentLongitude1+","); |
| 475 |
|
| 476 |
} else { |
| 477 |
branchesBuffer.append("\t\t\t\t\t"+(180+(currentLongitude1+180))+","); |
| 478 |
|
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
} else { |
| 483 |
branchesBuffer.append("\t\t\t\t\t"+(longitude+a*(longitudeDifference/divider))+","); |
| 484 |
} |
| 485 |
branchesBuffer.append((latitude+a*(latitudeDifference/divider))+","); |
| 486 |
branchesBuffer.append((maxAltitude*Math.sin(Math.acos(1 - a*(1.0/(divider/2.0)))))+"\r"); |
| 487 |
|
| 488 |
if (longitudeBreak) { |
| 489 |
|
| 490 |
if (longitude > 0) { |
| 491 |
currentLongitude2 = longitude+(a+1)*(longitudeDifference/divider); |
| 492 |
|
| 493 |
if (currentLongitude2 < 180) { |
| 494 |
branchesBuffer.append("\t\t\t\t\t"+(currentLongitude2)+","); |
| 495 |
} else { |
| 496 |
branchesBuffer.append("\t\t\t\t\t"+(-180-(180-currentLongitude2))+","); |
| 497 |
} |
| 498 |
} else { |
| 499 |
currentLongitude2 = longitude-(a+1)*(longitudeDifference/divider); |
| 500 |
|
| 501 |
if (currentLongitude2 > (-180)) { |
| 502 |
branchesBuffer.append("\t\t\t\t\t"+currentLongitude2+","); |
| 503 |
} else { |
| 504 |
branchesBuffer.append("\t\t\t\t\t"+(180+(currentLongitude2+180))+","); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
} else { |
| 509 |
branchesBuffer.append("\t\t\t\t\t"+(longitude+(a+1)*(longitudeDifference/divider))+","); |
| 510 |
} |
| 511 |
branchesBuffer.append((latitude+(a+1)*(latitudeDifference/divider))+","); |
| 512 |
branchesBuffer.append((maxAltitude*Math.sin(Math.acos(1 - (a+1)*(1.0/(divider/2.0)))))+"\r"); |
| 513 |
|
| 514 |
branchesBuffer.append("\t\t\t\t</coordinates>\r"); |
| 515 |
|
| 516 |
branchesBuffer.append("\t\t\t</LineString>\r"); |
| 517 |
branchesBuffer.append("\t\t</Placemark>\r"); |
| 518 |
|
| 519 |
styleBuffer.append("\t<Style id=\"branch"+ nodeNumber +"_part"+(a+1)+"_style\">\r"); |
| 520 |
styleBuffer.append("\t\t<LineStyle>\r"); |
| 521 |
if (useStateProbability) { |
| 522 |
double stateprobabilityDifference = (stateProbability - parentStateProbability)/divider; |
| 523 |
styleBuffer.append("\t\t\t<width>"+(branchWidthConstant+(parentStateProbability+((a + 1)*stateprobabilityDifference))*branchWidthMultiplier)+"</width>\r"); |
| 524 |
} else { |
| 525 |
styleBuffer.append("\t\t\t<width>"+branchWidth+"</width>\r"); |
| 526 |
} |
| 527 |
if (useHeights){ |
| 528 |
styleBuffer.append("\t\t\t<color>"+"FF"+ ContinuousKML.getKMLColor((treeToExport.getHeight(node) + (a + 1) * |
| 529 |
((treeToExport.getHeight(parentNode) - (treeToExport.getHeight(node)))/divider)), |
| 530 |
heightMinAndMax, startBranchColor, endBranchColor)+"</color>\r"); |
| 531 |
} else if (usePosterior){ |
| 532 |
styleBuffer.append("\t\t\t<color>"+"FF"+ ContinuousKML.getKMLColor(posteriorProb, |
| 533 |
posteriorMinAndMax, startBranchColor, endBranchColor)+"</color>\r"); |
| 534 |
} else { |
| 535 |
styleBuffer.append("\t\t\t<color>"+"FF"+branchColor+"</color>\r"); |
| 536 |
} |
| 537 |
styleBuffer.append("\t\t</LineStyle>\r"); |
| 538 |
styleBuffer.append("\t</Style>\r"); |
| 539 |
|
| 540 |
} |
| 541 |
branchesBuffer.append("\t</Folder>\r"); |
| 542 |
} |
| 543 |
|
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
public void writeTreeToKML(double time, double treeSliceBranchWidth, boolean showBranchAtMidPoint) { |
| 549 |
|
| 550 |
treeSliceBuffer.append("\t<Folder>\r"); |
| 551 |
treeSliceBuffer.append("\t\t\t<name>tree"+ time +"</name>\r"); |
| 552 |
|
| 553 |
int nodeNumber = 0; |
| 554 |
|
| 555 |
posteriorMinAndMax[1] = 1; |
| 556 |
posteriorMinAndMax[0] = getPosteriorMin(treeToExport); |
| 557 |
heightMinAndMax[0] = 0; |
| 558 |
heightMinAndMax[1] = treeToExport.getHeight(treeToExport.getRootNode()); |
| 559 |
|
| 560 |
for (Node node : treeToExport.getNodes()) { |
| 561 |
nodeNumber++; |
| 562 |
String state = ((((String)node.getAttribute(annotation)).replaceAll("\"","")).replaceAll(" ","")).trim(); |
| 563 |
|
| 564 |
|
| 565 |
if (state.contains("+")) { |
| 566 |
state = state.substring(0,state.indexOf("+")); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
if (!treeToExport.isRoot(node)) { |
| 571 |
|
| 572 |
Node parentNode = treeToExport.getParent(node); |
| 573 |
String parentState = ((((String)parentNode.getAttribute(annotation)).replaceAll("\"","")).replaceAll(" ","")).trim(); |
| 574 |
|
| 575 |
if (parentState.contains("+")) { |
| 576 |
parentState = parentState.substring(0,parentState.indexOf('+')); |
| 577 |
} |
| 578 |
|
| 579 |
if (!(state.toLowerCase()).equals(parentState.toLowerCase())) { |
| 580 |
|
| 581 |
double latitude = getCoordinate(state, stateNames, stateCoordinates, 0); |
| 582 |
double longitude = getCoordinate(state, stateNames, stateCoordinates, 1); |
| 583 |
|
| 584 |
if ((latitude == 0) && (longitude == 0)) { |
| 585 |
System.err.println(state+" has no coordinate??"); |
| 586 |
} |
| 587 |
double nodeHeight = treeToExport.getHeight(node); |
| 588 |
|
| 589 |
double parentLatitude = getCoordinate(parentState, stateNames, stateCoordinates, 0); |
| 590 |
double parentLongitude = getCoordinate(parentState, stateNames, stateCoordinates, 1); |
| 591 |
double parentHeight = treeToExport.getHeight(treeToExport.getParent(node)) |
| 592 |
; |
| 593 |
|
| 594 |
boolean longitudeBreak = longitudeBreak(longitude,parentLongitude); |
| 595 |
|
| 596 |
|
| 597 |
if ((parentHeight > time) && (nodeHeight <= time)) { |
| 598 |
|
| 599 |
|
| 600 |
if (!showBranchAtMidPoint) { |
| 601 |
|
| 602 |
latitude = parentLatitude + (latitude-parentLatitude)*((parentHeight-time)/(parentHeight-nodeHeight)); |
| 603 |
|
| 604 |
if (longitudeBreak) { |
| 605 |
if (longitude > 0) { |
| 606 |
double currentLongitude = parentLongitude - ((180-longitude)+(180+parentLongitude))*((parentHeight-time)/(parentHeight-nodeHeight)); |
| 607 |
|
| 608 |
if (currentLongitude < -180) { |
| 609 |
longitude = (180+(180+currentLongitude)); |
| 610 |
|
| 611 |
} else { |
| 612 |
longitude = currentLongitude; |
| 613 |
|
| 614 |
} |
| 615 |
} else { |
| 616 |
double currentLongitude = parentLongitude + ((180-parentLongitude)+(180+longitude))*((parentHeight-time)/(parentHeight-nodeHeight)); |
| 617 |
if (currentLongitude > 180) { |
| 618 |
longitude = (-180-(180-currentLongitude)); |
| 619 |
|
| 620 |
} else { |
| 621 |
longitude = currentLongitude; |
| 622 |
|
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
} else { |
| 627 |
longitude = parentLongitude + (longitude-parentLongitude)*((parentHeight-time)/(parentHeight-nodeHeight)); |
| 628 |
} |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
if (((parentHeight*timeScaler > time) && !(showBranchAtMidPoint)) || (showBranchAtMidPoint && (time < ((nodeHeight+((parentHeight-nodeHeight)/2.0))*timeScaler)))) { |
| 633 |
treeSliceBuffer.append("\t\t<Placemark>\r"); |
| 634 |
treeSliceBuffer.append("\t\t\t<name>branch"+ nodeNumber +"_"+parentState+"_"+state+"</name>\r"); |
| 635 |
|
| 636 |
treeSliceBuffer.append("\t\t\t<styleUrl>#tree"+time+"branch"+nodeNumber+"_style</styleUrl>\r"); |
| 637 |
|
| 638 |
treeSliceBuffer.append("\t\t\t<LineString>\r"); |
| 639 |
treeSliceBuffer.append("\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r"); |
| 640 |
treeSliceBuffer.append("\t\t\t\t<coordinates>\r"); |
| 641 |
treeSliceBuffer.append("\t\t\t\t\t"+longitude+","+latitude+",0\r"); |
| 642 |
treeSliceBuffer.append("\t\t\t\t\t"+parentLongitude+","+parentLatitude+",0\r"); |
| 643 |
treeSliceBuffer.append("\t\t\t\t</coordinates>\r"); |
| 644 |
treeSliceBuffer.append("\t\t\t</LineString>\r"); |
| 645 |
|
| 646 |
treeSliceBuffer.append("\t\t</Placemark>\r"); |
| 647 |
styleBuffer.append("\t<Style id=\"tree"+ time +"branch"+ nodeNumber +"_style\">\r"); |
| 648 |
styleBuffer.append("\t\t<LineStyle>\r"); |
| 649 |
styleBuffer.append("\t\t\t<width>"+treeSliceBranchWidth+"</width>\r"); |
| 650 |
styleBuffer.append("\t\t\t<color>"+"FF"+ ContinuousKML.getKMLColor((nodeHeight+((parentHeight-nodeHeight)/2.0)), |
| 651 |
heightMinAndMax, startBranchColor, endBranchColor)+"</color>\r"); |
| 652 |
styleBuffer.append("\t\t</LineStyle>\r"); |
| 653 |
styleBuffer.append("\t</Style>\r"); |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
} |
| 658 |
} |
| 659 |
treeSliceBuffer.append("\t</Folder>\r"); |
| 660 |
} |
| 661 |
|
| 662 |
public void writeLocationsKML() { |
| 663 |
|
| 664 |
for (int i = 0; i < stateNames.length; i++) { |
| 665 |
|
| 666 |
locationsBuffer.append("\t\t\t<Placemark>\r"); |
| 667 |
locationsBuffer.append("\t\t\t\t<name>"+stateNames[i]+"</name>\r"); |
| 668 |
|
| 669 |
locationsBuffer.append("\t\t\t\t<Point>\r"); |
| 670 |
locationsBuffer.append("\t\t\t\t\t<altitudeMode>relativeToGround</altitudeMode>\r"); |
| 671 |
locationsBuffer.append("\t\t\t\t\t<coordinates>"+stateCoordinates[i][1]+","+stateCoordinates[i][0]+","+"0</coordinates>\r"); |
| 672 |
locationsBuffer.append("\t\t\t\t</Point>\r"); |
| 673 |
|
| 674 |
locationsBuffer.append("\t\t\t</Placemark>\r"); |
| 675 |
|
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
public void writeLineagesToCircles() { |
| 680 |
|
| 681 |
String rootState = null; |
| 682 |
|
| 683 |
double rootHeight = (treeToExport.getHeight(treeToExport.getRootNode())); |
| 684 |
double delta = rootHeight/numberOfIntervals; |
| 685 |
|
| 686 |
double[][] numberOflineages = new double[(numberOfIntervals - 1)][stateNames.length + 1]; |
| 687 |
|
| 688 |
for (int i = 0; i < (numberOfIntervals - 1); i ++ ) { |
| 689 |
|
| 690 |
numberOflineages[i][0] = rootHeight-((i+1)*delta); |
| 691 |
|
| 692 |
} |
| 693 |
|
| 694 |
for (int j = 0; j < (numberOfIntervals - 1); j ++ ) { |
| 695 |
for (int k = 0; k < (stateNames.length); k ++ ) { |
| 696 |
|
| 697 |
int numberOfLineagesOfState = 0; |
| 698 |
|
| 699 |
int nodeNumber = 0; |
| 700 |
for (Node node : treeToExport.getNodes()) { |
| 701 |
nodeNumber ++; |
| 702 |
|
| 703 |
if (!treeToExport.isRoot(node)) { |
| 704 |
|
| 705 |
Node parentNode = treeToExport.getParent(node); |
| 706 |
String state = (((String)node.getAttribute(annotation)).replaceAll("\"","")).trim(); |
| 707 |
String parentState = (((String)parentNode.getAttribute(annotation)).replaceAll("\"","")).trim(); |
| 708 |
|
| 709 |
|
| 710 |
if ((treeToExport.getHeight(node) <= numberOflineages[j][0]) && (treeToExport.getHeight(parentNode) > numberOflineages[j][0])) { |
| 711 |
|
| 712 |
if ((state.equals(parentState)) && (parentState.equals(stateNames[k]))) { |
| 713 |
|
| 714 |
numberOfLineagesOfState ++; |
| 715 |
|
| 716 |
} |
| 717 |
|
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
} else { |
| 722 |
rootState = (((String)node.getAttribute(annotation)).replaceAll("\"","")).trim(); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
numberOflineages[j][k+1] = numberOfLineagesOfState; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
double rootLat = 0; |
| 733 |
double rootLong = 0; |
| 734 |
|
| 735 |
for (int a = 0; a < stateCoordinates.length; a++ ) { |
| 736 |
if (rootState.equals(stateNames[a])) { |
| 737 |
rootLat = stateCoordinates[a][0]; |
| 738 |
rootLong = stateCoordinates[a][1]; |
| 739 |
} |
| 740 |
} |
| 741 |
writeCircle(rootLat, rootLong, 36, radius, rootState, rootHeight*timeScaler, (rootHeight-delta)*timeScaler, circleBuffer); |
| 742 |
styleBuffer.append("\t<Style id=\"circle_"+rootHeight*timeScaler+"_style\">\r"); |
| 743 |
styleBuffer.append("\t\t<LineStyle>\r\t\t\t<width>0.1</width>\r\t\t</LineStyle>\r"); |
| 744 |
styleBuffer.append("\t\t<PolyStyle>\r"); |
| 745 |
styleBuffer.append("\t\t\t<color>"+circleOpacity+ ContinuousKML.getKMLColor(rootHeight, |
| 746 |
heightMinAndMax, startBranchColor, endBranchColor)+"</color>\r"); |
| 747 |
styleBuffer.append("\t\t\t<outline>0</outline>\r"); |
| 748 |
styleBuffer.append("\t\t</PolyStyle>\r"); |
| 749 |
styleBuffer.append("\t</Style>\r"); |
| 750 |
|
| 751 |
|
| 752 |
for (int o = 0; o < (numberOfIntervals - 1); o ++ ) { |
| 753 |
for (int p = 0; p < (stateNames.length); p ++ ) { |
| 754 |
|
| 755 |
if (numberOflineages[o][p+1] > 0) { |
| 756 |
writeCircle(stateCoordinates[p][0], stateCoordinates[p][1], 36, radius*Math.sqrt(numberOflineages[o][p+1]), stateNames[p], numberOflineages[o][0]*timeScaler, (numberOflineages[o][0]-delta)*timeScaler, circleBuffer); |
| 757 |
|
| 758 |
} |
| 759 |
} |
| 760 |
styleBuffer.append("\t<Style id=\"circle_"+numberOflineages[o][0]*timeScaler+"_style\">\r"); |
| 761 |
styleBuffer.append("\t\t<LineStyle>\r\t\t\t<width>0.1</width>\r\t\t</LineStyle>\r"); |
| 762 |
styleBuffer.append("\t\t<PolyStyle>\r"); |
| 763 |
styleBuffer.append("\t\t\t<color>"+circleOpacity+ ContinuousKML.getKMLColor(numberOflineages[o][0], |
| 764 |
heightMinAndMax, startBranchColor, endBranchColor)+"</color>\r"); |
| 765 |
styleBuffer.append("\t\t\t<outline>0</outline>\r"); |
| 766 |
styleBuffer.append("\t\t</PolyStyle>\r"); |
| 767 |
styleBuffer.append("\t</Style>\r"); |
| 768 |
|
| 769 |
} |
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
public void compileBuffer(StringBuffer buffer) { |
| 774 |
|
| 775 |
buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r"); |
| 776 |
buffer.append("<kml xmlns=\"http://earth.google.com/kml/2.2\">\r"); |
| 777 |
|
| 778 |
buffer.append("<Document>\r"); |
| 779 |
|
| 780 |
buffer.append("\t<name>"+documentName+"</name>\r"); |
| 781 |
buffer.append(styleBuffer); |
| 782 |
|
| 783 |
buffer.append("\t<Folder>\r"); |
| 784 |
buffer.append("\t<name>branches</name>\r"); |
| 785 |
buffer.append("\t<description>branches on which the state changes</description>\r"); |
| 786 |
buffer.append(branchesBuffer); |
| 787 |
buffer.append("\t</Folder>\r"); |
| 788 |
|
| 789 |
buffer.append("\t<Folder>\r"); |
| 790 |
buffer.append("\t\t<name>Locations</name>\r"); |
| 791 |
buffer.append("\t\t<description>Location states</description>\r"); |
| 792 |
buffer.append(locationsBuffer); |
| 793 |
buffer.append("\t</Folder>\r"); |
| 794 |
|
| 795 |
buffer.append("\t<Folder>\r"); |
| 796 |
buffer.append("\t\t<name>circles</name>\r"); |
| 797 |
buffer.append("\t\t<description>circles representing the number of lineages maintaining that state at any moment</description>\r"); |
| 798 |
buffer.append(circleBuffer); |
| 799 |
buffer.append("\t</Folder>\r"); |
| 800 |
|
| 801 |
buffer.append("\t<Folder>\r"); |
| 802 |
buffer.append("\t\t<name>treeSlices</name>\r"); |
| 803 |
buffer.append("\t\t<description>tree slices for particular times</description>\r"); |
| 804 |
buffer.append(treeSliceBuffer); |
| 805 |
buffer.append("\t</Folder>\r"); |
| 806 |
|
| 807 |
buffer.append("</Document>\r"); |
| 808 |
|
| 809 |
buffer.append("</kml>"); |
| 810 |
|
| 811 |
} |
| 812 |
|
| 813 |
private static String[] convertToYearMonthDay(double fractionalDate) { |
| 814 |
|
| 815 |
String[] yearMonthDay = new String[3]; |
| 816 |
|
| 817 |
int year = (int) fractionalDate; |
| 818 |
String yearString; |
| 819 |
|
| 820 |
if (year < 0) { |
| 821 |
yearMonthDay[0] = ""+year; |
| 822 |
yearMonthDay[1] = ""+0; |
| 823 |
yearMonthDay[2] = ""+0; |
| 824 |
return yearMonthDay; |
| 825 |
} |
| 826 |
|
| 827 |
else if (year < 10) { |
| 828 |
yearString = "000"+year; |
| 829 |
} else if (year < 100) { |
| 830 |
yearString = "00"+year; |
| 831 |
} else if (year < 1000) { |
| 832 |
yearString = "0"+year; |
| 833 |
} else { |
| 834 |
yearString = ""+year; |
| 835 |
} |
| 836 |
|
| 837 |
yearMonthDay[0] = yearString; |
| 838 |
|
| 839 |
double fractionalMonth = fractionalDate - year; |
| 840 |
|
| 841 |
int month = ((int) (12.0 * fractionalMonth)) + 1; |
| 842 |
String monthString; |
| 843 |
|
| 844 |
if (month < 10) { |
| 845 |
monthString = "0"+month; |
| 846 |
} else { |
| 847 |
monthString = ""+month; |
| 848 |
} |
| 849 |
|
| 850 |
yearMonthDay[1] = monthString; |
| 851 |
|
| 852 |
int day = ((int) Math.round(30*((12*fractionalMonth +1)- month)) + 1); |
| 853 |
String dayString; |
| 854 |
|
| 855 |
if (day < 10) { |
| 856 |
dayString = "0"+day; |
| 857 |
} else { |
| 858 |
dayString = ""+day; |
| 859 |
} |
| 860 |
|
| 861 |
yearMonthDay[2] = dayString; |
| 862 |
|
| 863 |
return yearMonthDay; |
| 864 |
|
| 865 |
} |
| 866 |
|
| 867 |
private static double getCoordinate(String state, String[] locations, double[][] stateCoordinates, int latlong) { |
| 868 |
|
| 869 |
double coordinate = 0; |
| 870 |
|
| 871 |
for(int i = 0; i<locations.length; i++) { |
| 872 |
if (locations[i].equals(state)) { |
| 873 |
coordinate = stateCoordinates[i][latlong]; |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
return coordinate; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
private static double getPosteriorMin(RootedTree tree) { |
| 882 |
|
| 883 |
double min = 1; |
| 884 |
|
| 885 |
for (Node node : tree.getNodes()) { |
| 886 |
if (!(tree.isRoot(node) || tree.isExternal(node))) { |
| 887 |
if ((Double)node.getAttribute("posterior") < min) { |
| 888 |
min = (Double)node.getAttribute("posterior"); |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
return min; |
| 894 |
|
| 895 |
} |
| 896 |
|
| 897 |
private void writeCircle(double centerLat, double centerLong, int numberOfPoints, double radius, String centerState, double beginDate, double endDate, StringBuffer buffer) { |
| 898 |
|
| 899 |
double lat1, long1, lat2, long2; |
| 900 |
double dlat, dlong, d_rad; |
| 901 |
double a, c, d; |
| 902 |
double delta_pts; |
| 903 |
double radial, lat_rad, dlon_rad, lon_rad; |
| 904 |
|
| 905 |
double degreeToRadian = Math.PI/180.0; |
| 906 |
|
| 907 |
String[] begin = convertToYearMonthDay(mostRecentDate - beginDate); |
| 908 |
String[] end = convertToYearMonthDay(mostRecentDate - endDate); |
| 909 |
|
| 910 |
|
| 911 |
lat1 = Math.toRadians(centerLat); |
| 912 |
long1 = Math.toRadians(centerLong); |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
d = radius; |
| 918 |
d_rad = d/6378137; |
| 919 |
|
| 920 |
buffer.append("\t\t\t<Placemark>\r" + |
| 921 |
"\t\t\t\t<name>"+centerState+"_"+beginDate+"_"+endDate+"</name>\r" + |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
"\t\t\t\t<TimeSpan>\r"); |
| 926 |
if (ancient) { |
| 927 |
buffer.append("\t\t\t\t\t<begin>"+Math.round(mostRecentDate - beginDate)+"</begin>\r"); |
| 928 |
} else { |
| 929 |
buffer.append("\t\t\t\t\t<begin>"+begin[0]+"-"+begin[1]+"-"+begin[2]+"</begin>\r"); |
| 930 |
} |
| 931 |
if (ancient) { |
| 932 |
buffer.append("\t\t\t\t\t<end>"+Math.round(mostRecentDate - endDate)+"</end>\r"); |
| 933 |
} else { |
| 934 |
buffer.append("\t\t\t\t\t<end>"+end[0]+"-"+end[1]+"-"+end[2]+"</end>\r"); |
| 935 |
} |
| 936 |
buffer.append("\t\t\t\t</TimeSpan>\r" + |
| 937 |
"\t\t\t\t<styleUrl>#circle_"+beginDate+"_style</styleUrl>\r" + |
| 938 |
"\t\t\t\t<Polygon>\r" + |
| 939 |
"\t\t\t\t\t<altitudeMode>clampToGround</altitudeMode>\r" + |
| 940 |
"\t\t\t\t\t<tessellate>1</tessellate>\r" + |
| 941 |
"\t\t\t\t\t<outerBoundaryIs>\r" + |
| 942 |
"\t\t\t\t\t\t<LinearRing>\r" + |
| 943 |
"\t\t\t\t\t\t\t<coordinates>\r"); |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
for(int i=0; i<=numberOfPoints; i++) { |
| 948 |
delta_pts = 360/(double)numberOfPoints; |
| 949 |
radial = Math.toRadians((double)i*delta_pts); |
| 950 |
|
| 951 |
|
| 952 |
lat_rad = Math.asin(Math.sin(lat1)* Math.cos(d_rad) + Math.cos(lat1)* Math.sin(d_rad)* Math.cos(radial)); |
| 953 |
dlon_rad = Math.atan2(Math.sin(radial)* Math.sin(d_rad)* Math.cos(lat1), Math.cos(d_rad)- Math.sin(lat1)* Math.sin(lat_rad)); |
| 954 |
lon_rad = ((long1 + dlon_rad + Math.PI) % (2*Math.PI)) - Math.PI; |
| 955 |
|
| 956 |
buffer.append("\t\t\t\t\t\t\t\t"); |
| 957 |
buffer.append(Math.toDegrees(lon_rad) + ", "); |
| 958 |
buffer.append(Math.toDegrees(lat_rad) + ", 0"); |
| 959 |
buffer.append('\r'); |
| 960 |
|
| 961 |
} |
| 962 |
buffer.append("\t\t\t\t\t\t\t</coordinates>\r" + |
| 963 |
"\t\t\t\t\t\t</LinearRing>\r" + |
| 964 |
"\t\t\t\t\t</outerBoundaryIs>\r" + |
| 965 |
"\t\t\t\t</Polygon>\r" + |
| 966 |
"\t\t\t</Placemark>\r"); |
| 967 |
|
| 968 |
|
| 969 |
} |
| 970 |
private static void print2DArray(double[][] array, String name) { |
| 971 |
try { |
| 972 |
PrintWriter outFile = new PrintWriter(new FileWriter(name), true); |
| 973 |
|
| 974 |
for (int i = 0; i < array.length; i++) { |
| 975 |
for (int j = 0; j < array[0].length; j++) { |
| 976 |
outFile.print(array[i][j]+"\t"); |
| 977 |
} |
| 978 |
outFile.println(""); |
| 979 |
} |
| 980 |
outFile.close(); |
| 981 |
|
| 982 |
} catch(IOException io) { |
| 983 |
System.err.print("Error writing to file: " + name); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
private double[] getMinMaxLatLong( String[][] locations, String[] locationNames, double[][] locationCoordinates) { |
| 988 |
|
| 989 |
double[] minMaxLatLong = new double[4]; |
| 990 |
minMaxLatLong[0] = Double.MAX_VALUE; |
| 991 |
minMaxLatLong[1] = -Double.MAX_VALUE; |
| 992 |
minMaxLatLong[2] = Double.MAX_VALUE; |
| 993 |
minMaxLatLong[3] = -Double.MAX_VALUE; |
| 994 |
|
| 995 |
|
| 996 |
for (int i = 0; i < locations.length; i++) { |
| 997 |
locationCoordinates[i][0] = Double.parseDouble(locations[i][(locations[0].length - 2)]); |
| 998 |
if (locationCoordinates[i][0] < minMaxLatLong[0]) { |
| 999 |
minMaxLatLong[0] = locationCoordinates[i][0]; |
| 1000 |
} |
| 1001 |
if (locationCoordinates[i][0] > minMaxLatLong[1]) { |
| 1002 |
minMaxLatLong[1] = locationCoordinates[i][0]; |
| 1003 |
} |
| 1004 |
locationCoordinates[i][1] = Double.parseDouble(locations[i][(locations[0].length - 1)]); |
| 1005 |
if (locationCoordinates[i][1] < minMaxLatLong[2]) { |
| 1006 |
minMaxLatLong[2] = locationCoordinates[i][1]; |
| 1007 |
} |
| 1008 |
if (locationCoordinates[i][1] > minMaxLatLong[3]) { |
| 1009 |
minMaxLatLong[3] = locationCoordinates[i][1]; |
| 1010 |
} |
| 1011 |
|
| 1012 |
} |
| 1013 |
return minMaxLatLong; |
| 1014 |
} |
| 1015 |
|
| 1016 |
private void getTaxaNamesCoordinates(String[][] locations, String[] locationNames, double[][] locationCoordinates) { |
| 1017 |
|
| 1018 |
for (int i = 0; i < locations.length; i++) { |
| 1019 |
locationNames[i] = locations[i][(locations[0].length - 3)]; |
| 1020 |
locationCoordinates[i][0] = Double.parseDouble(locations[i][(locations[0].length - 2)]); |
| 1021 |
locationCoordinates[i][1] = Double.parseDouble(locations[i][(locations[0].length - 1)]); |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
private static double getTaxaCoordinate(Taxon taxon, String[] locations, double[][] coordinates, int latOrLong) { |
| 1026 |
|
| 1027 |
double coordinate = 0; |
| 1028 |
|
| 1029 |
for(int i = 0; i<locations.length; i++) { |
| 1030 |
if ((locations[i].toLowerCase()).equals((taxon.getName()).toLowerCase())) { |
| 1031 |
coordinate = coordinates[i][latOrLong]; |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
return coordinate; |
| 1036 |
} |
| 1037 |
|
| 1038 |
private static boolean taxonHasSeparateCoordinate(Taxon taxon, String[] locations){ |
| 1039 |
|
| 1040 |
boolean hasCoordinate = false; |
| 1041 |
|
| 1042 |
for(int i = 0; i<locations.length; i++) { |
| 1043 |
if ((locations[i].toLowerCase()).equals((taxon.getName()).toLowerCase())) { |
| 1044 |
hasCoordinate = true; |
| 1045 |
} |
| 1046 |
} |
| 1047 |
return hasCoordinate; |
| 1048 |
} |
| 1049 |
private static boolean longitudeBreak(double longitude, double parentLongitude){ |
| 1050 |
|
| 1051 |
boolean longitudeBreak = false; |
| 1052 |
double trialDistance = 0; |
| 1053 |
double longitudeDifference = parentLongitude - longitude; |
| 1054 |
|
| 1055 |
if (longitude < 0) { |
| 1056 |
trialDistance += (longitude + 180); |
| 1057 |
trialDistance += (180 - parentLongitude); |
| 1058 |
|
| 1059 |
} else { |
| 1060 |
trialDistance += (parentLongitude + 180); |
| 1061 |
trialDistance += (180 - longitude); |
| 1062 |
|
| 1063 |
} |
| 1064 |
|
| 1065 |
if (trialDistance < Math.abs(longitudeDifference)) { |
| 1066 |
longitudeDifference = trialDistance; |
| 1067 |
longitudeBreak = true; |
| 1068 |
} |
| 1069 |
return longitudeBreak; |
| 1070 |
} |
| 1071 |
} |