|
| 1 | + |
| 2 | +package Satellite; |
| 3 | + |
| 4 | + |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import org.greenrobot.eventbus.EventBus; |
| 8 | + |
| 9 | +import java.io.BufferedInputStream; |
| 10 | +import java.io.DataInputStream; |
| 11 | +import java.io.File; |
| 12 | +import java.io.FileInputStream; |
| 13 | +import java.io.FileNotFoundException; |
| 14 | +import java.io.FileOutputStream; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.InputStream; |
| 17 | +import java.io.OutputStream; |
| 18 | + |
| 19 | +class EGM96 { |
| 20 | + |
| 21 | + // ---------------------------------------------------------------------------- Singleton Class |
| 22 | + private static EGM96 instance = new EGM96(); |
| 23 | + |
| 24 | + private EGM96(){} |
| 25 | + |
| 26 | + public static EGM96 getInstance(){ |
| 27 | + return instance; |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + private int BOUNDARY = 3; // The grid extensions (in each of the 4 sides) of the real 721 x 1440 grid |
| 33 | + private short[][] EGMGrid = new short[BOUNDARY + 1440 + BOUNDARY][BOUNDARY + 721 + BOUNDARY]; |
| 34 | + private boolean isEGMGridLoaded = false; |
| 35 | + private boolean isEGMGridLoading = false; |
| 36 | + private boolean isEGMFileCopying = false; |
| 37 | + private String EGMFileName; |
| 38 | + private String EGMFileNameLocalCopy; |
| 39 | + |
| 40 | + public double EGM96_VALUE_INVALID = -100000; |
| 41 | + |
| 42 | + public void LoadGridFromFile(String FileName, String FileNameLocalCopy) { |
| 43 | + if (!isEGMGridLoaded && !isEGMGridLoading) { |
| 44 | + isEGMGridLoading = true; |
| 45 | + EGMFileName = FileName; |
| 46 | + EGMFileNameLocalCopy = FileNameLocalCopy; |
| 47 | + new Thread(new LoadEGM96Grid()).start(); |
| 48 | + } else { |
| 49 | + if (isEGMGridLoading) Log.w("myApp", "[#] EGM96.java - Grid is already loading, please wait"); |
| 50 | + if (isEGMGridLoaded) Log.w("myApp", "[#] EGM96.java - Grid already loaded"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /* public void UnloadGrid() { |
| 55 | + isEGMGridLoaded = false; |
| 56 | + isEGMGridLoading = false; |
| 57 | + //listener.onEGMGridLoaded(isEGMGridLoaded); |
| 58 | + EventBus.getDefault().post(EventBusMSG.UPDATE_FIX); |
| 59 | + EventBus.getDefault().post(EventBusMSG.UPDATE_TRACK); |
| 60 | + EventBus.getDefault().post(EventBusMSG.UPDATE_TRACKLIST); |
| 61 | + } |
| 62 | +*/ |
| 63 | + public boolean isEGMGridLoaded() { |
| 64 | + return isEGMGridLoaded; |
| 65 | + } |
| 66 | + |
| 67 | + public boolean isEGMGridLoading() { |
| 68 | + return isEGMGridLoading; |
| 69 | + } |
| 70 | + |
| 71 | + public double getEGMCorrection(double Latitude, double Longitude) { |
| 72 | + // This function calculates and return the EGM96 altitude correction value of the input coordinates, in m; |
| 73 | + // Input coordinates are: -90 < Latitude < 90; -180 < Longitude < 360 (android range -180 < Longitude < 180); |
| 74 | + |
| 75 | + if (isEGMGridLoaded) { |
| 76 | + double Lat = 90.0 - Latitude; |
| 77 | + double Lon = Longitude; |
| 78 | + if (Lon < 0) Lon += 360.0; |
| 79 | + |
| 80 | + int ilon = (int) (Lon / 0.25) + BOUNDARY; |
| 81 | + int ilat = (int) (Lat / 0.25) + BOUNDARY; |
| 82 | + |
| 83 | + try { |
| 84 | + // Creating points for interpolation |
| 85 | + short hc11 = EGMGrid[ilon][ilat]; |
| 86 | + short hc12 = EGMGrid[ilon][ilat + 1]; |
| 87 | + short hc21 = EGMGrid[ilon + 1][ilat]; |
| 88 | + short hc22 = EGMGrid[ilon + 1][ilat + 1]; |
| 89 | + |
| 90 | + // Interpolation: |
| 91 | + // Latitude |
| 92 | + double hc1 = hc11 + (hc12 - hc11) * (Lat % 0.25) / 0.25; |
| 93 | + double hc2 = hc21 + (hc22 - hc21) * (Lat % 0.25) / 0.25; |
| 94 | + // Longitude |
| 95 | + //double hc = (hc1 + (hc2 - hc1) * (Lon % 0.25) / 0.25) / 100; |
| 96 | + //Log.w("myApp", "[#] EGM96.java - getEGMCorrection(" + Latitude + ", " + Longitude + ") = " + hc); |
| 97 | + |
| 98 | + return ((hc1 + (hc2 - hc1) * (Lon % 0.25) / 0.25) / 100); |
| 99 | + } catch (ArrayIndexOutOfBoundsException e) { |
| 100 | + return EGM96_VALUE_INVALID; |
| 101 | + } |
| 102 | + } |
| 103 | + else return EGM96_VALUE_INVALID; |
| 104 | + } |
| 105 | + |
| 106 | + private void copyFile(InputStream in, OutputStream out) throws IOException { |
| 107 | + byte[] buffer = new byte[1024]; |
| 108 | + int read; |
| 109 | + while ((read = in.read(buffer)) != -1) { |
| 110 | + out.write(buffer, 0, read); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void DeleteFile(String filename) { |
| 115 | + File file = new File(filename); |
| 116 | + if (file.exists ()) file.delete(); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + // The Thread that loads the grid in background ------------------------------------------------ |
| 121 | + |
| 122 | + private class LoadEGM96Grid implements Runnable { |
| 123 | + // Thread: Load EGM grid |
| 124 | + |
| 125 | + @Override |
| 126 | + public void run() { |
| 127 | + Thread.currentThread().setPriority(Thread.MIN_PRIORITY); |
| 128 | + Log.w("myApp", "[#] EGM96.java - Start loading grid"); |
| 129 | + |
| 130 | + boolean islocalcopypresent = false; |
| 131 | + boolean issharedcopypresent = false; |
| 132 | + |
| 133 | + File localfile = new File(EGMFileNameLocalCopy); |
| 134 | + if (localfile.exists() && (localfile.length() == 2076480)) islocalcopypresent = true; |
| 135 | + |
| 136 | + File sharedfile = new File(EGMFileName); |
| 137 | + if (sharedfile.exists() && (sharedfile.length() == 2076480)) issharedcopypresent = true; |
| 138 | + |
| 139 | + File file = new File(islocalcopypresent ? EGMFileNameLocalCopy : EGMFileName); |
| 140 | + if (islocalcopypresent || issharedcopypresent) { |
| 141 | + Log.w("myApp", "[#] EGM96.java - From file: " + file.getAbsolutePath()); |
| 142 | + |
| 143 | + FileInputStream fin; |
| 144 | + try { |
| 145 | + fin = new FileInputStream(file); |
| 146 | + } catch (FileNotFoundException e) { |
| 147 | + isEGMGridLoaded = false; |
| 148 | + isEGMGridLoading = false; |
| 149 | + Log.w("myApp", "[#] EGM96.java - FileNotFoundException"); |
| 150 | + //Toast.makeText(getApplicationContext(), "Oops", Toast.LENGTH_SHORT).show(); |
| 151 | + //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| 152 | + return; |
| 153 | + } |
| 154 | + BufferedInputStream bin = new BufferedInputStream(fin); |
| 155 | + DataInputStream din = new DataInputStream(bin); |
| 156 | + int i; |
| 157 | + int i_lon = BOUNDARY; |
| 158 | + int i_lat = BOUNDARY; |
| 159 | + int count = (int) ((file.length() / 2)); |
| 160 | + |
| 161 | + for (i = 0; (i < count); i++) { |
| 162 | + try { |
| 163 | + EGMGrid[i_lon][i_lat] = din.readShort(); |
| 164 | + i_lon++; |
| 165 | + if (i_lon >= (1440 + BOUNDARY)) { |
| 166 | + i_lat++; |
| 167 | + i_lon = BOUNDARY; |
| 168 | + } |
| 169 | + } catch (IOException e) { |
| 170 | + isEGMGridLoaded = false; |
| 171 | + isEGMGridLoading = false; |
| 172 | + Log.w("myApp", "[#] EGM96.java - IOException"); |
| 173 | + return; |
| 174 | + //Toast.makeText(getApplicationContext(), "Oops", Toast.LENGTH_SHORT).show(); |
| 175 | + //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + if (BOUNDARY > 0) { |
| 180 | + // Fill boundaries with correct data, in order to speed up retrieving for interpolation; |
| 181 | + // fill left + right boundaries |
| 182 | + //Log.w("myApp", "[#] EGM96.java - LR BOUNDARIES"); |
| 183 | + for (int ix = 0; (ix < BOUNDARY); ix++) { |
| 184 | + for (int iy = BOUNDARY; (iy < BOUNDARY + 721); iy++) { |
| 185 | + EGMGrid[ix][iy] = EGMGrid[ix + 1440][iy]; |
| 186 | + EGMGrid[BOUNDARY + ix + 1440][iy] = EGMGrid[BOUNDARY + ix][iy]; |
| 187 | + } |
| 188 | + } |
| 189 | + // fill top + bottom boundaries |
| 190 | + //Log.w("myApp", "[#] EGM96.java - TOP DOWN BOUNDARIES"); |
| 191 | + for (int iy = 0; (iy < BOUNDARY); iy++) { |
| 192 | + for (int ix = 0; (ix < BOUNDARY + 1440 + BOUNDARY); ix++) { |
| 193 | + if (ix > 720) { |
| 194 | + EGMGrid[ix][iy] = EGMGrid[ix - 720][BOUNDARY + BOUNDARY - iy]; |
| 195 | + EGMGrid[ix][BOUNDARY + iy + 721] = EGMGrid[ix - 720][BOUNDARY + 721-2 - iy]; |
| 196 | + } |
| 197 | + else { |
| 198 | + EGMGrid[ix][iy] = EGMGrid[ix + 720][BOUNDARY + BOUNDARY - iy]; |
| 199 | + EGMGrid[ix][BOUNDARY + iy + 721] = EGMGrid[ix + 720][BOUNDARY + 721-2 - iy]; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + isEGMGridLoading = false; |
| 206 | + isEGMGridLoaded = true; |
| 207 | + Log.w("myApp", "[#] EGM96.java - Grid Successfully Loaded: " + file.getAbsolutePath()); |
| 208 | + //Toast.makeText(getApplicationContext(), "EGM96 correction grid loaded", Toast.LENGTH_SHORT).show(); |
| 209 | + |
| 210 | + if (issharedcopypresent) { |
| 211 | + if (!islocalcopypresent) new Thread(new CopyEGM96Grid()).start(); |
| 212 | + else { |
| 213 | + DeleteFile(EGMFileName); // Delete the EGM file from the shared folder |
| 214 | + Log.w("myApp", "[#] EGM96.java - EGM File already present into FilesDir. File deleted from shared folder"); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + } else { |
| 219 | + isEGMGridLoading = false; |
| 220 | + isEGMGridLoaded = false; |
| 221 | + if (!file.exists()) { Log.w("myApp", "[#] EGM96.java - File not found"); } |
| 222 | + if (!file.canRead()) { Log.w("myApp", "[#] EGM96.java - Cannot read file"); } |
| 223 | + if (file.length() != 2076480) { |
| 224 | + Log.w("myApp", "[#] EGM96.java - File has invalid length: " + file.length());} |
| 225 | + //Toast.makeText(getApplicationContext(), "EGM96 correction not available", Toast.LENGTH_SHORT).show(); |
| 226 | + } |
| 227 | + EventBus.getDefault().post(EventBusMSG.UPDATE_FIX); |
| 228 | + EventBus.getDefault().post(EventBusMSG.UPDATE_TRACK); |
| 229 | + EventBus.getDefault().post(EventBusMSG.UPDATE_TRACKLIST); |
| 230 | + //listener.onEGMGridLoaded(isEGMGridLoaded); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + // The Thread that copies the EGM grid in FilesDir (in background) ----------------------------- |
| 235 | + |
| 236 | + private class CopyEGM96Grid implements Runnable { |
| 237 | + // Thread: Copy the EGM grid in FilesDir |
| 238 | + |
| 239 | + @Override |
| 240 | + public void run() { |
| 241 | + Thread.currentThread().setPriority(Thread.MIN_PRIORITY); |
| 242 | + Log.w("myApp", "[#] EGM96.java - Copy EGM96 Grid into FilesDir"); |
| 243 | + |
| 244 | + if (isEGMFileCopying) return; |
| 245 | + |
| 246 | + isEGMFileCopying = true; |
| 247 | + |
| 248 | + File sd_cpy = new File(EGMFileNameLocalCopy); |
| 249 | + if (sd_cpy.exists()) sd_cpy.delete(); |
| 250 | + |
| 251 | + File sd_old = new File(EGMFileName); |
| 252 | + if (sd_old.exists()) { |
| 253 | + InputStream in = null; |
| 254 | + OutputStream out = null; |
| 255 | + try { |
| 256 | + in = new FileInputStream(EGMFileName); |
| 257 | + out = new FileOutputStream(EGMFileNameLocalCopy); |
| 258 | + copyFile(in, out); |
| 259 | + in.close(); |
| 260 | + in = null; |
| 261 | + out.flush(); |
| 262 | + out.close(); |
| 263 | + out = null; |
| 264 | + Log.w("myApp", "[#] EGM96.java - EGM File copy completed"); |
| 265 | + DeleteFile(EGMFileName); // Delete the EGM file from the shared folder |
| 266 | + Log.w("myApp", "[#] EGM96.java - EGM File deleted from shared folder"); |
| 267 | + |
| 268 | + } catch(Exception e) { |
| 269 | + Log.w("MyApp", "[#] EGM96.java - Unable to make local copy of EGM file: " + e.getMessage()); |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + isEGMFileCopying = false; |
| 274 | + } |
| 275 | + } |
| 276 | +} |
0 commit comments