File: SrtmLowResSource.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (156 lines) | stat: -rw-r--r-- 3,688 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package tim.prune.function.srtm;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import tim.prune.GpsPrune;


/**
 * Low-resolution source of SRTM data
 */
public class SrtmLowResSource extends SrtmSource
{
	/** tile data loaded from file */
	private byte[] _tileData = null;

	/** URL prefix for all tiles */
	private static final String URL_PREFIX = "https://srtm.kurviger.de/SRTM3/";
	/** Directory names for each continent */
	private static final String[] CONTINENTS = {"", "Eurasia", "North_America", "Australia",
		"Islands", "South_America", "Africa"};


	/** Constructor */
	SrtmLowResSource(String inDiskPath) {
		super(inDiskPath);
	}

	@Override
	public int getTilePixels() {
		return 1201;
	}

	/**
	 * Get the Url for the given tile
	 * @param inTile Tile to get the data for
	 * @return single URL
	 */
	public URL getUrl(SrtmTile inTile)
	{
		if (inTile == null) {return null;}
		if (_tileData == null)
		{
			_tileData = readDatFile();
			if (_tileData == null)
			{
				System.err.println("Build error: resource srtmtiles.dat missing!");
				return null;
			}
		}

		URL url = null;
		// Get byte from lookup array
		int idx = (inTile.getLatitude() + 59)*360 + (inTile.getLongitude() + 180);
		try
		{
			int dir = _tileData[idx];
			if (dir > 0)
			{
				try {
					url = new URL(URL_PREFIX + CONTINENTS[dir] + "/"
						+ getFilename(inTile));
				} catch (MalformedURLException e) {} // ignore error, url stays null
			}
		} catch (ArrayIndexOutOfBoundsException e) {} // ignore error, url stays null

		return url;
	}

	/**
	 * @return filename with which this tile data will be cached
	 */
	public String getFilename(SrtmTile inTile) {
		return inTile.getTileName() + ".hgt.zip";
	}

	/**
	 * Read the dat file and get the contents
	 * @return byte array containing file contents
	 */
	private static byte[] readDatFile()
	{
		String filename = "/tim/prune/function/srtm/srtmtiles.dat";
		try (InputStream in = SrtmLowResSource.class.getResourceAsStream(filename))
		{
			// Need absolute path to dat file
			if (in != null)
			{
				byte[] buffer = new byte[in.available()];
				in.read(buffer);
				in.close();
				return buffer;
			}
		} catch (IOException e) {
			System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
		}
		return null;
	}

	/**
	 * Download a single tile of SRTM data
	 * @param inTile tile to get
	 */
	public Result downloadTile(SrtmTile inTile)
	{
		URL url = getUrl(inTile);
		if (url == null) {
			return Result.NOTHING_TO_DO;
		}

		// Check the cache is ok
		File srtmDir = getCacheDir();
		if (srtmDir != null)
		{
			if (srtmDir.exists() && !srtmDir.isDirectory()) {
				// exists but isn't a directory - can't be used
				return Result.CACHE_FAILED;
			}
			if (!srtmDir.exists() && !srtmDir.mkdir()) {
				// can't create the srtm directory
				return Result.CACHE_FAILED;
			}
		}
		else {
			// no cache set up
			return Result.CACHE_FAILED;
		}

		InputStream inStream = null;
		Result result = Result.NOTHING_TO_DO;
		try
		{
			URLConnection conn = url.openConnection();
			conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
			inStream = conn.getInputStream();
			result = writeFileFromStream(url, inStream) ? Result.DOWNLOADED : Result.DOWNLOAD_FAILED;
		}
		catch (IOException ioe) {
			System.err.println(ioe.getClass().getName() + " - " + ioe.getMessage());
			result = Result.DOWNLOAD_FAILED;
		}
		// Make sure stream is closed
		if (inStream != null)
		{
			try {
				inStream.close();
			} catch (Exception e) {}
		}

		return result;
	}
}