Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tester;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- class I360 {
- private static class Plane {
- private String icao24;
- private String callSign;
- private String originCountry;
- private Integer timePosition;
- private Integer lastContact;
- private Double longitude;
- private Double latitude;
- private Double geoAltitude;
- private Boolean onGround;
- private Double velocity;
- private Double heading;
- private Double verticalRate;
- private Integer[] sensors;
- private Double baroAltitude;
- private String squawk;
- private Boolean spi;
- private Integer positionSource;
- Plane(String icao24, String callSign, String originCountry, Integer timePosition,
- Integer lastContact, Double longitude, Double latitude, Double geoAltitude,
- Boolean onGround, Double velocity, Double heading, Double verticalRate,
- Integer[] sensors, Double baroAltitude, String squawk, Boolean spi,
- int positionSource) {
- this.icao24 = icao24;
- this.callSign = callSign;
- this.originCountry = originCountry;
- this.timePosition = timePosition;
- this.lastContact = lastContact;
- this.longitude = longitude;
- this.latitude = latitude;
- this.geoAltitude = geoAltitude;
- this.onGround = onGround;
- this.velocity = velocity;
- this.heading = heading;
- this.verticalRate = verticalRate;
- this.sensors = sensors;
- this.baroAltitude = baroAltitude;
- this.squawk = squawk;
- this.spi = spi;
- this.positionSource = positionSource;
- }
- public String getIcao24() { return icao24; }
- public String getCallSign() { return callSign; }
- public String getOriginCountry() { return originCountry; }
- public Integer getTimePosition() { return timePosition; }
- public Integer getLastContact() { return lastContact; }
- public Double getLongitude() { return longitude; }
- public Double getLatitude() { return latitude; }
- public Double getGeoAltitude() { return geoAltitude; }
- public Boolean isOnGround() { return onGround; }
- public Double getVelocity() { return velocity; }
- public Double getHeading() { return heading; }
- public Double getVerticalRate() { return verticalRate; }
- public Integer[] getSensors() { return sensors; }
- public Double getBaroAltitude() { return baroAltitude; }
- public String getSquawk() { return squawk; }
- public Boolean getSpi() { return spi; }
- public Integer getPositionSource() { return positionSource; }
- @Override
- public String toString() {
- return "[" + icao24 + "," + callSign + "," + originCountry + ","
- + timePosition + "," + lastContact + "," + longitude + ","
- + latitude + "," + geoAltitude + "," + onGround + ","
- + velocity + "," + heading + "," + verticalRate + ","
- + sensors + "," + baroAltitude + "," + squawk + ","
- + spi + "," + positionSource + "]";
- }
- }
- private static class JsonObject {
- private String jsonString;
- private int jsonLength;
- private int clsCrlBrcInd;
- private List<Integer> bracketIndexes;
- JsonObject(String jsonString) {
- this.jsonString = jsonString;
- this.jsonLength = jsonString.length();
- this.clsCrlBrcInd = jsonString.length() - 1;
- this.bracketIndexes = new ArrayList<>();
- initIndexLists(jsonString);
- }
- private void initIndexLists(String jsonString) {
- for (int index = 0; index < jsonString.length(); index++) {
- char currentCharacter = jsonString.charAt(index);
- if (currentCharacter == '[' || currentCharacter == ']') {
- this.bracketIndexes.add(index);
- }
- }
- }
- public String getJsonString() { return jsonString; }
- public int getJsonLength() { return jsonLength; }
- public int getClsCrlBrcInd() { return clsCrlBrcInd; }
- public List<Integer> getBrackIndexes() { return bracketIndexes; }
- }
- private static List<Plane> getPlanesFromApi(JsonObject json) {
- List<Plane> planes = new ArrayList<>();
- List<Integer> brackIndxs = json.getBrackIndexes();
- for (int currOpenBrackInd = brackIndxs.get(1);
- (brackIndxs.get(brackIndxs.indexOf(currOpenBrackInd) + 2) + 1) != json.getClsCrlBrcInd(); ) {
- int nextBrackInd = brackIndxs.get(brackIndxs.indexOf(currOpenBrackInd) + 1);
- int currObjClsBrack = nextBrackInd;
- if (json.getJsonString().charAt(nextBrackInd) == '[') {
- int arrayOpnBrack = nextBrackInd;
- int arrayClsBrack = brackIndxs.get(brackIndxs.indexOf(arrayOpnBrack) + 1);
- currObjClsBrack = brackIndxs.get(brackIndxs.indexOf(currOpenBrackInd) + 3);
- String jsonString =
- json.getJsonString().substring(currOpenBrackInd + 1, currObjClsBrack);
- String jsonBeforeArray = jsonString.substring(0, arrayOpnBrack);
- String jsonAfterArray = jsonString.substring(arrayClsBrack + 1, currObjClsBrack);
- String[] valuesBeforeArray = jsonBeforeArray.split(",");
- String[] valuesAfterArray = jsonAfterArray.split(",");
- System.out.println(valuesAfterArray.length);
- if (valuesBeforeArray.length != 12 || valuesAfterArray.length != 4) {
- throw new IllegalStateException("Incorrect array length");
- }
- String[] sensorsArray =
- jsonString.substring(arrayOpnBrack + 1, arrayClsBrack).split(",");
- planes.add(new Plane(
- nullSafeStripQuotes(valuesBeforeArray[0]),
- nullSafeStripQuotes(valuesBeforeArray[1]),
- nullSafeStripQuotes(valuesBeforeArray[2]),
- nullSafeParseInt(valuesBeforeArray[3]),
- nullSafeParseInt(valuesBeforeArray[4]),
- nullSafeParseDouble(valuesBeforeArray[5]),
- nullSafeParseDouble(valuesBeforeArray[6]),
- nullSafeParseDouble(valuesBeforeArray[7]),
- nullSafeParseBoolean(valuesBeforeArray[8]),
- nullSafeParseDouble(valuesBeforeArray[9]),
- nullSafeParseDouble(valuesBeforeArray[10]),
- nullSafeParseDouble(valuesBeforeArray[11]),
- convertStringArrayToIntegerArray(sensorsArray),
- nullSafeParseDouble(valuesAfterArray[0]),
- nullSafeStripQuotes(valuesAfterArray[1]),
- nullSafeParseBoolean(valuesAfterArray[2]),
- nullSafeParseInt(valuesAfterArray[3])));
- currOpenBrackInd =
- brackIndxs.get(brackIndxs.indexOf(currObjClsBrack) + 1);
- } else {
- String jsonString =
- json.getJsonString().substring(currOpenBrackInd + 1, currObjClsBrack);
- String[] valueArray = jsonString.split(",");
- planes.add(new Plane(
- nullSafeStripQuotes(valueArray[0]),
- nullSafeStripQuotes(valueArray[1]),
- nullSafeStripQuotes(valueArray[2]),
- nullSafeParseInt(valueArray[3]),
- nullSafeParseInt(valueArray[4]),
- nullSafeParseDouble(valueArray[5]),
- nullSafeParseDouble(valueArray[6]),
- nullSafeParseDouble(valueArray[7]),
- nullSafeParseBoolean(valueArray[8]),
- nullSafeParseDouble(valueArray[9]),
- nullSafeParseDouble(valueArray[10]),
- nullSafeParseDouble(valueArray[11]),
- null,
- nullSafeParseDouble(valueArray[13]),
- nullSafeStripQuotes(valueArray[14]),
- nullSafeParseBoolean(valueArray[15]),
- nullSafeParseInt(valueArray[16])));
- currOpenBrackInd =
- brackIndxs.get(brackIndxs.indexOf(currObjClsBrack) + 1);
- }
- }
- return planes;
- }
- private static String nullSafeStripQuotes(String possibleString) {
- if (possibleString.equals("null")) {
- return null;
- } else {
- return stripQuotes(possibleString);
- }
- }
- private static Integer nullSafeParseInt(String possibleInt) {
- if (possibleInt.equals("null")) {
- return null;
- } else {
- return Integer.parseInt(possibleInt);
- }
- }
- private static Double nullSafeParseDouble(String possibleFloat) {
- if (possibleFloat.equals("null")) {
- return null;
- } else {
- return Double.parseDouble(possibleFloat);
- }
- }
- private static Boolean nullSafeParseBoolean(String possibleBoolean) {
- if (possibleBoolean.equals("null")) {
- return null;
- } else {
- return Boolean.parseBoolean(possibleBoolean);
- }
- }
- private static String getHttpResponse(String urlString) throws Exception {
- StringBuilder response = new StringBuilder();
- try {
- URL url = new URL(urlString);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- try (BufferedReader br = new BufferedReader(
- new InputStreamReader(conn.getInputStream()))) {
- String line;
- while ((line = br.readLine()) != null) {
- response.append(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- return response.toString();
- }
- private static Integer[] convertStringArrayToIntegerArray(String[] stringArray) {
- Integer[] intArray = new Integer[stringArray.length];
- for (int i = 0; i < stringArray.length; i++) {
- intArray[i] = nullSafeParseInt(stringArray[i]);
- }
- return intArray;
- }
- private static String stripQuotes(String stringWithQuotes) {
- return stringWithQuotes.substring(1, stringWithQuotes.length() - 1);
- }
- private static Plane getPlaneClosestTo(double latitude, double longitude, List<Plane> planes) {
- Plane closestPlane = planes.get(0);
- for (Plane plane : planes) {
- if (plane.getLatitude() == null || plane.getLongitude() == null) {
- continue;
- }
- if (getDistanceBetween(latitude, longitude,
- plane.getLatitude(), plane.getLongitude())
- < getDistanceBetween(latitude, longitude,
- closestPlane.getLatitude(), closestPlane.getLongitude())) {
- closestPlane = plane;
- }
- }
- return closestPlane;
- }
- private static double getDistanceBetween(double x1, double y1, double x2, double y2) {
- return Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow(y1 - y2, 2));
- }
- public static void main(String[] args) throws Exception {
- JsonObject apiCall = new JsonObject(
- getHttpResponse("https://opensky-network.org/api/states/all"));
- List<Plane> planes = getPlanesFromApi(apiCall);
- Plane closestPlaneToEifelTower =
- getPlaneClosestTo(48.8584, 2.2945, planes);
- Plane closestPlaneToJohnFKennedyAirport =
- getPlaneClosestTo(40.6413, 73.7781, planes);
- System.out.println("Closest plane to Eifel Tower:");
- System.out.println("Callsign: " + closestPlaneToEifelTower.getCallSign());
- System.out.println("Latitude: " + closestPlaneToEifelTower.getLatitude());
- System.out.println("Longitude: " + closestPlaneToEifelTower.getLongitude());
- System.out.println("Geometric Altitude: " + closestPlaneToEifelTower.getGeoAltitude());
- System.out.println("Country of origin: " + closestPlaneToEifelTower.getOriginCountry());
- System.out.println("ICAO24 ID: " + closestPlaneToEifelTower.getIcao24());
- System.out.println();
- System.out.println("Closest plane to John F. Kennedy Airport:");
- System.out.println("Callsign: " + closestPlaneToJohnFKennedyAirport.getCallSign());
- System.out.println("Latitude: " + closestPlaneToJohnFKennedyAirport.getLatitude());
- System.out.println("Longitude: " + closestPlaneToJohnFKennedyAirport.getLongitude());
- System.out.println("Geometric Altitude: " + closestPlaneToJohnFKennedyAirport.getGeoAltitude());
- System.out.println("Country of origin: " + closestPlaneToJohnFKennedyAirport.getOriginCountry());
- System.out.println("ICAO24 ID: " + closestPlaneToJohnFKennedyAirport.getIcao24());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement