Reads BoatDataSource into StreamedCourseXMLReader so the latter can function as a single source of static information for the race.
- Mock.StreamedCourseXMLReader populates participant and mark list from BoatDataSource - Added CompoundMarker class to accommodate Mark data from Boat XML - Had to check for Yacht type in Visualiser.BoatXMLReader - Added a missing coordinate value to raceTest.xml - Marker class still exists due to large number of tests, which should be transitioned to CompoundMark - IMPORTANT: race is no longer functional #story[881]
This commit is contained in:
@@ -6,6 +6,8 @@ import javafx.stage.Stage;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.DataInput.*;
|
||||
import seng302.Model.Event;
|
||||
import seng302.Model.StreamedCourseXMLException;
|
||||
import seng302.Model.StreamedCourseXMLReader;
|
||||
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
@@ -13,6 +15,7 @@ import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.ParseException;
|
||||
|
||||
public class App extends Application {
|
||||
|
||||
@@ -35,9 +38,9 @@ public class App extends Application {
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
try {
|
||||
RaceDataSource raceData = new RaceXMLReader("mockXML/raceTest.xml");
|
||||
RegattaDataSource regattaData = new RegattaXMLReader("mockXML/regattaTest.xml");
|
||||
BoatDataSource boatData = new BoatXMLReader("mockXML/boatTest.xml");
|
||||
RaceDataSource raceData = new StreamedCourseXMLReader("mockXML/raceTest.xml", boatData);
|
||||
Event raceEvent = new Event(raceData, regattaData, boatData);
|
||||
raceEvent.start();
|
||||
} catch (IOException e) {
|
||||
@@ -46,6 +49,10 @@ public class App extends Application {
|
||||
e.printStackTrace();
|
||||
} catch (ParserConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
} catch (StreamedCourseXMLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.w3c.dom.Element;
|
||||
import seng302.DataInput.RaceDataSource;
|
||||
import seng302.Exceptions.InvalidRaceDataException;
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.CompoundMarker;
|
||||
import seng302.Model.CompoundMark;
|
||||
import seng302.Model.GPSCoordinate;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
@@ -151,9 +151,9 @@ public class RaceData {
|
||||
Element compoundMarkSeqElement = doc.createElement("CompoundMarkSequence");
|
||||
|
||||
int i = 1;
|
||||
for (CompoundMarker compoundMarker : dataSource.getCompoundMarkers()) {
|
||||
for (CompoundMark compoundMark : dataSource.getCompoundMarks()) {
|
||||
|
||||
courseElement.appendChild(createCompoundMarker(compoundMarker, i));
|
||||
courseElement.appendChild(createCompoundMarker(compoundMark, i));
|
||||
compoundMarkSeqElement.appendChild(createCornerElement(i));
|
||||
i++;
|
||||
}
|
||||
@@ -177,21 +177,21 @@ public class RaceData {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a compound compoundMarker holding one or two marks,and a sequence number
|
||||
* Creates a compound compoundMark holding one or two marks,and a sequence number
|
||||
*
|
||||
* @param compoundMarker compoundMarker
|
||||
* @param compoundMark compoundMark
|
||||
* @param i sequence number
|
||||
* @return Element compound mark element
|
||||
*/
|
||||
private Element createCompoundMarker(CompoundMarker compoundMarker, int i) {
|
||||
private Element createCompoundMarker(CompoundMark compoundMark, int i) {
|
||||
Element compoundMarkElement = doc.createElement("CompoundMark");
|
||||
compoundMarkElement.setAttribute("CompoundMarkID", i + "");
|
||||
compoundMarkElement.setAttribute("Name", compoundMarker.getName());
|
||||
compoundMarkElement.setAttribute("Name", compoundMark.getName());
|
||||
|
||||
compoundMarkElement.appendChild(createMark(compoundMarker.getMark1()));
|
||||
compoundMarkElement.appendChild(createMark(compoundMark.getMark1()));
|
||||
|
||||
if (!(compoundMarker.getMark1().equals(compoundMarker.getMark2()))) {
|
||||
compoundMarkElement.appendChild(createMark(compoundMarker.getMark2()));
|
||||
if (!(compoundMark.getMark1().equals(compoundMark.getMark2()))) {
|
||||
compoundMarkElement.appendChild(createMark(compoundMark.getMark2()));
|
||||
}
|
||||
|
||||
return compoundMarkElement;
|
||||
|
||||
@@ -3,12 +3,12 @@ package seng302.DataInput;
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.Mark;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by cbt24 on 10/05/17.
|
||||
*/
|
||||
public interface BoatDataSource {
|
||||
List<Boat> getBoats();
|
||||
List<Mark> getMarkerBoats();
|
||||
Map<Integer, Boat> getBoats();
|
||||
Map<Integer, Mark> getMarkerBoats();
|
||||
}
|
||||
|
||||
@@ -4,13 +4,12 @@ import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.GPSCoordinate;
|
||||
import seng302.Model.Mark;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -51,7 +50,7 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
for (int i = 0; i < nBoats.getChildNodes().getLength(); i++) {
|
||||
Node boat = nBoats.getChildNodes().item(i);
|
||||
if (boat.getNodeName().equals("Boat")) {
|
||||
readSingleBoat(boat);
|
||||
readBoatNode(boat);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,34 +77,39 @@ public class BoatXMLReader extends XMLReader implements BoatDataSource {
|
||||
* Reads the information about one boat
|
||||
* Ignored values: ShapeID, StoweName, HullNum, Skipper, Type
|
||||
*/
|
||||
private void readSingleBoat(Node boatNode) {
|
||||
Boat boat;
|
||||
|
||||
private void readBoatNode(Node boatNode) {
|
||||
int sourceID = Integer.parseInt(boatNode.getAttributes().getNamedItem("SourceID").getTextContent());
|
||||
String name = boatNode.getAttributes().getNamedItem("BoatName").getTextContent();
|
||||
|
||||
if (isYachtNode(boatNode)) {
|
||||
String shortName = boatNode.getAttributes().getNamedItem("ShortName").getTextContent();
|
||||
if (exists(boatNode, "Country")) {
|
||||
String country = boatNode.getAttributes().getNamedItem("Country").getTextContent();
|
||||
boat = new Boat(sourceID, name, country);
|
||||
} else {
|
||||
boat = new Boat(sourceID, name, shortName);
|
||||
}
|
||||
boatMap.put(sourceID, boat);
|
||||
if (isYachtNode(boatNode)) readYacht(boatNode, sourceID, name);
|
||||
else readMark(boatNode, sourceID, name);
|
||||
}
|
||||
|
||||
private void readYacht(Node boatNode, int sourceID, String name) {
|
||||
String shortName = boatNode.getAttributes().getNamedItem("ShortName").getTextContent();
|
||||
if (exists(boatNode, "Country")) {
|
||||
String country = boatNode.getAttributes().getNamedItem("Country").getTextContent();
|
||||
boatMap.put(sourceID, new Boat(sourceID, name, country));
|
||||
} else {
|
||||
Mark mark = new Mark(name);
|
||||
markerMap.put(sourceID, mark);
|
||||
boatMap.put(sourceID, new Boat(sourceID, name, shortName));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Boat> getBoats() {
|
||||
return new ArrayList<>(boatMap.values());
|
||||
private void readMark(Node boatNode, int sourceID, String name) {
|
||||
Node nCoord = ((Element)boatNode).getElementsByTagName("GPSposition").item(0);
|
||||
double x = Double.parseDouble(nCoord.getAttributes().getNamedItem("X").getTextContent());
|
||||
double y = Double.parseDouble(nCoord.getAttributes().getNamedItem("Y").getTextContent());
|
||||
Mark mark = new Mark(sourceID, name, new GPSCoordinate(x,y));
|
||||
markerMap.put(sourceID, mark);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Mark> getMarkerBoats() {
|
||||
return new ArrayList<>(markerMap.values());
|
||||
public Map<Integer, Boat> getBoats() {
|
||||
return boatMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Integer, Mark> getMarkerBoats() {
|
||||
return markerMap;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ package seng302.DataInput;
|
||||
|
||||
;
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.CompoundMark;
|
||||
import seng302.Model.GPSCoordinate;
|
||||
import seng302.Model.Leg;
|
||||
import seng302.Model.CompoundMarker;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -18,13 +19,13 @@ public interface RaceDataSource {
|
||||
|
||||
List<GPSCoordinate> getBoundary();
|
||||
|
||||
List<CompoundMarker> getCompoundMarkers();
|
||||
List<CompoundMark> getCompoundMarks();
|
||||
|
||||
int getRaceId();
|
||||
|
||||
String getRaceType();
|
||||
|
||||
GPSCoordinate getMark();
|
||||
ZonedDateTime getZonedDateTime();
|
||||
|
||||
GPSCoordinate getMapTopLeft();
|
||||
|
||||
|
||||
@@ -5,18 +5,17 @@ import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import seng302.Model.Boat;
|
||||
import seng302.Model.CompoundMarker;
|
||||
import seng302.Model.GPSCoordinate;
|
||||
import seng302.Model.Leg;
|
||||
import seng302.Model.*;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by fwy13 on 26/03/2017.
|
||||
* @deprecated please use {@link seng302.Model.StreamedCourseXMLReader}
|
||||
*/
|
||||
public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
private static double COORDINATEPADDING = 0.0005;
|
||||
@@ -27,7 +26,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
private GPSCoordinate mark, startPt1, startPt2, finishPt1, finishPt2, leewardPt1, leewardPt2, windwardPt1, windwardPt2;
|
||||
private GPSCoordinate mapTopLeft, mapBottomRight;
|
||||
private List<GPSCoordinate> boundary = new ArrayList<>();
|
||||
private List<CompoundMarker> compoundMarkers = new ArrayList<>();
|
||||
private List<CompoundMark> compoundMarks = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constractor for Race XML
|
||||
@@ -106,8 +105,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
NodeList nMarkers = doc.getElementsByTagName("marker");
|
||||
|
||||
for (int i = 0; i < nMarkers.getLength(); i++) {
|
||||
CompoundMarker compoundMarker = getMarker((Element) nMarkers.item(i));
|
||||
if (compoundMarker.getName() != null) compoundMarkers.add(compoundMarker);
|
||||
CompoundMark compoundMark = getMarker((Element) nMarkers.item(i));
|
||||
if (compoundMark.getName() != null) compoundMarks.add(compoundMark);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,10 +120,10 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
for (int i = 0; i < nLegs.getLength(); i++) {
|
||||
String label = getTextValueOfNode((Element) nLegs.item(i), "name");
|
||||
NodeList start = ((Element) nLegs.item(i)).getElementsByTagName("start");
|
||||
CompoundMarker startCompoundMarker = getMarker(start);
|
||||
CompoundMark startCompoundMark = getMarker(start);
|
||||
NodeList finish = ((Element) nLegs.item(i)).getElementsByTagName("finish");
|
||||
CompoundMarker finishCompoundMarker = getMarker(finish);
|
||||
legs.add(new Leg(label, startCompoundMarker, finishCompoundMarker, i));
|
||||
CompoundMark finishCompoundMark = getMarker(finish);
|
||||
legs.add(new Leg(label, startCompoundMark, finishCompoundMark, i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,7 +203,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
* @param start base nodelist this should be the tag that contains <coordinate></coordinate>
|
||||
* @return
|
||||
*/
|
||||
private CompoundMarker getMarker(NodeList start) {
|
||||
private CompoundMark getMarker(NodeList start) {
|
||||
return getMarker(start, 0);
|
||||
}
|
||||
|
||||
@@ -215,7 +214,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
* @param startIndex index in the node that has the coordinate tag
|
||||
* @return
|
||||
*/
|
||||
private CompoundMarker getMarker(NodeList start, int startIndex) {
|
||||
private CompoundMark getMarker(NodeList start, int startIndex) {
|
||||
return getMarker(start, startIndex, 0);
|
||||
}
|
||||
|
||||
@@ -227,7 +226,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
* @param nodeIndex coordinate index
|
||||
* @return
|
||||
*/
|
||||
private CompoundMarker getMarker(NodeList start, int startIndex, int nodeIndex) {
|
||||
private CompoundMark getMarker(NodeList start, int startIndex, int nodeIndex) {
|
||||
NodeList nodeList = ((Element) start.item(startIndex)).getElementsByTagName("marker");
|
||||
Element marker = (Element) nodeList.item(nodeIndex);
|
||||
return getMarker(marker);
|
||||
@@ -239,7 +238,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
* @param markerNode marker to turn into coordinates
|
||||
* @return
|
||||
*/
|
||||
private CompoundMarker getMarker(Element markerNode) {
|
||||
private CompoundMark getMarker(Element markerNode) {
|
||||
|
||||
NodeList nCoordinates = markerNode.getElementsByTagName("coordinate");
|
||||
|
||||
@@ -251,7 +250,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
side2 = side1;
|
||||
}
|
||||
NodeList name = markerNode.getElementsByTagName("name");
|
||||
return name.getLength() == 1 ? new CompoundMarker(getTextValueOfNode((Element) markerNode, "name"), side1, side2) : new CompoundMarker(side1, side2);
|
||||
return null;//name.getLength() == 1 ? new CompoundMark(getTextValueOfNode((Element) markerNode, "name"), side1, side2) : new CompoundMark(side1, side2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -362,11 +361,16 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public List<CompoundMarker> getCompoundMarkers() {
|
||||
return compoundMarkers;
|
||||
public List<CompoundMark> getCompoundMarks() {
|
||||
return compoundMarks;
|
||||
}
|
||||
|
||||
public String getRaceType() {
|
||||
return "FLEET";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime getZonedDateTime() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +41,8 @@ public class Boat {
|
||||
public double calculateAzimuth() {
|
||||
|
||||
GeodeticCalculator calc = new GeodeticCalculator();
|
||||
GPSCoordinate start = currentLeg.getStartCompoundMarker().getAverageGPSCoordinate();
|
||||
GPSCoordinate end = currentLeg.getEndCompoundMarker().getAverageGPSCoordinate();
|
||||
GPSCoordinate start = currentLeg.getStartCompoundMark().getAverageGPSCoordinate();
|
||||
GPSCoordinate end = currentLeg.getEndCompoundMark().getAverageGPSCoordinate();
|
||||
|
||||
calc.setStartingGeographicPoint(start.getLongitude(), start.getLatitude());
|
||||
calc.setDestinationGeographicPoint(end.getLongitude(), end.getLatitude());
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package seng302.Model;
|
||||
|
||||
import org.geotools.referencing.GeodeticCalculator;
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
/**
|
||||
* Created by esa46 on 29/03/17.
|
||||
*/
|
||||
public class CompoundMark extends Marker{
|
||||
|
||||
private GPSCoordinate averageGPSCoordinate;
|
||||
private Mark mark1;
|
||||
private Mark mark2 = null;
|
||||
private String name;
|
||||
|
||||
public CompoundMark(Mark mark1) {
|
||||
super(mark1.getPosition());
|
||||
this.mark1 = mark1;
|
||||
this.averageGPSCoordinate = calculateAverage();
|
||||
|
||||
}
|
||||
|
||||
public CompoundMark(Mark mark1, Mark mark2) {
|
||||
super(mark1.getPosition(), mark2.getPosition());
|
||||
this.mark1 = mark1;
|
||||
this.mark2 = mark2;
|
||||
this.averageGPSCoordinate = calculateAverage();
|
||||
|
||||
}
|
||||
|
||||
public CompoundMark(String name, Mark mark1, Mark mark2) {
|
||||
super(name, mark1.getPosition(), mark2.getPosition());
|
||||
this.name = name;
|
||||
this.mark1 = mark1;
|
||||
this.mark2 = mark2;
|
||||
this.averageGPSCoordinate = calculateAverage();
|
||||
|
||||
}
|
||||
|
||||
public GPSCoordinate getMark1() {
|
||||
return mark1.getPosition();
|
||||
}
|
||||
|
||||
public GPSCoordinate getMark2() {
|
||||
return mark2.getPosition();
|
||||
}
|
||||
|
||||
public GPSCoordinate getAverageGPSCoordinate() {
|
||||
return averageGPSCoordinate;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
private GPSCoordinate calculateAverage() {
|
||||
if(mark2 != null) {
|
||||
GeodeticCalculator calc = new GeodeticCalculator();
|
||||
calc.setStartingGeographicPoint(mark1.getPosition().getLongitude(), mark1.getPosition().getLatitude());
|
||||
calc.setDestinationGeographicPoint(mark2.getPosition().getLongitude(), mark2.getPosition().getLatitude());
|
||||
double azimuth = calc.getAzimuth();
|
||||
double distance = calc.getOrthodromicDistance();
|
||||
|
||||
GeodeticCalculator middleCalc = new GeodeticCalculator();
|
||||
middleCalc.setStartingGeographicPoint(mark1.getPosition().getLongitude(), mark1.getPosition().getLatitude());
|
||||
middleCalc.setDirection(azimuth, distance / 2);
|
||||
Point2D middlePoint = middleCalc.getDestinationGeographicPoint();
|
||||
return new GPSCoordinate(middlePoint.getY(), middlePoint.getX());
|
||||
} else return mark1.getPosition();
|
||||
}
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public class Event {
|
||||
mockOutput.parseXMLString(regattaData, 26);
|
||||
|
||||
System.out.println("Sending Race");
|
||||
String raceData = readFile("mock/src/main/resources//mockXML/raceTest.xml", StandardCharsets.UTF_8);
|
||||
String raceData = readFile("mock/src/main/resources/mockXML/raceTest.xml", StandardCharsets.UTF_8);
|
||||
mockOutput.setRaceXml(raceData);
|
||||
mockOutput.parseXMLString(raceData, 26);
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ import seng302.Constants;
|
||||
public class Leg {
|
||||
private String name; //nautical miles
|
||||
private double distance;
|
||||
private CompoundMarker startCompoundMarker;
|
||||
private CompoundMarker endCompoundMarker;
|
||||
private Marker startCompoundMark;
|
||||
private Marker endCompoundMark;
|
||||
private int legNumber;
|
||||
|
||||
/**
|
||||
@@ -22,10 +22,10 @@ public class Leg {
|
||||
* @param end marker
|
||||
* @param number Leg's position in race
|
||||
*/
|
||||
public Leg(String name, CompoundMarker start, CompoundMarker end, int number) {
|
||||
public Leg(String name, Marker start, Marker end, int number) {
|
||||
this.name = name;
|
||||
this.startCompoundMarker = start;
|
||||
this.endCompoundMarker = end;
|
||||
this.startCompoundMark = start;
|
||||
this.endCompoundMark = end;
|
||||
this.legNumber = number;
|
||||
calculateDistance();
|
||||
}
|
||||
@@ -69,20 +69,20 @@ public class Leg {
|
||||
}
|
||||
|
||||
|
||||
public CompoundMarker getStartCompoundMarker() {
|
||||
return startCompoundMarker;
|
||||
public Marker getStartCompoundMark() {
|
||||
return startCompoundMark;
|
||||
}
|
||||
|
||||
public void setStartCompoundMarker(CompoundMarker startCompoundMarker) {
|
||||
this.startCompoundMarker = startCompoundMarker;
|
||||
public void setStartCompoundMark(Marker startCompoundMark) {
|
||||
this.startCompoundMark = startCompoundMark;
|
||||
}
|
||||
|
||||
public CompoundMarker getEndCompoundMarker() {
|
||||
return endCompoundMarker;
|
||||
public Marker getEndCompoundMark() {
|
||||
return endCompoundMark;
|
||||
}
|
||||
|
||||
public void setEndCompoundMarker(CompoundMarker endCompoundMarker) {
|
||||
this.endCompoundMarker = endCompoundMarker;
|
||||
public void setEndCompoundMark(Marker endCompoundMark) {
|
||||
this.endCompoundMark = endCompoundMark;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,8 +92,8 @@ public class Leg {
|
||||
|
||||
GeodeticCalculator calc = new GeodeticCalculator();
|
||||
//Load start and end of leg
|
||||
GPSCoordinate startMarker = this.startCompoundMarker.getAverageGPSCoordinate();
|
||||
GPSCoordinate endMarker = this.endCompoundMarker.getAverageGPSCoordinate();
|
||||
GPSCoordinate startMarker = this.startCompoundMark.getAverageGPSCoordinate();
|
||||
GPSCoordinate endMarker = this.endCompoundMark.getAverageGPSCoordinate();
|
||||
calc.setStartingGeographicPoint(startMarker.getLongitude(), startMarker.getLatitude());
|
||||
calc.setDestinationGeographicPoint(endMarker.getLongitude(), endMarker.getLatitude());
|
||||
this.distance = calc.getOrthodromicDistance() / Constants.NMToMetersConversion;
|
||||
|
||||
@@ -4,13 +4,25 @@ package seng302.Model;
|
||||
* Created by cbt24 on 10/05/17.
|
||||
*/
|
||||
public class Mark {
|
||||
private int sourceID;
|
||||
private String name;
|
||||
private GPSCoordinate position;
|
||||
|
||||
public Mark(String name) {
|
||||
public Mark(int sourceID, String name, GPSCoordinate position) {
|
||||
this.sourceID = sourceID;
|
||||
this.name = name;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return sourceID;
|
||||
}
|
||||
|
||||
public GPSCoordinate getPosition() {
|
||||
return position;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -6,7 +6,7 @@ import java.awt.geom.Point2D;
|
||||
/**
|
||||
* Created by esa46 on 29/03/17.
|
||||
*/
|
||||
public class CompoundMarker {
|
||||
public class Marker {
|
||||
|
||||
private GPSCoordinate averageGPSCoordinate;
|
||||
private GPSCoordinate mark1;
|
||||
@@ -14,7 +14,7 @@ public class CompoundMarker {
|
||||
private String name;
|
||||
private boolean doubleMarker = false;
|
||||
|
||||
public CompoundMarker(GPSCoordinate mark1) {
|
||||
public Marker(GPSCoordinate mark1) {
|
||||
|
||||
this.mark1 = mark1;
|
||||
this.mark2 = mark1;
|
||||
@@ -22,7 +22,7 @@ public class CompoundMarker {
|
||||
|
||||
}
|
||||
|
||||
public CompoundMarker(GPSCoordinate mark1, GPSCoordinate mark2) {
|
||||
public Marker(GPSCoordinate mark1, GPSCoordinate mark2) {
|
||||
|
||||
this.mark1 = mark1;
|
||||
this.mark2 = mark2;
|
||||
@@ -30,7 +30,7 @@ public class CompoundMarker {
|
||||
|
||||
}
|
||||
|
||||
public CompoundMarker(String name, GPSCoordinate mark1, GPSCoordinate mark2) {
|
||||
public Marker(String name, GPSCoordinate mark1, GPSCoordinate mark2) {
|
||||
|
||||
this.name = name;
|
||||
this.mark1 = mark1;
|
||||
@@ -71,4 +71,4 @@ public class CompoundMarker {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -216,19 +216,14 @@ public class Race implements Runnable {
|
||||
public void initialiseBoats() {
|
||||
Leg officialStart = legs.get(0);
|
||||
String name = officialStart.getName();
|
||||
CompoundMarker endCompoundMarker = officialStart.getEndCompoundMarker();
|
||||
|
||||
ArrayList<CompoundMarker> startCompoundMarkers = getSpreadStartingPositions();
|
||||
ArrayList<GPSCoordinate> startingPositions = getSpreadStartingPositions();
|
||||
for (int i = 0; i < startingBoats.size(); i++) {
|
||||
Boat boat = startingBoats.get(i);
|
||||
if (boat != null) {
|
||||
boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
|
||||
Leg startLeg = new Leg(name, 0);
|
||||
boat.setCurrentPosition(startCompoundMarkers.get(i).getAverageGPSCoordinate());
|
||||
startLeg.setStartCompoundMarker(startCompoundMarkers.get(i));
|
||||
startLeg.setEndCompoundMarker(endCompoundMarker);
|
||||
startLeg.calculateDistance();
|
||||
boat.setCurrentLeg(startLeg);
|
||||
boat.setCurrentPosition(startingPositions.get(i));
|
||||
boat.setCurrentLeg(officialStart);
|
||||
boat.setHeading(boat.calculateHeading());
|
||||
}
|
||||
}
|
||||
@@ -239,27 +234,27 @@ public class Race implements Runnable {
|
||||
*
|
||||
* @return list of starting positions
|
||||
*/
|
||||
public ArrayList<CompoundMarker> getSpreadStartingPositions() {
|
||||
public ArrayList<GPSCoordinate> getSpreadStartingPositions() {
|
||||
|
||||
int nBoats = startingBoats.size();
|
||||
CompoundMarker compoundMarker = legs.get(0).getStartCompoundMarker();
|
||||
Marker compoundMark = legs.get(0).getStartCompoundMark();
|
||||
|
||||
GeodeticCalculator initialCalc = new GeodeticCalculator();
|
||||
initialCalc.setStartingGeographicPoint(compoundMarker.getMark1().getLongitude(), compoundMarker.getMark1().getLatitude());
|
||||
initialCalc.setDestinationGeographicPoint(compoundMarker.getMark2().getLongitude(), compoundMarker.getMark2().getLatitude());
|
||||
initialCalc.setStartingGeographicPoint(compoundMark.getMark1().getLongitude(), compoundMark.getMark1().getLatitude());
|
||||
initialCalc.setDestinationGeographicPoint(compoundMark.getMark2().getLongitude(), compoundMark.getMark2().getLatitude());
|
||||
|
||||
double azimuth = initialCalc.getAzimuth();
|
||||
double distanceBetweenMarkers = initialCalc.getOrthodromicDistance();
|
||||
double distanceBetweenBoats = distanceBetweenMarkers / (nBoats + 1);
|
||||
|
||||
GeodeticCalculator positionCalc = new GeodeticCalculator();
|
||||
positionCalc.setStartingGeographicPoint(compoundMarker.getMark1().getLongitude(), compoundMarker.getMark1().getLatitude());
|
||||
ArrayList<CompoundMarker> positions = new ArrayList<>();
|
||||
positionCalc.setStartingGeographicPoint(compoundMark.getMark1().getLongitude(), compoundMark.getMark1().getLatitude());
|
||||
ArrayList<GPSCoordinate> positions = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < nBoats; i++) {
|
||||
positionCalc.setDirection(azimuth, distanceBetweenBoats);
|
||||
Point2D position = positionCalc.getDestinationGeographicPoint();
|
||||
positions.add(new CompoundMarker(new GPSCoordinate(position.getY(), position.getX())));
|
||||
positions.add(new GPSCoordinate(position.getY(), position.getX()));
|
||||
|
||||
positionCalc = new GeodeticCalculator();
|
||||
positionCalc.setStartingGeographicPoint(position);
|
||||
@@ -302,7 +297,7 @@ public class Race implements Runnable {
|
||||
//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().getStartCompoundMarker().getAverageGPSCoordinate(),
|
||||
boat.setCurrentPosition(calculatePosition(boat.getCurrentLeg().getStartCompoundMark().getAverageGPSCoordinate(),
|
||||
totalDistanceTravelled, boat.calculateAzimuth()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package seng302.Model;
|
||||
|
||||
/**
|
||||
* Created by cbt24 on 25/04/17.
|
||||
*/
|
||||
public class StreamedCourseXMLException extends Throwable {
|
||||
}
|
||||
@@ -0,0 +1,274 @@
|
||||
package seng302.Model;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.DataInput.BoatDataSource;
|
||||
import seng302.DataInput.RaceDataSource;
|
||||
import seng302.DataInput.XMLReader;
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.text.ParseException;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by jjg64 on 21/04/17.
|
||||
*/
|
||||
public class StreamedCourseXMLReader extends XMLReader implements RaceDataSource {
|
||||
private static final double COORDINATEPADDING = 0.000;
|
||||
private GPSCoordinate mapTopLeft, mapBottomRight;
|
||||
private final List<GPSCoordinate> boundary = new ArrayList<>();
|
||||
private final Map<Integer,Element> compoundMarkMap = new HashMap<>();
|
||||
private final Map<Integer, Boat> participants = new HashMap<>();
|
||||
private final List<Leg> legs = new ArrayList<>();
|
||||
private final List<CompoundMark> compoundMarks = new ArrayList<>();
|
||||
private ZonedDateTime creationTimeDate;
|
||||
private ZonedDateTime raceStartTime;
|
||||
private int raceID;
|
||||
private String raceType;
|
||||
private boolean postpone;
|
||||
|
||||
private Map<Integer, Boat> boats;
|
||||
private Map<Integer, Mark> marks;
|
||||
|
||||
/**
|
||||
* Constructor for Streamed Race XML
|
||||
* @param filePath path of the file
|
||||
* @throws IOException error
|
||||
* @throws SAXException error
|
||||
* @throws ParserConfigurationException error
|
||||
* @throws ParseException error
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
public StreamedCourseXMLReader(String filePath, BoatDataSource boatData) throws IOException, SAXException, ParserConfigurationException, ParseException, StreamedCourseXMLException {
|
||||
this(filePath, boatData, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for Streamed Race XML
|
||||
* @param filePath file path to read
|
||||
* @param read whether or not to read and store the files straight away.
|
||||
* @throws IOException error
|
||||
* @throws SAXException error
|
||||
* @throws ParserConfigurationException error
|
||||
* @throws ParseException error
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
public StreamedCourseXMLReader(String filePath, BoatDataSource boatData, boolean read) throws IOException, SAXException, ParserConfigurationException, ParseException, StreamedCourseXMLException {
|
||||
super(filePath);
|
||||
this.boats = boatData.getBoats();
|
||||
this.marks = boatData.getMarkerBoats();
|
||||
if (read) {
|
||||
read();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* reads
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
private void read() throws StreamedCourseXMLException {
|
||||
readRace();
|
||||
readParticipants();
|
||||
readCourse();
|
||||
}
|
||||
|
||||
/**
|
||||
* reads a race
|
||||
*/
|
||||
private void readRace() {
|
||||
DateTimeFormatter dateFormat = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
|
||||
Element settings = (Element) doc.getElementsByTagName("Race").item(0);
|
||||
NamedNodeMap raceTimeTag = doc.getElementsByTagName("RaceStartTime").item(0).getAttributes();
|
||||
|
||||
if (raceTimeTag.getNamedItem("Time") != null) dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
|
||||
|
||||
|
||||
raceID = Integer.parseInt(getTextValueOfNode(settings, "RaceID"));
|
||||
raceType = getTextValueOfNode(settings, "RaceType");
|
||||
|
||||
creationTimeDate = ZonedDateTime.parse(getTextValueOfNode(settings, "CreationTimeDate"), dateFormat);
|
||||
|
||||
if (raceTimeTag.getNamedItem("Time") != null) raceStartTime = ZonedDateTime.parse(raceTimeTag.getNamedItem("Time").getTextContent(), dateFormat);
|
||||
else raceStartTime = ZonedDateTime.parse(raceTimeTag.getNamedItem("Start").getTextContent(), dateFormat);
|
||||
|
||||
postpone = Boolean.parseBoolean(raceTimeTag.getNamedItem("Postpone").getTextContent());
|
||||
}
|
||||
|
||||
private void readParticipants() {
|
||||
Element nParticipants = (Element) doc.getElementsByTagName("Participants").item(0);
|
||||
nParticipants.getChildNodes().getLength();
|
||||
for (int i = 0; i < nParticipants.getChildNodes().getLength(); i++) {
|
||||
int sourceID;
|
||||
Node yacht = nParticipants.getChildNodes().item(i);
|
||||
if (yacht.getNodeName().equals("Yacht")) {
|
||||
if (exists(yacht, "SourceID")) {
|
||||
sourceID = Integer.parseInt(yacht.getAttributes().getNamedItem("SourceID").getTextContent());
|
||||
participants.put(sourceID, boats.get(sourceID));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* reads a course
|
||||
* @throws StreamedCourseXMLException error
|
||||
*/
|
||||
private void readCourse() throws StreamedCourseXMLException {
|
||||
readCompoundMarks();
|
||||
readCompoundMarkSequence();
|
||||
readCourseLimit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Indexes CompoundMark elements by their ID for use in generating the course, and populates list of Markers.
|
||||
* @see CompoundMark
|
||||
*/
|
||||
private void readCompoundMarks() throws StreamedCourseXMLException {
|
||||
Element nCourse = (Element) doc.getElementsByTagName("Course").item(0);
|
||||
for(int i = 0; i < nCourse.getChildNodes().getLength(); i++) {
|
||||
Node compoundMark = nCourse.getChildNodes().item(i);
|
||||
if(compoundMark.getNodeName().equals("CompoundMark")) {
|
||||
int compoundMarkID = getCompoundMarkID((Element) compoundMark);
|
||||
compoundMarkMap.put(compoundMarkID, (Element)compoundMark);
|
||||
compoundMarks.add(getCompoundMark(compoundMarkID));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a CompoundMark from the CompoundMark element with given ID.
|
||||
* @param compoundMarkID index of required CompoundMark element
|
||||
* @return generated CompoundMark
|
||||
* @throws StreamedCourseXMLException if CompoundMark element contains unhandled number of compoundMarks
|
||||
* @see CompoundMark
|
||||
*/
|
||||
private CompoundMark getCompoundMark(int compoundMarkID) throws StreamedCourseXMLException {
|
||||
Element compoundMark = compoundMarkMap.get(compoundMarkID);
|
||||
NodeList nMarks = compoundMark.getElementsByTagName("Mark");
|
||||
CompoundMark marker;
|
||||
|
||||
switch(nMarks.getLength()) {
|
||||
case 1: marker = new CompoundMark(getMark((Element)nMarks.item(0))); break;
|
||||
case 2: marker = new CompoundMark(getMark((Element)nMarks.item(0)), getMark((Element)nMarks.item(1))); break;
|
||||
default: throw new StreamedCourseXMLException();
|
||||
}
|
||||
|
||||
return marker;
|
||||
}
|
||||
|
||||
private Mark getMark(Element mark) {
|
||||
int sourceID = Integer.parseInt(mark.getAttribute("SourceID"));
|
||||
System.out.println(marks.get(sourceID).getPosition());
|
||||
return marks.get(sourceID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads "compoundMarkID" attribute of CompoundMark or Corner element
|
||||
* @param element with "compoundMarkID" attribute
|
||||
* @return value of "compoundMarkID" attribute
|
||||
*/
|
||||
private int getCompoundMarkID(Element element) {
|
||||
return Integer.parseInt(element.getAttribute("CompoundMarkID"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads "name" attribute of CompoundMark element with corresponding CompoundMarkID
|
||||
* @param compoundMarkID unique ID for CompoundMark element
|
||||
* @return value of "name" attribute
|
||||
*/
|
||||
private String getCompoundMarkName(int compoundMarkID) {
|
||||
return compoundMarkMap.get(compoundMarkID).getAttribute("Name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates list of legs given CompoundMarkSequence element and referenced CompoundMark elements.
|
||||
* @throws StreamedCourseXMLException if compoundMarks cannot be resolved from CompoundMark
|
||||
*/
|
||||
private void readCompoundMarkSequence() throws StreamedCourseXMLException {
|
||||
Element nCompoundMarkSequence = (Element) doc.getElementsByTagName("CompoundMarkSequence").item(0);
|
||||
NodeList nCorners = nCompoundMarkSequence.getElementsByTagName("Corner");
|
||||
Element markXML = (Element)nCorners.item(0);
|
||||
CompoundMark lastCompoundMark = getCompoundMark(getCompoundMarkID(markXML));
|
||||
String legName = getCompoundMarkName(getCompoundMarkID(markXML));
|
||||
for(int i = 1; i < nCorners.getLength(); i++) {
|
||||
markXML = (Element)nCorners.item(i);
|
||||
CompoundMark currentCompoundMark = getCompoundMark(getCompoundMarkID(markXML));
|
||||
legs.add(new Leg(legName, lastCompoundMark, currentCompoundMark, i-1));
|
||||
lastCompoundMark = currentCompoundMark;
|
||||
legName = getCompoundMarkName(getCompoundMarkID(markXML));
|
||||
}
|
||||
}
|
||||
|
||||
private void readCourseLimit() {
|
||||
Element nCourseLimit = (Element) doc.getElementsByTagName("CourseLimit").item(0);
|
||||
for(int i = 0; i < nCourseLimit.getChildNodes().getLength(); i++) {
|
||||
Node limit = nCourseLimit.getChildNodes().item(i);
|
||||
if (limit.getNodeName().equals("Limit")) {
|
||||
double lat = Double.parseDouble(limit.getAttributes().getNamedItem("Lat").getTextContent());
|
||||
double lon = Double.parseDouble(limit.getAttributes().getNamedItem("Lon").getTextContent());
|
||||
boundary.add(new GPSCoordinate(lat, lon));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double maxLatitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude() + COORDINATEPADDING;
|
||||
double maxLongitude = boundary.stream().max(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude() + COORDINATEPADDING;
|
||||
double minLatitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLatitude)).get().getLatitude() + COORDINATEPADDING;
|
||||
double minLongitude = boundary.stream().min(Comparator.comparingDouble(GPSCoordinate::getLongitude)).get().getLongitude() + COORDINATEPADDING;
|
||||
|
||||
mapTopLeft = new GPSCoordinate(minLatitude, minLongitude);
|
||||
mapBottomRight = new GPSCoordinate(maxLatitude, maxLongitude);
|
||||
}
|
||||
|
||||
public List<GPSCoordinate> getBoundary() {
|
||||
return boundary;
|
||||
}
|
||||
|
||||
public GPSCoordinate getMapTopLeft() {
|
||||
return mapTopLeft;
|
||||
}
|
||||
|
||||
public GPSCoordinate getMapBottomRight() {
|
||||
return mapBottomRight;
|
||||
}
|
||||
|
||||
public List<Leg> getLegs() {
|
||||
return legs;
|
||||
}
|
||||
|
||||
public List<CompoundMark> getCompoundMarks() { return compoundMarks; }
|
||||
|
||||
public Double getPadding() {
|
||||
return COORDINATEPADDING;
|
||||
}
|
||||
|
||||
public ZonedDateTime getCreationTimeDate() {
|
||||
return creationTimeDate;
|
||||
}
|
||||
|
||||
public ZonedDateTime getZonedDateTime() {
|
||||
return raceStartTime;
|
||||
}
|
||||
|
||||
public int getRaceId() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public String getRaceType() {
|
||||
return raceType;
|
||||
}
|
||||
|
||||
public boolean isPostpone() {
|
||||
return postpone;
|
||||
}
|
||||
|
||||
public List<Boat> getBoats() {
|
||||
return new ArrayList<>(participants.values());
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@
|
||||
<Mark Name="Marker1" TargetLat="32.293039" TargetLng="-64.843983" SourceID="103"/>
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="3" Name="Windward Gate">
|
||||
<Mark Name="WGL" SeqId="1" TargetLat="32.28468" TargetLng="" SourceID="104"/>
|
||||
<Mark Name="WGL" SeqId="1" TargetLat="32.28468" TargetLng="-64.850045" SourceID="104"/>
|
||||
<Mark Name="WGR" SeqId="2" TargetLat="32.280164" TargetLng="-64.847591" SourceID="105"/>
|
||||
</CompoundMark>
|
||||
<CompoundMark CompoundMarkID="4" Name="Leeward Gate">
|
||||
|
||||
Reference in New Issue
Block a user