-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemDownloader.java
213 lines (171 loc) · 6.78 KB
/
DemDownloader.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// DemDownloader.java
// Bobby Krupczak and ChatGPT
// download a digital elevation model/map
// from OpenTopography.org and write out
// to a tiff file; API Key is fetched
// from env variable OPENTOPOGRAPHY_API_KEY
// run from command-line via:
// java -cp . DemDownloader lat lon length
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.function.Consumer;
public class DemDownloader
{
private String apiKeyStr; // API Key
private static final String URL_STR = "https://portal.opentopography.org/API/globaldem?";
private static final String DEM_TYPE_STR = "SRTMGL1";
private int responseCode;
private int responseBytes;
private double s, w, n, e; // Bounding box coordinates
private String filenameSuffix = ".tiff";
private String outputFormatStr = "GTiff";
public DemDownloader(double lat, double lon, double length) {
double[] boundingBox = getBoundingBox(lat, lon, length);
n = boundingBox[0];
s = boundingBox[1];
e = boundingBox[2];
w = boundingBox[3];
System.out.println(n+","+s+","+e+","+w);
apiKeyStr = "NeedApiKey";
// read API key from environment variable
apiKeyStr = System.getenv("OPENTOPOGRAPHY_API_KEY");
if (apiKeyStr.equals("")) {
apiKeyStr = "GiveMeApiKey";
}
}
// Method to download DEM with blocking
//
public boolean syncDownload() throws IOException {
String requestURLStr = URL_STR +
"demtype=" + DEM_TYPE_STR +
"&south=" + s +
"&north=" + n +
"&west=" + w +
"&east=" + e +
"&outputFormat=" + outputFormatStr +
"&API_Key=" + apiKeyStr;
boolean b = false;
System.out.println("urlStr is "+requestURLStr);
URL url = new URL(requestURLStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
System.out.println("Request failed, error code "+responseCode);
return false;
}
// read and write out the data to file
String filename = "DEM_LatLon_"+s+"_"+w+"_"+n+"_"+e+filenameSuffix;
try {
InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(filename);
byte[] buffer = new byte[4096];
int bytesRead = 0;
int totalBytes = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer,0,bytesRead);
totalBytes += bytesRead;
}
outputStream.close();
System.out.println("Wrote "+totalBytes+" bytes to "+filename);
b = true;
}
catch (Exception e) {
System.out.println("Write to fail failed "+e);
b = false;
}
connection.disconnect();
return true;
} // download
// down a DEM async or in background
// callback will indicate success or error
// pass an object that implements a callback metho
// onCallback() method
public void asyncDownload(Consumer<String> consumer)
{
Thread aThread = new Thread(new Runnable() {
@Override
public void run()
{
boolean b;
// call the sync download from within our thread
//
try {
b = syncDownload();
consumer.accept("Download returned "+b);
}
catch (Exception e) {
consumer.accept(e.toString());
}
} // run
});
aThread.start();
System.out.println("asyncDownloading started");
} // downloaAdsync
// calculate bounding box; return [n,s,e,w]
private double[] getBoundingBox(double centerLat, double centerLon, double length)
{
final double metersInDegreeLatitude = 111320; // Approximate meters in one degree of latitude
// Calculate deltas
double deltaLat = (length / 2) / metersInDegreeLatitude;
double deltaLon = (length / 2) / (metersInDegreeLatitude * Math.cos(Math.toRadians(centerLat)));
// Calculate bounding box
double north = centerLat + deltaLat;
double south = centerLat - deltaLat;
double east = centerLon + deltaLon;
double west = centerLon - deltaLon;
return new double[]{truncateDouble(north,6), truncateDouble(south,6), truncateDouble(east,6), truncateDouble(west,6)};
}
// Method to calculate the bounding box
// private double[] getBoundingBox(double centerLat, double centerLon, double length) {
// double d = Math.sqrt(2.0) * (length / 2.0);
// double[] sw = calculateCoordinate(centerLat, centerLon, 225.0, d);
// double[] ne = calculateCoordinate(centerLat, centerLon, 45.0, d);
// return new double[]{truncateDouble(sw[0], 6), truncateDouble(sw[1], 6),
// truncateDouble(ne[0], 6), truncateDouble(ne[1], 6)};
// }
// // Helper method to calculate new coordinate; ChatGPT
// private double[] calculateCoordinate(double lat, double lon, double bearing, double distance) {
// double radius = 6371e3; // Earth's radius in meters
// double angularDistance = distance / radius;
// double latRad = Math.toRadians(lat);
// double lonRad = Math.toRadians(lon);
// bearing = Math.toRadians(bearing);
// double newLat = Math.asin(Math.sin(latRad) * Math.cos(angularDistance) +
// Math.cos(latRad) * Math.sin(angularDistance) * Math.cos(bearing));
// double newLon = lonRad + Math.atan2(Math.sin(bearing) * Math.sin(angularDistance) * Math.cos(latRad),
// Math.cos(angularDistance) - Math.sin(latRad) * Math.sin(newLat));
// return new double[]{Math.toDegrees(newLat), Math.toDegrees(newLon)};
// }
public static double truncateDouble(double val, int precision) {
double scale = Math.pow(10, precision);
return Math.round(val * scale) / scale;
}
public static void main(String[] args)
{
System.out.println("DemDownloader starting");
if (args.length < 3) {
System.out.println("java DemDownloader lat lon length");
System.exit(-1);
}
Double lat = Double.parseDouble(args[0]);
Double lon = Double.parseDouble(args[1]);
Double len = Double.parseDouble(args[2]);
System.out.println("Fetching DEM at ("+lat+","+lon+") "+len+" hxw meters");
DemDownloader aDownloader = new DemDownloader(lat,lon,len);
try {
aDownloader.asyncDownload(new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
});
} catch (Exception e) {
System.out.println("Download failed "+e);
}
} // main
} // DemDownloader