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

Last change on this file since 1523 was 1465, checked in by stoecker, 15 years ago

fix #1967. patch by xeen. This will break plugins using ProgressDialog until recompiled

  • Property svn:eol-style set to native
File size: 4.3 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.Main;
10import org.openstreetmap.josm.data.osm.DataSet;
11import org.openstreetmap.josm.data.gpx.GpxData;
12import org.xml.sax.SAXException;
13
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
25 public BoundingBoxDownloader(double lat1, double lon1, double lat2, double lon2) {
26 this.lat1 = lat1;
27 this.lon1 = lon1;
28 this.lat2 = lat2;
29 this.lon2 = lon2;
30 // store the bounding box in the preferences so it can be
31 // re-used across invocations of josm
32 Main.pref.put("osm-download.bounds", lat1+";"+lon1+";"+lat2+";"+lon2);
33 }
34
35 /**
36 * Retrieve raw gps waypoints from the server API.
37 * @return A list of all primitives retrieved. Currently, the list of lists
38 * contain only one list, since the server cannot distinguish between
39 * ways.
40 */
41 public GpxData parseRawGps() throws IOException, SAXException {
42 Main.pleaseWaitDlg.progress.setValue(0);
43 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
44 try {
45 String url = "trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
46
47 boolean done = false;
48 GpxData result = null;
49 for (int i = 0;!done;++i) {
50 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
51 InputStream in = getInputStream(url+i, Main.pleaseWaitDlg);
52 if (in == null)
53 break;
54 GpxData currentGpx = new GpxReader(in, null).data;
55 if (result == null) {
56 result = currentGpx;
57 } else if (currentGpx.hasTrackPoints()) {
58 result.mergeFrom(currentGpx);
59 } else{
60 done = true;
61 }
62 in.close();
63 activeConnection = null;
64 }
65 result.fromServer = true;
66 return result;
67 } catch (IllegalArgumentException e) {
68 // caused by HttpUrlConnection in case of illegal stuff in the response
69 if (cancel)
70 return null;
71 throw new SAXException("Illegal characters within the HTTP-header response", e);
72 } catch (IOException e) {
73 if (cancel)
74 return null;
75 throw e;
76 } catch (SAXException e) {
77 throw e;
78 } catch (Exception e) {
79 if (cancel)
80 return null;
81 if (e instanceof RuntimeException)
82 throw (RuntimeException)e;
83 throw new RuntimeException(e);
84 }
85 }
86
87 /**
88 * Read the data from the osm server address.
89 * @return A data set containing all data retrieved from that url
90 */
91 public DataSet parseOsm() throws SAXException, IOException {
92 try {
93 Main.pleaseWaitDlg.progress.setValue(0);
94 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
95 Main.pleaseWaitDlg.setIndeterminate(true);
96 final InputStream in = getInputStream("map?bbox="+lon1+","+lat1+","+lon2+","+lat2, Main.pleaseWaitDlg);
97 Main.pleaseWaitDlg.setIndeterminate(false);
98 if (in == null)
99 return null;
100 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
101 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg);
102 in.close();
103 activeConnection = null;
104 return data;
105 } catch (IOException e) {
106 if (cancel)
107 return null;
108 throw e;
109 } catch (SAXException e) {
110 throw e;
111 } catch (Exception e) {
112 if (cancel)
113 return null;
114 if (e instanceof RuntimeException)
115 throw (RuntimeException)e;
116 throw new RuntimeException(e);
117 }
118 }
119}
Note: See TracBrowser for help on using the repository browser.