Implemented Map Boundaries in XML

- RaceXMLReader will calculate the top left latitude and longitude and the bottom right latitude and longitude, and make the layout the map accordingly.
- The Boundaries are draw on the Race Canvas in Aqua.
#story[22]
main
Fan-Wu Yang 9 years ago
parent 879de9869b
commit 224c16a713

@ -143,12 +143,16 @@ public class RaceController extends Controller{
BoatInRace[] boats = new BoatInRace[raceXMLReader.getBoats().size()]; BoatInRace[] boats = new BoatInRace[raceXMLReader.getBoats().size()];
boats = raceXMLReader.getBoats().toArray(boats); boats = raceXMLReader.getBoats().toArray(boats);
//BoatInRace[] boats = generateAC35Competitors(); //BoatInRace[] boats = generateAC35Competitors();
double lat1 = raceXMLReader.getMapTopLeft().getLatitude();
raceMap = new ResizableRaceCanvas(); double long1 = raceXMLReader.getMapTopLeft().getLongitude();
double lat2 = raceXMLReader.getMapBottomRight().getLatitude();
double long2 = raceXMLReader.getMapBottomRight().getLongitude();
raceMap = new ResizableRaceCanvas(lat1, long1, lat2, long2);
raceMap.setMouseTransparent(true); raceMap.setMouseTransparent(true);
raceMap.widthProperty().bind(canvasBase.widthProperty()); raceMap.widthProperty().bind(canvasBase.widthProperty());
raceMap.heightProperty().bind(canvasBase.heightProperty()); raceMap.heightProperty().bind(canvasBase.heightProperty());
raceMap.setBoats(boats); raceMap.setBoats(boats);
raceMap.setRaceBoundaries(raceXMLReader.getBoundary());
raceMap.drawRaceMap(); raceMap.drawRaceMap();
raceMap.setVisible(true); raceMap.setVisible(true);

@ -1,6 +1,7 @@
package seng302.Model; package seng302.Model;
import com.sun.corba.se.impl.orbutil.graph.Graph;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.scene.canvas.Canvas; import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext; import javafx.scene.canvas.GraphicsContext;
@ -29,6 +30,7 @@ public class ResizableRaceCanvas extends Canvas {
private BoatInRace[] boats; private BoatInRace[] boats;
private RaceController controller; private RaceController controller;
private boolean raceAnno = true; private boolean raceAnno = true;
private ArrayList<GPSCoordinate> raceBoundaries;
/** /**
* Sets the boats that are to be displayed in this race. * Sets the boats that are to be displayed in this race.
@ -41,6 +43,7 @@ public class ResizableRaceCanvas extends Canvas {
public ResizableRaceCanvas(RaceMap map) { public ResizableRaceCanvas(RaceMap map) {
super();
this.map = map; this.map = map;
gc = this.getGraphicsContext2D(); gc = this.getGraphicsContext2D();
// Redraw canvas when size changes. // Redraw canvas when size changes.
@ -53,6 +56,12 @@ public class ResizableRaceCanvas extends Canvas {
*/ */
public ResizableRaceCanvas() { public ResizableRaceCanvas() {
this(null); this(null);
setMap(new RaceMap(32.278, -64.863, 32.320989, -64.821, (int) getWidth(), (int) getHeight()));
}
public ResizableRaceCanvas(double lat1, double long1, double lat2, double long2){
this(null);
setMap(new RaceMap(lat1, long1, lat2, long2, (int) getWidth(), (int) getHeight()));
} }
/** /**
@ -154,6 +163,18 @@ public class ResizableRaceCanvas extends Canvas {
gc.fillText(text, xCoord, yCoord); gc.fillText(text, xCoord, yCoord);
} }
public void drawBoundaries(){
gc.setFill(Color.AQUA);
double xpoints[] = new double[raceBoundaries.size()];
double ypoints[] = new double[raceBoundaries.size()];
for (int i = 0; i < raceBoundaries.size(); i++){
GraphCoordinate coord = map.convertGPS(raceBoundaries.get(i));
xpoints[i] = coord.getX();
ypoints[i] = coord.getY();
}
gc.fillPolygon(xpoints, ypoints, xpoints.length);
}
/** /**
* Draws the Race Map * Draws the Race Map
*/ */
@ -161,16 +182,17 @@ public class ResizableRaceCanvas extends Canvas {
double width = getWidth(); double width = getWidth();
double height = getHeight(); double height = getHeight();
gc.clearRect(0, 0, width, height); gc.clearRect(0, 0, width, height);
//System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height); //System.out.println("Race Map Canvas Width: "+ width + ", Height:" + height);
this.map = new RaceMap(32.278, -64.863, 32.320989, -64.821, (int) width, (int) height);
if (map == null) { if (map == null) {
return; return;//TODO this should return a exception in the future
} }
this.map.setHeight((int)height);
this.map.setWidth((int)width);
//finish line //finish line
gc.setLineWidth(2); gc.setLineWidth(2);
drawBoundaries();
GraphCoordinate finishLineCoord1 = this.map.convertGPS(Constants.finishLineMarker1); GraphCoordinate finishLineCoord1 = this.map.convertGPS(Constants.finishLineMarker1);
GraphCoordinate finishLineCoord2 = this.map.convertGPS(Constants.finishLineMarker2); GraphCoordinate finishLineCoord2 = this.map.convertGPS(Constants.finishLineMarker2);
displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED); displayLine(finishLineCoord1, finishLineCoord2, Color.DARKRED);
@ -189,7 +211,6 @@ public class ResizableRaceCanvas extends Canvas {
displayLine(startline1, startline2, Color.GREEN); displayLine(startline1, startline2, Color.GREEN);
if (boats != null) { if (boats != null) {
for (BoatInRace boat : boats) { for (BoatInRace boat : boats) {
if (boat != null) { if (boat != null) {
@ -226,6 +247,10 @@ public class ResizableRaceCanvas extends Canvas {
} }
} }
public void setRaceBoundaries(ArrayList<GPSCoordinate> boundaries) {
this.raceBoundaries = boundaries;
}
/** /**
* Set the Canvas to resizable. * Set the Canvas to resizable.
* *

@ -50,4 +50,12 @@ public class RaceMap {
public GraphCoordinate convertGPS(GPSCoordinate coordinate) { public GraphCoordinate convertGPS(GPSCoordinate coordinate) {
return convertGPS(coordinate.getLatitude(), coordinate.getLongitude()); return convertGPS(coordinate.getLatitude(), coordinate.getLongitude());
} }
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
} }

@ -19,7 +19,9 @@ public class RaceXMLReader extends XMLReader{
private Color[] colors = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN};//TODO make this established in xml or come up with a better system. private Color[] colors = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN};//TODO make this established in xml or come up with a better system.
private ArrayList<Leg> legs = new ArrayList<>(); private ArrayList<Leg> legs = new ArrayList<>();
private GPSCoordinate mark, startPt1, startPt2, finishPt1, finishPt2, leewardPt1, leewardPt2, windwardPt1, windwardPt2; private GPSCoordinate mark, startPt1, startPt2, finishPt1, finishPt2, leewardPt1, leewardPt2, windwardPt1, windwardPt2;
private GPSCoordinate mapTopLeft, mapBottomRight;
private ArrayList<GPSCoordinate> boundary = new ArrayList<>(); private ArrayList<GPSCoordinate> boundary = new ArrayList<>();
private static double COORDINATEPADDING = 0.0005;
public RaceXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException { public RaceXMLReader(String filePath) throws IOException, SAXException, ParserConfigurationException {
@ -73,9 +75,61 @@ public class RaceXMLReader extends XMLReader{
NodeList nCourse = doc.getElementsByTagName("course"); NodeList nCourse = doc.getElementsByTagName("course");
NodeList nBounds = ((Element)nCourse.item(0)).getElementsByTagName("boundaries"); NodeList nBounds = ((Element)nCourse.item(0)).getElementsByTagName("boundaries");
nBounds = ((Element) nBounds.item(0)).getElementsByTagName("coordinate");
int maxLatitudeIndex = 0;
double maxLatitude = -Double.MIN_VALUE;
int maxLongitudeIndex = 0;
double maxLongitude = -180;
int minLatitudeIndex = 0;
double minLatitude = Double.MAX_VALUE;
int minLongitudeIndex = 0;
double minLongitude = Double.MAX_VALUE;
for (int i = 0; i < nBounds.getLength(); i++){ for (int i = 0; i < nBounds.getLength(); i++){
boundary.add(getCoordinates(nBounds, i)); boundary.add(getCoordinates((Element) nBounds.item(i)));
if (boundary.get(i).getLatitude() > maxLatitude){
maxLatitudeIndex = i;
maxLatitude = boundary.get(i).getLatitude();
}
if (boundary.get(i).getLatitude() < minLatitude){
minLatitudeIndex = i;
minLatitude = boundary.get(i).getLatitude();
}
if (boundary.get(i).getLongitude() > maxLongitude){
maxLongitudeIndex = i;
maxLongitude = boundary.get(i).getLongitude();
}
if (boundary.get(i).getLongitude() < minLongitude){
minLongitudeIndex = i;
minLongitude = boundary.get(i).getLongitude();
}
} }
System.out.println(nBounds.getLength());
System.out.println(maxLatitude);
System.out.println(minLatitude);
System.out.println(maxLongitude);
System.out.println(minLongitude);
double difference = 0;//this will hold the largest difference so we can make the map square.
double latitudeDiff = Math.abs(Math.abs(boundary.get(maxLatitudeIndex).getLatitude()) - Math.abs(boundary.get(minLatitudeIndex).getLatitude()));
double longitudeDiff = Math.abs(Math.abs(boundary.get(maxLongitudeIndex).getLongitude()) - Math.abs(boundary.get(minLongitudeIndex).getLongitude()));
if (latitudeDiff >= longitudeDiff){
difference = latitudeDiff - longitudeDiff;
maxLongitude += difference/2;
minLongitude -= difference/2;
}else{
difference = longitudeDiff - latitudeDiff;
maxLatitude += difference/2;
minLatitude -= difference/2;
}
maxLatitude += COORDINATEPADDING;
minLatitude -= COORDINATEPADDING;
maxLongitude += COORDINATEPADDING;
minLongitude -= COORDINATEPADDING;
//now create map boundaries
//top left canvas point is min logitude, max latitude
//bottom right of canvas point is min longitude, max latitude.
mapTopLeft = new GPSCoordinate(minLatitude, minLongitude);
mapBottomRight = new GPSCoordinate(maxLatitude, maxLongitude);
NodeList nMarks = ((Element)nCourse.item(0)).getElementsByTagName("marker"); NodeList nMarks = ((Element)nCourse.item(0)).getElementsByTagName("marker");
startPt1 = getCoordinates(nMarks, 0); startPt1 = getCoordinates(nMarks, 0);
@ -162,4 +216,12 @@ public class RaceXMLReader extends XMLReader{
public ArrayList<GPSCoordinate> getBoundary() { public ArrayList<GPSCoordinate> getBoundary() {
return boundary; return boundary;
} }
public GPSCoordinate getMapTopLeft() {
return mapTopLeft;
}
public GPSCoordinate getMapBottomRight() {
return mapBottomRight;
}
} }

@ -119,12 +119,48 @@
<course> <course>
<boundaries> <boundaries>
<coordinate> <coordinate>
<latitude>32.278</latitude> <latitude>32.313922</latitude>
<longitude>-64.863</longitude> <longitude>-64.837168</longitude>
</coordinate> </coordinate>
<coordinate> <coordinate>
<latitude>32.30989</latitude> <latitude>32.317403</latitude>
<longitude>-64.821</longitude> <longitude>-64.838627</longitude>
</coordinate>
<coordinate>
<latitude>32.317911</latitude>
<longitude>-64.836996</longitude>
</coordinate>
<coordinate>
<latitude>32.317548</latitude>
<longitude>-64.835022</longitude>
</coordinate>
<coordinate>
<latitude>32.304273</latitude>
<longitude>-64.822834</longitude>
</coordinate>
<coordinate>
<latitude>32.279097</latitude>
<longitude>-64.841545</longitude>
</coordinate>
<coordinate>
<latitude>32.279604</latitude>
<longitude>-64.849871</longitude>
</coordinate>
<coordinate>
<latitude>32.289545</latitude>
<longitude>-64.854162</longitude>
</coordinate>
<coordinate>
<latitude>32.290198</latitude>
<longitude>-64.858711</longitude>
</coordinate>
<coordinate>
<latitude>32.297164</latitude>
<longitude>-64.856394</longitude>
</coordinate>
<coordinate>
<latitude>32.296148</latitude>
<longitude>-64.849184</longitude>
</coordinate> </coordinate>
</boundaries> </boundaries>
<marker> <marker>

Loading…
Cancel
Save