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

Last change on this file since 1724 was 1670, checked in by Gubaer, 15 years ago

fixed: bug in OsmApi.getOsmApi()
cleanup: exception handling in interfacing with OSM API
new: new action for updating individual elements with the their current state on the server (including new menu item in the file menu)
new: improved user feedback in case of conflicts
new: handles 410 Gone conflicts when uploading a changeset
new: undoable command for "purging" a primitive from the current dataset (necessary if the primitive is already deleted on the server and the user wants to remove it from its local dataset)
new: undoable command for "undeleting" an already deleted primitive on the server (kind of "cloning")
new: after a full upload, checks whether there are primitives in the local dataset which might be deleted on the server.
new: data structures for history data
new: history download support in io package

  • 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.osm.DataSet;
11import org.openstreetmap.josm.data.gpx.GpxData;
12import org.xml.sax.SAXException;
13
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(double lat1, double lon1, double lat2, double lon2) {
26 this.lat1 = lat1;
27 this.lon1 = lon1;
28 this.lat2 = lat2;
29 this.lon2 = lon2;
30 // store the bounding box in the preferences so it can be
31 // re-used across invocations of josm
32 Main.pref.put("osm-download.bounds", lat1+";"+lon1+";"+lat2+";"+lon2);
33 }
34
35 /**
36 * Retrieve raw gps waypoints from the server API.
37 * @return A list of all primitives retrieved. Currently, the list of lists
38 * contain only one list, since the server cannot distinguish between
39 * ways.
40 */
41 public GpxData parseRawGps() throws IOException, SAXException {
42 Main.pleaseWaitDlg.progress.setValue(0);
43 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
44 try {
45 String url = "trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
46
47 boolean done = false;
48 GpxData result = null;
49 for (int i = 0;!done;++i) {
50 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
51 InputStream in = getInputStream(url+i, Main.pleaseWaitDlg);
52 if (in == null) {
53 break;
54 }
55 GpxData currentGpx = new GpxReader(in, null).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 (Exception e) {
80 if (cancel)
81 return null;
82 if (e instanceof RuntimeException)
83 throw (RuntimeException)e;
84 throw new RuntimeException(e);
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() throws OsmTransferException {
94 try {
95 Main.pleaseWaitDlg.progress.setValue(0);
96 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
97 Main.pleaseWaitDlg.setIndeterminate(true);
98 final InputStream in = getInputStream("map?bbox="+lon1+","+lat1+","+lon2+","+lat2, Main.pleaseWaitDlg);
99 Main.pleaseWaitDlg.setIndeterminate(false);
100 if (in == null)
101 return null;
102 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
103 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg);
104 in.close();
105 activeConnection = null;
106 return data;
107 } catch (IOException e) {
108 if (cancel)
109 return null;
110 throw new OsmTransferException(e);
111 } catch (SAXException e) {
112 throw new OsmTransferException(e);
113 } catch(OsmTransferException e) {
114 throw e;
115 } catch (Exception e) {
116 if (cancel)
117 return null;
118 throw new OsmTransferException(e);
119 }
120 }
121}
Note: See TracBrowser for help on using the repository browser.