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

Last change on this file since 2852 was 2795, checked in by jttt, 14 years ago

Allow to load incomplete gpx files

  • 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 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}
Note: See TracBrowser for help on using the repository browser.