1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.Bounds;
|
---|
11 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
14 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
15 | import org.xml.sax.SAXException;
|
---|
16 |
|
---|
17 |
|
---|
18 | public class BoundingBoxDownloader extends OsmServerReader {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * The boundings of the desired map data.
|
---|
22 | */
|
---|
23 | private final double lat1;
|
---|
24 | private final double lon1;
|
---|
25 | private final double lat2;
|
---|
26 | private final double lon2;
|
---|
27 |
|
---|
28 | public BoundingBoxDownloader(double lat1, double lon1, double lat2, double lon2) {
|
---|
29 | this.lat1 = lat1;
|
---|
30 | this.lon1 = lon1;
|
---|
31 | this.lat2 = lat2;
|
---|
32 | this.lon2 = 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 | final InputStream in = getInputStream("map?bbox="+lon1+","+lat1+","+lon2+","+lat2, Main.pleaseWaitDlg);
|
---|
96 | if (in == null)
|
---|
97 | return null;
|
---|
98 | Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
|
---|
99 | final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg);
|
---|
100 | String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5");
|
---|
101 | Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2));
|
---|
102 | DataSource src = new DataSource(bounds, origin);
|
---|
103 | data.dataSources.add(src);
|
---|
104 | in.close();
|
---|
105 | activeConnection = null;
|
---|
106 | return data;
|
---|
107 | } catch (IOException e) {
|
---|
108 | if (cancel)
|
---|
109 | return null;
|
---|
110 | throw e;
|
---|
111 | } catch (SAXException e) {
|
---|
112 | throw e;
|
---|
113 | } catch (Exception e) {
|
---|
114 | if (cancel)
|
---|
115 | return null;
|
---|
116 | if (e instanceof RuntimeException)
|
---|
117 | throw (RuntimeException)e;
|
---|
118 | throw new RuntimeException(e);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|