*/
public class LookupSrtmFunction extends GenericFunction implements Runnable
{
+ /** Whether to overwrite all altitudes, or only fill in missing data */
+ private boolean _overwriteAlt = false;
/** Progress dialog */
private ProgressDialog _progress = null;
/** Track to process */
private boolean _running = false;
/** Expected size of hgt file in bytes */
- private static final long HGT_SIZE = 2884802L;
+ private static final int HGT_ROW = 3601;
+ private static final int HGT_SIZE = 2 * HGT_ROW * HGT_ROW;
/** Altitude below which is considered void */
private static final int VOID_VAL = -32768;
* Constructor
* @param inApp App object
*/
- public LookupSrtmFunction(App inApp) {
+ public LookupSrtmFunction(App inApp, boolean overwriteAlt) {
super(inApp);
+ _overwriteAlt = overwriteAlt;
}
/** @return name key */
public String getNameKey() {
- return "function.lookupsrtm";
+ if (_overwriteAlt) {
+ return "function.overwritesrtm";
+ } else {
+ return "function.lookupsrtm";
+ }
}
/**
{
// Compile list of tiles to get
ArrayList<SrtmTile> tileList = new ArrayList<SrtmTile>();
- boolean hasZeroAltitudePoints = false;
- boolean hasNonZeroAltitudePoints = false;
- // First, loop to see what kind of points we have
- for (int i = 0; i < _track.getNumPoints(); i++)
- {
- if (_track.getPoint(i).hasAltitude())
- {
- if (_track.getPoint(i).getAltitude().getValue() == 0) {
- hasZeroAltitudePoints = true;
- }
- else {
- hasNonZeroAltitudePoints = true;
- }
- }
- }
- // Should we overwrite the zero altitude values?
- boolean overwriteZeros = hasZeroAltitudePoints && !hasNonZeroAltitudePoints;
- // If non-zero values present as well, ask user whether to overwrite the zeros or not
- if (hasNonZeroAltitudePoints && hasZeroAltitudePoints && JOptionPane.showConfirmDialog(_parentFrame,
- I18nManager.getText("dialog.lookupsrtm.overwritezeros"), I18nManager.getText(getNameKey()),
- JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
- {
- overwriteZeros = true;
- }
+
+ boolean overwriteZeros = overwriteZeros();
// Now loop again to extract the required tiles
for (int i = 0; i < _track.getNumPoints(); i++)
{
// Consider points which don't have altitudes or have zero values
- if (!_track.getPoint(i).hasAltitude()
- || (overwriteZeros && _track.getPoint(i).getAltitude().getValue() == 0))
+ if (needsAltitude(_track.getPoint(i), overwriteZeros))
{
SrtmTile tile = new SrtmTile(_track.getPoint(i));
boolean alreadyGot = false;
}
}
+ /**
+ * true if we need to set the altitude of this point
+ */
+ private boolean needsAltitude(DataPoint point, boolean overwriteZeros)
+ {
+ if (_overwriteAlt)
+ {
+ return true;
+ }
+ if (!point.hasAltitude())
+ {
+ return true;
+ }
+ if (overwriteZeros && point.getAltitude().getValue() == 0)
+ {
+ return true;
+ }
+ return false;
+ }
+
+ private boolean overwriteZeros()
+ {
+ // nothing to do if we overwrite all altitudes anyway
+ if (_overwriteAlt) {
+ return true;
+ }
+
+ boolean hasZeroAltitudePoints = false;
+ boolean hasNonZeroAltitudePoints = false;
+ // First, loop to see what kind of points we have
+ for (int i = 0; i < _track.getNumPoints(); i++)
+ {
+ if (_track.getPoint(i).hasAltitude())
+ {
+ if (_track.getPoint(i).getAltitude().getValue() == 0) {
+ hasZeroAltitudePoints = true;
+ }
+ else {
+ hasNonZeroAltitudePoints = true;
+ }
+ }
+ }
+ // Should we overwrite the zero altitude values?
+ boolean overwriteZeros = hasZeroAltitudePoints && !hasNonZeroAltitudePoints;
+ // If non-zero values present as well, ask user whether to overwrite the zeros or not
+ if (hasNonZeroAltitudePoints && hasZeroAltitudePoints && JOptionPane.showConfirmDialog(_parentFrame,
+ I18nManager.getText("dialog.lookupsrtm.overwritezeros"), I18nManager.getText(getNameKey()),
+ JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
+ {
+ overwriteZeros = true;
+ }
+ return overwriteZeros;
+ }
+
/**
* Lookup the values from SRTM data
* @param inTileList list of tiles to get
* @param inOverwriteZeros true to overwrite zero altitude values
*/
- private void lookupValues(ArrayList<SrtmTile> inTileList, boolean inOverwriteZeros)
+ private void lookupValues(ArrayList<SrtmTile> inTileList, boolean overwriteZeros)
{
- UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo());
+ UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo(), _overwriteAlt);
int numAltitudesFound = 0;
// Update progress bar
if (_progress != null)
{
// Set progress
_progress.setValue(t);
- final int ARRLENGTH = 1201 * 1201;
+ final int ARRLENGTH = HGT_ROW * HGT_ROW;
int[] heights = new int[ARRLENGTH];
// Open zipinputstream on url and check size
ZipInputStream inStream = getStreamToHgtFile(urls[t]);
for (int p = 0; p < _track.getNumPoints(); p++)
{
DataPoint point = _track.getPoint(p);
- if (!point.hasAltitude()
- || (inOverwriteZeros && point.getAltitude().getValue() == 0))
+ if (needsAltitude(point, overwriteZeros))
{
if (new SrtmTile(point).equals(tile))
{
- double x = (point.getLongitude().getDouble() - tile.getLongitude()) * 1200;
- double y = 1201 - (point.getLatitude().getDouble() - tile.getLatitude()) * 1200;
- int idx1 = ((int)y)*1201 + (int)x;
+ double x = (point.getLongitude().getDouble() - tile.getLongitude()) * (HGT_ROW - 1);
+ double y = HGT_ROW - (point.getLatitude().getDouble() - tile.getLatitude()) * (HGT_ROW - 1);
+ int idx1 = ((int)y)*HGT_ROW + (int)x;
try
{
- int[] fouralts = {heights[idx1], heights[idx1+1], heights[idx1-1201], heights[idx1-1200]};
+ int[] fouralts = {heights[idx1], heights[idx1+1], heights[idx1-HGT_ROW], heights[idx1-HGT_ROW+1]};
int numVoids = (fouralts[0]==VOID_VAL?1:0) + (fouralts[1]==VOID_VAL?1:0)
+ (fouralts[2]==VOID_VAL?1:0) + (fouralts[3]==VOID_VAL?1:0);
// if (numVoids > 0) System.out.println(numVoids + " voids found");
// System.out.println("Lookup: Trying online: " + inUrl.toString());
_hadToDownload = true;
// MAYBE: Only download if we're in online mode?
- return new ZipInputStream(inUrl.openStream());
+ // return new ZipInputStream(inUrl.openStream());
+ return null;
}
/**