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

Last change on this file since 6360 was 6244, checked in by Don-vip, 11 years ago

fix #9091 - rework osm/gpx download tasks - take into account recent changes from OSM server:

  • new hashtag scheme
  • GPX traces now returned as Bzip2-compressed files
  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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.Utils;
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 this.lat1 = downloadArea.getMinLat();
37 this.lon1 = downloadArea.getMinLon();
38 this.lat2 = downloadArea.getMaxLat();
39 this.lon2 = downloadArea.getMaxLon();
40 this.crosses180th = downloadArea.crosses180thMeridian();
41 }
42
43 private GpxData downloadRawGps(String url, ProgressMonitor progressMonitor) throws IOException, OsmTransferException, SAXException {
44 boolean done = false;
45 GpxData result = null;
46 for (int i = 0;!done;++i) {
47 progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
48 InputStream in = getInputStream(url+i, progressMonitor.createSubTaskMonitor(1, true));
49 if (in == null) {
50 break;
51 }
52 progressMonitor.setTicks(0);
53 GpxReader reader = new GpxReader(in);
54 gpxParsedProperly = reader.parse(false);
55 GpxData currentGpx = reader.getGpxData();
56 if (result == null) {
57 result = currentGpx;
58 } else if (currentGpx.hasTrackPoints()) {
59 result.mergeFrom(currentGpx);
60 } else{
61 done = true;
62 }
63 Utils.close(in);
64 activeConnection = null;
65 }
66 result.fromServer = true;
67 return result;
68 }
69
70 @Override
71 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
72 progressMonitor.beginTask("", 1);
73 try {
74 progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
75 if (crosses180th) {
76 // API 0.6 does not support requests crossing the 180th meridian, so make two requests
77 GpxData result = downloadRawGps("trackpoints?bbox="+lon1+","+lat1+",180.0,"+lat2+"&page=", progressMonitor);
78 result.mergeFrom(downloadRawGps("trackpoints?bbox=-180.0,"+lat1+","+lon2+","+lat2+"&page=", progressMonitor));
79 return result;
80 } else {
81 // Simple request
82 return downloadRawGps("trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=", progressMonitor);
83 }
84 } catch (IllegalArgumentException e) {
85 // caused by HttpUrlConnection in case of illegal stuff in the response
86 if (cancel)
87 return null;
88 throw new OsmTransferException("Illegal characters within the HTTP-header response.", e);
89 } catch (IOException e) {
90 if (cancel)
91 return null;
92 throw new OsmTransferException(e);
93 } catch (SAXException e) {
94 throw new OsmTransferException(e);
95 } catch (OsmTransferException e) {
96 throw e;
97 } catch (RuntimeException e) {
98 if (cancel)
99 return null;
100 throw e;
101 } finally {
102 progressMonitor.finishTask();
103 }
104 }
105
106 protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) {
107 return "map?bbox=" + lon1 + "," + lat1 + "," + lon2 + "," + lat2;
108 }
109
110 @Override
111 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
112 progressMonitor.beginTask(tr("Contacting OSM Server..."), 10);
113 InputStream in = null;
114 try {
115 DataSet ds = null;
116 progressMonitor.indeterminateSubTask(null);
117 if (crosses180th) {
118 // API 0.6 does not support requests crossing the 180th meridian, so make two requests
119 in = getInputStream(getRequestForBbox(lon1, lat1, 180.0, lat2), progressMonitor.createSubTaskMonitor(9, false));
120 if (in == null)
121 return null;
122 ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
123
124 in = getInputStream(getRequestForBbox(-180.0, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false));
125 if (in == null)
126 return null;
127 DataSet ds2 = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
128 if (ds2 == null)
129 return null;
130 ds.mergeFrom(ds2);
131
132 } else {
133 // Simple request
134 in = getInputStream(getRequestForBbox(lon1, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false));
135 if (in == null)
136 return null;
137 ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
138 }
139 return ds;
140 } catch(OsmTransferException e) {
141 throw e;
142 } catch (Exception e) {
143 throw new OsmTransferException(e);
144 } finally {
145 progressMonitor.finishTask();
146 Utils.close(in);
147 activeConnection = null;
148 }
149 }
150}
Note: See TracBrowser for help on using the repository browser.