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

Last change on this file since 2284 was 2181, checked in by stoecker, 15 years ago

lots of i18n fixes

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