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

Last change on this file since 4701 was 4580, checked in by Don-vip, 12 years ago

see #2212 - Allow JOSM to download data crossing the 180th meridian

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