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

Last change on this file since 2683 was 2676, checked in by jttt, 14 years ago

Fix some of the warnings found by FindBugs

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