1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
13 | import org.openstreetmap.josm.data.notes.Note;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
17 | import org.xml.sax.SAXException;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Read content from OSM server for a given bounding box
|
---|
21 | * @since 627
|
---|
22 | */
|
---|
23 | public class BoundingBoxDownloader extends OsmServerReader {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * The boundings of the desired map data.
|
---|
27 | */
|
---|
28 | protected final double lat1;
|
---|
29 | protected final double lon1;
|
---|
30 | protected final double lat2;
|
---|
31 | protected final double lon2;
|
---|
32 | protected final boolean crosses180th;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code BoundingBoxDownloader}.
|
---|
36 | * @param downloadArea The area to download
|
---|
37 | */
|
---|
38 | public BoundingBoxDownloader(Bounds downloadArea) {
|
---|
39 | CheckParameterUtil.ensureParameterNotNull(downloadArea, "downloadArea");
|
---|
40 | this.lat1 = downloadArea.getMinLat();
|
---|
41 | this.lon1 = downloadArea.getMinLon();
|
---|
42 | this.lat2 = downloadArea.getMaxLat();
|
---|
43 | this.lon2 = downloadArea.getMaxLon();
|
---|
44 | this.crosses180th = downloadArea.crosses180thMeridian();
|
---|
45 | }
|
---|
46 |
|
---|
47 | private GpxData downloadRawGps(String url, ProgressMonitor progressMonitor) throws IOException, OsmTransferException, SAXException {
|
---|
48 | boolean done = false;
|
---|
49 | GpxData result = null;
|
---|
50 | for (int i = 0;!done;++i) {
|
---|
51 | progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
|
---|
52 | try (InputStream in = getInputStream(url+i, progressMonitor.createSubTaskMonitor(1, true))) {
|
---|
53 | if (in == null) {
|
---|
54 | break;
|
---|
55 | }
|
---|
56 | progressMonitor.setTicks(0);
|
---|
57 | GpxReader reader = new GpxReader(in);
|
---|
58 | gpxParsedProperly = reader.parse(false);
|
---|
59 | GpxData currentGpx = reader.getGpxData();
|
---|
60 | if (result == null) {
|
---|
61 | result = currentGpx;
|
---|
62 | } else if (currentGpx.hasTrackPoints()) {
|
---|
63 | result.mergeFrom(currentGpx);
|
---|
64 | } else{
|
---|
65 | done = true;
|
---|
66 | }
|
---|
67 | }
|
---|
68 | activeConnection = null;
|
---|
69 | }
|
---|
70 | if (result != null) {
|
---|
71 | result.fromServer = true;
|
---|
72 | }
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
78 | progressMonitor.beginTask("", 1);
|
---|
79 | try {
|
---|
80 | progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
|
---|
81 | if (crosses180th) {
|
---|
82 | // API 0.6 does not support requests crossing the 180th meridian, so make two requests
|
---|
83 | GpxData result = downloadRawGps("trackpoints?bbox="+lon1+","+lat1+",180.0,"+lat2+"&page=", progressMonitor);
|
---|
84 | result.mergeFrom(downloadRawGps("trackpoints?bbox=-180.0,"+lat1+","+lon2+","+lat2+"&page=", progressMonitor));
|
---|
85 | return result;
|
---|
86 | } else {
|
---|
87 | // Simple request
|
---|
88 | return downloadRawGps("trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=", progressMonitor);
|
---|
89 | }
|
---|
90 | } catch (IllegalArgumentException e) {
|
---|
91 | // caused by HttpUrlConnection in case of illegal stuff in the response
|
---|
92 | if (cancel)
|
---|
93 | return null;
|
---|
94 | throw new OsmTransferException("Illegal characters within the HTTP-header response.", e);
|
---|
95 | } catch (IOException e) {
|
---|
96 | if (cancel)
|
---|
97 | return null;
|
---|
98 | throw new OsmTransferException(e);
|
---|
99 | } catch (SAXException e) {
|
---|
100 | throw new OsmTransferException(e);
|
---|
101 | } catch (OsmTransferException e) {
|
---|
102 | throw e;
|
---|
103 | } catch (RuntimeException e) {
|
---|
104 | if (cancel)
|
---|
105 | return null;
|
---|
106 | throw e;
|
---|
107 | } finally {
|
---|
108 | progressMonitor.finishTask();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) {
|
---|
113 | return "map?bbox=" + lon1 + "," + lat1 + "," + lon2 + "," + lat2;
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
118 | progressMonitor.beginTask(tr("Contacting OSM Server..."), 10);
|
---|
119 | try {
|
---|
120 | DataSet ds = null;
|
---|
121 | progressMonitor.indeterminateSubTask(null);
|
---|
122 | if (crosses180th) {
|
---|
123 | // API 0.6 does not support requests crossing the 180th meridian, so make two requests
|
---|
124 | DataSet ds2 = null;
|
---|
125 |
|
---|
126 | try (InputStream in = getInputStream(getRequestForBbox(lon1, lat1, 180.0, lat2), progressMonitor.createSubTaskMonitor(9, false))) {
|
---|
127 | if (in == null)
|
---|
128 | return null;
|
---|
129 | ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
130 | }
|
---|
131 |
|
---|
132 | try (InputStream in = getInputStream(getRequestForBbox(-180.0, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false))) {
|
---|
133 | if (in == null)
|
---|
134 | return null;
|
---|
135 | ds2 = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
136 | }
|
---|
137 | if (ds2 == null)
|
---|
138 | return null;
|
---|
139 | ds.mergeFrom(ds2);
|
---|
140 |
|
---|
141 | } else {
|
---|
142 | // Simple request
|
---|
143 | try (InputStream in = getInputStream(getRequestForBbox(lon1, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false))) {
|
---|
144 | if (in == null)
|
---|
145 | return null;
|
---|
146 | ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
147 | }
|
---|
148 | }
|
---|
149 | return ds;
|
---|
150 | } catch(OsmTransferException e) {
|
---|
151 | throw e;
|
---|
152 | } catch (Exception e) {
|
---|
153 | throw new OsmTransferException(e);
|
---|
154 | } finally {
|
---|
155 | progressMonitor.finishTask();
|
---|
156 | activeConnection = null;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public List<Note> parseNotes(Integer noteLimit, Integer daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
162 | progressMonitor.beginTask("Downloading notes");
|
---|
163 | noteLimit = checkNoteLimit(noteLimit);
|
---|
164 | daysClosed = checkDaysClosed(daysClosed);
|
---|
165 | String url = new StringBuilder()
|
---|
166 | .append("notes?limit=")
|
---|
167 | .append(noteLimit)
|
---|
168 | .append("&closed=")
|
---|
169 | .append(daysClosed)
|
---|
170 | .append("&bbox=")
|
---|
171 | .append(lon1)
|
---|
172 | .append(",").append(lat1)
|
---|
173 | .append(",").append(lon2)
|
---|
174 | .append(",").append(lat2)
|
---|
175 | .toString();
|
---|
176 | try {
|
---|
177 | InputStream is = getInputStream(url, progressMonitor.createSubTaskMonitor(1, false));
|
---|
178 | NoteReader reader = new NoteReader(is);
|
---|
179 | return reader.parse();
|
---|
180 | } catch (IOException e) {
|
---|
181 | throw new OsmTransferException(e);
|
---|
182 | } catch (SAXException e) {
|
---|
183 | throw new OsmTransferException(e);
|
---|
184 | } finally {
|
---|
185 | progressMonitor.finishTask();
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | private Integer checkNoteLimit(Integer limit) {
|
---|
190 | if (limit == null) {
|
---|
191 | limit = Main.pref.getInteger("osm.notes.downloadLimit", 1000);
|
---|
192 | }
|
---|
193 | if (limit > 10000) {
|
---|
194 | Main.error("Requested note limit is over API hard limit of 10000. Reducing to 10000.");
|
---|
195 | limit = 10000;
|
---|
196 | }
|
---|
197 | if (limit < 1) {
|
---|
198 | Main.error("Requested note limit is less than 1. Setting to 1.");
|
---|
199 | limit = 1;
|
---|
200 | }
|
---|
201 | Main.debug("returning note limit: " + limit);
|
---|
202 | return limit;
|
---|
203 | }
|
---|
204 |
|
---|
205 | private Integer checkDaysClosed(Integer days) {
|
---|
206 | if (days == null) {
|
---|
207 | days = Main.pref.getInteger("osm.notes.daysClosed", 1);
|
---|
208 | }
|
---|
209 | if (days < -1) {
|
---|
210 | Main.error("Requested days closed must be greater than -1");
|
---|
211 | days = -1;
|
---|
212 | }
|
---|
213 | Main.debug("returning days closed: " + days);
|
---|
214 | return days;
|
---|
215 | }
|
---|
216 |
|
---|
217 | }
|
---|