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.data.Bounds;
|
---|
10 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
12 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
13 | import org.xml.sax.SAXException;
|
---|
14 |
|
---|
15 | public 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(Bounds downloadArea) {
|
---|
26 | this.lat1 = downloadArea.getMin().lat();
|
---|
27 | this.lon1 = downloadArea.getMin().lon();
|
---|
28 | this.lat2 = downloadArea.getMax().lat();
|
---|
29 | this.lon2 = downloadArea.getMax().lon();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Retrieve raw gps waypoints from the server API.
|
---|
34 | * @return A list of all primitives retrieved. Currently, the list of lists
|
---|
35 | * contain only one list, since the server cannot distinguish between
|
---|
36 | * ways.
|
---|
37 | */
|
---|
38 | public GpxData parseRawGps(ProgressMonitor progressMonitor) throws IOException, SAXException,OsmTransferException {
|
---|
39 | progressMonitor.beginTask("", 1);
|
---|
40 | try {
|
---|
41 | progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
|
---|
42 | String url = "trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
|
---|
43 |
|
---|
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 | reader.parse(false);
|
---|
55 | GpxData currentGpx = reader.data;
|
---|
56 | if (result == null) {
|
---|
57 | result = currentGpx;
|
---|
58 | } else if (currentGpx.hasTrackPoints()) {
|
---|
59 | result.mergeFrom(currentGpx);
|
---|
60 | } else{
|
---|
61 | done = true;
|
---|
62 | }
|
---|
63 | in.close();
|
---|
64 | activeConnection = null;
|
---|
65 | }
|
---|
66 | result.fromServer = true;
|
---|
67 | return result;
|
---|
68 | } catch (IllegalArgumentException e) {
|
---|
69 | // caused by HttpUrlConnection in case of illegal stuff in the response
|
---|
70 | if (cancel)
|
---|
71 | return null;
|
---|
72 | throw new SAXException("Illegal characters within the HTTP-header response.", e);
|
---|
73 | } catch (IOException e) {
|
---|
74 | if (cancel)
|
---|
75 | return null;
|
---|
76 | throw e;
|
---|
77 | } catch (SAXException e) {
|
---|
78 | throw e;
|
---|
79 | } catch (OsmTransferException e) {
|
---|
80 | throw e;
|
---|
81 | } catch (RuntimeException e) {
|
---|
82 | if (cancel)
|
---|
83 | return null;
|
---|
84 | throw e;
|
---|
85 | } finally {
|
---|
86 | progressMonitor.finishTask();
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Read the data from the osm server address.
|
---|
92 | * @return A data set containing all data retrieved from that url
|
---|
93 | */
|
---|
94 | @Override
|
---|
95 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
96 | progressMonitor.beginTask(tr("Contacting OSM Server..."), 10);
|
---|
97 | InputStream in = null;
|
---|
98 | try {
|
---|
99 | progressMonitor.indeterminateSubTask(null);
|
---|
100 | in = getInputStream("map?bbox="+lon1+","+lat1+","+lon2+","+lat2, progressMonitor.createSubTaskMonitor(9, false));
|
---|
101 | if (in == null)
|
---|
102 | return null;
|
---|
103 | return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
104 | } catch(OsmTransferException e) {
|
---|
105 | throw e;
|
---|
106 | } catch (Exception e) {
|
---|
107 | throw new OsmTransferException(e);
|
---|
108 | } finally {
|
---|
109 | progressMonitor.finishTask();
|
---|
110 | if (in != null) {
|
---|
111 | try {in.close();} catch(IOException e) {}
|
---|
112 | }
|
---|
113 | activeConnection = null;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|