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

Last change on this file since 6113 was 6070, checked in by stoecker, 11 years ago

see #8853 remove tabs, trailing spaces, windows line ends, strange characters

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