source: josm/trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java@ 17379

Last change on this file since 17379 was 16553, checked in by Don-vip, 4 years ago

see #19334 - javadoc fixes + protected constructors for abstract classes

  • Property svn:eol-style set to native
File size: 10.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.net.SocketException;
9import java.util.List;
10
11import org.openstreetmap.josm.data.Bounds;
12import org.openstreetmap.josm.data.DataSource;
13import org.openstreetmap.josm.data.gpx.GpxData;
14import org.openstreetmap.josm.data.notes.Note;
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
17import org.openstreetmap.josm.tools.CheckParameterUtil;
18import org.openstreetmap.josm.tools.JosmRuntimeException;
19import org.openstreetmap.josm.tools.Logging;
20import org.xml.sax.SAXException;
21
22/**
23 * Read content from OSM server for a given bounding box
24 * @since 627
25 */
26public class BoundingBoxDownloader extends OsmServerReader {
27
28 /**
29 * The boundings of the desired map data.
30 */
31 protected final double lat1;
32 protected final double lon1;
33 protected final double lat2;
34 protected final double lon2;
35 protected final boolean crosses180th;
36
37 /**
38 * Constructs a new {@code BoundingBoxDownloader}.
39 * @param downloadArea The area to download
40 */
41 public BoundingBoxDownloader(Bounds downloadArea) {
42 CheckParameterUtil.ensureParameterNotNull(downloadArea, "downloadArea");
43 this.lat1 = downloadArea.getMinLat();
44 this.lon1 = downloadArea.getMinLon();
45 this.lat2 = downloadArea.getMaxLat();
46 this.lon2 = downloadArea.getMaxLon();
47 this.crosses180th = downloadArea.crosses180thMeridian();
48 }
49
50 private GpxData downloadRawGps(Bounds b, ProgressMonitor progressMonitor) throws IOException, OsmTransferException, SAXException {
51 boolean done = false;
52 GpxData result = null;
53 final int pointsPerPage = 5000; // see https://wiki.openstreetmap.org/wiki/API_v0.6#GPS_traces
54 String url = "trackpoints?bbox="+b.getMinLon()+','+b.getMinLat()+','+b.getMaxLon()+','+b.getMaxLat()+"&page=";
55 for (int i = 0; !done && !isCanceled(); ++i) {
56 progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * pointsPerPage, (i + 1) * pointsPerPage));
57 try (InputStream in = getInputStream(url+i, progressMonitor.createSubTaskMonitor(1, true))) {
58 if (in == null) {
59 break;
60 }
61 progressMonitor.setTicks(0);
62 GpxReader reader = new GpxReader(in);
63 gpxParsedProperly = reader.parse(false);
64 GpxData currentGpx = reader.getGpxData();
65 long count = 0;
66 if (currentGpx.hasTrackPoints()) {
67 count = currentGpx.getTrackPoints().count();
68 }
69 if (count < pointsPerPage)
70 done = true;
71 Logging.debug("got {0} gpx points", count);
72 if (result == null) {
73 result = currentGpx;
74 } else {
75 result.mergeFrom(currentGpx);
76 }
77 } catch (OsmApiException ex) {
78 throw ex; // this avoids infinite loop in case of API error such as bad request (ex: bbox too large, see #12853)
79 } catch (OsmTransferException | SocketException ex) {
80 if (isCanceled()) {
81 final OsmTransferCanceledException canceledException = new OsmTransferCanceledException("Operation canceled");
82 canceledException.initCause(ex);
83 Logging.warn(canceledException);
84 }
85 }
86 activeConnection = null;
87 }
88 if (result != null) {
89 result.fromServer = true;
90 result.dataSources.add(new DataSource(b, "OpenStreetMap server"));
91 }
92 return result;
93 }
94
95 @Override
96 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
97 progressMonitor.beginTask("", 1);
98 try {
99 progressMonitor.indeterminateSubTask(getTaskName());
100 if (crosses180th) {
101 // API 0.6 does not support requests crossing the 180th meridian, so make two requests
102 GpxData result = downloadRawGps(new Bounds(lat1, lon1, lat2, 180.0), progressMonitor);
103 if (result != null)
104 result.mergeFrom(downloadRawGps(new Bounds(lat1, -180.0, lat2, lon2), progressMonitor));
105 return result;
106 } else {
107 // Simple request
108 return downloadRawGps(new Bounds(lat1, lon1, lat2, lon2), progressMonitor);
109 }
110 } catch (IllegalArgumentException e) {
111 // caused by HttpUrlConnection in case of illegal stuff in the response
112 if (cancel)
113 return null;
114 throw new OsmTransferException("Illegal characters within the HTTP-header response.", e);
115 } catch (IOException e) {
116 if (cancel)
117 return null;
118 throw new OsmTransferException(e);
119 } catch (SAXException e) {
120 throw new OsmTransferException(e);
121 } catch (OsmTransferException e) {
122 throw e;
123 } catch (JosmRuntimeException | IllegalStateException e) {
124 if (cancel)
125 return null;
126 throw e;
127 } finally {
128 progressMonitor.finishTask();
129 }
130 }
131
132 /**
133 * Returns the name of the download task to be displayed in the {@link ProgressMonitor}.
134 * @return task name
135 */
136 protected String getTaskName() {
137 return tr("Contacting OSM Server...");
138 }
139
140 /**
141 * Builds the request part for the bounding box.
142 * @param lon1 left
143 * @param lat1 bottom
144 * @param lon2 right
145 * @param lat2 top
146 * @return "map?bbox=left,bottom,right,top"
147 */
148 protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) {
149 return "map?bbox=" + lon1 + ',' + lat1 + ',' + lon2 + ',' + lat2;
150 }
151
152 /**
153 * Parse the given input source and return the dataset.
154 * @param source input stream
155 * @param progressMonitor progress monitor
156 * @return dataset
157 * @throws IllegalDataException if an error was found while parsing the OSM data
158 *
159 * @see OsmReader#parseDataSet(InputStream, ProgressMonitor)
160 */
161 protected DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
162 return OsmReader.parseDataSet(source, progressMonitor);
163 }
164
165 @Override
166 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
167 progressMonitor.beginTask(getTaskName(), 10);
168 try {
169 DataSet ds = null;
170 progressMonitor.indeterminateSubTask(null);
171 if (crosses180th) {
172 // API 0.6 does not support requests crossing the 180th meridian, so make two requests
173 DataSet ds2 = null;
174
175 try (InputStream in = getInputStream(getRequestForBbox(lon1, lat1, 180.0, lat2),
176 progressMonitor.createSubTaskMonitor(9, false))) {
177 if (in == null)
178 return null;
179 ds = parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
180 }
181
182 try (InputStream in = getInputStream(getRequestForBbox(-180.0, lat1, lon2, lat2),
183 progressMonitor.createSubTaskMonitor(9, false))) {
184 if (in == null)
185 return null;
186 ds2 = parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
187 }
188 if (ds2 == null)
189 return null;
190 ds.mergeFrom(ds2);
191
192 } else {
193 // Simple request
194 try (InputStream in = getInputStream(getRequestForBbox(lon1, lat1, lon2, lat2),
195 progressMonitor.createSubTaskMonitor(9, false))) {
196 if (in == null)
197 return null;
198 ds = parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
199 }
200 }
201 return ds;
202 } catch (OsmTransferException e) {
203 throw e;
204 } catch (IllegalDataException | IOException e) {
205 throw new OsmTransferException(e);
206 } finally {
207 progressMonitor.finishTask();
208 activeConnection = null;
209 }
210 }
211
212 @Override
213 public List<Note> parseNotes(int noteLimit, int daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException {
214 progressMonitor.beginTask(tr("Downloading notes"));
215 CheckParameterUtil.ensureThat(noteLimit > 0, "Requested note limit is less than 1.");
216 // see result_limit in https://github.com/openstreetmap/openstreetmap-website/blob/master/app/controllers/notes_controller.rb
217 CheckParameterUtil.ensureThat(noteLimit <= 10_000, "Requested note limit is over API hard limit of 10000.");
218 CheckParameterUtil.ensureThat(daysClosed >= -1, "Requested note limit is less than -1.");
219 String url = "notes?limit=" + noteLimit + "&closed=" + daysClosed + "&bbox=" + lon1 + ',' + lat1 + ',' + lon2 + ',' + lat2;
220 try (InputStream is = getInputStream(url, progressMonitor.createSubTaskMonitor(1, false))) {
221 final List<Note> notes = new NoteReader(is).parse();
222 if (notes.size() == noteLimit) {
223 throw new MoreNotesException(notes, noteLimit);
224 }
225 return notes;
226 } catch (IOException | SAXException e) {
227 throw new OsmTransferException(e);
228 } finally {
229 progressMonitor.finishTask();
230 }
231 }
232
233 /**
234 * Indicates that the number of fetched notes equals the specified limit. Thus there might be more notes to download.
235 */
236 public static class MoreNotesException extends RuntimeException {
237 /**
238 * The downloaded notes
239 */
240 public final transient List<Note> notes;
241 /**
242 * The download limit sent to the server.
243 */
244 public final int limit;
245
246 /**
247 * Constructs a {@code MoreNotesException}.
248 * @param notes downloaded notes
249 * @param limit download limit sent to the server
250 */
251 public MoreNotesException(List<Note> notes, int limit) {
252 this.notes = notes;
253 this.limit = limit;
254 }
255 }
256
257 /**
258 * Determines if download is complete for the given bounding box.
259 * @return true if download is complete for the given bounding box (not filtered)
260 */
261 public boolean considerAsFullDownload() {
262 return true;
263 }
264
265}
Note: See TracBrowser for help on using the repository browser.