source: josm/src/org/openstreetmap/josm/io/OsmServerReader.java@ 75

Last change on this file since 75 was 75, checked in by imi, 18 years ago
  • added editable colors
  • changed preferences format (again)
File size: 3.0 KB
Line 
1package org.openstreetmap.josm.io;
2
3import java.io.IOException;
4import java.io.InputStreamReader;
5import java.io.Reader;
6import java.net.HttpURLConnection;
7import java.net.URL;
8import java.util.Collection;
9import java.util.LinkedList;
10
11import org.jdom.JDOMException;
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.data.coor.LatLon;
14import org.openstreetmap.josm.data.osm.DataSet;
15import org.xml.sax.SAXException;
16
17/**
18 * This DataReader read directly from the REST API of the osm server.
19 *
20 * @author imi
21 */
22public class OsmServerReader extends OsmConnection {
23
24 /**
25 * The boundings of the desired map data.
26 */
27 private final double lat1;
28 private final double lon1;
29 private final double lat2;
30 private final double lon2;
31
32 /**
33 * Construct the reader and store the information for attaching
34 */
35 public OsmServerReader(double lat1, double lon1, double lat2, double lon2) {
36 this.lon2 = lon2;
37 this.lat2 = lat2;
38 this.lon1 = lon1;
39 this.lat1 = lat1;
40 }
41
42
43 /**
44 * Retrieve raw gps waypoints from the server API.
45 * @return A list of all primitives retrieved. Currently, the list of lists
46 * contain only one list, since the server cannot distinguish between
47 * ways.
48 */
49 public Collection<Collection<LatLon>> parseRawGps() throws IOException, JDOMException {
50 String url = Main.pref.get("osm-server.url")+"/0.3/trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
51 Collection<Collection<LatLon>> data = new LinkedList<Collection<LatLon>>();
52 Collection<LatLon> list = new LinkedList<LatLon>();
53
54 for (int i = 0;;++i) {
55 Reader r = getReader(url+i);
56 if (r == null)
57 break;
58 RawGpsReader gpsReader = new RawGpsReader(r);
59 Collection<Collection<LatLon>> allWays = gpsReader.parse();
60 boolean foundSomething = false;
61 for (Collection<LatLon> t : allWays) {
62 if (!t.isEmpty()) {
63 foundSomething = true;
64 list.addAll(t);
65 }
66 }
67 if (!foundSomething)
68 break;
69 r.close();
70 }
71
72 data.add(list);
73 return data;
74 }
75
76
77 /**
78 * Read the data from the osm server address.
79 * @return A data set containing all data retrieved from that url
80 */
81 public DataSet parseOsm() throws SAXException, IOException {
82 Reader r = getReader(Main.pref.get("osm-server.url")+"/0.3/map?bbox="+lon1+","+lat1+","+lon2+","+lat2);
83 if (r == null)
84 return null;
85 DataSet data = OsmReader.parseDataSet(r);
86 r.close();
87 return data;
88 }
89
90
91 /**
92 * Open a connection to the given url and return a reader on the input stream
93 * from that connection. In case of user cancel, return <code>null</code>.
94 * @param url The exact url to connect to.
95 * @return An reader reading the input stream (servers answer) or <code>null</code>.
96 */
97 private Reader getReader(String urlStr) throws IOException {
98 System.out.println("download: "+urlStr);
99 initAuthentication();
100 URL url = new URL(urlStr);
101 HttpURLConnection con = (HttpURLConnection)url.openConnection();
102 con.setConnectTimeout(20000);
103 if (con.getResponseCode() == 401 && isCancelled())
104 return null;
105 return new InputStreamReader(con.getInputStream());
106 }
107}
Note: See TracBrowser for help on using the repository browser.