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

Last change on this file since 4521 was 4521, checked in by Don-vip, 13 years ago

fix #6960 - Download GPX traces with Ctrl+L

  • Property svn:eol-style set to native
File size: 4.2 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
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 @Override
39 public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
40 progressMonitor.beginTask("", 1);
41 try {
42 progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
43 String url = "trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
44
45 boolean done = false;
46 GpxData result = null;
47 for (int i = 0;!done;++i) {
48 progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
49 InputStream in = getInputStream(url+i, progressMonitor.createSubTaskMonitor(1, true));
50 if (in == null) {
51 break;
52 }
53 progressMonitor.setTicks(0);
54 GpxReader reader = new GpxReader(in);
55 reader.parse(false);
56 GpxData currentGpx = reader.data;
57 if (result == null) {
58 result = currentGpx;
59 } else if (currentGpx.hasTrackPoints()) {
60 result.mergeFrom(currentGpx);
61 } else{
62 done = true;
63 }
64 in.close();
65 activeConnection = null;
66 }
67 result.fromServer = true;
68 return result;
69 } catch (IllegalArgumentException e) {
70 // caused by HttpUrlConnection in case of illegal stuff in the response
71 if (cancel)
72 return null;
73 throw new OsmTransferException("Illegal characters within the HTTP-header response.", e);
74 } catch (IOException e) {
75 if (cancel)
76 return null;
77 throw new OsmTransferException(e);
78 } catch (SAXException e) {
79 throw new OsmTransferException(e);
80 } catch (OsmTransferException e) {
81 throw e;
82 } catch (RuntimeException e) {
83 if (cancel)
84 return null;
85 throw e;
86 } finally {
87 progressMonitor.finishTask();
88 }
89 }
90
91 /**
92 * Read the data from the osm server address.
93 * @return A data set containing all data retrieved from that url
94 */
95 @Override
96 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
97 progressMonitor.beginTask(tr("Contacting OSM Server..."), 10);
98 InputStream in = null;
99 try {
100 progressMonitor.indeterminateSubTask(null);
101 in = getInputStream("map?bbox="+lon1+","+lat1+","+lon2+","+lat2, progressMonitor.createSubTaskMonitor(9, false));
102 if (in == null)
103 return null;
104 return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
105 } catch(OsmTransferException e) {
106 throw e;
107 } catch (Exception e) {
108 throw new OsmTransferException(e);
109 } finally {
110 progressMonitor.finishTask();
111 if (in != null) {
112 try {in.close();} catch(IOException e) {}
113 }
114 activeConnection = null;
115 }
116 }
117}
Note: See TracBrowser for help on using the repository browser.