Changing xml reader to take both coordinates of a marker

- Have commented out tests for now because  a lot of rearranging and fixing needed

#story[20]
This commit is contained in:
Erika Savell
2017-03-29 12:08:40 +13:00
parent d1d46f2cf5
commit eb4c02557d
11 changed files with 650 additions and 539 deletions
@@ -187,42 +187,42 @@ public class RaceController extends Controller{
});
}
/**
* Function for the Bermuda Race.
* @return legs in the Bermuda Race.
*/
private ArrayList<Leg> generateBermudaCourseLegs() {
ArrayList<Leg> legs = new ArrayList<>();
Leg leg1 = new Leg("Start to Marker 1", Constants.startLineMarker1, Constants.startLineMarker2, Constants.mark1, null, 0);
Leg leg2 = new Leg("Marker 1 to Leeward Gate", Constants.mark1, Constants.leewardGate1, 1);
Leg leg3 = new Leg("Leeward Gate to Windward Gate", Constants.leewardGate1, Constants.windwardGate1, 2);
Leg leg4 = new Leg("Windward Gate to Leeward Gate", Constants.windwardGate1, Constants.leewardGate1, 3);
Leg leg5 = new Leg("Leeward Gate to Finish", Constants.leewardGate1, Constants.finishLineMarker1, 4);
legs.add(leg1);
legs.add(leg2);
legs.add(leg3);
legs.add(leg4);
legs.add(leg5);
return legs;
}
/**
* Generates an example list of competitors (Official AC35 competitors)
* @return List of official AC35 competing boats
*/
private BoatInRace[] generateAC35Competitors() {
BoatInRace[] boats = new BoatInRace[6];
int i = 0;
for (BoatInRace boat : Constants.OFFICIAL_AC35_COMPETITORS) {
boat.setCurrentPosition(Constants.startLineMarker1);
boats[i] = boat;
i++;
}
return boats;
}
//
// /**
// * Function for the Bermuda Race.
// * @return legs in the Bermuda Race.
// */
// private ArrayList<Leg> generateBermudaCourseLegs() {
// ArrayList<Leg> legs = new ArrayList<>();
// Leg leg1 = new Leg("Start to Marker 1", Constants.startLineMarker1, Constants.startLineMarker2, Constants.mark1, null, 0);
// Leg leg2 = new Leg("Marker 1 to Leeward Gate", Constants.mark1, Constants.leewardGate1, 1);
// Leg leg3 = new Leg("Leeward Gate to Windward Gate", Constants.leewardGate1, Constants.windwardGate1, 2);
// Leg leg4 = new Leg("Windward Gate to Leeward Gate", Constants.windwardGate1, Constants.leewardGate1, 3);
// Leg leg5 = new Leg("Leeward Gate to Finish", Constants.leewardGate1, Constants.finishLineMarker1, 4);
// legs.add(leg1);
// legs.add(leg2);
// legs.add(leg3);
// legs.add(leg4);
// legs.add(leg5);
// return legs;
// }
//
// /**
// * Generates an example list of competitors (Official AC35 competitors)
// * @return List of official AC35 competing boats
// */
// private BoatInRace[] generateAC35Competitors() {
//
// BoatInRace[] boats = new BoatInRace[6];
//
// int i = 0;
// for (BoatInRace boat : Constants.OFFICIAL_AC35_COMPETITORS) {
// boat.setCurrentPosition(Constants.startLineMarker1);
// boats[i] = boat;
// i++;
// }
// return boats;
// }
/**
* Set the value for the race clock label
+5 -2
View File
@@ -43,8 +43,11 @@ public class BoatInRace extends Boat {
public double calculateAzimuth() {
GeodeticCalculator calc = new GeodeticCalculator();
calc.setStartingGeographicPoint(currentLeg.getStartGraphCoordinate().getLongitude(), currentLeg.getStartGraphCoordinate().getLatitude());
calc.setDestinationGeographicPoint(currentLeg.getEndGraphCoordinate().getLongitude(), currentLeg.getEndGraphCoordinate().getLatitude());
GPSCoordinate start = currentLeg.getStartMarker().getAverageGPSCoordinate();
GPSCoordinate end = currentLeg.getEndMarker().getAverageGPSCoordinate();
calc.setStartingGeographicPoint(start.getLongitude(), start.getLatitude());
calc.setDestinationGeographicPoint(end.getLongitude(), end.getLatitude());
return calc.getAzimuth();
}
@@ -42,7 +42,7 @@ public class ConstantVelocityRace extends Race {
//update boat's distance travelled
boat.setDistanceTravelledInLeg(totalDistanceTravelled);
//Calculate boat's new position by adding the distance travelled onto the start point of the leg
boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartGraphCoordinate(),
boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartMarker().getAverageGPSCoordinate(),
totalDistanceTravelled, boat.calculateAzimuth()));
}
}
+29 -1
View File
@@ -62,7 +62,6 @@ public class Leg {
// return copy;
// }
/**
* Returns the leg number that the leg exists in the Race
*
@@ -73,6 +72,35 @@ public class Leg {
return legNumber;
}
public void setName(String name) {
this.name = name;
}
public void setDistance(double distance) {
this.distance = distance;
}
public Marker getStartMarker() {
return startMarker;
}
public void setStartMarker(Marker startMarker) {
this.startMarker = startMarker;
}
public Marker getEndMarker() {
return endMarker;
}
public void setEndMarker(Marker endMarker) {
this.endMarker = endMarker;
}
public void setLegNumber(int legNumber) {
this.legNumber = legNumber;
}
/**
* Calculates the distance that the legs are in nautical miles (1.852 km).
*
+27 -27
View File
@@ -282,31 +282,31 @@ public abstract class Race implements Runnable {
* Creates a list of starting positions for the different boats, so they do not appear cramped at the start line
* @return
*/
public ArrayList<GPSCoordinate> getSpreadStartingPositions() {
int nBoats = startingBoats.size();
GPSCoordinate marker1 = legs.get(0).getStartMarker1();
GPSCoordinate marker2 = legs.get(0).getStartMarker2();
GeodeticCalculator initialCalc = new GeodeticCalculator();
initialCalc.setStartingGeographicPoint(marker1.getLongitude(), marker1.getLatitude());
initialCalc.setDestinationGeographicPoint(marker2.getLongitude(), marker2.getLatitude());
double azimuth = initialCalc.getAzimuth();
double distanceBetweenMarkers = initialCalc.getOrthodromicDistance();
double distanceBetweenBoats = distanceBetweenMarkers / (nBoats + 1);
GeodeticCalculator positionCalc = new GeodeticCalculator();
positionCalc.setStartingGeographicPoint(marker1.getLongitude(), marker1.getLatitude());
ArrayList<GPSCoordinate> positions = new ArrayList<>();
for (int i = 0; i < nBoats; i++) {
positionCalc.setDirection(azimuth, distanceBetweenBoats);
Point2D position = positionCalc.getDestinationGeographicPoint();
positions.add(new GPSCoordinate(position.getY(), position.getX()));
positionCalc = new GeodeticCalculator();
positionCalc.setStartingGeographicPoint(position);
}
return positions;
}
// public ArrayList<GPSCoordinate> getSpreadStartingPositions() {
//
// int nBoats = startingBoats.size();
// GPSCoordinate marker1 = legs.get(0).getStartMarker1();
// GPSCoordinate marker2 = legs.get(0).getStartMarker2();
// GeodeticCalculator initialCalc = new GeodeticCalculator();
// initialCalc.setStartingGeographicPoint(marker1.getLongitude(), marker1.getLatitude());
// initialCalc.setDestinationGeographicPoint(marker2.getLongitude(), marker2.getLatitude());
//
// double azimuth = initialCalc.getAzimuth();
// double distanceBetweenMarkers = initialCalc.getOrthodromicDistance();
// double distanceBetweenBoats = distanceBetweenMarkers / (nBoats + 1);
//
// GeodeticCalculator positionCalc = new GeodeticCalculator();
// positionCalc.setStartingGeographicPoint(marker1.getLongitude(), marker1.getLatitude());
// ArrayList<GPSCoordinate> positions = new ArrayList<>();
//
// for (int i = 0; i < nBoats; i++) {
// positionCalc.setDirection(azimuth, distanceBetweenBoats);
// Point2D position = positionCalc.getDestinationGeographicPoint();
// positions.add(new GPSCoordinate(position.getY(), position.getX()));
//
// positionCalc = new GeodeticCalculator();
// positionCalc.setStartingGeographicPoint(position);
// }
// return positions;
// }
}
+31 -3
View File
@@ -6,6 +6,7 @@ import org.xml.sax.SAXException;
import seng302.Model.Boat;
import seng302.Model.BoatInRace;
import seng302.Model.Leg;
import seng302.Model.Marker;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
@@ -61,11 +62,11 @@ public class RaceXMLReader extends XMLReader{
for (int i = 0; i < nLegs.getLength(); i++){
String label = getTextValueOfNode((Element) nLegs.item(i), "name");
NodeList start = ((Element) nLegs.item(i)).getElementsByTagName("start");
GPSCoordinate startCoord = getCoordinates(start);
Marker startMarker = getMarker(start);
NodeList finish = ((Element) nLegs.item(i)).getElementsByTagName("finish");
GPSCoordinate finishCoord = getCoordinates(finish);
Marker finishMarker = getMarker(finish);
//System.out.println(String.format("%s, %s, %s, %s",label, startCoord, finishCoord, i));
legs.add(new Leg(label, startCoord, finishCoord, i));
legs.add(new Leg(label, startMarker, finishMarker, i));
}
}
@@ -89,6 +90,33 @@ public class RaceXMLReader extends XMLReader{
finishPt2 = getCoordinates(nMarks, 4, 1);
}
private Marker getMarker(NodeList start) { return getMarker(start, 0); }
private Marker getMarker(NodeList start, int startIndex) {
return getMarker(start, startIndex, 0);
}
private Marker getMarker(NodeList start, int startIndex, int nodeIndex) {
NodeList nodeList = ((Element) start.item(startIndex)).getElementsByTagName("marker");
Element marker = (Element) nodeList.item(nodeIndex);
return getMarker(marker);
}
private Marker getMarker(Element markerNode) {
NodeList nCoordinates = markerNode.getElementsByTagName("coordinate");
GPSCoordinate side1 = getCoordinates((Element) nCoordinates.item(0));
GPSCoordinate side2;
if (nCoordinates.getLength() > 1) {
side2 = getCoordinates((Element) nCoordinates.item(0));
} else {
side2 = side1;
}
return new Marker(side1, side2);
}
private GPSCoordinate getCoordinates(NodeList start){
return getCoordinates(start, 0);
}
+88 -38
View File
@@ -41,78 +41,128 @@
<leg>
<name>Start to Mark 1</name>
<start>
<coordinate>
<latitude>32.296577</latitude>
<longitude>-64.854304</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.296577</latitude>
<longitude>-64.854304</longitude>
</coordinate>
<coordinate>
<latitude>32.293771</latitude>
<longitude>-64.855242</longitude>
</coordinate>
</marker>
</start>
<finish>
<coordinate>
<latitude>32.293039</latitude>
<longitude>-64.843983</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.293039</latitude>
<longitude>-64.843983</longitude>
</coordinate>
</marker>
</finish>
</leg>
<leg>
<name>Mark 1 to Leeward Gate</name>
<start>
<coordinate>
<latitude>32.293039</latitude>
<longitude>-64.843983</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.293039</latitude>
<longitude>-64.843983</longitude>
</coordinate>
</marker>
</start>
<finish>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<coordinate>
<latitude>32.308046</latitude>
<longitude>-64.831785</longitude>
</coordinate>
</marker>
</finish>
</leg>
<leg>
<name>Leeward Gate to Windward Gate</name>
<start>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<coordinate>
<latitude>32.308046</latitude>
<longitude>-64.831785</longitude>
</coordinate>
</marker>
</start>
<finish>
<coordinate>
<latitude>32.284680</latitude>
<longitude>-64.850045</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.284680</latitude>
<longitude>-64.850045</longitude>
</coordinate>
<coordinate>
<latitude>32.280164</latitude>
<longitude>-64.847591</longitude>
</coordinate>
</marker>
</finish>
</leg>
<leg>
<name>Windward Gate to Leeward Gate</name>
<start>
<coordinate>
<marker>
<coordinate>
<latitude>32.284680</latitude>
<longitude>-64.850045</longitude>
</coordinate>
</coordinate>
<coordinate>
<latitude>32.280164</latitude>
<longitude>-64.847591</longitude>
</coordinate>
</marker>
</start>
<finish>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<coordinate>
<latitude>32.308046</latitude>
<longitude>-64.831785</longitude>
</coordinate>
</marker>
</finish>
</leg>
<leg>
<name>Leeward Gate to Finish</name>
<start>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.309693</latitude>
<longitude>-64.835249</longitude>
</coordinate>
<coordinate>
<latitude>32.308046</latitude>
<longitude>-64.831785</longitude>
</coordinate>
</marker>
</start>
<finish>
<coordinate>
<latitude>32.317379</latitude>
<longitude>-64.839291</longitude>
</coordinate>
<marker>
<coordinate>
<latitude>32.317379</latitude>
<longitude>-64.839291</longitude>
</coordinate>
<coordinate>
<latitude>32.317257</latitude>
<longitude>-64.836260</longitude>
</coordinate>
</marker>
</finish>
</leg>
</legs>