Seperated race xml creation into a class of its own

- Created RaceXMLCreator class
- made a function to convert limit to GPS Coordinate (limitToGPSCoordinate)
- can rotate a GPS Coordinate around a point (rotate)
- can get center of a race with (getCenter)
#story[1092]
main
Fan-Wu Yang 8 years ago
parent 4e8d4f2433
commit f23c3f854d

@ -3,8 +3,7 @@ package mock.app;
import mock.dataInput.PolarParser;
import mock.model.MockRace;
import mock.model.Polars;
import mock.xml.Race;
import mock.xml.RaceFactory;
import mock.xml.RaceXMLCreator;
import network.Messages.LatestMessages;
import shared.dataInput.*;
import shared.enums.XMLFileType;
@ -12,13 +11,9 @@ import shared.exceptions.InvalidBoatDataException;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.InvalidRegattaDataException;
import shared.exceptions.XMLReaderException;
import shared.model.CompoundMark;
import shared.model.Constants;
import shared.model.Corner;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.TransformerException;
import java.io.*;
import java.net.UnknownHostException;
@ -49,7 +44,8 @@ public class Event {
*/
private Event() {
try {
this.raceXML = getRaceXMLAtCurrentTime(alterRaceToWind("mock/mockXML/raceSchemaTest.xml", 0));
this.raceXML = getRaceXMLAtCurrentTime(RaceXMLCreator.alterRaceToWind("mock/mockXML/raceSchemaTest.xml", 90));
System.out.println(this.raceXML);
this.boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatsSinglePlayer.xml", StandardCharsets.UTF_8);
this.regattaXML = XMLReader.readXMLFileToString("mock/mockXML/regattaTest.xml", StandardCharsets.UTF_8);
this.xmlFileType = XMLFileType.Contents;
@ -143,92 +139,4 @@ public class Event {
}
private Race copyRace(RaceXMLReader reader) throws InvalidRaceDataException, XMLReaderException {
mock.xml.RaceFactory raceFactory = new RaceFactory();
mock.xml.Race race = raceFactory.createRace();
race.setRaceID(String.valueOf(reader.getRaceId()));
race.setRaceType(String.valueOf(reader.getRaceType()));
race.setCreationTimeDate("CREATION_TIME");//this needs to be changed later
//Race Start Time
mock.xml.Race.RaceStartTime raceStartTime = raceFactory.createRaceRaceStartTime();
raceStartTime.setPostpone("false");
raceStartTime.setTime("START_TIME");//this needs to be changed later
race.setRaceStartTime(raceStartTime);
//Participants
Race.Participants participants = raceFactory.createRaceParticipants();
for (int i :reader.getParticipants()) {
Race.Participants.Yacht yacht = raceFactory.createRaceParticipantsYacht();
yacht.setSourceID(String.valueOf(i));
participants.getYacht().add(yacht); //java schema does not clone the array so this is "safe".
}
race.setParticipants(participants);
//compound marks sequence
Race.CompoundMarkSequence cms = raceFactory.createRaceCompoundMarkSequence();
for (Corner mark: reader.getCornersList()){
Race.CompoundMarkSequence.Corner corner = raceFactory.createRaceCompoundMarkSequenceCorner();
corner.setCompoundMarkID(String.valueOf(mark.getId()));
corner.setSeqID(String.valueOf(mark.getSeqID()));
cms.getCorner().add(corner);
}
race.setCompoundMarkSequence(cms);
//Course
Race.Course course = raceFactory.createRaceCourse();
for (CompoundMark mark: reader.getCompoundMarks()){
Race.Course.CompoundMark cm = raceFactory.createRaceCourseCompoundMark();
cm.setName(mark.getName());
cm.setCompoundMarkID(String.valueOf(mark.getId()));
if (mark.getMark1() != null){
Race.Course.CompoundMark.Mark m1 = raceFactory.createRaceCourseCompoundMarkMark();
m1.setName(mark.getMark1().getName());
m1.setTargetLat(String.valueOf(mark.getMark1().getPosition().getLatitude()));
m1.setTargetLng(String.valueOf(mark.getMark1().getPosition().getLongitude()));
m1.setSourceID(String.valueOf(mark.getMark1().getSourceID()));
cm.getMark().add(m1);
}
//make this not copy paste later but just need the concept working right now
if (mark.getMark2() != null){
Race.Course.CompoundMark.Mark m2 = raceFactory.createRaceCourseCompoundMarkMark();
m2.setName(mark.getMark1().getName());
m2.setTargetLat(String.valueOf(mark.getMark1().getPosition().getLatitude()));
m2.setTargetLng(String.valueOf(mark.getMark1().getPosition().getLongitude()));
m2.setSourceID(String.valueOf(mark.getMark1().getSourceID()));
cm.getMark().add(m2);
}
course.getCompoundMark().add(cm);
}
race.setCourse(course);
//Course Limit
Race.CourseLimit courseLimit = raceFactory.createRaceCourseLimit();
for (int i = 0; i < reader.getBoundary().size(); i++){
Race.CourseLimit.Limit limit = raceFactory.createRaceCourseLimitLimit();
limit.setSeqID(String.valueOf(i+1));
limit.setLat(String.valueOf(reader.getBoundary().get(i).getLatitude()));
limit.setLon(String.valueOf(reader.getBoundary().get(i).getLongitude()));
courseLimit.getLimit().add(limit);
}
race.setCourseLimit(courseLimit);
return race;
}
private String alterRaceToWind(String s, double degrees) throws XMLReaderException, InvalidRaceDataException, JAXBException {
RaceXMLReader reader = new RaceXMLReader(s, XMLFileType.ResourcePath);
mock.xml.Race race = copyRace(reader);
JAXBContext context = JAXBContext.newInstance(mock.xml.Race.class);
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(race, sw);
return sw.toString();
}
}

@ -1,127 +0,0 @@
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2017.08.03 at 02:28:57 AM NZST
//
package mock.xml;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the race package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: race
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Race }
*
*/
public Race createRace() {
return new Race();
}
/**
* Create an instance of {@link Race.CourseLimit }
*
*/
public Race.CourseLimit createRaceCourseLimit() {
return new Race.CourseLimit();
}
/**
* Create an instance of {@link Race.Course }
*
*/
public Race.Course createRaceCourse() {
return new Race.Course();
}
/**
* Create an instance of {@link Race.Course.CompoundMark }
*
*/
public Race.Course.CompoundMark createRaceCourseCompoundMark() {
return new Race.Course.CompoundMark();
}
/**
* Create an instance of {@link Race.CompoundMarkSequence }
*
*/
public Race.CompoundMarkSequence createRaceCompoundMarkSequence() {
return new Race.CompoundMarkSequence();
}
/**
* Create an instance of {@link Race.Participants }
*
*/
public Race.Participants createRaceParticipants() {
return new Race.Participants();
}
/**
* Create an instance of {@link Race.RaceStartTime }
*
*/
public Race.RaceStartTime createRaceRaceStartTime() {
return new Race.RaceStartTime();
}
/**
* Create an instance of {@link Race.CourseLimit.Limit }
*
*/
public Race.CourseLimit.Limit createRaceCourseLimitLimit() {
return new Race.CourseLimit.Limit();
}
/**
* Create an instance of {@link Race.Course.CompoundMark.Mark }
*
*/
public Race.Course.CompoundMark.Mark createRaceCourseCompoundMarkMark() {
return new Race.Course.CompoundMark.Mark();
}
/**
* Create an instance of {@link Race.CompoundMarkSequence.Corner }
*
*/
public Race.CompoundMarkSequence.Corner createRaceCompoundMarkSequenceCorner() {
return new Race.CompoundMarkSequence.Corner();
}
/**
* Create an instance of {@link Race.Participants.Yacht }
*
*/
public Race.Participants.Yacht createRaceParticipantsYacht() {
return new Race.Participants.Yacht();
}
}

