Skip to content

Commit f93c50a

Browse files
Adding ability to process coordinates before drawing. Adding the ability to AltitudeProcessor
1 parent d4a4a79 commit f93c50a

File tree

4 files changed

+103
-33
lines changed

4 files changed

+103
-33
lines changed

src/main/java/com/codingpupper3033/codebtekml/events/KeyPressed.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codingpupper3033.codebtekml.events;
22

33
import com.codingpupper3033.codebtekml.KeyInit;
4-
import com.codingpupper3033.codebtekml.gui.screens.GuiDrawKML;
4+
import com.codingpupper3033.codebtekml.helpers.map.altitude.AltitudeProcessor;
55
import net.minecraft.client.Minecraft;
66
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
77
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
@@ -15,7 +15,8 @@ public class KeyPressed {
1515
public void clientTick(ClientTickEvent event) {
1616
if (Minecraft.getMinecraft().currentScreen == null) { // Keystrokes that only work on the default game menu
1717
// Open KML Menu
18-
if (KeyInit.openKMLMenuKeybind.isPressed()) Minecraft.getMinecraft().displayGuiScreen(new GuiDrawKML(null));
18+
//if (KeyInit.openKMLMenuKeybind.isPressed()) Minecraft.getMinecraft().displayGuiScreen(new GuiDrawKML(null));
19+
if (KeyInit.openKMLMenuKeybind.isPressed()) AltitudeProcessor.defaultProcessor.processCoordinateQueue();
1920
}
2021
}
2122
}

