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

Last change on this file since 7392 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

  • Property svn:eol-style set to native
File size: 6.1 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;
8
9import org.openstreetmap.josm.data.Bounds;
10import org.openstreetmap.josm.data.gpx.GpxData;
11import org.openstreetmap.josm.data.osm.DataSet;
12import org.openstreetmap.josm.gui.progress.ProgressMonitor;
13import org.openstreetmap.josm.tools.CheckParameterUtil;
14import org.xml.sax.SAXException;
15
16/**
17 * Read content from OSM server for a given bounding box
18 * @since 627
19 */
20public class BoundingBoxDownloader extends OsmServerReader {
21
22 /**
23 * The boundings of the desired map data.
24 */
25 protected final double lat1;
26 protected final double lon1;
27 protected final double lat2;
28 protected final double lon2;
29 protected final boolean crosses180th;
30
31 /**
32 * Constructs a new {@code BoundingBoxDownloader}.
33 * @param downloadArea The area to download
34 */
35 public BoundingBoxDownloader(Bounds downloadArea) {
36 CheckParameterUtil.ensureParameterNotNull(downloadArea, "downloadArea");
37 this.lat1 = downloadArea.getMinLat();
38 this.lon1 = downloadArea.getMinLon();
39 this.lat2 = downloadArea.getMaxLat();
40 this.lon2 = downloadArea.getMaxLon();
41 this.crosses180th = downloadArea.crosses180thMeridian();
42 }
43
44 private GpxData downloadRawGps(String url, ProgressMonitor progressMonitor) throws IOException, OsmTransferException, SAXException {
45 boolean done = false;
46 GpxData result = null;
47 for (int i = 0;!done;++i) {
48 progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
49 try (InputStream in = getInputStream(url+i, progressMonitor.createSubTaskMonitor(1, true))) {
50 if (in == null) {
51 break;
52 }
53 progressMonitor.setTicks(0);
54 GpxReader reader = new GpxReader(in);
55 gpxParsedProperly = reader.parse(false);
56 GpxData currentGpx = reader.getGpxData();
57 if (result == null) {
58 result = currentGpx;
59 } else if (currentGpx.hasTrackPoints()) {
60 result.mergeFrom(currentGpx);
61 } else{
62 done = true;
63 }
64 }
65 activeConnection = null;
66 }
67 if (result != null) {
68 result.fromServer = true;
69 }
70 return result;
71 }
72
73 @Override
74 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
75 progressMonitor.beginTask("", 1);
76 try {
77 progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
78 if (crosses180th) {
79 // API 0.6 does not support requests crossing the 180th meridian, so make two requests
80 GpxData result = downloadRawGps("trackpoints?bbox="+lon1+","+lat1+",180.0,"+lat2+"&page=", progressMonitor);
81 result.mergeFrom(downloadRawGps("trackpoints?bbox=-180.0,"+lat1+","+lon2+","+lat2+"&page=", progressMonitor));
82 return result;
83 } else {
84 // Simple request
85 return downloadRawGps("trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=", progressMonitor);
86 }
87 } catch (IllegalArgumentException e) {
88 // caused by HttpUrlConnection in case of illegal stuff in the response
89 if (cancel)
90 return null;
91 throw new OsmTransferException("Illegal characters within the HTTP-header response.", e);
92 } catch (IOException e) {
93 if (cancel)
94 return null;
95 throw new OsmTransferException(e);
96 } catch (SAXException e) {
97 throw new OsmTransferException(e);
98 } catch (OsmTransferException e) {
99 throw e;
100 } catch (RuntimeException e) {
101 if (cancel)
102 return null;
103 throw e;
104 } finally {
105 progressMonitor.finishTask();
106 }
107 }
108
109 protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) {
110 return "map?bbox=" + lon1 + "," + lat1 + "," + lon2 + "," + lat2;
111 }
112
113 @Override
114 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
115 progressMonitor.beginTask(tr("Contacting OSM Server..."), 10);
116 try {
117 DataSet ds = null;
118 progressMonitor.indeterminateSubTask(null);
119 if (crosses180th) {
120 // API 0.6 does not support requests crossing the 180th meridian, so make two requests
121 DataSet ds2 = null;
122
123 try (InputStream in = getInputStream(getRequestForBbox(lon1, lat1, 180.0, lat2), progressMonitor.createSubTaskMonitor(9, false))) {
124 if (in == null)
125 return null;
126 ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
127 }
128
129 try (InputStream in = getInputStream(getRequestForBbox(-180.0, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false))) {
130 if (in == null)
131 return null;
132 ds2 = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
133 }
134 if (ds2 == null)
135 return null;
136 ds.mergeFrom(ds2);
137
138 } else {
139 // Simple request
140 try (InputStream in = getInputStream(getRequestForBbox(lon1, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false))) {
141 if (in == null)
142 return null;
143 ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
144 }
145 }
146 return ds;
147 } catch(OsmTransferException e) {
148 throw e;
149 } catch (Exception e) {
150 throw new OsmTransferException(e);
151 } finally {
152 progressMonitor.finishTask();
153 activeConnection = null;
154 }
155 }
156}
Note: See TracBrowser for help on using the repository browser.