@ -0,0 +1,142 @@
package mock.xml;
import shared.dataInput.RaceXMLReader;
import shared.enums.XMLFileType;
import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.XMLReaderException;
import shared.model.CompoundMark;
import shared.model.Corner;
import shared.model.GPSCoordinate;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
/**
* Created by Gondr on 3/08/2017.
*/
public class RaceXMLCreator {
public static Race copyRace(RaceXMLReader reader) throws InvalidRaceDataException, XMLReaderException {
RaceFactory raceFactory = new RaceFactory();
Race race = raceFactory.createRace();
race.setRaceID(String.valueOf(reader.getRaceId()));
race.setRaceType(String.valueOf(reader.getRaceType()));
race.setCreationTimeDate("CREATION_TIME");//this needs to be changed later
//Race Start Time
Race.RaceStartTime raceStartTime = raceFactory.createRaceRaceStartTime();
raceStartTime.setPostpone("false");
raceStartTime.setTime("START_TIME");//this needs to be changed later
race.setRaceStartTime(raceStartTime);
//Participants
Race.Participants participants = raceFactory.createRaceParticipants();
for (int i :reader.getParticipants()) {
Race.Participants.Yacht yacht = raceFactory.createRaceParticipantsYacht();
yacht.setSourceID(String.valueOf(i));
participants.getYacht().add(yacht); //java schema does not clone the array so this is "safe".
}
race.setParticipants(participants);
//compound marks sequence
Race.CompoundMarkSequence cms = raceFactory.createRaceCompoundMarkSequence();
for (Corner mark: reader.getCornersList()){
Race.CompoundMarkSequence.Corner corner = raceFactory.createRaceCompoundMarkSequenceCorner();
corner.setCompoundMarkID(String.valueOf(mark.getId()));
corner.setSeqID(String.valueOf(mark.getSeqID()));
cms.getCorner().add(corner);
}
race.setCompoundMarkSequence(cms);
//Course
Race.Course course = raceFactory.createRaceCourse();
for (CompoundMark mark: reader.getCompoundMarks()){
Race.Course.CompoundMark cm = raceFactory.createRaceCourseCompoundMark();
cm.setName(mark.getName());
cm.setCompoundMarkID(String.valueOf(mark.getId()));
if (mark.getMark1() != null){
Race.Course.CompoundMark.Mark m1 = raceFactory.createRaceCourseCompoundMarkMark();
m1.setName(mark.getMark1().getName());
m1.setTargetLat(String.valueOf(mark.getMark1().getPosition().getLatitude()));
m1.setTargetLng(String.valueOf(mark.getMark1().getPosition().getLongitude()));
m1.setSourceID(String.valueOf(mark.getMark1().getSourceID()));
cm.getMark().add(m1);
}
//make this not copy paste later but just need the concept working right now
if (mark.getMark2() != null){
Race.Course.CompoundMark.Mark m2 = raceFactory.createRaceCourseCompoundMarkMark();
m2.setName(mark.getMark1().getName());
m2.setTargetLat(String.valueOf(mark.getMark1().getPosition().getLatitude()));
m2.setTargetLng(String.valueOf(mark.getMark1().getPosition().getLongitude()));
m2.setSourceID(String.valueOf(mark.getMark1().getSourceID()));
cm.getMark().add(m2);
}
course.getCompoundMark().add(cm);
}
race.setCourse(course);
//Course Limit
Race.CourseLimit courseLimit = raceFactory.createRaceCourseLimit();
for (int i = 0; i < reader.getBoundary().size(); i++){
Race.CourseLimit.Limit limit = raceFactory.createRaceCourseLimitLimit();
limit.setSeqID(String.valueOf(i+1));
limit.setLat(String.valueOf(reader.getBoundary().get(i).getLatitude()));
limit.setLon(String.valueOf(reader.getBoundary().get(i).getLongitude()));
courseLimit.getLimit().add(limit);
}
race.setCourseLimit(courseLimit);
return race;
}
public static String alterRaceToWind(String s, double degrees) throws XMLReaderException, InvalidRaceDataException, JAXBException {
RaceXMLReader reader = new RaceXMLReader(s, XMLFileType.ResourcePath);
Race race = copyRace(reader);
GPSCoordinate center = getCenter(race);
for(Race.CourseLimit.Limit limit: race.getCourseLimit().getLimit()){
GPSCoordinate rotatedLim = rotate(center, limitToGPSCoordinate(limit), degrees);
limit.setLat(String.valueOf(rotatedLim.getLatitude()));
limit.setLon(String.valueOf(rotatedLim.getLongitude()));
}
JAXBContext context = JAXBContext.newInstance(Race.class);
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(race, sw);
return sw.toString();
}
public static GPSCoordinate limitToGPSCoordinate(Race.CourseLimit.Limit limit){
return new GPSCoordinate(Double.parseDouble(limit.getLat()), Double.parseDouble(limit.getLon()));
}
public static GPSCoordinate rotate(GPSCoordinate pivot, GPSCoordinate point, double degrees){
double radDeg = Math.toRadians(degrees);
double deltaLat = (point.getLatitude() - pivot.getLatitude());
double deltaLon = (point.getLongitude() - pivot.getLongitude());
//map to (0,1) vector and use vector maths to rotate.
double resLat = deltaLat * Math.cos(radDeg) - deltaLon * Math.sin(radDeg) + pivot.getLatitude();
double resLon = deltaLat * Math.sin(radDeg) + deltaLon * Math.cos(radDeg) + pivot.getLongitude();
return new GPSCoordinate(resLat, resLon);
}
public static GPSCoordinate getCenter(Race race){
double avgLat = 0;
double avgLng = 0;
for (Race.CourseLimit.Limit limit: race.getCourseLimit().getLimit()){
avgLat += Double.parseDouble(limit.getLat());
avgLng += Double.parseDouble(limit.getLon());
}
avgLat = avgLat/race.getCourseLimit().getLimit().size();
avgLng = avgLng/race.getCourseLimit().getLimit().size();
return new GPSCoordinate(avgLat, avgLng);
}
}
Loading…
Cancel
Save