src/main/java/com/codingpupper3033/codebtekml/helpers/map/Coordinate.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ public class Coordinate {
1919

2020
private final AltitudeMode altitudeMode;
2121

22+
/**
23+
* Instantiates a new Coordinate.
24+
*
25+
* @param lat the latitude
26+
* @param lon the longitude
27+
* @param elv the elevation
28+
* @param altitudeMode the altitude mode
29+
* @see AltitudeMode
30+
*/
31+
public Coordinate(double lat, double lon, double elv, AltitudeMode altitudeMode) {
32+
this.lon = lon;
33+
this.lat = lat;
34+
this.elv = elv;
35+
this.altitudeMode = altitudeMode;
36+
}
37+
2238
public double getLongitude() {
2339
return lon;
2440
}
@@ -35,21 +51,8 @@ public AltitudeMode getAltitudeMode() {
3551
return altitudeMode;
3652
}
3753

38-
39-
/**
40-
* Instantiates a new Coordinate.
41-
*
42-
* @param lat the latitude
43-
* @param lon the longitude
44-
* @param elv the elevation
45-
* @param altitudeMode the altitude mode
46-
* @see AltitudeMode
47-
*/
48-
public Coordinate(double lat, double lon, double elv, AltitudeMode altitudeMode) {
49-
this.lon = lon;
50-
this.lat = lat;
51-
this.elv = elv;
52-
this.altitudeMode = altitudeMode;
54+
public void setGroundLevel(Double groundLevel) {
55+
this.groundLevel = groundLevel;
5356
}
5457

5558
@Override
@@ -58,6 +61,7 @@ public String toString() {
5861
", " + lon +
5962
", " + elv +
6063
", " + altitudeMode.getNodeName() +
64+
", " + groundLevel +
6165
']';
6266
}
6367

@@ -74,7 +78,7 @@ public double getAltitude() throws NoAltitudeException, IOException {
7478
case RELATIVE_TO_GROUND:
7579
case DEFAULT:
7680
if (groundLevel == null) {
77-
groundLevel = AltitudeProcessor.defaultProcessor.getAltitude(this);
81+
groundLevel = AltitudeProcessor.defaultProcessor.getGroundLevel(this);
7882
}
7983
return groundLevel + getElevation();
8084
default:

src/main/java/com/codingpupper3033/codebtekml/helpers/map/altitude/AltitudeProcessor.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.codingpupper3033.codebtekml.helpers.map.Coordinate;
44

55
import java.io.IOException;
6+
import java.util.LinkedList;
7+
import java.util.Queue;
68

79
/**
810
* Class that will process the altitude of a coordinate, based upon an AltitudeMode.
@@ -11,17 +13,34 @@
1113
* @author Joshua Miller
1214
*/
1315
public class AltitudeProcessor {
16+
/**
17+
* Default Altitude Processor
18+
* Currently it uses Open Elevation, as there appears to be no way to get ground level in java.
19+
* The downside is that it requires internet access to make API requests.
20+
*/
21+
public static AltitudeProcessor defaultProcessor = new OpenElevationAltitudeProcessor();
22+
23+
/**
24+
* Can we use this?
25+
*/
26+
public boolean enabled = true;
27+
28+
/**
29+
* List of coordinates to process
30+
*/
31+
public Queue<Coordinate> coordinateProcessorQueue = new LinkedList<>();
32+
1433
/**
1534
* Default altitude method. Should not be used.
1635
* @param coordinate Coordinate containing the given altitude
1736
* @return
1837
* @throws IOException
1938
* @throws NoAltitudeException
2039
*/
21-
public double getAltitude(Coordinate coordinate) throws IOException, NoAltitudeException {
40+
public double getGroundLevel(Coordinate coordinate) throws IOException, NoAltitudeException {
2241
if (!enabled) throw new NoAltitudeException();
2342
return 0;
24-
};
43+
}
2544

2645
/**
2746
* Default altitude method. Should not be used.
@@ -30,20 +49,26 @@ public double getAltitude(Coordinate coordinate) throws IOException, NoAltitudeE
3049
* @throws IOException
3150
* @throws NoAltitudeException
3251
*/
33-
public double[] getAltitudes(Coordinate[] coordinates) throws IOException, NoAltitudeException {
52+
public double[] getGroundLevels(Coordinate[] coordinates) throws IOException, NoAltitudeException {
3453
if (!enabled) throw new NoAltitudeException();
3554
return new double[]{};
36-
};
55+
}
3756

38-
/**
39-
* Default Altitude Processor
40-
* Currently it uses Open Elevation, as there appears to be no way to get ground level in java.
41-
* The downside is that it requires internet access to make API requests.
42-
*/
43-
public static AltitudeProcessor defaultProcessor = new OpenElevationAltitudeProcessor();
57+
public void addCoordinateToProcessQueue(Coordinate coordinate) {
58+
coordinateProcessorQueue.add(coordinate);
59+
}
60+
61+
public Queue<Coordinate> getCoordinateProcessorQueue() {
62+
return coordinateProcessorQueue;
63+
}
4464

4565
/**
46-
* Can we use this?
66+
* Empties the process coordinate queue
67+
*
68+
* @return
4769
*/
48-
public boolean enabled = true;
70+
public boolean processCoordinateQueue() {
71+
72+
return false;
73+
}
4974
}

src/main/java/com/codingpupper3033/codebtekml/helpers/map/altitude/OpenElevationAltitudeProcessor.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ public class OpenElevationAltitudeProcessor extends AltitudeProcessor {
2424
public static final String RESULTS_KEY = "results"; // JSON Key to get the results
2525
public static final String ELEVATION_KEY = "elevation"; // JSON Key to get the elevation
2626

27+
// Max amount of locations to ask for per request
28+
public static final int MAX_COORDINATES_PER_REQUEST = 16;
29+
2730
/**
2831
* @param coordinate Coordinate to get the altitude of
2932
* @return elevation of the ground at the coordinate
3033
* @throws IOException
3134
*/
3235
@Override
33-
public double getAltitude(Coordinate coordinate) throws IOException {
34-
return getAltitudes(new Coordinate[]{coordinate})[0];
36+
public double getGroundLevel(Coordinate coordinate) throws IOException {
37+
return getGroundLevels(new Coordinate[]{coordinate})[0];
3538
}
3639

3740
/**
@@ -40,7 +43,7 @@ public double getAltitude(Coordinate coordinate) throws IOException {
4043
* @throws IOException
4144
*/
4245
@Override
43-
public double[] getAltitudes(Coordinate[] coordinates) throws IOException {
46+
public double[] getGroundLevels(Coordinate[] coordinates) throws IOException {
4447
StringBuffer coordsBuffer = new StringBuffer(); // Buffer of the coordinates to request from the API
4548

4649
for (int i = 0; i < coordinates.length; i++) { // Add all coordinates
@@ -83,4 +86,41 @@ public double[] getAltitudes(Coordinate[] coordinates) throws IOException {
8386

8487
return out;
8588
}
89+
90+
91+
/**
92+
* Processes the ground level for all Coordinates in the queue
93+
* @return
94+
*/
95+
@Override
96+
public boolean processCoordinateQueue() {
97+
super.processCoordinateQueue();
98+
99+
for (int i = 0; i <17; i++) {
100+
coordinateProcessorQueue.add(new Coordinate(42.7264177662084, -73.67230722692297,0,AltitudeMode.RELATIVE_TO_GROUND));
101+
}
102+
103+
while (!coordinateProcessorQueue.isEmpty()) { // Keep working until all out
104+
Coordinate[] setOfLocationsToProcess = new Coordinate[Math.min(MAX_COORDINATES_PER_REQUEST,coordinateProcessorQueue.size())]; // Make it only as big as needed
105+
int i = 0;
106+
while (!coordinateProcessorQueue.isEmpty() && i < setOfLocationsToProcess.length) { // Only do the max size at a time
107+
setOfLocationsToProcess[i] = coordinateProcessorQueue.poll();
108+
i++;
109+
}
110+
111+
// Process them
112+
try {
113+
double[] groundLevels = getGroundLevels(setOfLocationsToProcess);
114+
115+
for (int j = 0; j < setOfLocationsToProcess.length; j++) { // Add the result to the coordinate
116+
setOfLocationsToProcess[j].setGroundLevel(groundLevels[j]);
117+
}
118+
return true;
119+
} catch (IOException e) {
120+
return false;
121+
}
122+
}
123+
124+
return false;
125+
}
86126
}

0 commit comments

Comments
 (0)